Xbee robot with telemetry, controller code

Dependencies:   4DGL-uLCD-SE DebounceIn PinDetect mbed-rtos mbed

Fork of 2Xbee_Controller by Hanbin Ying

Committer:
yhbyhb4433
Date:
Fri Apr 29 19:42:40 2016 +0000
Revision:
2:887ff47839ed
Parent:
1:2c67639795a4
Xbee robot with telemetry, controller code

Who changed what in which revision?

UserRevisionLine numberNew 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 2:887ff47839ed 21 DigitalOut led1(LED1); //four leds to display the command users send in
yhbyhb4433 1:2c67639795a4 22 DigitalOut led2(LED2);
yhbyhb4433 1:2c67639795a4 23 DigitalOut led3(LED3);
yhbyhb4433 1:2c67639795a4 24 DigitalOut led4(LED4);
yhbyhb4433 2:887ff47839ed 25 Mutex uLCD_lock; //mutex lock for the uLCD communication
yhbyhb4433 1:2c67639795a4 26 volatile int voltage;
yhbyhb4433 1:2c67639795a4 27 volatile int distance;
4180_1 0:9439ccb44422 28
yhbyhb4433 1:2c67639795a4 29
yhbyhb4433 2:887ff47839ed 30 void read_battery()
yhbyhb4433 2:887ff47839ed 31 //read battery information back from the robot, need a mutex lock when
yhbyhb4433 2:887ff47839ed 32 //the information is written to the variable, otherwise "display_battery"
yhbyhb4433 2:887ff47839ed 33 //thread will try to access the the variable while it's changed
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 2:887ff47839ed 45 void read_distance()
yhbyhb4433 2:887ff47839ed 46 //read distance information back from the robot, need a mutex lock when
yhbyhb4433 2:887ff47839ed 47 //the information is written to the variable, otherwise "display_distance"
yhbyhb4433 2:887ff47839ed 48 //thread will try to access the the variable while it's changed
yhbyhb4433 1:2c67639795a4 49 {
yhbyhb4433 1:2c67639795a4 50 xbee1.putc(16);
yhbyhb4433 1:2c67639795a4 51 //if (xbee1.readable()) {
yhbyhb4433 1:2c67639795a4 52 uLCD_lock.lock();
yhbyhb4433 1:2c67639795a4 53 distance = xbee1.getc();
yhbyhb4433 1:2c67639795a4 54 uLCD_lock.unlock();
yhbyhb4433 1:2c67639795a4 55 //}
yhbyhb4433 1:2c67639795a4 56 //pc.printf("distance: %i\n",distance);
yhbyhb4433 1:2c67639795a4 57 }
yhbyhb4433 2:887ff47839ed 58 void display_distance(void const *args) //update the distance sensor information in the LCD once every 0.3s
yhbyhb4433 1:2c67639795a4 59 {
4180_1 0:9439ccb44422 60 while(1) {
yhbyhb4433 1:2c67639795a4 61
yhbyhb4433 1:2c67639795a4 62 uLCD_lock.lock();
yhbyhb4433 1:2c67639795a4 63 uLCD.locate(1,5);
yhbyhb4433 2:887ff47839ed 64 float temp = float(1/(float(distance)/232*3.3))*30.53-2.64; //distance*232 scales reading back to a float between 0-1. Then a linear
yhbyhb4433 2:887ff47839ed 65 //approximation is used to convert float reading to cm reading
yhbyhb4433 1:2c67639795a4 66 uLCD.printf("%3.3fcm",temp);
yhbyhb4433 1:2c67639795a4 67 uLCD_lock.unlock();
yhbyhb4433 1:2c67639795a4 68 //pc.printf("%i\n",distance);
yhbyhb4433 1:2c67639795a4 69 Thread::wait(300);
yhbyhb4433 1:2c67639795a4 70
4180_1 0:9439ccb44422 71 }
4180_1 0:9439ccb44422 72 }
4180_1 0:9439ccb44422 73
yhbyhb4433 2:887ff47839ed 74 void display_battery(void const *args) //update the battery information in the LCD once every 0.3s
yhbyhb4433 1:2c67639795a4 75 {
4180_1 0:9439ccb44422 76 while(1) {
yhbyhb4433 1:2c67639795a4 77 uLCD_lock.lock();
yhbyhb4433 1:2c67639795a4 78 uLCD.locate(1,3);
yhbyhb4433 1:2c67639795a4 79 uLCD.printf("%2.2fV",float(voltage)/232*3.3*2);
yhbyhb4433 1:2c67639795a4 80 //pc.printf("%i\n",voltage);
yhbyhb4433 1:2c67639795a4 81 uLCD_lock.unlock();
yhbyhb4433 1:2c67639795a4 82 Thread::wait(300);
4180_1 0:9439ccb44422 83 }
4180_1 0:9439ccb44422 84 }
4180_1 0:9439ccb44422 85
yhbyhb4433 1:2c67639795a4 86 int main()
yhbyhb4433 1:2c67639795a4 87 {
yhbyhb4433 1:2c67639795a4 88 forward.mode(PullUp);
yhbyhb4433 1:2c67639795a4 89 reverse.mode(PullUp);
yhbyhb4433 1:2c67639795a4 90 left.mode(PullUp);
yhbyhb4433 1:2c67639795a4 91 right.mode(PullUp);
yhbyhb4433 1:2c67639795a4 92
yhbyhb4433 1:2c67639795a4 93 // reset the xbees (at least 200ns)
yhbyhb4433 1:2c67639795a4 94 rst1 = 0;
yhbyhb4433 1:2c67639795a4 95 wait_ms(100);
yhbyhb4433 1:2c67639795a4 96 rst1 = 1;
yhbyhb4433 1:2c67639795a4 97 wait_ms(100);
yhbyhb4433 2:887ff47839ed 98 uLCD.baudrate(3000000); //update the
yhbyhb4433 1:2c67639795a4 99 uLCD.cls();
yhbyhb4433 1:2c67639795a4 100 wait(.01);
yhbyhb4433 1:2c67639795a4 101 uLCD.locate(1,2);
yhbyhb4433 1:2c67639795a4 102 uLCD.printf("Battery:\n");
yhbyhb4433 1:2c67639795a4 103 uLCD.locate(1,4);
yhbyhb4433 1:2c67639795a4 104 uLCD.printf("Distance:\n");
yhbyhb4433 1:2c67639795a4 105 Thread thread4(display_distance);
yhbyhb4433 1:2c67639795a4 106 Thread thread5(display_battery);
yhbyhb4433 1:2c67639795a4 107
yhbyhb4433 1:2c67639795a4 108 while (1) {
yhbyhb4433 2:887ff47839ed 109 // loop to check if each pushbutton is pushed. If pushed, send a corresponding command to the robot
yhbyhb4433 1:2c67639795a4 110 if (forward==0) {
yhbyhb4433 1:2c67639795a4 111 led1 = 1;
yhbyhb4433 1:2c67639795a4 112 xbee1.putc(11);
yhbyhb4433 1:2c67639795a4 113 } else if (reverse==0) {
yhbyhb4433 1:2c67639795a4 114 led3 = 1;
yhbyhb4433 1:2c67639795a4 115 xbee1.putc(12);
yhbyhb4433 1:2c67639795a4 116 } else if (left==0) {
yhbyhb4433 1:2c67639795a4 117 led2 = 1;
yhbyhb4433 1:2c67639795a4 118 xbee1.putc(13);
yhbyhb4433 1:2c67639795a4 119 } else if (right == 0) {
yhbyhb4433 1:2c67639795a4 120 led4 = 1;
yhbyhb4433 1:2c67639795a4 121 xbee1.putc(14);
yhbyhb4433 1:2c67639795a4 122 } else {
yhbyhb4433 1:2c67639795a4 123 xbee1.putc(0);
yhbyhb4433 2:887ff47839ed 124 led1=led2=led3=led4=0; //reset all LED at the end of loop
yhbyhb4433 1:2c67639795a4 125 }
yhbyhb4433 2:887ff47839ed 126 read_distance(); //request distance information at the end of every loop
yhbyhb4433 2:887ff47839ed 127 read_battery(); //request battery information at the end of every loop
4180_1 0:9439ccb44422 128 }
yhbyhb4433 1:2c67639795a4 129 }