Part 1 + 3

Dependencies:   Joystick_skeleton_Pengineers mbed

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
csharer
Date:
Tue Oct 25 15:09:17 2016 +0000
Revision:
4:3044edf0cc5d
Parent:
3:1d10014a4646
Child:
6:4a70f13c0bca
updated name;

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 "MRF24J40.h"
csharer 0:0ebe6f55caee 10 #include <string>
csharer 0:0ebe6f55caee 11 #include "Joystick.h"
csharer 0:0ebe6f55caee 12
csharer 0:0ebe6f55caee 13 #define SEND //Uncomment if you want to transmit data
csharer 1:98c414bbfe8a 14 #define RECEIVE //Uncomment if you want to receive data
csharer 0:0ebe6f55caee 15
csharer 0:0ebe6f55caee 16 #define NONE 250
csharer 0:0ebe6f55caee 17
csharer 0:0ebe6f55caee 18 //============================
csharer 0:0ebe6f55caee 19 //== Pin Assignments ==
csharer 0:0ebe6f55caee 20 //============================
csharer 0:0ebe6f55caee 21 //Knobs
csharer 0:0ebe6f55caee 22 #define POT1 p17 //Knob1
csharer 0:0ebe6f55caee 23 #define POT2 p18 //Knob2
csharer 0:0ebe6f55caee 24 #define POT3 p16 //Knob3
csharer 0:0ebe6f55caee 25 #define POT4 p15 //Knob4
csharer 0:0ebe6f55caee 26 //JoyStick
csharer 0:0ebe6f55caee 27 #define POTV p19 //Vertial
csharer 0:0ebe6f55caee 28 #define POTH p20 //Horizontal
csharer 0:0ebe6f55caee 29 //MRF24J
csharer 0:0ebe6f55caee 30 #define SDI p11
csharer 0:0ebe6f55caee 31 #define SDO p12
csharer 0:0ebe6f55caee 32 #define SCK p13
csharer 0:0ebe6f55caee 33 #define CS p7
csharer 0:0ebe6f55caee 34 #define RESET p8
csharer 0:0ebe6f55caee 35 //Button
csharer 0:0ebe6f55caee 36 #define BUTTON1 p21
csharer 0:0ebe6f55caee 37 #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 38
csharer 0:0ebe6f55caee 39 //============================
csharer 0:0ebe6f55caee 40 //== Objects ==
csharer 0:0ebe6f55caee 41 //============================
csharer 0:0ebe6f55caee 42 //Knobs
csharer 0:0ebe6f55caee 43 AnalogIn pot1(POT1);
csharer 0:0ebe6f55caee 44 AnalogIn pot2(POT2);
csharer 0:0ebe6f55caee 45 AnalogIn pot3(POT3);
csharer 0:0ebe6f55caee 46 AnalogIn pot4(POT4);
csharer 0:0ebe6f55caee 47 float knob1, knob2, knob3, knob4;
csharer 0:0ebe6f55caee 48
csharer 0:0ebe6f55caee 49 //Joystick
csharer 0:0ebe6f55caee 50 Joystick jstick(POTV, POTH);
csharer 0:0ebe6f55caee 51 float jstick_h, jstick_v;
csharer 0:0ebe6f55caee 52
csharer 0:0ebe6f55caee 53 //MRF24J40
csharer 0:0ebe6f55caee 54 PinName mosi(SDI);
csharer 0:0ebe6f55caee 55 PinName miso(SDO);
csharer 0:0ebe6f55caee 56 PinName sck(SCK);
csharer 0:0ebe6f55caee 57 PinName cs(CS);
csharer 0:0ebe6f55caee 58 PinName reset(RESET);
csharer 0:0ebe6f55caee 59 // RF tranceiver
csharer 0:0ebe6f55caee 60 MRF24J40 mrf(mosi, miso, sck, cs, reset);
csharer 0:0ebe6f55caee 61
csharer 0:0ebe6f55caee 62 //Button
csharer 0:0ebe6f55caee 63 DigitalIn Button(BUTTON1);
csharer 0:0ebe6f55caee 64 bool button;
csharer 0:0ebe6f55caee 65
csharer 0:0ebe6f55caee 66 // LEDs
csharer 0:0ebe6f55caee 67 DigitalOut led1(LED1);
csharer 0:0ebe6f55caee 68 DigitalOut led2(LED2);
csharer 0:0ebe6f55caee 69 DigitalOut led3(LED3);
csharer 0:0ebe6f55caee 70 DigitalOut led4(LED4);
csharer 0:0ebe6f55caee 71
csharer 0:0ebe6f55caee 72 // Timer
csharer 0:0ebe6f55caee 73 Timer timer;
csharer 0:0ebe6f55caee 74
csharer 0:0ebe6f55caee 75 // Serial port for showing RX data.
csharer 0:0ebe6f55caee 76 Serial pc(USBTX, USBRX);
csharer 0:0ebe6f55caee 77
csharer 0:0ebe6f55caee 78 // Send / receive buffers.
csharer 0:0ebe6f55caee 79 // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends
csharer 0:0ebe6f55caee 80 // to reject data that doesn't have the right header. So the first
csharer 0:0ebe6f55caee 81 // 8 bytes in txBuffer look like a valid header. The remaining 120
csharer 0:0ebe6f55caee 82 // bytes can be used for anything you like.
csharer 0:0ebe6f55caee 83 char txBuffer[128];
csharer 0:0ebe6f55caee 84 char rxBuffer[128];
csharer 0:0ebe6f55caee 85 int rxLen;
csharer 0:0ebe6f55caee 86
csharer 0:0ebe6f55caee 87 //***************** Do not change these methods (please) *****************//
csharer 0:0ebe6f55caee 88 /**
csharer 0:0ebe6f55caee 89 * Receive data from the MRF24J40.
csharer 0:0ebe6f55caee 90 *
csharer 0:0ebe6f55caee 91 * @param data A pointer to a char array to hold the data
csharer 0:0ebe6f55caee 92 * @param maxLength The max amount of data to read.
csharer 0:0ebe6f55caee 93 */
csharer 0:0ebe6f55caee 94 int rf_receive(char *data, uint8_t maxLength)
csharer 0:0ebe6f55caee 95 {
csharer 0:0ebe6f55caee 96 uint8_t len = mrf.Receive((uint8_t *)data, maxLength);
csharer 0:0ebe6f55caee 97 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
csharer 0:0ebe6f55caee 98
csharer 0:0ebe6f55caee 99 if(len > 10) {
csharer 0:0ebe6f55caee 100 //Remove the header and footer of the message
csharer 0:0ebe6f55caee 101 for(uint8_t i = 0; i < len-2; i++) {
csharer 0:0ebe6f55caee 102 if(i<8) {
csharer 0:0ebe6f55caee 103 //Make sure our header is valid first
csharer 0:0ebe6f55caee 104 if(data[i] != header[i])
csharer 0:0ebe6f55caee 105 return 0;
csharer 0:0ebe6f55caee 106 } else {
csharer 0:0ebe6f55caee 107 data[i-8] = data[i];
csharer 0:0ebe6f55caee 108 }
csharer 0:0ebe6f55caee 109 }
csharer 0:0ebe6f55caee 110 //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
csharer 0:0ebe6f55caee 111 }
csharer 0:0ebe6f55caee 112 return ((int)len)-10;
csharer 0:0ebe6f55caee 113 }
csharer 0:0ebe6f55caee 114
csharer 0:0ebe6f55caee 115 /**
csharer 0:0ebe6f55caee 116 * Send data to another MRF24J40.
csharer 0:0ebe6f55caee 117 *
csharer 0:0ebe6f55caee 118 * @param data The string to send
csharer 0:0ebe6f55caee 119 * @param maxLength The length of the data to send.
csharer 0:0ebe6f55caee 120 * If you are sending a null-terminated string you can pass strlen(data)+1
csharer 0:0ebe6f55caee 121 */
csharer 0:0ebe6f55caee 122 void rf_send(char *data, uint8_t len)
csharer 0:0ebe6f55caee 123 {
csharer 0:0ebe6f55caee 124 //We need to prepend the message with a valid ZigBee header
csharer 0:0ebe6f55caee 125 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
csharer 0:0ebe6f55caee 126 uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) );
csharer 0:0ebe6f55caee 127
csharer 0:0ebe6f55caee 128 for(uint8_t i = 0; i < len+8; i++) {
csharer 0:0ebe6f55caee 129 //prepend the 8-byte header
csharer 0:0ebe6f55caee 130 send_buf[i] = (i<8) ? header[i] : data[i-8];
csharer 0:0ebe6f55caee 131 }
csharer 0:0ebe6f55caee 132 //pc.printf("Sent: %s\r\n", send_buf+8);
csharer 0:0ebe6f55caee 133
csharer 0:0ebe6f55caee 134 mrf.Send(send_buf, len+8);
csharer 0:0ebe6f55caee 135 free(send_buf);
csharer 0:0ebe6f55caee 136 }
csharer 0:0ebe6f55caee 137 //***************** You can start coding here *****************//
csharer 0:0ebe6f55caee 138
csharer 1:98c414bbfe8a 139 //Returns true if c is a letter (upper or lower case), false otherwise
csharer 0:0ebe6f55caee 140 bool isLetter(char c) {
csharer 1:98c414bbfe8a 141
csharer 1:98c414bbfe8a 142 /*
csharer 1:98c414bbfe8a 143 * YOUR IMPLIMENTATION HERE
csharer 1:98c414bbfe8a 144 *
csharer 1:98c414bbfe8a 145 *
csharer 1:98c414bbfe8a 146 *
csharer 1:98c414bbfe8a 147 */
csharer 1:98c414bbfe8a 148
csharer 0:0ebe6f55caee 149 return false;
csharer 0:0ebe6f55caee 150 }
csharer 0:0ebe6f55caee 151
csharer 1:98c414bbfe8a 152 //Returns true if c is a number character (0-9), false otherwise
csharer 0:0ebe6f55caee 153 bool isNumber(char c) {
csharer 1:98c414bbfe8a 154
csharer 1:98c414bbfe8a 155 /*
csharer 1:98c414bbfe8a 156 * YOUR IMPLIMENTATION HERE
csharer 1:98c414bbfe8a 157 *
csharer 1:98c414bbfe8a 158 *
csharer 1:98c414bbfe8a 159 *
csharer 1:98c414bbfe8a 160 */
csharer 1:98c414bbfe8a 161
csharer 0:0ebe6f55caee 162 return false;
csharer 0:0ebe6f55caee 163 }
csharer 0:0ebe6f55caee 164
csharer 0:0ebe6f55caee 165 //Pulls data out of rxBuffer and updates global variables accordingly
csharer 1:98c414bbfe8a 166 //Len is the length of the rxBuffer we are going to scan
csharer 0:0ebe6f55caee 167 void communication_protocal(int len)
csharer 0:0ebe6f55caee 168 {
csharer 0:0ebe6f55caee 169 bool found_name = false;
csharer 0:0ebe6f55caee 170 bool found_num = false;
csharer 0:0ebe6f55caee 171 bool complete_name = false;
csharer 0:0ebe6f55caee 172 bool complete_num = false;
csharer 0:0ebe6f55caee 173 uint8_t name_start = NONE; uint8_t name_end = NONE;
csharer 0:0ebe6f55caee 174 uint8_t num_start = NONE; uint8_t num_end = NONE;
csharer 0:0ebe6f55caee 175
csharer 0:0ebe6f55caee 176 //Loop through all charaters in rxBuffer
csharer 0:0ebe6f55caee 177 for(uint8_t i = 0; i <= rxLen; i++) {
csharer 0:0ebe6f55caee 178 char c = rxBuffer[i];
csharer 0:0ebe6f55caee 179 //pc.printf("Indexed char '%c'\r\n", c);
csharer 0:0ebe6f55caee 180
csharer 1:98c414bbfe8a 181 //Is it the start of a name? (Check if its a letter)
csharer 0:0ebe6f55caee 182 if(isLetter(c) & name_start==NONE) { //if a num
csharer 0:0ebe6f55caee 183 //If We havent found a name yet, this is start of a name
csharer 0:0ebe6f55caee 184 if(found_name == false) {
csharer 0:0ebe6f55caee 185 //pc.printf("found name start at: '%c'\r\n", c);
csharer 0:0ebe6f55caee 186 name_start = i;
csharer 0:0ebe6f55caee 187 found_name = true;
csharer 0:0ebe6f55caee 188 }
csharer 0:0ebe6f55caee 189 }
csharer 0:0ebe6f55caee 190 //Is is a 'end of name' charater? Check for ' ', ':', '-'
csharer 0:0ebe6f55caee 191 else if(((c == ' ') | (c == ':') | (c == '-')) & found_name & !complete_name) {// found end name character
csharer 0:0ebe6f55caee 192 if(found_name) {
csharer 0:0ebe6f55caee 193 complete_name = true;
csharer 0:0ebe6f55caee 194 name_end = i;
csharer 0:0ebe6f55caee 195 //pc.printf("found end of name at: '%c'\r\n", txBuffer[name_end]);
csharer 0:0ebe6f55caee 196 }
csharer 0:0ebe6f55caee 197 }
csharer 0:0ebe6f55caee 198
csharer 0:0ebe6f55caee 199 //Is it a 'start of a number' charater? Check if its a number, or '-', or a '.'
csharer 0:0ebe6f55caee 200 else if( (isNumber(c) | (c=='-') | (c=='.')) & complete_name & num_start==NONE) {
csharer 0:0ebe6f55caee 201 if(found_num == false) {
csharer 0:0ebe6f55caee 202 //pc.printf("found num start at: '%c'\r\n",c);
csharer 0:0ebe6f55caee 203 num_start = i;
csharer 0:0ebe6f55caee 204 found_num = true;
csharer 0:0ebe6f55caee 205 }
csharer 0:0ebe6f55caee 206 }
csharer 0:0ebe6f55caee 207 //Is it a 'end of number' character? Check if its a ' ', ':', '-', or a letter
csharer 0:0ebe6f55caee 208 else if( (((c==' ')|(c==':')|(c=='-')) | isLetter(c)) & found_num & complete_name) {
csharer 0:0ebe6f55caee 209 if(found_num) {
csharer 0:0ebe6f55caee 210 complete_num = true;
csharer 0:0ebe6f55caee 211 num_end = i;
csharer 0:0ebe6f55caee 212 //pc.printf("found end of num at: '%c' \r\n", txBuffer[num_end]);
csharer 0:0ebe6f55caee 213 }
csharer 0:0ebe6f55caee 214 }
csharer 0:0ebe6f55caee 215
csharer 0:0ebe6f55caee 216 //If we have a complete name AND number value (ie. start and end of each != NONE)
csharer 0:0ebe6f55caee 217 if(found_name & found_num & complete_name & complete_num) {
csharer 0:0ebe6f55caee 218 //pc.printf("Found MATCH\r\n");
csharer 0:0ebe6f55caee 219 //Reset flags
csharer 0:0ebe6f55caee 220 found_name = false;
csharer 0:0ebe6f55caee 221 found_num = false;
csharer 0:0ebe6f55caee 222 complete_name = false;
csharer 0:0ebe6f55caee 223 complete_num = false;
csharer 0:0ebe6f55caee 224
csharer 0:0ebe6f55caee 225 //Set name
csharer 0:0ebe6f55caee 226 uint8_t nameLen = uint8_t((name_end-name_start) + 1);
csharer 0:0ebe6f55caee 227 char * name[nameLen];
csharer 0:0ebe6f55caee 228 *name = &rxBuffer[name_start];
csharer 0:0ebe6f55caee 229 rxBuffer[name_end] = '\0';
csharer 0:0ebe6f55caee 230
csharer 0:0ebe6f55caee 231 //Set num
csharer 0:0ebe6f55caee 232 uint8_t numLen = uint8_t((num_end-num_start) + 1);
csharer 0:0ebe6f55caee 233 char * num[numLen];
csharer 0:0ebe6f55caee 234 *num = &rxBuffer[num_start];
csharer 0:0ebe6f55caee 235 rxBuffer[num_end] = '\0';
csharer 0:0ebe6f55caee 236
csharer 1:98c414bbfe8a 237 //Now that we have isolated a name and its number value
csharer 1:98c414bbfe8a 238 //we want to set the corresponding value to this number.
csharer 1:98c414bbfe8a 239 //Ex: if name is 'Knob4' and num is '0.34', we want to the the
csharer 1:98c414bbfe8a 240 // variable name knob4 to the value 0.34.
csharer 1:98c414bbfe8a 241 //Do this for all variable names in COMMUNICATION_FORMAT
csharer 1:98c414bbfe8a 242 //HINT: look up strcmp, and atof
csharer 1:98c414bbfe8a 243 /*
csharer 1:98c414bbfe8a 244 * YOUR IMPLIMENTATION HERE
csharer 1:98c414bbfe8a 245 *
csharer 1:98c414bbfe8a 246 *
csharer 1:98c414bbfe8a 247 *
csharer 1:98c414bbfe8a 248 */
csharer 1:98c414bbfe8a 249
csharer 0:0ebe6f55caee 250
csharer 0:0ebe6f55caee 251 //Reset flags
csharer 0:0ebe6f55caee 252 name_start = NONE;
csharer 0:0ebe6f55caee 253 name_end = NONE;
csharer 0:0ebe6f55caee 254 num_start = NONE;
csharer 0:0ebe6f55caee 255 num_end = NONE;
csharer 0:0ebe6f55caee 256 }
csharer 0:0ebe6f55caee 257 }
csharer 0:0ebe6f55caee 258 }
csharer 0:0ebe6f55caee 259
csharer 0:0ebe6f55caee 260 int main (void)
csharer 0:0ebe6f55caee 261 {
csharer 0:0ebe6f55caee 262 //Set the Channel. 0 is default, 15 is max
csharer 4:3044edf0cc5d 263 uint8_t channel = 2; //channel = (Group# - 1)
csharer 0:0ebe6f55caee 264 mrf.SetChannel(channel);
csharer 0:0ebe6f55caee 265
csharer 0:0ebe6f55caee 266 //Set Baud rate (9600-115200 is ideal)
csharer 0:0ebe6f55caee 267 pc.baud(115200);
csharer 0:0ebe6f55caee 268 pc.printf("\r\n Start! \r\n");
csharer 0:0ebe6f55caee 269
csharer 0:0ebe6f55caee 270 //Start Timer
csharer 0:0ebe6f55caee 271 timer.start();
csharer 0:0ebe6f55caee 272
csharer 1:98c414bbfe8a 273 //Scale Joystick Values, range[-100, 100]
csharer 1:98c414bbfe8a 274 jstick.setScale(-100, 100);
csharer 0:0ebe6f55caee 275
csharer 0:0ebe6f55caee 276 while(1) {
csharer 0:0ebe6f55caee 277 //(1) Read Joystick Values, round to int8_t presision
csharer 0:0ebe6f55caee 278 jstick_h = (int8_t)jstick.horizontal();
csharer 0:0ebe6f55caee 279 jstick_v = (int8_t)jstick.vertical();
csharer 1:98c414bbfe8a 280 //pc.printf("H: %0.2f V:%0.2f \r\n", jstick.horizontal(), jstick.vertical());
csharer 0:0ebe6f55caee 281
csharer 0:0ebe6f55caee 282 //(2) Read Pot Values, Scale, and round to precision
csharer 0:0ebe6f55caee 283 knob1 = (uint8_t)(pot1.read() * 100); //rounded to uint8_t
csharer 0:0ebe6f55caee 284 knob2 = (pot2.read() * 100);
csharer 0:0ebe6f55caee 285 knob3 = (pot3.read());
csharer 0:0ebe6f55caee 286 knob4 = (int)(pot4.read() * 100); //rounded to float
csharer 0:0ebe6f55caee 287
csharer 0:0ebe6f55caee 288 //(3)Read Button Val, Add to buffer
csharer 0:0ebe6f55caee 289 button = !Button.read(); //button is active low
csharer 0:0ebe6f55caee 290
csharer 0:0ebe6f55caee 291 #ifdef RECEIVE
csharer 0:0ebe6f55caee 292 //RECEIVE DATA: Try to receive some data
csharer 0:0ebe6f55caee 293 rxLen = rf_receive(rxBuffer, 128);
csharer 0:0ebe6f55caee 294 if(rxLen > 0) {
csharer 0:0ebe6f55caee 295 //Toggle the Led
csharer 0:0ebe6f55caee 296 led1 = led1^1;
csharer 0:0ebe6f55caee 297
csharer 0:0ebe6f55caee 298 //(4) Process data with our protocal
csharer 0:0ebe6f55caee 299 communication_protocal(rxLen);
csharer 0:0ebe6f55caee 300
csharer 0:0ebe6f55caee 301 //Print values once we recieve and process data
csharer 1:98c414bbfe8a 302 pc.printf("Received| ");
csharer 0:0ebe6f55caee 303 pc.printf(COMMUNICATION_FORMAT, jstick_h, jstick_v, knob1, knob2, knob3, knob4, button);
csharer 0:0ebe6f55caee 304 pc.printf("\r\n");
csharer 0:0ebe6f55caee 305 }//main if
csharer 0:0ebe6f55caee 306 #endif
csharer 0:0ebe6f55caee 307
csharer 0:0ebe6f55caee 308 #ifdef SEND
csharer 0:0ebe6f55caee 309 //SEND DATA: Send some data every 1/2 second
csharer 0:0ebe6f55caee 310 if(timer.read_ms() >= 500) {
csharer 0:0ebe6f55caee 311 //Reset the timer to 0
csharer 0:0ebe6f55caee 312 timer.reset();
csharer 0:0ebe6f55caee 313 // Toggle LED 2.
csharer 0:0ebe6f55caee 314 led2 = led2^1;
csharer 0:0ebe6f55caee 315
csharer 0:0ebe6f55caee 316 //(5) Add all values to buffer to be sent
csharer 0:0ebe6f55caee 317 sprintf(txBuffer, COMMUNICATION_FORMAT, jstick_h, jstick_v, knob1, knob2, knob3, knob4, button);
csharer 0:0ebe6f55caee 318
csharer 0:0ebe6f55caee 319 //(6) Send the buffer
csharer 0:0ebe6f55caee 320 rf_send(txBuffer, strlen(txBuffer) + 1);
csharer 1:98c414bbfe8a 321 pc.printf("Sent| %s\r\n", txBuffer);
csharer 0:0ebe6f55caee 322 }
csharer 0:0ebe6f55caee 323 #endif
csharer 0:0ebe6f55caee 324
csharer 0:0ebe6f55caee 325 } //end loop
csharer 0:0ebe6f55caee 326 }//end main