Interface to a standard tarco sensor. Measure the periode and the cal the speed
Tarco.cpp@7:752b8065ce2d, 2014-03-07 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Fri Mar 07 08:01:26 2014 +0000
- Revision:
- 7:752b8065ce2d
- Parent:
- 6:2ad5029f7b1a
- Child:
- 8:2e6b17f1da02
Fungerende version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gert_lauritsen | 2:c50da089786a | 1 | #include "Tarco.h" |
gert_lauritsen | 4:35024179a736 | 2 | #include "mbed.h" |
gert_lauritsen | 4:35024179a736 | 3 | |
gert_lauritsen | 4:35024179a736 | 4 | DigitalOut debug(LED2); |
gert_lauritsen | 0:8421e11df32a | 5 | |
gert_lauritsen | 4:35024179a736 | 6 | Tarco::Tarco(PinName tarcosignal, char mode) : |
gert_lauritsen | 4:35024179a736 | 7 | _tarcosensor(tarcosignal) |
gert_lauritsen | 4:35024179a736 | 8 | { |
gert_lauritsen | 4:35024179a736 | 9 | tarcomode=mode; |
gert_lauritsen | 7:752b8065ce2d | 10 | // AdvPeriode=0; |
gert_lauritsen | 4:35024179a736 | 11 | if (mode==0) |
gert_lauritsen | 7:752b8065ce2d | 12 | _tarcosensor.fall(this, &Tarco::RunMeanMeasure); |
gert_lauritsen | 4:35024179a736 | 13 | if (mode==1) |
gert_lauritsen | 7:752b8065ce2d | 14 | _tarcosensor.fall(this, &Tarco::Rotationtimer); |
gert_lauritsen | 0:8421e11df32a | 15 | } |
gert_lauritsen | 0:8421e11df32a | 16 | |
gert_lauritsen | 7:752b8065ce2d | 17 | void Tarco::Rotationtimer() |
gert_lauritsen | 4:35024179a736 | 18 | { |
gert_lauritsen | 7:752b8065ce2d | 19 | //Da switchAuto motoren kommer ud med 24 pulser/rotation laver vi en prescale på 24 |
gert_lauritsen | 7:752b8065ce2d | 20 | // |
gert_lauritsen | 7:752b8065ce2d | 21 | static int pulscounter; |
gert_lauritsen | 7:752b8065ce2d | 22 | if (pulscounter++>=PulsPrRotation-1) { |
gert_lauritsen | 4:35024179a736 | 23 | t.stop(); |
gert_lauritsen | 7:752b8065ce2d | 24 | AdvPeriode[0]=t.read_us(); |
gert_lauritsen | 4:35024179a736 | 25 | t.reset(); |
gert_lauritsen | 4:35024179a736 | 26 | t.start(); |
gert_lauritsen | 7:752b8065ce2d | 27 | pulscounter=0; |
gert_lauritsen | 4:35024179a736 | 28 | } |
gert_lauritsen | 0:8421e11df32a | 29 | } |
gert_lauritsen | 0:8421e11df32a | 30 | |
gert_lauritsen | 4:35024179a736 | 31 | |
gert_lauritsen | 7:752b8065ce2d | 32 | void Tarco::RunMeanMeasure() |
gert_lauritsen | 4:35024179a736 | 33 | { |
gert_lauritsen | 7:752b8065ce2d | 34 | static unsigned int TarcoHead; |
gert_lauritsen | 7:752b8065ce2d | 35 | //Måler tarco ved at lave en runing mean på periodetiderne, hvis der en variation |
gert_lauritsen | 7:752b8065ce2d | 36 | //på over 20% bliver den pågældende periode skippet |
gert_lauritsen | 7:752b8065ce2d | 37 | static bool MistedPeriod; //dette var en lang periode |
gert_lauritsen | 7:752b8065ce2d | 38 | int thisperiode; |
gert_lauritsen | 7:752b8065ce2d | 39 | float periodechange=0; |
gert_lauritsen | 4:35024179a736 | 40 | t.stop(); |
gert_lauritsen | 5:451dd1189b0f | 41 | if (TarcoHead>=TarcoRunMean) TarcoHead=0; |
gert_lauritsen | 6:2ad5029f7b1a | 42 | thisperiode=t.read_us(); |
gert_lauritsen | 7:752b8065ce2d | 43 | if (lastperiode!=0) |
gert_lauritsen | 7:752b8065ce2d | 44 | periodechange=(float) thisperiode/lastperiode; |
gert_lauritsen | 6:2ad5029f7b1a | 45 | if((periodechange<1.2) || (lastperiode==0) || MistedPeriod) { |
gert_lauritsen | 7:752b8065ce2d | 46 | AdvPeriode[TarcoHead++]=thisperiode; |
gert_lauritsen | 7:752b8065ce2d | 47 | MistedPeriod=false; |
gert_lauritsen | 7:752b8065ce2d | 48 | } else |
gert_lauritsen | 7:752b8065ce2d | 49 | MistedPeriod=true; |
gert_lauritsen | 4:35024179a736 | 50 | t.reset(); |
gert_lauritsen | 5:451dd1189b0f | 51 | t.start(); |
gert_lauritsen | 0:8421e11df32a | 52 | } |
gert_lauritsen | 0:8421e11df32a | 53 | |
gert_lauritsen | 4:35024179a736 | 54 | float Tarco::Speed() |
gert_lauritsen | 4:35024179a736 | 55 | { |
gert_lauritsen | 4:35024179a736 | 56 | //returns puls/sek [hz] |
gert_lauritsen | 7:752b8065ce2d | 57 | unsigned int periodetid; |
gert_lauritsen | 7:752b8065ce2d | 58 | lastfrekvens=0; |
gert_lauritsen | 7:752b8065ce2d | 59 | switch (tarcomode) { |
gert_lauritsen | 7:752b8065ce2d | 60 | case 0: { |
gert_lauritsen | 7:752b8065ce2d | 61 | periodetid=0; |
gert_lauritsen | 7:752b8065ce2d | 62 | for (int i=0; i<TarcoRunMean; i++) periodetid+=AdvPeriode[i]; |
gert_lauritsen | 7:752b8065ce2d | 63 | lastperiode=periodetid/TarcoRunMean; |
gert_lauritsen | 7:752b8065ce2d | 64 | lastfrekvens=(1e6/periodetid)*(TarcoRunMean/25); |
gert_lauritsen | 7:752b8065ce2d | 65 | } break; |
gert_lauritsen | 7:752b8065ce2d | 66 | case 1: { |
gert_lauritsen | 7:752b8065ce2d | 67 | lastfrekvens=(1e6/AdvPeriode[0]); |
gert_lauritsen | 7:752b8065ce2d | 68 | } break; |
gert_lauritsen | 7:752b8065ce2d | 69 | default: break; |
gert_lauritsen | 7:752b8065ce2d | 70 | } |
gert_lauritsen | 7:752b8065ce2d | 71 | return (float) lastfrekvens; |
gert_lauritsen | 2:c50da089786a | 72 | } |
gert_lauritsen | 4:35024179a736 | 73 | |
gert_lauritsen | 4:35024179a736 | 74 | float Tarco::RPM() |
gert_lauritsen | 4:35024179a736 | 75 | { |
gert_lauritsen | 7:752b8065ce2d | 76 | float rpm=lastfrekvens*60; |
gert_lauritsen | 7:752b8065ce2d | 77 | return (float) rpm; |
gert_lauritsen | 4:35024179a736 | 78 | } |
gert_lauritsen | 4:35024179a736 | 79 | |
gert_lauritsen | 4:35024179a736 | 80 |