Bayley Wang
/
foc-ed_in_the_bot_compact
robot
PwmIn/PwmIn.cpp@19:a6cf15f89f3d, 2016-11-02 (annotated)
- Committer:
- bwang
- Date:
- Wed Nov 02 12:52:00 2016 +0000
- Revision:
- 19:a6cf15f89f3d
- Parent:
- 18:3863ca45cf26
- Child:
- 75:591556ce033d
formatting changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 19:a6cf15f89f3d | 1 | #include "mbed.h" |
bwang | 18:3863ca45cf26 | 2 | #include "PwmIn.h" |
bwang | 19:a6cf15f89f3d | 3 | #include "MathHelpers.h" |
dicarloj | 13:41d102a53caf | 4 | |
bwang | 16:f283d6032fe5 | 5 | PwmIn::PwmIn(PinName pin, int usec_min, int usec_max) |
dicarloj | 13:41d102a53caf | 6 | { |
dicarloj | 13:41d102a53caf | 7 | int_in = new InterruptIn(pin); |
dicarloj | 13:41d102a53caf | 8 | dig_in = new DigitalIn(pin); |
bwang | 16:f283d6032fe5 | 9 | int_in->rise(this, &PwmIn::handle_rise); |
bwang | 16:f283d6032fe5 | 10 | int_in->fall(this, &PwmIn::handle_fall); |
dicarloj | 13:41d102a53caf | 11 | this->usec_min = usec_min; |
dicarloj | 13:41d102a53caf | 12 | this->usec_max = usec_max; |
dicarloj | 13:41d102a53caf | 13 | } |
dicarloj | 13:41d102a53caf | 14 | |
dicarloj | 13:41d102a53caf | 15 | |
bwang | 16:f283d6032fe5 | 16 | bool PwmIn::get_enabled() |
dicarloj | 13:41d102a53caf | 17 | { |
dicarloj | 13:41d102a53caf | 18 | return enabled; |
dicarloj | 13:41d102a53caf | 19 | } |
dicarloj | 13:41d102a53caf | 20 | |
bwang | 16:f283d6032fe5 | 21 | void PwmIn::handle_rise() |
dicarloj | 13:41d102a53caf | 22 | { |
dicarloj | 13:41d102a53caf | 23 | enabled = true; |
dicarloj | 13:41d102a53caf | 24 | timer.stop(); |
dicarloj | 13:41d102a53caf | 25 | timer.reset(); |
dicarloj | 13:41d102a53caf | 26 | timer.start(); |
dicarloj | 13:41d102a53caf | 27 | was_on = true; |
dicarloj | 13:41d102a53caf | 28 | } |
dicarloj | 13:41d102a53caf | 29 | |
bwang | 16:f283d6032fe5 | 30 | void PwmIn::handle_fall() |
dicarloj | 13:41d102a53caf | 31 | { |
dicarloj | 13:41d102a53caf | 32 | was_on = false; |
dicarloj | 13:41d102a53caf | 33 | usecs = timer.read_us(); |
dicarloj | 13:41d102a53caf | 34 | timer.stop(); |
dicarloj | 13:41d102a53caf | 35 | timer.reset(); |
dicarloj | 13:41d102a53caf | 36 | timer.start(); |
dicarloj | 13:41d102a53caf | 37 | } |
dicarloj | 13:41d102a53caf | 38 | |
bwang | 16:f283d6032fe5 | 39 | float PwmIn::get_throttle() |
dicarloj | 13:41d102a53caf | 40 | { |
dicarloj | 13:41d102a53caf | 41 | if(timer.read_us() > 40000) enabled = false; |
dicarloj | 13:41d102a53caf | 42 | if(!enabled) return -1; |
dicarloj | 13:41d102a53caf | 43 | return constrain(map((float)usecs, usec_min, usec_max, 0, 1), 0, 1); |
dicarloj | 13:41d102a53caf | 44 | } |
dicarloj | 13:41d102a53caf | 45 |