a

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
hydroguy45
Date:
Thu Oct 25 02:36:38 2018 +0000
Revision:
9:b8801ba7d7c2
Parent:
7:ca226305d28b
Child:
10:dafae6093487
Ready for lab5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 3:1d10014a4646 1 //ESE519 Lab6 Controller Part 1 (INCOMPLETE IMPLIMENTATION)
csharer 0:0ebe6f55caee 2 //Author: Carter Sharer
csharer 0:0ebe6f55caee 3 //Date: 10/18/2016
csharer 0:0ebe6f55caee 4 //Sources: ESE350 Whack-A-Mole: https://developer.mbed.org/users/mlab4/code/ESE350-Whack-a-Mole/
csharer 0:0ebe6f55caee 5 //This is a simple working templete for sending and receving between two MRF24J40 Modules
csharer 0:0ebe6f55caee 6 //Contains a communication protocol for sending a receiving
csharer 0:0ebe6f55caee 7
csharer 0:0ebe6f55caee 8 #include "mbed.h"
csharer 0:0ebe6f55caee 9 #include <string>
csharer 0:0ebe6f55caee 10 #include "Joystick.h"
hydroguy45 7:ca226305d28b 11 #include "wifiGETWrapper.h"
hydroguy45 9:b8801ba7d7c2 12 #define SEND //Uncomment if you want to transmit data
csharer 0:0ebe6f55caee 13
csharer 0:0ebe6f55caee 14 #define NONE 250
csharer 0:0ebe6f55caee 15
csharer 0:0ebe6f55caee 16 //============================
csharer 0:0ebe6f55caee 17 //== Pin Assignments ==
csharer 0:0ebe6f55caee 18 //============================
csharer 0:0ebe6f55caee 19 //Knobs
csharer 0:0ebe6f55caee 20 #define POT1 p17 //Knob1
csharer 0:0ebe6f55caee 21 #define POT2 p18 //Knob2
csharer 0:0ebe6f55caee 22 #define POT3 p16 //Knob3
csharer 0:0ebe6f55caee 23 #define POT4 p15 //Knob4
csharer 0:0ebe6f55caee 24 //JoyStick
csharer 0:0ebe6f55caee 25 #define POTV p19 //Vertial
csharer 0:0ebe6f55caee 26 #define POTH p20 //Horizontal
csharer 0:0ebe6f55caee 27 //Button
csharer 0:0ebe6f55caee 28 #define BUTTON1 p21
hydroguy45 7:ca226305d28b 29 #define COMMUNICATION_FORMAT "Jstick_h:|%0.0f|Jstick_v:|%0.0f|Knob1|%0.2f|Knob2|%0.2f|Knob3|%0.2f|Knob4|%0.2f|Button:|%d"
csharer 0:0ebe6f55caee 30
csharer 0:0ebe6f55caee 31 //============================
csharer 0:0ebe6f55caee 32 //== Objects ==
csharer 0:0ebe6f55caee 33 //============================
csharer 0:0ebe6f55caee 34 //Knobs
csharer 0:0ebe6f55caee 35 AnalogIn pot1(POT1);
csharer 0:0ebe6f55caee 36 AnalogIn pot2(POT2);
csharer 0:0ebe6f55caee 37 AnalogIn pot3(POT3);
csharer 0:0ebe6f55caee 38 AnalogIn pot4(POT4);
csharer 0:0ebe6f55caee 39 float knob1, knob2, knob3, knob4;
csharer 0:0ebe6f55caee 40
csharer 0:0ebe6f55caee 41 //Joystick
csharer 0:0ebe6f55caee 42 Joystick jstick(POTV, POTH);
csharer 0:0ebe6f55caee 43 float jstick_h, jstick_v;
csharer 0:0ebe6f55caee 44
csharer 0:0ebe6f55caee 45 //Button
csharer 0:0ebe6f55caee 46 DigitalIn Button(BUTTON1);
csharer 0:0ebe6f55caee 47 bool button;
csharer 0:0ebe6f55caee 48
csharer 0:0ebe6f55caee 49 // LEDs
csharer 0:0ebe6f55caee 50 DigitalOut led1(LED1);
csharer 0:0ebe6f55caee 51 DigitalOut led2(LED2);
csharer 0:0ebe6f55caee 52 DigitalOut led3(LED3);
csharer 0:0ebe6f55caee 53 DigitalOut led4(LED4);
csharer 0:0ebe6f55caee 54
csharer 0:0ebe6f55caee 55 // Timer
csharer 0:0ebe6f55caee 56 Timer timer;
csharer 0:0ebe6f55caee 57
csharer 0:0ebe6f55caee 58 // Serial port for showing RX data.
csharer 0:0ebe6f55caee 59 Serial pc(USBTX, USBRX);
csharer 0:0ebe6f55caee 60
csharer 0:0ebe6f55caee 61 // Send / receive buffers.
csharer 0:0ebe6f55caee 62 // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends
csharer 0:0ebe6f55caee 63 // to reject data that doesn't have the right header. So the first
csharer 0:0ebe6f55caee 64 // 8 bytes in txBuffer look like a valid header. The remaining 120
csharer 0:0ebe6f55caee 65 // bytes can be used for anything you like.
csharer 0:0ebe6f55caee 66 char txBuffer[128];
csharer 0:0ebe6f55caee 67 char rxBuffer[128];
csharer 0:0ebe6f55caee 68 int rxLen;
csharer 0:0ebe6f55caee 69 //***************** You can start coding here *****************//
csharer 0:0ebe6f55caee 70
csharer 1:98c414bbfe8a 71 //Returns true if c is a letter (upper or lower case), false otherwise
csharer 0:0ebe6f55caee 72 bool isLetter(char c) {
csharer 1:98c414bbfe8a 73
csharer 1:98c414bbfe8a 74 /*
csharer 1:98c414bbfe8a 75 * YOUR IMPLIMENTATION HERE
csharer 1:98c414bbfe8a 76 *
csharer 1:98c414bbfe8a 77 *
csharer 1:98c414bbfe8a 78 *
csharer 1:98c414bbfe8a 79 */
csharer 1:98c414bbfe8a 80
csharer 0:0ebe6f55caee 81 return false;
csharer 0:0ebe6f55caee 82 }
csharer 0:0ebe6f55caee 83
csharer 1:98c414bbfe8a 84 //Returns true if c is a number character (0-9), false otherwise
csharer 0:0ebe6f55caee 85 bool isNumber(char c) {
csharer 1:98c414bbfe8a 86
csharer 1:98c414bbfe8a 87 /*
csharer 1:98c414bbfe8a 88 * YOUR IMPLIMENTATION HERE
csharer 1:98c414bbfe8a 89 *
csharer 1:98c414bbfe8a 90 *
csharer 1:98c414bbfe8a 91 *
csharer 1:98c414bbfe8a 92 */
csharer 1:98c414bbfe8a 93
csharer 0:0ebe6f55caee 94 return false;
csharer 0:0ebe6f55caee 95 }
csharer 0:0ebe6f55caee 96
csharer 0:0ebe6f55caee 97 //Pulls data out of rxBuffer and updates global variables accordingly
csharer 1:98c414bbfe8a 98 //Len is the length of the rxBuffer we are going to scan
csharer 0:0ebe6f55caee 99 void communication_protocal(int len)
csharer 0:0ebe6f55caee 100 {
csharer 0:0ebe6f55caee 101 bool found_name = false;
csharer 0:0ebe6f55caee 102 bool found_num = false;
csharer 0:0ebe6f55caee 103 bool complete_name = false;
csharer 0:0ebe6f55caee 104 bool complete_num = false;
csharer 0:0ebe6f55caee 105 uint8_t name_start = NONE; uint8_t name_end = NONE;
csharer 0:0ebe6f55caee 106 uint8_t num_start = NONE; uint8_t num_end = NONE;
csharer 0:0ebe6f55caee 107
csharer 0:0ebe6f55caee 108 //Loop through all charaters in rxBuffer
csharer 0:0ebe6f55caee 109 for(uint8_t i = 0; i <= rxLen; i++) {
csharer 0:0ebe6f55caee 110 char c = rxBuffer[i];
csharer 0:0ebe6f55caee 111 //pc.printf("Indexed char '%c'\r\n", c);
csharer 0:0ebe6f55caee 112
csharer 1:98c414bbfe8a 113 //Is it the start of a name? (Check if its a letter)
csharer 0:0ebe6f55caee 114 if(isLetter(c) & name_start==NONE) { //if a num
csharer 0:0ebe6f55caee 115 //If We havent found a name yet, this is start of a name
csharer 0:0ebe6f55caee 116 if(found_name == false) {
csharer 0:0ebe6f55caee 117 //pc.printf("found name start at: '%c'\r\n", c);
csharer 0:0ebe6f55caee 118 name_start = i;
csharer 0:0ebe6f55caee 119 found_name = true;
csharer 0:0ebe6f55caee 120 }
csharer 0:0ebe6f55caee 121 }
csharer 0:0ebe6f55caee 122 //Is is a 'end of name' charater? Check for ' ', ':', '-'
hydroguy45 7:ca226305d28b 123 else if(((c == '|') | (c == ':') | (c == '-')) & found_name & !complete_name) {// found end name character
csharer 0:0ebe6f55caee 124 if(found_name) {
csharer 0:0ebe6f55caee 125 complete_name = true;
csharer 0:0ebe6f55caee 126 name_end = i;
csharer 0:0ebe6f55caee 127 //pc.printf("found end of name at: '%c'\r\n", txBuffer[name_end]);
csharer 0:0ebe6f55caee 128 }
csharer 0:0ebe6f55caee 129 }
csharer 0:0ebe6f55caee 130
csharer 0:0ebe6f55caee 131 //Is it a 'start of a number' charater? Check if its a number, or '-', or a '.'
csharer 0:0ebe6f55caee 132 else if( (isNumber(c) | (c=='-') | (c=='.')) & complete_name & num_start==NONE) {
csharer 0:0ebe6f55caee 133 if(found_num == false) {
csharer 0:0ebe6f55caee 134 //pc.printf("found num start at: '%c'\r\n",c);
csharer 0:0ebe6f55caee 135 num_start = i;
csharer 0:0ebe6f55caee 136 found_num = true;
csharer 0:0ebe6f55caee 137 }
csharer 0:0ebe6f55caee 138 }
csharer 0:0ebe6f55caee 139 //Is it a 'end of number' character? Check if its a ' ', ':', '-', or a letter
hydroguy45 7:ca226305d28b 140 else if( (((c=='|')|(c==':')|(c=='-')) | isLetter(c)) & found_num & complete_name) {
csharer 0:0ebe6f55caee 141 if(found_num) {
csharer 0:0ebe6f55caee 142 complete_num = true;
csharer 0:0ebe6f55caee 143 num_end = i;
csharer 0:0ebe6f55caee 144 //pc.printf("found end of num at: '%c' \r\n", txBuffer[num_end]);
csharer 0:0ebe6f55caee 145 }
csharer 0:0ebe6f55caee 146 }
csharer 0:0ebe6f55caee 147
csharer 0:0ebe6f55caee 148 //If we have a complete name AND number value (ie. start and end of each != NONE)
csharer 0:0ebe6f55caee 149 if(found_name & found_num & complete_name & complete_num) {
csharer 0:0ebe6f55caee 150 //pc.printf("Found MATCH\r\n");
csharer 0:0ebe6f55caee 151 //Reset flags
csharer 0:0ebe6f55caee 152 found_name = false;
csharer 0:0ebe6f55caee 153 found_num = false;
csharer 0:0ebe6f55caee 154 complete_name = false;
csharer 0:0ebe6f55caee 155 complete_num = false;
csharer 0:0ebe6f55caee 156
csharer 0:0ebe6f55caee 157 //Set name
csharer 0:0ebe6f55caee 158 uint8_t nameLen = uint8_t((name_end-name_start) + 1);
csharer 0:0ebe6f55caee 159 char * name[nameLen];
csharer 0:0ebe6f55caee 160 *name = &rxBuffer[name_start];
csharer 0:0ebe6f55caee 161 rxBuffer[name_end] = '\0';
csharer 0:0ebe6f55caee 162
csharer 0:0ebe6f55caee 163 //Set num
csharer 0:0ebe6f55caee 164 uint8_t numLen = uint8_t((num_end-num_start) + 1);
csharer 0:0ebe6f55caee 165 char * num[numLen];
csharer 0:0ebe6f55caee 166 *num = &rxBuffer[num_start];
csharer 0:0ebe6f55caee 167 rxBuffer[num_end] = '\0';
csharer 0:0ebe6f55caee 168
csharer 1:98c414bbfe8a 169 //Now that we have isolated a name and its number value
csharer 1:98c414bbfe8a 170 //we want to set the corresponding value to this number.
csharer 1:98c414bbfe8a 171 //Ex: if name is 'Knob4' and num is '0.34', we want to the the
csharer 1:98c414bbfe8a 172 // variable name knob4 to the value 0.34.
csharer 1:98c414bbfe8a 173 //Do this for all variable names in COMMUNICATION_FORMAT
csharer 1:98c414bbfe8a 174 //HINT: look up strcmp, and atof
csharer 1:98c414bbfe8a 175 /*
csharer 1:98c414bbfe8a 176 * YOUR IMPLIMENTATION HERE
csharer 1:98c414bbfe8a 177 *
csharer 1:98c414bbfe8a 178 *
csharer 1:98c414bbfe8a 179 *
csharer 1:98c414bbfe8a 180 */
csharer 1:98c414bbfe8a 181
csharer 0:0ebe6f55caee 182
csharer 0:0ebe6f55caee 183 //Reset flags
csharer 0:0ebe6f55caee 184 name_start = NONE;
csharer 0:0ebe6f55caee 185 name_end = NONE;
csharer 0:0ebe6f55caee 186 num_start = NONE;
csharer 0:0ebe6f55caee 187 num_end = NONE;
csharer 0:0ebe6f55caee 188 }
csharer 0:0ebe6f55caee 189 }
csharer 0:0ebe6f55caee 190 }
csharer 0:0ebe6f55caee 191
csharer 0:0ebe6f55caee 192 int main (void)
csharer 0:0ebe6f55caee 193 {
hydroguy45 7:ca226305d28b 194
csharer 0:0ebe6f55caee 195 //Set Baud rate (9600-115200 is ideal)
csharer 0:0ebe6f55caee 196 pc.baud(115200);
csharer 0:0ebe6f55caee 197 pc.printf("\r\n Start! \r\n");
hydroguy45 9:b8801ba7d7c2 198 //This is an example wifi connection! Please fill this in with the SSID of your BBBL and the password too.
hydroguy45 9:b8801ba7d7c2 199 const char * SSID = "BeagleBone-365E";
hydroguy45 9:b8801ba7d7c2 200 const char * password = "BeagleBone";
hydroguy45 9:b8801ba7d7c2 201 initConnection(SSID,password);
csharer 0:0ebe6f55caee 202 //Start Timer
csharer 0:0ebe6f55caee 203 timer.start();
csharer 0:0ebe6f55caee 204
csharer 1:98c414bbfe8a 205 //Scale Joystick Values, range[-100, 100]
csharer 1:98c414bbfe8a 206 jstick.setScale(-100, 100);
hydroguy45 6:e0c358a351aa 207 int counter = 0;
csharer 0:0ebe6f55caee 208 while(1) {
csharer 0:0ebe6f55caee 209 //(1) Read Joystick Values, round to int8_t presision
csharer 0:0ebe6f55caee 210 jstick_h = (int8_t)jstick.horizontal();
csharer 0:0ebe6f55caee 211 jstick_v = (int8_t)jstick.vertical();
hydroguy45 9:b8801ba7d7c2 212 pc.printf("H: %0.2f V:%0.2f \r\n", jstick_h, jstick_v);
csharer 0:0ebe6f55caee 213
csharer 0:0ebe6f55caee 214 //(2) Read Pot Values, Scale, and round to precision
csharer 0:0ebe6f55caee 215 knob1 = (uint8_t)(pot1.read() * 100); //rounded to uint8_t
csharer 0:0ebe6f55caee 216 knob2 = (pot2.read() * 100);
csharer 0:0ebe6f55caee 217 knob3 = (pot3.read());
csharer 0:0ebe6f55caee 218 knob4 = (int)(pot4.read() * 100); //rounded to float
csharer 0:0ebe6f55caee 219
csharer 0:0ebe6f55caee 220 //(3)Read Button Val, Add to buffer
csharer 0:0ebe6f55caee 221 button = !Button.read(); //button is active low
csharer 0:0ebe6f55caee 222
csharer 0:0ebe6f55caee 223 #ifdef SEND
csharer 0:0ebe6f55caee 224 //SEND DATA: Send some data every 1/2 second
csharer 0:0ebe6f55caee 225 if(timer.read_ms() >= 500) {
csharer 0:0ebe6f55caee 226 //Reset the timer to 0
csharer 0:0ebe6f55caee 227 timer.reset();
csharer 0:0ebe6f55caee 228 // Toggle LED 2.
csharer 0:0ebe6f55caee 229 led2 = led2^1;
csharer 0:0ebe6f55caee 230
csharer 0:0ebe6f55caee 231 //(5) Add all values to buffer to be sent
csharer 0:0ebe6f55caee 232 sprintf(txBuffer, COMMUNICATION_FORMAT, jstick_h, jstick_v, knob1, knob2, knob3, knob4, button);
csharer 0:0ebe6f55caee 233
csharer 0:0ebe6f55caee 234 //(6) Send the buffer
hydroguy45 7:ca226305d28b 235 sendGET(txBuffer);
csharer 1:98c414bbfe8a 236 pc.printf("Sent| %s\r\n", txBuffer);
csharer 0:0ebe6f55caee 237 }
csharer 0:0ebe6f55caee 238 #endif
csharer 0:0ebe6f55caee 239
csharer 0:0ebe6f55caee 240 } //end loop
csharer 0:0ebe6f55caee 241 }//end main