Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE DebounceIn PinDetect mbed-rtos mbed
Fork of 2Xbee_Controller by
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 DigitalOut led1(LED1); //four leds to display the command users send in 00022 DigitalOut led2(LED2); 00023 DigitalOut led3(LED3); 00024 DigitalOut led4(LED4); 00025 Mutex uLCD_lock; //mutex lock for the uLCD communication 00026 volatile int voltage; 00027 volatile int distance; 00028 00029 00030 void read_battery() 00031 //read battery information back from the robot, need a mutex lock when 00032 //the information is written to the variable, otherwise "display_battery" 00033 //thread will try to access the the variable while it's changed 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 //read distance information back from the robot, need a mutex lock when 00047 //the information is written to the variable, otherwise "display_distance" 00048 //thread will try to access the the variable while it's changed 00049 { 00050 xbee1.putc(16); 00051 //if (xbee1.readable()) { 00052 uLCD_lock.lock(); 00053 distance = xbee1.getc(); 00054 uLCD_lock.unlock(); 00055 //} 00056 //pc.printf("distance: %i\n",distance); 00057 } 00058 void display_distance(void const *args) //update the distance sensor information in the LCD once every 0.3s 00059 { 00060 while(1) { 00061 00062 uLCD_lock.lock(); 00063 uLCD.locate(1,5); 00064 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 00065 //approximation is used to convert float reading to cm reading 00066 uLCD.printf("%3.3fcm",temp); 00067 uLCD_lock.unlock(); 00068 //pc.printf("%i\n",distance); 00069 Thread::wait(300); 00070 00071 } 00072 } 00073 00074 void display_battery(void const *args) //update the battery information in the LCD once every 0.3s 00075 { 00076 while(1) { 00077 uLCD_lock.lock(); 00078 uLCD.locate(1,3); 00079 uLCD.printf("%2.2fV",float(voltage)/232*3.3*2); 00080 //pc.printf("%i\n",voltage); 00081 uLCD_lock.unlock(); 00082 Thread::wait(300); 00083 } 00084 } 00085 00086 int main() 00087 { 00088 forward.mode(PullUp); 00089 reverse.mode(PullUp); 00090 left.mode(PullUp); 00091 right.mode(PullUp); 00092 00093 // reset the xbees (at least 200ns) 00094 rst1 = 0; 00095 wait_ms(100); 00096 rst1 = 1; 00097 wait_ms(100); 00098 uLCD.baudrate(3000000); //update the 00099 uLCD.cls(); 00100 wait(.01); 00101 uLCD.locate(1,2); 00102 uLCD.printf("Battery:\n"); 00103 uLCD.locate(1,4); 00104 uLCD.printf("Distance:\n"); 00105 Thread thread4(display_distance); 00106 Thread thread5(display_battery); 00107 00108 while (1) { 00109 // loop to check if each pushbutton is pushed. If pushed, send a corresponding command to the robot 00110 if (forward==0) { 00111 led1 = 1; 00112 xbee1.putc(11); 00113 } else if (reverse==0) { 00114 led3 = 1; 00115 xbee1.putc(12); 00116 } else if (left==0) { 00117 led2 = 1; 00118 xbee1.putc(13); 00119 } else if (right == 0) { 00120 led4 = 1; 00121 xbee1.putc(14); 00122 } else { 00123 xbee1.putc(0); 00124 led1=led2=led3=led4=0; //reset all LED at the end of loop 00125 } 00126 read_distance(); //request distance information at the end of every loop 00127 read_battery(); //request battery information at the end of every loop 00128 } 00129 }
Generated on Tue Jul 26 2022 12:05:12 by
1.7.2
