![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Xbee robot with telemetry, controller code
Dependencies: 4DGL-uLCD-SE DebounceIn PinDetect mbed-rtos mbed
Fork of 2Xbee_Controller by
main.cpp@1:2c67639795a4, 2016-04-29 (annotated)
- Committer:
- yhbyhb4433
- Date:
- Fri Apr 29 19:08:24 2016 +0000
- Revision:
- 1:2c67639795a4
- Parent:
- 0:9439ccb44422
- Child:
- 2:887ff47839ed
Xbee robot with telemetry_controller code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yhbyhb4433 | 1:2c67639795a4 | 1 | |
4180_1 | 0:9439ccb44422 | 2 | #include "mbed.h" |
yhbyhb4433 | 1:2c67639795a4 | 3 | #include "DebounceIn.h" |
yhbyhb4433 | 1:2c67639795a4 | 4 | #include "PinDetect.h" |
yhbyhb4433 | 1:2c67639795a4 | 5 | #include "uLCD_4DGL.h" |
4180_1 | 0:9439ccb44422 | 6 | #include "rtos.h" |
yhbyhb4433 | 1:2c67639795a4 | 7 | |
yhbyhb4433 | 1:2c67639795a4 | 8 | Ticker flipper; |
yhbyhb4433 | 1:2c67639795a4 | 9 | uLCD_4DGL uLCD(p13,p14,p28); // serial tx, serial rx, reset pin; |
yhbyhb4433 | 1:2c67639795a4 | 10 | Serial xbee1(p9, p10); |
yhbyhb4433 | 1:2c67639795a4 | 11 | DigitalOut rst1(p11); |
yhbyhb4433 | 1:2c67639795a4 | 12 | DebounceIn forward(p16); |
yhbyhb4433 | 1:2c67639795a4 | 13 | DebounceIn left(p17); |
yhbyhb4433 | 1:2c67639795a4 | 14 | DebounceIn reverse(p18); |
yhbyhb4433 | 1:2c67639795a4 | 15 | DebounceIn right(p24); |
yhbyhb4433 | 1:2c67639795a4 | 16 | //forward 16 |
yhbyhb4433 | 1:2c67639795a4 | 17 | //left 17 |
yhbyhb4433 | 1:2c67639795a4 | 18 | //reverse 18 |
yhbyhb4433 | 1:2c67639795a4 | 19 | //right 19 |
yhbyhb4433 | 1:2c67639795a4 | 20 | |
yhbyhb4433 | 1:2c67639795a4 | 21 | Serial pc(USBTX, USBRX); |
yhbyhb4433 | 1:2c67639795a4 | 22 | DigitalOut led1(LED1); |
yhbyhb4433 | 1:2c67639795a4 | 23 | DigitalOut led2(LED2); |
yhbyhb4433 | 1:2c67639795a4 | 24 | DigitalOut led3(LED3); |
yhbyhb4433 | 1:2c67639795a4 | 25 | DigitalOut led4(LED4); |
yhbyhb4433 | 1:2c67639795a4 | 26 | Mutex xbee_lock; |
yhbyhb4433 | 1:2c67639795a4 | 27 | Mutex uLCD_lock; |
yhbyhb4433 | 1:2c67639795a4 | 28 | int a; |
yhbyhb4433 | 1:2c67639795a4 | 29 | volatile int voltage; |
yhbyhb4433 | 1:2c67639795a4 | 30 | volatile int distance; |
4180_1 | 0:9439ccb44422 | 31 | |
yhbyhb4433 | 1:2c67639795a4 | 32 | |
yhbyhb4433 | 1:2c67639795a4 | 33 | void read_battery() |
yhbyhb4433 | 1:2c67639795a4 | 34 | { |
yhbyhb4433 | 1:2c67639795a4 | 35 | |
yhbyhb4433 | 1:2c67639795a4 | 36 | xbee1.putc(15); |
yhbyhb4433 | 1:2c67639795a4 | 37 | //if (xbee1.readable()) { |
yhbyhb4433 | 1:2c67639795a4 | 38 | uLCD_lock.lock(); |
yhbyhb4433 | 1:2c67639795a4 | 39 | voltage = xbee1.getc(); |
yhbyhb4433 | 1:2c67639795a4 | 40 | uLCD_lock.unlock(); |
yhbyhb4433 | 1:2c67639795a4 | 41 | //} |
yhbyhb4433 | 1:2c67639795a4 | 42 | //pc.printf("voltage: %i\n",voltage); |
4180_1 | 0:9439ccb44422 | 43 | } |
yhbyhb4433 | 1:2c67639795a4 | 44 | |
yhbyhb4433 | 1:2c67639795a4 | 45 | void read_distance() |
yhbyhb4433 | 1:2c67639795a4 | 46 | { |
yhbyhb4433 | 1:2c67639795a4 | 47 | xbee1.putc(16); |
yhbyhb4433 | 1:2c67639795a4 | 48 | //if (xbee1.readable()) { |
yhbyhb4433 | 1:2c67639795a4 | 49 | uLCD_lock.lock(); |
yhbyhb4433 | 1:2c67639795a4 | 50 | distance = xbee1.getc(); |
yhbyhb4433 | 1:2c67639795a4 | 51 | uLCD_lock.unlock(); |
yhbyhb4433 | 1:2c67639795a4 | 52 | //} |
yhbyhb4433 | 1:2c67639795a4 | 53 | //pc.printf("distance: %i\n",distance); |
yhbyhb4433 | 1:2c67639795a4 | 54 | } |
yhbyhb4433 | 1:2c67639795a4 | 55 | void display_distance(void const *args) |
yhbyhb4433 | 1:2c67639795a4 | 56 | { |
4180_1 | 0:9439ccb44422 | 57 | while(1) { |
yhbyhb4433 | 1:2c67639795a4 | 58 | |
yhbyhb4433 | 1:2c67639795a4 | 59 | uLCD_lock.lock(); |
yhbyhb4433 | 1:2c67639795a4 | 60 | uLCD.locate(1,5); |
yhbyhb4433 | 1:2c67639795a4 | 61 | float temp = float(1/(float(distance)/232*3.3))*30.53-2.64; |
yhbyhb4433 | 1:2c67639795a4 | 62 | uLCD.printf("%3.3fcm",temp); |
yhbyhb4433 | 1:2c67639795a4 | 63 | uLCD_lock.unlock(); |
yhbyhb4433 | 1:2c67639795a4 | 64 | //pc.printf("%i\n",distance); |
yhbyhb4433 | 1:2c67639795a4 | 65 | Thread::wait(300); |
yhbyhb4433 | 1:2c67639795a4 | 66 | |
4180_1 | 0:9439ccb44422 | 67 | } |
4180_1 | 0:9439ccb44422 | 68 | } |
4180_1 | 0:9439ccb44422 | 69 | |
yhbyhb4433 | 1:2c67639795a4 | 70 | void display_battery(void const *args) |
yhbyhb4433 | 1:2c67639795a4 | 71 | { |
4180_1 | 0:9439ccb44422 | 72 | while(1) { |
yhbyhb4433 | 1:2c67639795a4 | 73 | uLCD_lock.lock(); |
yhbyhb4433 | 1:2c67639795a4 | 74 | uLCD.locate(1,3); |
yhbyhb4433 | 1:2c67639795a4 | 75 | uLCD.printf("%2.2fV",float(voltage)/232*3.3*2); |
yhbyhb4433 | 1:2c67639795a4 | 76 | //pc.printf("%i\n",voltage); |
yhbyhb4433 | 1:2c67639795a4 | 77 | uLCD_lock.unlock(); |
yhbyhb4433 | 1:2c67639795a4 | 78 | Thread::wait(300); |
4180_1 | 0:9439ccb44422 | 79 | } |
4180_1 | 0:9439ccb44422 | 80 | } |
4180_1 | 0:9439ccb44422 | 81 | |
yhbyhb4433 | 1:2c67639795a4 | 82 | int main() |
yhbyhb4433 | 1:2c67639795a4 | 83 | { |
yhbyhb4433 | 1:2c67639795a4 | 84 | forward.mode(PullUp); |
yhbyhb4433 | 1:2c67639795a4 | 85 | reverse.mode(PullUp); |
yhbyhb4433 | 1:2c67639795a4 | 86 | left.mode(PullUp); |
yhbyhb4433 | 1:2c67639795a4 | 87 | right.mode(PullUp); |
yhbyhb4433 | 1:2c67639795a4 | 88 | |
yhbyhb4433 | 1:2c67639795a4 | 89 | // reset the xbees (at least 200ns) |
yhbyhb4433 | 1:2c67639795a4 | 90 | rst1 = 0; |
yhbyhb4433 | 1:2c67639795a4 | 91 | wait_ms(100); |
yhbyhb4433 | 1:2c67639795a4 | 92 | rst1 = 1; |
yhbyhb4433 | 1:2c67639795a4 | 93 | wait_ms(100); |
yhbyhb4433 | 1:2c67639795a4 | 94 | uLCD.baudrate(3000000); |
yhbyhb4433 | 1:2c67639795a4 | 95 | uLCD.cls(); |
yhbyhb4433 | 1:2c67639795a4 | 96 | wait(.01); |
yhbyhb4433 | 1:2c67639795a4 | 97 | uLCD.locate(1,2); |
yhbyhb4433 | 1:2c67639795a4 | 98 | uLCD.printf("Battery:\n"); |
yhbyhb4433 | 1:2c67639795a4 | 99 | uLCD.locate(1,4); |
yhbyhb4433 | 1:2c67639795a4 | 100 | uLCD.printf("Distance:\n"); |
yhbyhb4433 | 1:2c67639795a4 | 101 | //Thread thread2(read_distance); |
yhbyhb4433 | 1:2c67639795a4 | 102 | //Thread thread3(read_battery); |
yhbyhb4433 | 1:2c67639795a4 | 103 | Thread thread4(display_distance); |
yhbyhb4433 | 1:2c67639795a4 | 104 | Thread thread5(display_battery); |
yhbyhb4433 | 1:2c67639795a4 | 105 | |
yhbyhb4433 | 1:2c67639795a4 | 106 | //led1=1; |
yhbyhb4433 | 1:2c67639795a4 | 107 | //} |
yhbyhb4433 | 1:2c67639795a4 | 108 | while (1) { |
yhbyhb4433 | 1:2c67639795a4 | 109 | if (forward==0) { |
yhbyhb4433 | 1:2c67639795a4 | 110 | led1 = 1; |
yhbyhb4433 | 1:2c67639795a4 | 111 | xbee1.putc(11); |
yhbyhb4433 | 1:2c67639795a4 | 112 | } else if (reverse==0) { |
yhbyhb4433 | 1:2c67639795a4 | 113 | led3 = 1; |
yhbyhb4433 | 1:2c67639795a4 | 114 | xbee1.putc(12); |
yhbyhb4433 | 1:2c67639795a4 | 115 | } else if (left==0) { |
yhbyhb4433 | 1:2c67639795a4 | 116 | led2 = 1; |
yhbyhb4433 | 1:2c67639795a4 | 117 | xbee1.putc(13); |
yhbyhb4433 | 1:2c67639795a4 | 118 | } else if (right == 0) { |
yhbyhb4433 | 1:2c67639795a4 | 119 | led4 = 1; |
yhbyhb4433 | 1:2c67639795a4 | 120 | xbee1.putc(14); |
yhbyhb4433 | 1:2c67639795a4 | 121 | } else { |
yhbyhb4433 | 1:2c67639795a4 | 122 | xbee1.putc(0); |
yhbyhb4433 | 1:2c67639795a4 | 123 | led1=led2=led3=led4=0; |
yhbyhb4433 | 1:2c67639795a4 | 124 | } |
yhbyhb4433 | 1:2c67639795a4 | 125 | read_distance(); |
yhbyhb4433 | 1:2c67639795a4 | 126 | read_battery(); |
4180_1 | 0:9439ccb44422 | 127 | } |
yhbyhb4433 | 1:2c67639795a4 | 128 | } |