April 28, 2016 1441 rev 0.1
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "cmsis_os.h" 00003 #include <stdlib.h> 00004 #include <stdio.h> 00005 #include <iostream> 00006 #include <string> 00007 #include <ctype.h> 00008 00009 RawSerial pc(USBTX,USBRX); 00010 RawSerial XBEE(p37,p31); 00011 PwmOut pwm(p27); 00012 AnalogIn pot(p15); 00013 00014 #define ZERO (float)0.01 00015 #define BAUD_RATE (int)115200 00016 00017 float pot_value = 0.0; 00018 bool slave_manual = false; 00019 bool xbee_read_done = false; 00020 bool xbee_decode_done = false; 00021 //string received = ""; // May need to delete 00022 string sent = ""; 00023 string slaveMode = ""; 00024 char xbeeBuffer [8] = {}; 00025 int sDimAmt = 0; 00026 int sTemp = 0; 00027 int data [6] = {}; 00028 00029 // XBEE Receive thread 00030 void xbee_receive(void const *args) 00031 { 00032 while(1) 00033 { 00034 while(XBEE.readable()) 00035 { 00036 int i = 0; 00037 char temp; 00038 temp = XBEE.getc(); 00039 xbeeBuffer[i] = temp; 00040 i = i+1; 00041 osDelay(10); 00042 } 00043 xbee_read_done = true; 00044 pc.printf(xbeeBuffer); // DEBUG 00045 } 00046 } 00047 00048 // XBEE Decode thread 00049 void xbee_decode(void const *args) 00050 { 00051 while(1) 00052 { 00053 if(xbee_read_done) 00054 { 00055 xbee_read_done = false; // Reset flag 00056 00057 for (int i = 0; i < 6; i++) 00058 { 00059 data[i] = (int)xbeeBuffer[i+2]-48; 00060 } 00061 00062 sDimAmt = (data[0]*100)+(data[1]*10)+(data[2]); //Data 0:2 is the 3 digit percentiloe for Master 00063 sTemp = (data[3]*100)+(data[4]*10)+(data[5]); 00064 pc.printf("SLave dim amount: %03d\n\r", sDimAmt); 00065 xbee_decode_done = true; 00066 } 00067 osDelay(10); 00068 } 00069 } 00070 00071 // XBEE Action thread 00072 void xbee_action(void const *args) 00073 { 00074 while(1) 00075 { 00076 pot_value = pot.read(); 00077 if(pot_value > ZERO) 00078 { 00079 slave_manual = true; 00080 pwm.write(pot_value); 00081 slaveMode = "M"; 00082 } 00083 else if (pot_value < ZERO) 00084 { 00085 slave_manual = false; 00086 slaveMode = "A"; 00087 } 00088 00089 if(xbee_decode_done) 00090 { 00091 xbee_decode_done = false; 00092 sent = "S" + slaveMode; 00093 for(int i = 0; i < 6; i++) 00094 { 00095 sent += xbeeBuffer[i+2]-48; 00096 } 00097 pc.printf("%s", sent); 00098 } 00099 osDelay(10); 00100 } 00101 } 00102 00103 osThreadDef(xbee_receive, osPriorityNormal, DEFAULT_STACK_SIZE); 00104 osThreadDef(xbee_decode, osPriorityNormal, DEFAULT_STACK_SIZE); 00105 osThreadDef(xbee_action, osPriorityNormal, DEFAULT_STACK_SIZE); 00106 00107 int main () 00108 { 00109 osThreadCreate(osThread(xbee_receive),NULL); 00110 osThreadCreate(osThread(xbee_decode),NULL); 00111 osThreadCreate(osThread(xbee_action),NULL); 00112 pc.baud(BAUD_RATE); 00113 XBEE.baud(BAUD_RATE); 00114 while(1) 00115 { 00116 osDelay(30); 00117 } 00118 } 00119
Generated on Mon Aug 8 2022 05:34:37 by
1.7.2