![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Working Thruster and Servo RC
Dependencies: PwmIn mbed Servo
main.cpp@1:1d2b72d09c56, 2019-05-27 (annotated)
- Committer:
- WoodyERAU
- Date:
- Mon May 27 21:56:20 2019 +0000
- Revision:
- 1:1d2b72d09c56
- Parent:
- 0:4fb155dd1c7a
- Child:
- 2:cc7237f357e4
Controller works for LEDs. Controlling thrusters and servos in progress.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WoodyERAU | 0:4fb155dd1c7a | 1 | #include "mbed.h" |
WoodyERAU | 1:1d2b72d09c56 | 2 | #include "Servo.h" |
WoodyERAU | 1:1d2b72d09c56 | 3 | #include "PwmIn.h" |
WoodyERAU | 1:1d2b72d09c56 | 4 | |
WoodyERAU | 1:1d2b72d09c56 | 5 | //Initialize left and right servos |
WoodyERAU | 1:1d2b72d09c56 | 6 | Servo left_servo(p23); |
WoodyERAU | 1:1d2b72d09c56 | 7 | Servo right_servo(p24); |
WoodyERAU | 1:1d2b72d09c56 | 8 | |
WoodyERAU | 1:1d2b72d09c56 | 9 | //Pins for Thruster Power |
WoodyERAU | 1:1d2b72d09c56 | 10 | PwmOut Front(p25); |
WoodyERAU | 1:1d2b72d09c56 | 11 | PwmOut Back(p26); |
WoodyERAU | 1:1d2b72d09c56 | 12 | |
WoodyERAU | 1:1d2b72d09c56 | 13 | //Initialize LEDs |
WoodyERAU | 1:1d2b72d09c56 | 14 | PwmOut led1(LED1); |
WoodyERAU | 1:1d2b72d09c56 | 15 | PwmOut led2(LED2); |
WoodyERAU | 1:1d2b72d09c56 | 16 | PwmOut led3(LED3); |
WoodyERAU | 1:1d2b72d09c56 | 17 | PwmOut led4(LED4); |
WoodyERAU | 1:1d2b72d09c56 | 18 | |
WoodyERAU | 1:1d2b72d09c56 | 19 | //Initialize receiver pins |
WoodyERAU | 1:1d2b72d09c56 | 20 | PwmIn thro(p17); |
WoodyERAU | 1:1d2b72d09c56 | 21 | PwmIn elev(p15); |
WoodyERAU | 1:1d2b72d09c56 | 22 | PwmIn gear(p13); |
WoodyERAU | 1:1d2b72d09c56 | 23 | PwmIn aile(p14); |
WoodyERAU | 1:1d2b72d09c56 | 24 | PwmIn rudd(p16); |
WoodyERAU | 1:1d2b72d09c56 | 25 | |
WoodyERAU | 1:1d2b72d09c56 | 26 | //Set all PWM values to a defaalt of 0 |
WoodyERAU | 1:1d2b72d09c56 | 27 | float throttle_output = 0.0; |
WoodyERAU | 1:1d2b72d09c56 | 28 | float elevation_output = 0.0; |
WoodyERAU | 1:1d2b72d09c56 | 29 | float ESTOP_output = 0.0; |
WoodyERAU | 1:1d2b72d09c56 | 30 | float rudder_output = 0.0; |
WoodyERAU | 1:1d2b72d09c56 | 31 | float aileron_output = 0.0; |
WoodyERAU | 0:4fb155dd1c7a | 32 | |
WoodyERAU | 0:4fb155dd1c7a | 33 | |
WoodyERAU | 1:1d2b72d09c56 | 34 | int main() { |
WoodyERAU | 1:1d2b72d09c56 | 35 | |
WoodyERAU | 1:1d2b72d09c56 | 36 | //Calibration Sequence |
WoodyERAU | 1:1d2b72d09c56 | 37 | left_servo = 0.0; |
WoodyERAU | 1:1d2b72d09c56 | 38 | right_servo = 0.0; |
WoodyERAU | 1:1d2b72d09c56 | 39 | led1 = led2 = led3 = led4 = 1; |
WoodyERAU | 1:1d2b72d09c56 | 40 | wait(0.5); //ESC detects signal |
WoodyERAU | 1:1d2b72d09c56 | 41 | //Required ESC Calibration/Arming sequence |
WoodyERAU | 1:1d2b72d09c56 | 42 | //sends longest and shortest PWM pulse to learn and arm at power on |
WoodyERAU | 1:1d2b72d09c56 | 43 | left_servo = 1.0; //send longest PWM |
WoodyERAU | 1:1d2b72d09c56 | 44 | right_servo = 1.0; |
WoodyERAU | 1:1d2b72d09c56 | 45 | led1 = led2 = led3 = led4 = 0; |
WoodyERAU | 1:1d2b72d09c56 | 46 | wait(1); |
WoodyERAU | 1:1d2b72d09c56 | 47 | right_servo = 0.0; |
WoodyERAU | 1:1d2b72d09c56 | 48 | wait(1); |
WoodyERAU | 1:1d2b72d09c56 | 49 | //End calibration sequence |
WoodyERAU | 1:1d2b72d09c56 | 50 | |
WoodyERAU | 1:1d2b72d09c56 | 51 | while(1) { |
WoodyERAU | 1:1d2b72d09c56 | 52 | /*The progam has to switch from autonomous control and receiver |
WoodyERAU | 1:1d2b72d09c56 | 53 | control based of the 'gear_output' value |
WoodyERAU | 1:1d2b72d09c56 | 54 | */ |
WoodyERAU | 1:1d2b72d09c56 | 55 | |
WoodyERAU | 1:1d2b72d09c56 | 56 | //Read in all PWM signals and set them to a value between 0 and 1 |
WoodyERAU | 1:1d2b72d09c56 | 57 | elevation_output = (elev.pulsewidth()*1000)-1.2; |
WoodyERAU | 1:1d2b72d09c56 | 58 | throttle_output = (thro.pulsewidth()*1000)-1.2; |
WoodyERAU | 1:1d2b72d09c56 | 59 | rudder_output = (rudd.pulsewidth()*1000)-1.2; |
WoodyERAU | 1:1d2b72d09c56 | 60 | aileron_output = (aile.pulsewidth()*1000)-1.2; |
WoodyERAU | 1:1d2b72d09c56 | 61 | |
WoodyERAU | 1:1d2b72d09c56 | 62 | //ESTOP PWM |
WoodyERAU | 1:1d2b72d09c56 | 63 | ESTOP_output = (gear.pulsewidth()*1000)-1; // >.5 run... <.5 STOP |
WoodyERAU | 1:1d2b72d09c56 | 64 | |
WoodyERAU | 1:1d2b72d09c56 | 65 | |
WoodyERAU | 1:1d2b72d09c56 | 66 | if(ESTOP_output > 0.5) |
WoodyERAU | 1:1d2b72d09c56 | 67 | { |
WoodyERAU | 1:1d2b72d09c56 | 68 | led1 = throttle_output; |
WoodyERAU | 1:1d2b72d09c56 | 69 | led2 = aileron_output; |
WoodyERAU | 1:1d2b72d09c56 | 70 | led3 = elevation_output; |
WoodyERAU | 1:1d2b72d09c56 | 71 | led4 = rudder_output; |
WoodyERAU | 1:1d2b72d09c56 | 72 | } |
WoodyERAU | 1:1d2b72d09c56 | 73 | else |
WoodyERAU | 1:1d2b72d09c56 | 74 | { |
WoodyERAU | 1:1d2b72d09c56 | 75 | led1 = led2 = led3 = led4 = 0; |
WoodyERAU | 1:1d2b72d09c56 | 76 | } |
WoodyERAU | 1:1d2b72d09c56 | 77 | |
WoodyERAU | 1:1d2b72d09c56 | 78 | |
WoodyERAU | 1:1d2b72d09c56 | 79 | } |
WoodyERAU | 1:1d2b72d09c56 | 80 | } |
WoodyERAU | 1:1d2b72d09c56 | 81 |