Serial Controll of a Quadricopter's ESC using -Hobby King x230 -ZTW 12A ESC -Brushless 2222 three phase motors
main.cpp@0:ceb5a7253afd, 2013-10-19 (annotated)
- Committer:
- awmiller
- Date:
- Sat Oct 19 17:21:00 2013 +0000
- Revision:
- 0:ceb5a7253afd
main
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
awmiller | 0:ceb5a7253afd | 1 | #include "mbed.h" |
awmiller | 0:ceb5a7253afd | 2 | |
awmiller | 0:ceb5a7253afd | 3 | PwmOut led1(LED1); |
awmiller | 0:ceb5a7253afd | 4 | PwmOut led2(LED2); |
awmiller | 0:ceb5a7253afd | 5 | PwmOut mP22(p22); |
awmiller | 0:ceb5a7253afd | 6 | Serial pc(USBTX, USBRX); // tx, rx |
awmiller | 0:ceb5a7253afd | 7 | |
awmiller | 0:ceb5a7253afd | 8 | float LastPosition =0.02; |
awmiller | 0:ceb5a7253afd | 9 | volatile float Scale = 1; |
awmiller | 0:ceb5a7253afd | 10 | volatile float Multiplier = 100; |
awmiller | 0:ceb5a7253afd | 11 | volatile float offset = 1000; |
awmiller | 0:ceb5a7253afd | 12 | volatile char SetScale;// =0; |
awmiller | 0:ceb5a7253afd | 13 | volatile char SetMultiplier;//=0; |
awmiller | 0:ceb5a7253afd | 14 | |
awmiller | 0:ceb5a7253afd | 15 | void SerialPC_RX_CB() { |
awmiller | 0:ceb5a7253afd | 16 | // Note: you need to actually read from the serial to clear the RX interrupt |
awmiller | 0:ceb5a7253afd | 17 | char RXchar = pc.getc(); |
awmiller | 0:ceb5a7253afd | 18 | |
awmiller | 0:ceb5a7253afd | 19 | if(SetScale == 1) |
awmiller | 0:ceb5a7253afd | 20 | { |
awmiller | 0:ceb5a7253afd | 21 | if( (RXchar > '0') && (RXchar <= '9')) |
awmiller | 0:ceb5a7253afd | 22 | { |
awmiller | 0:ceb5a7253afd | 23 | SetScale =0; |
awmiller | 0:ceb5a7253afd | 24 | Scale = (RXchar - '0'); |
awmiller | 0:ceb5a7253afd | 25 | pc.printf("Scale Set\r\n"); |
awmiller | 0:ceb5a7253afd | 26 | } |
awmiller | 0:ceb5a7253afd | 27 | else pc.printf("Scale values must be 0-9"); |
awmiller | 0:ceb5a7253afd | 28 | } |
awmiller | 0:ceb5a7253afd | 29 | |
awmiller | 0:ceb5a7253afd | 30 | else |
awmiller | 0:ceb5a7253afd | 31 | if(SetMultiplier == 1) |
awmiller | 0:ceb5a7253afd | 32 | { |
awmiller | 0:ceb5a7253afd | 33 | if( (RXchar > '0') && (RXchar <= '9')) |
awmiller | 0:ceb5a7253afd | 34 | { |
awmiller | 0:ceb5a7253afd | 35 | SetMultiplier =0; |
awmiller | 0:ceb5a7253afd | 36 | Multiplier =1; |
awmiller | 0:ceb5a7253afd | 37 | for(int i =0;i < (RXchar - '0'); i++) |
awmiller | 0:ceb5a7253afd | 38 | Multiplier*=10; |
awmiller | 0:ceb5a7253afd | 39 | pc.printf("Multiplier Set to %f\r\n",Multiplier); |
awmiller | 0:ceb5a7253afd | 40 | } |
awmiller | 0:ceb5a7253afd | 41 | else { |
awmiller | 0:ceb5a7253afd | 42 | pc.printf("Multiplier values must be 0-3 representing zeroes"); |
awmiller | 0:ceb5a7253afd | 43 | } |
awmiller | 0:ceb5a7253afd | 44 | } |
awmiller | 0:ceb5a7253afd | 45 | |
awmiller | 0:ceb5a7253afd | 46 | else |
awmiller | 0:ceb5a7253afd | 47 | if((RXchar > 0x29) && (RXchar < 0x40)){ |
awmiller | 0:ceb5a7253afd | 48 | float Delta = ((float)( RXchar - '0')); |
awmiller | 0:ceb5a7253afd | 49 | Delta = (Delta*Scale * Multiplier)+offset; |
awmiller | 0:ceb5a7253afd | 50 | mP22.pulsewidth_us(Delta); |
awmiller | 0:ceb5a7253afd | 51 | pc.printf("Pulse Width Changed to : %f \r\n",Delta); |
awmiller | 0:ceb5a7253afd | 52 | |
awmiller | 0:ceb5a7253afd | 53 | } |
awmiller | 0:ceb5a7253afd | 54 | |
awmiller | 0:ceb5a7253afd | 55 | else |
awmiller | 0:ceb5a7253afd | 56 | if(RXchar == 's') |
awmiller | 0:ceb5a7253afd | 57 | { |
awmiller | 0:ceb5a7253afd | 58 | SetScale = 1; |
awmiller | 0:ceb5a7253afd | 59 | } |
awmiller | 0:ceb5a7253afd | 60 | if(RXchar == 'm') |
awmiller | 0:ceb5a7253afd | 61 | { |
awmiller | 0:ceb5a7253afd | 62 | SetMultiplier =1; |
awmiller | 0:ceb5a7253afd | 63 | } |
awmiller | 0:ceb5a7253afd | 64 | |
awmiller | 0:ceb5a7253afd | 65 | //else{ if(RXchar == 'm') mP22 = 0.01; |
awmiller | 0:ceb5a7253afd | 66 | // else if(RXchar == 'u') mP22 = 1.2; |
awmiller | 0:ceb5a7253afd | 67 | // } |
awmiller | 0:ceb5a7253afd | 68 | |
awmiller | 0:ceb5a7253afd | 69 | } |
awmiller | 0:ceb5a7253afd | 70 | |
awmiller | 0:ceb5a7253afd | 71 | int main() { |
awmiller | 0:ceb5a7253afd | 72 | |
awmiller | 0:ceb5a7253afd | 73 | pc.attach(&SerialPC_RX_CB); |
awmiller | 0:ceb5a7253afd | 74 | |
awmiller | 0:ceb5a7253afd | 75 | mP22 = LastPosition; |
awmiller | 0:ceb5a7253afd | 76 | SetScale = 0; |
awmiller | 0:ceb5a7253afd | 77 | SetMultiplier = 0; |
awmiller | 0:ceb5a7253afd | 78 | |
awmiller | 0:ceb5a7253afd | 79 | pc.printf("Set Scale/Offset with (s/o)\r\nEnter a number from 0-9 \r\n"); |
awmiller | 0:ceb5a7253afd | 80 | |
awmiller | 0:ceb5a7253afd | 81 | while(1) |
awmiller | 0:ceb5a7253afd | 82 | { |
awmiller | 0:ceb5a7253afd | 83 | if (SetScale == 1) |
awmiller | 0:ceb5a7253afd | 84 | { |
awmiller | 0:ceb5a7253afd | 85 | pc.printf("Change the Scale Factor according to P(x) = Offset + (Scale* Multiplier * x) \r\n"); |
awmiller | 0:ceb5a7253afd | 86 | while(SetScale == 1){ |
awmiller | 0:ceb5a7253afd | 87 | led2 = !led2; |
awmiller | 0:ceb5a7253afd | 88 | wait(100); |
awmiller | 0:ceb5a7253afd | 89 | } |
awmiller | 0:ceb5a7253afd | 90 | |
awmiller | 0:ceb5a7253afd | 91 | led2 = 0; |
awmiller | 0:ceb5a7253afd | 92 | //wait(100); |
awmiller | 0:ceb5a7253afd | 93 | pc.printf("Set Scale/Offset with (s/o)\r\nEnter a number from 0-9 \r\n"); |
awmiller | 0:ceb5a7253afd | 94 | } |
awmiller | 0:ceb5a7253afd | 95 | else if(SetMultiplier ==1) |
awmiller | 0:ceb5a7253afd | 96 | { |
awmiller | 0:ceb5a7253afd | 97 | pc.printf("Change the Multiplier according to P(x) = Offset + (Scale* Multiplier * x) \r\n"); |
awmiller | 0:ceb5a7253afd | 98 | while(SetMultiplier == 1){ |
awmiller | 0:ceb5a7253afd | 99 | led1 = !led1; |
awmiller | 0:ceb5a7253afd | 100 | wait(100); |
awmiller | 0:ceb5a7253afd | 101 | } |
awmiller | 0:ceb5a7253afd | 102 | led1 = 0; |
awmiller | 0:ceb5a7253afd | 103 | //wait(100); |
awmiller | 0:ceb5a7253afd | 104 | pc.printf("Set Scale/Offset with (s/o)\r\nEnter a number from 0-9 \r\n"); |
awmiller | 0:ceb5a7253afd | 105 | } |
awmiller | 0:ceb5a7253afd | 106 | |
awmiller | 0:ceb5a7253afd | 107 | } |
awmiller | 0:ceb5a7253afd | 108 | |
awmiller | 0:ceb5a7253afd | 109 | } |
awmiller | 0:ceb5a7253afd | 110 |