Hanbin Ying / Mbed 2 deprecated 2Xbee_Controller

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

Fork of LED_RTOS by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "DebounceIn.h"
00004 #include "PinDetect.h"
00005 #include "uLCD_4DGL.h"
00006 #include "rtos.h"
00007 
00008 Ticker flipper;
00009 uLCD_4DGL uLCD(p13,p14,p28); // serial tx, serial rx, reset pin;
00010 Serial xbee1(p9, p10);
00011 DigitalOut rst1(p11);
00012 DebounceIn forward(p16);
00013 DebounceIn left(p17);
00014 DebounceIn reverse(p18);
00015 DebounceIn right(p24);
00016 //forward   16
00017 //left      17
00018 //reverse   18
00019 //right     19
00020 
00021 Serial pc(USBTX, USBRX);
00022 DigitalOut led1(LED1);
00023 DigitalOut led2(LED2);
00024 DigitalOut led3(LED3);
00025 DigitalOut led4(LED4);
00026 Mutex xbee_lock;
00027 Mutex uLCD_lock;
00028 int a;
00029 volatile int voltage;
00030 volatile int distance;
00031 
00032 
00033 void read_battery()
00034 {
00035 
00036     xbee1.putc(15);
00037     //if (xbee1.readable()) {
00038         uLCD_lock.lock();
00039         voltage = xbee1.getc();
00040         uLCD_lock.unlock();
00041     //}
00042     //pc.printf("voltage: %i\n",voltage);
00043 }
00044 
00045 void read_distance()
00046 {
00047     xbee1.putc(16);
00048     //if (xbee1.readable()) {
00049         uLCD_lock.lock();
00050         distance = xbee1.getc();
00051         uLCD_lock.unlock();
00052     //}
00053     //pc.printf("distance: %i\n",distance);
00054 }
00055 void display_distance(void const *args)
00056 {
00057     while(1) {
00058 
00059         uLCD_lock.lock();
00060         uLCD.locate(1,5);
00061         float temp = float(1/(float(distance)/232*3.3))*30.53-2.64;
00062         uLCD.printf("%3.3fcm",temp);
00063         uLCD_lock.unlock();
00064         //pc.printf("%i\n",distance);
00065         Thread::wait(300);
00066 
00067     }
00068 }
00069 
00070 void display_battery(void const *args)
00071 {
00072     while(1) {
00073         uLCD_lock.lock();
00074         uLCD.locate(1,3);
00075         uLCD.printf("%2.2fV",float(voltage)/232*3.3*2);
00076         //pc.printf("%i\n",voltage);
00077         uLCD_lock.unlock();
00078         Thread::wait(300);
00079     }
00080 }
00081 
00082 int main()
00083 {
00084     forward.mode(PullUp);
00085     reverse.mode(PullUp);
00086     left.mode(PullUp);
00087     right.mode(PullUp);
00088 
00089     // reset the xbees (at least 200ns)
00090     rst1 = 0;
00091     wait_ms(100);
00092     rst1 = 1;
00093     wait_ms(100);
00094     uLCD.baudrate(3000000);
00095     uLCD.cls();
00096     wait(.01);
00097     uLCD.locate(1,2);
00098     uLCD.printf("Battery:\n");
00099     uLCD.locate(1,4);
00100     uLCD.printf("Distance:\n");
00101     //Thread thread2(read_distance);
00102     //Thread thread3(read_battery);
00103     Thread thread4(display_distance);
00104     Thread thread5(display_battery);
00105 
00106     //led1=1;
00107     //}
00108     while (1) {
00109         if (forward==0) {
00110             led1 = 1;
00111             xbee1.putc(11);
00112         } else if (reverse==0) {
00113             led3 = 1;
00114             xbee1.putc(12);
00115         } else if (left==0) {
00116             led2 = 1;
00117             xbee1.putc(13);
00118         } else if (right == 0) {
00119             led4 = 1;
00120             xbee1.putc(14);
00121         } else {
00122             xbee1.putc(0);
00123             led1=led2=led3=led4=0;
00124         }
00125         read_distance();
00126         read_battery();
00127     }
00128 }