BroBot Code for ESE350 Lab6 part 3 (Skeleton)

Dependencies:   MPU6050_V3 mbed-rtos mbed

Fork of BroBot_RTOS_ESE350 by Carter Sharer

Committer:
csharer
Date:
Wed Mar 22 21:59:10 2017 +0000
Revision:
20:a7cba632d0b1
Parent:
6:62cdb7482b50
Final Skeleton code for ESE350

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 6:62cdb7482b50 1 #include <string>
csharer 6:62cdb7482b50 2 #define NONE 250
csharer 6:62cdb7482b50 3
csharer 6:62cdb7482b50 4 //============================
csharer 6:62cdb7482b50 5 //== Pin Assignments ==
csharer 6:62cdb7482b50 6 //============================
csharer 6:62cdb7482b50 7 //Knobs
csharer 6:62cdb7482b50 8 #define POT1 p17 //Knob1
csharer 6:62cdb7482b50 9 #define POT2 p18 //Knob2
csharer 6:62cdb7482b50 10 #define POT3 p16 //Knob3
csharer 6:62cdb7482b50 11 #define POT4 p15 //Knob4
csharer 6:62cdb7482b50 12 //JoyStick
csharer 6:62cdb7482b50 13 #define POTV p19 //Vertial
csharer 6:62cdb7482b50 14 #define POTH p20 //Horizontal
csharer 6:62cdb7482b50 15 //MRF24J
csharer 6:62cdb7482b50 16 #define SDI p11
csharer 6:62cdb7482b50 17 #define SDO p12
csharer 6:62cdb7482b50 18 #define SCK p13
csharer 6:62cdb7482b50 19 #define CS p7
csharer 6:62cdb7482b50 20 #define RESET p8
csharer 6:62cdb7482b50 21 //Button
csharer 6:62cdb7482b50 22 #define BUTTON1 p21
csharer 6:62cdb7482b50 23
csharer 6:62cdb7482b50 24 //MRF24J40
csharer 6:62cdb7482b50 25 PinName mosi(SDI);
csharer 6:62cdb7482b50 26 PinName miso(SDO);
csharer 6:62cdb7482b50 27 PinName sck(SCK);
csharer 6:62cdb7482b50 28 PinName cs(CS);
csharer 6:62cdb7482b50 29 PinName reset(RESET);
csharer 6:62cdb7482b50 30 // RF tranceiver
csharer 6:62cdb7482b50 31 MRF24J40 mrf(mosi, miso, sck, cs, reset);
csharer 6:62cdb7482b50 32
csharer 6:62cdb7482b50 33 // Send / receive buffers.
csharer 6:62cdb7482b50 34 // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends
csharer 6:62cdb7482b50 35 // to reject data that doesn't have the right header. So the first
csharer 6:62cdb7482b50 36 // 8 bytes in txBuffer look like a valid header. The remaining 120
csharer 6:62cdb7482b50 37 // bytes can be used for anything you like.
csharer 6:62cdb7482b50 38 char txBuffer[128];
csharer 6:62cdb7482b50 39 char rxBuffer[128];
csharer 6:62cdb7482b50 40 int rxLen;
csharer 6:62cdb7482b50 41
csharer 6:62cdb7482b50 42
csharer 6:62cdb7482b50 43 //***************** Do not change these methods (please) *****************//
csharer 6:62cdb7482b50 44 /**
csharer 6:62cdb7482b50 45 * Receive data from the MRF24J40.
csharer 6:62cdb7482b50 46 *
csharer 6:62cdb7482b50 47 * @param data A pointer to a char array to hold the data
csharer 6:62cdb7482b50 48 * @param maxLength The max amount of data to read.
csharer 6:62cdb7482b50 49 */
csharer 6:62cdb7482b50 50 int rf_receive(char *data, uint8_t maxLength)
csharer 6:62cdb7482b50 51 {
csharer 6:62cdb7482b50 52 uint8_t len = mrf.Receive((uint8_t *)data, maxLength);
csharer 6:62cdb7482b50 53 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
csharer 6:62cdb7482b50 54
csharer 6:62cdb7482b50 55 if(len > 10) {
csharer 6:62cdb7482b50 56 //Remove the header and footer of the message
csharer 6:62cdb7482b50 57 for(uint8_t i = 0; i < len-2; i++) {
csharer 6:62cdb7482b50 58 if(i<8) {
csharer 6:62cdb7482b50 59 //Make sure our header is valid first
csharer 6:62cdb7482b50 60 if(data[i] != header[i]) {
csharer 6:62cdb7482b50 61 //mrf.Reset();
csharer 6:62cdb7482b50 62 return 0;
csharer 6:62cdb7482b50 63 }
csharer 6:62cdb7482b50 64 } else {
csharer 6:62cdb7482b50 65 data[i-8] = data[i];
csharer 6:62cdb7482b50 66 }
csharer 6:62cdb7482b50 67 }
csharer 6:62cdb7482b50 68 //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
csharer 6:62cdb7482b50 69 }
csharer 6:62cdb7482b50 70 return ((int)len)-10;
csharer 6:62cdb7482b50 71 }
csharer 6:62cdb7482b50 72
csharer 6:62cdb7482b50 73 /**
csharer 6:62cdb7482b50 74 * Send data to another MRF24J40.
csharer 6:62cdb7482b50 75 *
csharer 6:62cdb7482b50 76 * @param data The string to send
csharer 6:62cdb7482b50 77 * @param maxLength The length of the data to send.
csharer 6:62cdb7482b50 78 * If you are sending a null-terminated string you can pass strlen(data)+1
csharer 6:62cdb7482b50 79 */
csharer 6:62cdb7482b50 80 void rf_send(char *data, uint8_t len)
csharer 6:62cdb7482b50 81 {
csharer 6:62cdb7482b50 82 //We need to prepend the message with a valid ZigBee header
csharer 6:62cdb7482b50 83 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
csharer 6:62cdb7482b50 84 uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) );
csharer 6:62cdb7482b50 85
csharer 6:62cdb7482b50 86 for(uint8_t i = 0; i < len+8; i++) {
csharer 6:62cdb7482b50 87 //prepend the 8-byte header
csharer 6:62cdb7482b50 88 send_buf[i] = (i<8) ? header[i] : data[i-8];
csharer 6:62cdb7482b50 89 }
csharer 6:62cdb7482b50 90 //pc.printf("Sent: %s\r\n", send_buf+8);
csharer 6:62cdb7482b50 91
csharer 6:62cdb7482b50 92 mrf.Send(send_buf, len+8);
csharer 6:62cdb7482b50 93 free(send_buf);
csharer 6:62cdb7482b50 94 }
csharer 6:62cdb7482b50 95 //***************** You can start coding here *****************//
csharer 6:62cdb7482b50 96
csharer 6:62cdb7482b50 97 //Returns true if c is a letter, false otherwise
csharer 6:62cdb7482b50 98 bool isLetter(char c) {
csharer 6:62cdb7482b50 99 if(('a'<=c & c<='z') | ('A'<=c & c<='Z'))
csharer 6:62cdb7482b50 100 return true;
csharer 6:62cdb7482b50 101 return false;
csharer 6:62cdb7482b50 102 }
csharer 6:62cdb7482b50 103
csharer 6:62cdb7482b50 104 //Returns true if c is a number, false otherwise
csharer 6:62cdb7482b50 105 bool isNumber(char c) {
csharer 6:62cdb7482b50 106 if('0'<=c & c<='9')
csharer 6:62cdb7482b50 107 return true;
csharer 6:62cdb7482b50 108 return false;
csharer 6:62cdb7482b50 109 }
csharer 6:62cdb7482b50 110
csharer 6:62cdb7482b50 111 //Pulls data out fo rxBuffer and updates global variables accordingly
csharer 6:62cdb7482b50 112 void communication_protocal(int len)
csharer 6:62cdb7482b50 113 {
csharer 6:62cdb7482b50 114 bool found_name = false;
csharer 6:62cdb7482b50 115 bool found_num = false;
csharer 6:62cdb7482b50 116 bool complete_name = false;
csharer 6:62cdb7482b50 117 bool complete_num = false;
csharer 6:62cdb7482b50 118 uint8_t name_start = NONE; uint8_t name_end = NONE;
csharer 6:62cdb7482b50 119 uint8_t num_start = NONE; uint8_t num_end = NONE;
csharer 6:62cdb7482b50 120
csharer 6:62cdb7482b50 121 //Loop through all charaters in rxBuffer
csharer 6:62cdb7482b50 122 for(uint8_t i = 0; i <= rxLen; i++) {
csharer 6:62cdb7482b50 123 char c = rxBuffer[i];
csharer 6:62cdb7482b50 124 //pc.printf("Indexed char '%c'\r\n", c);
csharer 6:62cdb7482b50 125
csharer 6:62cdb7482b50 126 //Find the start of a name (Check if its a letter)
csharer 6:62cdb7482b50 127 if(isLetter(c) & name_start==NONE) { //if a num
csharer 6:62cdb7482b50 128 //If We havent found a name yet, this is start of a name
csharer 6:62cdb7482b50 129 if(found_name == false) {
csharer 6:62cdb7482b50 130 //pc.printf("found name start at: '%c'\r\n", c);
csharer 6:62cdb7482b50 131 name_start = i;
csharer 6:62cdb7482b50 132 found_name = true;
csharer 6:62cdb7482b50 133 }
csharer 6:62cdb7482b50 134 }
csharer 6:62cdb7482b50 135 //Find 'end of name' charater: ' ', ':', '-'
csharer 6:62cdb7482b50 136 else if(((c == ' ') | (c == ':') | (c == '-')) & found_name & !complete_name) {// found end name character
csharer 6:62cdb7482b50 137 if(found_name) {
csharer 6:62cdb7482b50 138 complete_name = true;
csharer 6:62cdb7482b50 139 name_end = i;
csharer 6:62cdb7482b50 140 //pc.printf("found end of name at: '%c'\r\n", txBuffer[name_end]);
csharer 6:62cdb7482b50 141 }
csharer 6:62cdb7482b50 142 }
csharer 6:62cdb7482b50 143
csharer 6:62cdb7482b50 144 //Find 'start of a number' charater, Check if its a number, or '-', or a '.'
csharer 6:62cdb7482b50 145 else if( (isNumber(c) | (c=='-') | (c=='.')) & complete_name & num_start==NONE) {
csharer 6:62cdb7482b50 146 if(found_num == false) {
csharer 6:62cdb7482b50 147 //pc.printf("found num start at: '%c'\r\n",c);
csharer 6:62cdb7482b50 148 num_start = i;
csharer 6:62cdb7482b50 149 found_num = true;
csharer 6:62cdb7482b50 150 }
csharer 6:62cdb7482b50 151 }
csharer 6:62cdb7482b50 152 //Found end of number character: ' ', ':', '-', or a letter
csharer 6:62cdb7482b50 153 else if( (((c==' ')|(c==':')|(c=='-')) | isLetter(c)) & found_num & complete_name) {
csharer 6:62cdb7482b50 154 if(found_num) {
csharer 6:62cdb7482b50 155 complete_num = true;
csharer 6:62cdb7482b50 156 num_end = i;
csharer 6:62cdb7482b50 157 //pc.printf("found end of num at: '%c' \r\n", txBuffer[num_end]);
csharer 6:62cdb7482b50 158 }
csharer 6:62cdb7482b50 159 }
csharer 6:62cdb7482b50 160
csharer 6:62cdb7482b50 161 //If we have a complete name AND number value (ie. start and end of each != NONE)
csharer 6:62cdb7482b50 162 if(found_name & found_num & complete_name & complete_num) {
csharer 6:62cdb7482b50 163 //pc.printf("Found MATCH\r\n");
csharer 6:62cdb7482b50 164 //Reset flags
csharer 6:62cdb7482b50 165 found_name = false;
csharer 6:62cdb7482b50 166 found_num = false;
csharer 6:62cdb7482b50 167 complete_name = false;
csharer 6:62cdb7482b50 168 complete_num = false;
csharer 6:62cdb7482b50 169
csharer 6:62cdb7482b50 170 //Set name
csharer 6:62cdb7482b50 171 uint8_t nameLen = uint8_t((name_end-name_start) + 1);
csharer 6:62cdb7482b50 172 char * name[nameLen];
csharer 6:62cdb7482b50 173 *name = &rxBuffer[name_start];
csharer 6:62cdb7482b50 174 rxBuffer[name_end] = '\0';
csharer 6:62cdb7482b50 175
csharer 6:62cdb7482b50 176 //Set num
csharer 6:62cdb7482b50 177 uint8_t numLen = uint8_t((num_end-num_start) + 1);
csharer 6:62cdb7482b50 178 char * num[numLen];
csharer 6:62cdb7482b50 179 *num = &rxBuffer[num_start];
csharer 6:62cdb7482b50 180 rxBuffer[num_end] = '\0';
csharer 6:62cdb7482b50 181
csharer 6:62cdb7482b50 182 //Set variables
csharer 6:62cdb7482b50 183 if(strcmp(*name, "Jstick_h\0")==0)
csharer 6:62cdb7482b50 184 jstick_h = atof(*num);
csharer 6:62cdb7482b50 185 else if(strcmp(*name, "Jstick_v\0")==0)
csharer 6:62cdb7482b50 186 jstick_v = atof(*num);
csharer 6:62cdb7482b50 187 else if(strcmp(*name, "Knob1\0")==0)
csharer 6:62cdb7482b50 188 knob1 = atof(*num);
csharer 6:62cdb7482b50 189 else if(strcmp(*name, "Knob2\0")==0)
csharer 6:62cdb7482b50 190 knob2 = atof(*num);
csharer 6:62cdb7482b50 191 else if(strcmp(*name, "Knob3\0")==0)
csharer 6:62cdb7482b50 192 knob3 = atof(*num);
csharer 6:62cdb7482b50 193 else if(strcmp(*name, "Knob4\0")==0)
csharer 6:62cdb7482b50 194 knob4 = atof(*num);
csharer 6:62cdb7482b50 195 else if(strcmp(*name, "Button\0")==0)
csharer 6:62cdb7482b50 196 button = (bool)atoi(*num);
csharer 6:62cdb7482b50 197
csharer 6:62cdb7482b50 198 //Reset flags
csharer 6:62cdb7482b50 199 name_start = NONE;
csharer 6:62cdb7482b50 200 name_end = NONE;
csharer 6:62cdb7482b50 201 num_start = NONE;
csharer 6:62cdb7482b50 202 num_end = NONE;
csharer 6:62cdb7482b50 203 }
csharer 6:62cdb7482b50 204 }
csharer 6:62cdb7482b50 205 }