Bayley Wang
/
foc-ed_in_the_bot_compact
robot
PwmIn/PwmIn.cpp@75:591556ce033d, 2017-02-24 (annotated)
- Committer:
- bwang
- Date:
- Fri Feb 24 21:37:14 2017 +0000
- Revision:
- 75:591556ce033d
- Parent:
- 19:a6cf15f89f3d
- Child:
- 78:b8df106126a7
throttle edges acquired during commutation are invalidated; physically impossible throttle readings are invalidated; removed two-step throttle filtering code in lieu of superior methods
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; |
bwang | 75:591556ce033d | 13 | |
bwang | 75:591556ce033d | 14 | usecs = usec_min; |
bwang | 75:591556ce033d | 15 | blocked = false; |
bwang | 75:591556ce033d | 16 | enabled = false; |
dicarloj | 13:41d102a53caf | 17 | } |
dicarloj | 13:41d102a53caf | 18 | |
dicarloj | 13:41d102a53caf | 19 | |
bwang | 16:f283d6032fe5 | 20 | bool PwmIn::get_enabled() |
dicarloj | 13:41d102a53caf | 21 | { |
dicarloj | 13:41d102a53caf | 22 | return enabled; |
dicarloj | 13:41d102a53caf | 23 | } |
dicarloj | 13:41d102a53caf | 24 | |
bwang | 16:f283d6032fe5 | 25 | void PwmIn::handle_rise() |
dicarloj | 13:41d102a53caf | 26 | { |
dicarloj | 13:41d102a53caf | 27 | enabled = true; |
bwang | 75:591556ce033d | 28 | |
dicarloj | 13:41d102a53caf | 29 | timer.stop(); |
dicarloj | 13:41d102a53caf | 30 | timer.reset(); |
dicarloj | 13:41d102a53caf | 31 | timer.start(); |
dicarloj | 13:41d102a53caf | 32 | } |
dicarloj | 13:41d102a53caf | 33 | |
bwang | 16:f283d6032fe5 | 34 | void PwmIn::handle_fall() |
dicarloj | 13:41d102a53caf | 35 | { |
bwang | 75:591556ce033d | 36 | int usecs_new = timer.read_us(); |
dicarloj | 13:41d102a53caf | 37 | timer.stop(); |
dicarloj | 13:41d102a53caf | 38 | timer.reset(); |
dicarloj | 13:41d102a53caf | 39 | timer.start(); |
bwang | 75:591556ce033d | 40 | |
bwang | 75:591556ce033d | 41 | if (blocked) { |
bwang | 75:591556ce033d | 42 | blocked = false; |
bwang | 75:591556ce033d | 43 | return; |
bwang | 75:591556ce033d | 44 | } |
bwang | 75:591556ce033d | 45 | |
bwang | 75:591556ce033d | 46 | if (usecs_new <= usec_max) usecs = usecs_new; |
dicarloj | 13:41d102a53caf | 47 | } |
dicarloj | 13:41d102a53caf | 48 | |
bwang | 16:f283d6032fe5 | 49 | float PwmIn::get_throttle() |
dicarloj | 13:41d102a53caf | 50 | { |
dicarloj | 13:41d102a53caf | 51 | if(timer.read_us() > 40000) enabled = false; |
dicarloj | 13:41d102a53caf | 52 | if(!enabled) return -1; |
dicarloj | 13:41d102a53caf | 53 | return constrain(map((float)usecs, usec_min, usec_max, 0, 1), 0, 1); |
dicarloj | 13:41d102a53caf | 54 | } |
dicarloj | 13:41d102a53caf | 55 |