Interface to a standard tarco sensor. Measure the periode and the cal the speed
Tarco.cpp@6:2ad5029f7b1a, 2013-11-20 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Wed Nov 20 13:29:29 2013 +0000
- Revision:
- 6:2ad5029f7b1a
- Parent:
- 5:451dd1189b0f
- Child:
- 7:752b8065ce2d
Fjerne den lange periode i tarco signallet
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 | 4:35024179a736 | 10 | p1=0; |
gert_lauritsen | 4:35024179a736 | 11 | p2=0; |
gert_lauritsen | 4:35024179a736 | 12 | pulsvariation=100; |
gert_lauritsen | 5:451dd1189b0f | 13 | // AdvPeriode=0; |
gert_lauritsen | 4:35024179a736 | 14 | if (mode==0) |
gert_lauritsen | 2:c50da089786a | 15 | _tarcosensor.fall(this, &Tarco::click); |
gert_lauritsen | 4:35024179a736 | 16 | if (mode==1) |
gert_lauritsen | 4:35024179a736 | 17 | _tarcosensor.rise(this, &Tarco::fsk); |
gert_lauritsen | 0:8421e11df32a | 18 | } |
gert_lauritsen | 0:8421e11df32a | 19 | |
gert_lauritsen | 4:35024179a736 | 20 | void Tarco::fsk() |
gert_lauritsen | 4:35024179a736 | 21 | { |
gert_lauritsen | 4:35024179a736 | 22 | //detect frekvensshift and start/Stop time to measure terco time |
gert_lauritsen | 4:35024179a736 | 23 | int pulstime; |
gert_lauritsen | 4:35024179a736 | 24 | fsktime.stop(); |
gert_lauritsen | 4:35024179a736 | 25 | pulstime=fsktime.read_us(); |
gert_lauritsen | 4:35024179a736 | 26 | fsktime.reset(); |
gert_lauritsen | 4:35024179a736 | 27 | fsktime.start(); |
gert_lauritsen | 4:35024179a736 | 28 | |
gert_lauritsen | 4:35024179a736 | 29 | if (pulstime>(lastpuls+pulsvariation)) { |
gert_lauritsen | 4:35024179a736 | 30 | t.stop(); |
gert_lauritsen | 5:451dd1189b0f | 31 | // AdvPeriode=t.read_us(); |
gert_lauritsen | 4:35024179a736 | 32 | debug=!debug; |
gert_lauritsen | 4:35024179a736 | 33 | t.reset(); |
gert_lauritsen | 4:35024179a736 | 34 | t.start(); |
gert_lauritsen | 4:35024179a736 | 35 | } |
gert_lauritsen | 4:35024179a736 | 36 | else |
gert_lauritsen | 4:35024179a736 | 37 | lastpuls=pulstime; |
gert_lauritsen | 0:8421e11df32a | 38 | } |
gert_lauritsen | 0:8421e11df32a | 39 | |
gert_lauritsen | 4:35024179a736 | 40 | |
gert_lauritsen | 4:35024179a736 | 41 | void Tarco::click() |
gert_lauritsen | 4:35024179a736 | 42 | { |
gert_lauritsen | 5:451dd1189b0f | 43 | static unsigned int TarcoHead; |
gert_lauritsen | 4:35024179a736 | 44 | //measure pulstime |
gert_lauritsen | 6:2ad5029f7b1a | 45 | static bool MistedPeriod; //dette var en lang periode |
gert_lauritsen | 6:2ad5029f7b1a | 46 | int thisperiode; |
gert_lauritsen | 6:2ad5029f7b1a | 47 | float periodechange; |
gert_lauritsen | 4:35024179a736 | 48 | t.stop(); |
gert_lauritsen | 5:451dd1189b0f | 49 | if (TarcoHead>=TarcoRunMean) TarcoHead=0; |
gert_lauritsen | 6:2ad5029f7b1a | 50 | thisperiode=t.read_us(); |
gert_lauritsen | 6:2ad5029f7b1a | 51 | if (lastperiode!=0) |
gert_lauritsen | 6:2ad5029f7b1a | 52 | periodechange=(float) thisperiode/lastperiode; |
gert_lauritsen | 6:2ad5029f7b1a | 53 | if((periodechange<1.2) || (lastperiode==0) || MistedPeriod) { |
gert_lauritsen | 6:2ad5029f7b1a | 54 | AdvPeriode[TarcoHead++]=thisperiode; |
gert_lauritsen | 6:2ad5029f7b1a | 55 | MistedPeriod=false; |
gert_lauritsen | 6:2ad5029f7b1a | 56 | } |
gert_lauritsen | 6:2ad5029f7b1a | 57 | else |
gert_lauritsen | 6:2ad5029f7b1a | 58 | MistedPeriod=true; |
gert_lauritsen | 4:35024179a736 | 59 | t.reset(); |
gert_lauritsen | 5:451dd1189b0f | 60 | t.start(); |
gert_lauritsen | 0:8421e11df32a | 61 | } |
gert_lauritsen | 0:8421e11df32a | 62 | |
gert_lauritsen | 4:35024179a736 | 63 | float Tarco::Speed() |
gert_lauritsen | 4:35024179a736 | 64 | { |
gert_lauritsen | 4:35024179a736 | 65 | //returns puls/sek [hz] |
gert_lauritsen | 5:451dd1189b0f | 66 | unsigned int periodetid; |
gert_lauritsen | 5:451dd1189b0f | 67 | periodetid=0; |
gert_lauritsen | 5:451dd1189b0f | 68 | for (int i=0; i<TarcoRunMean; i++) periodetid+=AdvPeriode[i]; |
gert_lauritsen | 6:2ad5029f7b1a | 69 | lastperiode=periodetid/TarcoRunMean; |
gert_lauritsen | 6:2ad5029f7b1a | 70 | lastfrekvens=(1e6/periodetid)*(TarcoRunMean/25); |
gert_lauritsen | 5:451dd1189b0f | 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 | 4:35024179a736 | 76 | if (AdvPeriode) { |
gert_lauritsen | 5:451dd1189b0f | 77 | float rpm=lastfrekvens*60; |
gert_lauritsen | 4:35024179a736 | 78 | return (float) rpm; |
gert_lauritsen | 4:35024179a736 | 79 | } else return 0; |
gert_lauritsen | 4:35024179a736 | 80 | } |
gert_lauritsen | 4:35024179a736 | 81 | |
gert_lauritsen | 4:35024179a736 | 82 |