Jared DiCarlo
/
foc-ed_in_the_bot_compact
working version
Fork of foc-ed_in_the_bot_compact by
pwm_in.cpp@13:41d102a53caf, 2016-10-30 (annotated)
- Committer:
- dicarloj
- Date:
- Sun Oct 30 02:06:03 2016 +0000
- Revision:
- 13:41d102a53caf
IT WORKS THIS VERSION
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dicarloj | 13:41d102a53caf | 1 | #include "pwm_in.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 | |
dicarloj | 13:41d102a53caf | 16 | PWM_IN::PWM_IN(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); |
dicarloj | 13:41d102a53caf | 20 | int_in->rise(this, &PWM_IN::handle_rise); |
dicarloj | 13:41d102a53caf | 21 | int_in->fall(this, &PWM_IN::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 | |
dicarloj | 13:41d102a53caf | 27 | bool PWM_IN::get_enabled() |
dicarloj | 13:41d102a53caf | 28 | { |
dicarloj | 13:41d102a53caf | 29 | return enabled; |
dicarloj | 13:41d102a53caf | 30 | } |
dicarloj | 13:41d102a53caf | 31 | |
dicarloj | 13:41d102a53caf | 32 | void PWM_IN::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 | |
dicarloj | 13:41d102a53caf | 41 | void PWM_IN::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 | |
dicarloj | 13:41d102a53caf | 50 | float PWM_IN::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 |