Code for robot

Dependencies:   mbed

Fork of Yusheng-final_project by Britney Dorval

Committer:
britneyd
Date:
Wed Apr 26 20:58:31 2017 +0000
Revision:
11:dc410a980771
Parent:
6:ae3e6aefe908
Code for robot;

Who changed what in which revision?

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