Interface to a standard tarco sensor. Measure the periode and the cal the speed
Tarco.cpp@4:35024179a736, 2013-11-19 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Tue Nov 19 09:19:01 2013 +0000
- Revision:
- 4:35024179a736
- Parent:
- 3:6d3f2034e5f1
- Child:
- 5:451dd1189b0f
Ver 1.0 tarco. ; Kan ikke rigtigt lave missing puls detect
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 | 4:35024179a736 | 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 | 4:35024179a736 | 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 | 4:35024179a736 | 43 | /* |
gert_lauritsen | 4:35024179a736 | 44 | //measure pulstime |
gert_lauritsen | 4:35024179a736 | 45 | t.stop(); |
gert_lauritsen | 4:35024179a736 | 46 | AdvPeriode=t.read_us(); |
gert_lauritsen | 4:35024179a736 | 47 | t.reset(); |
gert_lauritsen | 4:35024179a736 | 48 | t.start();*/ |
gert_lauritsen | 4:35024179a736 | 49 | count++; |
gert_lauritsen | 0:8421e11df32a | 50 | } |
gert_lauritsen | 0:8421e11df32a | 51 | |
gert_lauritsen | 4:35024179a736 | 52 | float Tarco::Speed() |
gert_lauritsen | 4:35024179a736 | 53 | { |
gert_lauritsen | 4:35024179a736 | 54 | //returns puls/sek [hz] |
gert_lauritsen | 4:35024179a736 | 55 | t.stop(); |
gert_lauritsen | 4:35024179a736 | 56 | AdvPeriode=t.read_us()/count; |
gert_lauritsen | 4:35024179a736 | 57 | t.reset(); count=0; |
gert_lauritsen | 4:35024179a736 | 58 | t.start(); |
gert_lauritsen | 4:35024179a736 | 59 | float frek=(1e6/AdvPeriode)/24.3; |
gert_lauritsen | 4:35024179a736 | 60 | return (float) frek; |
gert_lauritsen | 2:c50da089786a | 61 | } |
gert_lauritsen | 4:35024179a736 | 62 | |
gert_lauritsen | 4:35024179a736 | 63 | float Tarco::RPM() |
gert_lauritsen | 4:35024179a736 | 64 | { |
gert_lauritsen | 4:35024179a736 | 65 | if (AdvPeriode) { |
gert_lauritsen | 4:35024179a736 | 66 | float rpm=(60e6/AdvPeriode)/24.3; |
gert_lauritsen | 4:35024179a736 | 67 | return (float) rpm; |
gert_lauritsen | 4:35024179a736 | 68 | } else return 0; |
gert_lauritsen | 4:35024179a736 | 69 | } |
gert_lauritsen | 4:35024179a736 | 70 | |
gert_lauritsen | 4:35024179a736 | 71 |