Bayley Wang
/
foc-ed_in_the_bot_compact
robot
PwmIn/PwmIn.cpp@18:3863ca45cf26, 2016-10-31 (annotated)
- Committer:
- bwang
- Date:
- Mon Oct 31 06:53:28 2016 +0000
- Revision:
- 18:3863ca45cf26
- Parent:
- pwm_in.cpp@16:f283d6032fe5
- Child:
- 19:a6cf15f89f3d
added functionality for torque->dq, throttle->torque commands
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 18:3863ca45cf26 | 1 | #include "PwmIn.h" |
dicarloj | 13:41d102a53caf | 2 | #include "mbed.h" |
dicarloj | 13:41d102a53caf | 3 | |
dicarloj | 13:41d102a53caf | 4 | float map(float x, float in_min, float in_max, float out_min, float out_max) |
dicarloj | 13:41d102a53caf | 5 | { |
dicarloj | 13:41d102a53caf | 6 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
dicarloj | 13:41d102a53caf | 7 | } |
dicarloj | 13:41d102a53caf | 8 | |
dicarloj | 13:41d102a53caf | 9 | float constrain(float in, float min, float max) |
dicarloj | 13:41d102a53caf | 10 | { |
dicarloj | 13:41d102a53caf | 11 | if(in > max) return max; |
dicarloj | 13:41d102a53caf | 12 | if(in < min) return min; |
dicarloj | 13:41d102a53caf | 13 | return in; |
dicarloj | 13:41d102a53caf | 14 | } |
dicarloj | 13:41d102a53caf | 15 | |
bwang | 16:f283d6032fe5 | 16 | PwmIn::PwmIn(PinName pin, int usec_min, int usec_max) |
dicarloj | 13:41d102a53caf | 17 | { |
dicarloj | 13:41d102a53caf | 18 | int_in = new InterruptIn(pin); |
dicarloj | 13:41d102a53caf | 19 | dig_in = new DigitalIn(pin); |
bwang | 16:f283d6032fe5 | 20 | int_in->rise(this, &PwmIn::handle_rise); |
bwang | 16:f283d6032fe5 | 21 | int_in->fall(this, &PwmIn::handle_fall); |
dicarloj | 13:41d102a53caf | 22 | this->usec_min = usec_min; |
dicarloj | 13:41d102a53caf | 23 | this->usec_max = usec_max; |
dicarloj | 13:41d102a53caf | 24 | } |
dicarloj | 13:41d102a53caf | 25 | |
dicarloj | 13:41d102a53caf | 26 | |
bwang | 16:f283d6032fe5 | 27 | bool PwmIn::get_enabled() |
dicarloj | 13:41d102a53caf | 28 | { |
dicarloj | 13:41d102a53caf | 29 | return enabled; |
dicarloj | 13:41d102a53caf | 30 | } |
dicarloj | 13:41d102a53caf | 31 | |
bwang | 16:f283d6032fe5 | 32 | void PwmIn::handle_rise() |
dicarloj | 13:41d102a53caf | 33 | { |
dicarloj | 13:41d102a53caf | 34 | enabled = true; |
dicarloj | 13:41d102a53caf | 35 | timer.stop(); |
dicarloj | 13:41d102a53caf | 36 | timer.reset(); |
dicarloj | 13:41d102a53caf | 37 | timer.start(); |
dicarloj | 13:41d102a53caf | 38 | was_on = true; |
dicarloj | 13:41d102a53caf | 39 | } |
dicarloj | 13:41d102a53caf | 40 | |
bwang | 16:f283d6032fe5 | 41 | void PwmIn::handle_fall() |
dicarloj | 13:41d102a53caf | 42 | { |
dicarloj | 13:41d102a53caf | 43 | was_on = false; |
dicarloj | 13:41d102a53caf | 44 | usecs = timer.read_us(); |
dicarloj | 13:41d102a53caf | 45 | timer.stop(); |
dicarloj | 13:41d102a53caf | 46 | timer.reset(); |
dicarloj | 13:41d102a53caf | 47 | timer.start(); |
dicarloj | 13:41d102a53caf | 48 | } |
dicarloj | 13:41d102a53caf | 49 | |
bwang | 16:f283d6032fe5 | 50 | float PwmIn::get_throttle() |
dicarloj | 13:41d102a53caf | 51 | { |
dicarloj | 13:41d102a53caf | 52 | if(timer.read_us() > 40000) enabled = false; |
dicarloj | 13:41d102a53caf | 53 | if(!enabled) return -1; |
dicarloj | 13:41d102a53caf | 54 | return constrain(map((float)usecs, usec_min, usec_max, 0, 1), 0, 1); |
dicarloj | 13:41d102a53caf | 55 | } |
dicarloj | 13:41d102a53caf | 56 |