
Linear Servo MBED Control
main.cpp@0:33e05a8577f2, 2018-02-09 (annotated)
- Committer:
- m182376
- Date:
- Fri Feb 09 02:00:48 2018 +0000
- Revision:
- 0:33e05a8577f2
Linear Servo MBED Control
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
m182376 | 0:33e05a8577f2 | 1 | #include "mbed.h" |
m182376 | 0:33e05a8577f2 | 2 | #include "Servo.h" |
m182376 | 0:33e05a8577f2 | 3 | #include "Timer.h" |
m182376 | 0:33e05a8577f2 | 4 | |
m182376 | 0:33e05a8577f2 | 5 | DigitalIn pixhawk(LED1); |
m182376 | 0:33e05a8577f2 | 6 | Servo gas(p21); |
m182376 | 0:33e05a8577f2 | 7 | Servo brake(p22); |
m182376 | 0:33e05a8577f2 | 8 | Timer timer; |
m182376 | 0:33e05a8577f2 | 9 | float delta; |
m182376 | 0:33e05a8577f2 | 10 | |
m182376 | 0:33e05a8577f2 | 11 | int main() |
m182376 | 0:33e05a8577f2 | 12 | { |
m182376 | 0:33e05a8577f2 | 13 | while(1) { |
m182376 | 0:33e05a8577f2 | 14 | // read pulseIn |
m182376 | 0:33e05a8577f2 | 15 | while (!pixhawk); // wait for high 1 or -1 |
m182376 | 0:33e05a8577f2 | 16 | timer.reset(); |
m182376 | 0:33e05a8577f2 | 17 | timer.start(); |
m182376 | 0:33e05a8577f2 | 18 | while (pixhawk); // wait for low |
m182376 | 0:33e05a8577f2 | 19 | timer.stop(); |
m182376 | 0:33e05a8577f2 | 20 | delta = timer.read_us(); //provides pwm on time in microseconds |
m182376 | 0:33e05a8577f2 | 21 | |
m182376 | 0:33e05a8577f2 | 22 | if(delta > 0) { |
m182376 | 0:33e05a8577f2 | 23 | gas = delta*1000; // convert to PWM signal for gas |
m182376 | 0:33e05a8577f2 | 24 | } else if(delta <= 0 && delta > 0.33) { |
m182376 | 0:33e05a8577f2 | 25 | brake = -.4; // light braking |
m182376 | 0:33e05a8577f2 | 26 | } else { |
m182376 | 0:33e05a8577f2 | 27 | brake = -1; // full brake |
m182376 | 0:33e05a8577f2 | 28 | } |
m182376 | 0:33e05a8577f2 | 29 | } |
m182376 | 0:33e05a8577f2 | 30 | } |