Real controller code

Dependencies:   Joystick_skeleton_neverused mbed

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
csharer
Date:
Thu Oct 20 22:16:49 2016 +0000
Revision:
0:0ebe6f55caee
Child:
1:98c414bbfe8a
This is the complete implementation for lab6 part 1 controller

Who changed what in which revision?

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