
Real controller code
Dependencies: Joystick_skeleton_neverused mbed
Fork of ESE519_Lab6_part1_skeleton by
Diff: main.cpp
- Revision:
- 6:74cc716a4ae3
- Parent:
- 4:3044edf0cc5d
--- a/main.cpp Tue Oct 25 15:11:18 2016 +0000 +++ b/main.cpp Wed Apr 26 21:02:51 2017 +0000 @@ -11,7 +11,12 @@ #include "Joystick.h" #define SEND //Uncomment if you want to transmit data -#define RECEIVE //Uncomment if you want to receive data +//#define RECEIVE //Uncomment if you want to receive data +#define RF_BUFLEN 200 + +char serialBuffer[128]; +int serialBufferIndex = 0; +volatile int receivedSerialData = 0; #define NONE 250 @@ -34,7 +39,7 @@ #define RESET p8 //Button #define BUTTON1 p21 -#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" +#define COMMUNICATION_FORMAT "Jstick_h: %0.0f Jstick_v: %0.0f Button: %d Theta: %f Distance: %f" //============================ //== Objects == @@ -49,6 +54,14 @@ //Joystick Joystick jstick(POTV, POTH); float jstick_h, jstick_v; +float theta; +float dis; +float waypoints[100]; +float theta_send; +float dis_send; +int sending_index = 0; +int sending = 0; +int index = 0; //MRF24J40 PinName mosi(SDI); @@ -61,7 +74,7 @@ //Button DigitalIn Button(BUTTON1); -bool button; +volatile bool button; // LEDs DigitalOut led1(LED1); @@ -80,8 +93,8 @@ // to reject data that doesn't have the right header. So the first // 8 bytes in txBuffer look like a valid header. The remaining 120 // bytes can be used for anything you like. -char txBuffer[128]; -char rxBuffer[128]; +char txBuffer[RF_BUFLEN]; +char rxBuffer[RF_BUFLEN]; int rxLen; //***************** Do not change these methods (please) *****************// @@ -132,6 +145,8 @@ //pc.printf("Sent: %s\r\n", send_buf+8); mrf.Send(send_buf, len+8); + //mrf.Send((uint8_t*)data, len); + //pc.printf("len+8: %d\r\n", len + 8); free(send_buf); } //***************** You can start coding here *****************// @@ -139,12 +154,9 @@ //Returns true if c is a letter (upper or lower case), false otherwise bool isLetter(char c) { - /* - * YOUR IMPLIMENTATION HERE - * - * - * - */ + if(((c>64)&&(c<91))||(c>96)&&(c<123)){ + return true; + } return false; } @@ -152,12 +164,9 @@ //Returns true if c is a number character (0-9), false otherwise bool isNumber(char c) { - /* - * YOUR IMPLIMENTATION HERE - * - * - * - */ + if((c>47)&&(c<58)){ + return true; + } return false; } @@ -240,13 +249,33 @@ // variable name knob4 to the value 0.34. //Do this for all variable names in COMMUNICATION_FORMAT //HINT: look up strcmp, and atof - /* - * YOUR IMPLIMENTATION HERE - * - * - * - */ - + if(strcmp(*name,"Knob1")==0){ + knob1 = atof(*num); + } + else if(strcmp(*name,"Knob2") == 0){ + knob2 = atof(*num); + } + else if(strcmp(*name,"Knob3") == 0){ + knob3 = atof(*num); + } + else if(strcmp(*name,"Knob4") == 0){ + knob4 = atof(*num); + } + else if(strcmp(*name,"Jstick_h") == 0){ + jstick_h = atof(*num); + } + else if(strcmp(*name,"Jstick_v") == 0){ + jstick_v = atof(*num); + } + else if(strcmp(*name,"Button") == 0){ + button = atof(*num); + } + else if(strcmp(*name,"Theta") == 0){ + theta = atof(*num); + } + else if(strcmp(*name,"Distance") == 0){ + dis = atof(*num); + } //Reset flags name_start = NONE; @@ -257,68 +286,126 @@ } } +void serial_getchar() { + char ch = pc.getc(); + //pc.printf("Got character: %c\r\n", ch); + if (ch == '\n' || ch == '\r') { + receivedSerialData = 1; + serialBuffer[serialBufferIndex] = 0; + serialBufferIndex = 0; + pc.printf("Got serial string: %s\r\n", serialBuffer); + } else { + serialBuffer[serialBufferIndex] = ch; + serialBufferIndex++; + } +} + +void reading_input(void) +{ + if (receivedSerialData) { + receivedSerialData = 0; + //button = 1; + sscanf(serialBuffer, "%f %f", &theta, &dis); + pc.printf("Scanned in theta: %f\r\n", theta); + pc.printf("Scanned in dis: %f\r\n", dis); + } + //pc.scanf("%f %f", &theta, &dis); +} + int main (void) { //Set the Channel. 0 is default, 15 is max - uint8_t channel = 2; //channel = (Group# - 1) + uint8_t channel = 9; //channel = (Group# - 1) mrf.SetChannel(channel); //Set Baud rate (9600-115200 is ideal) pc.baud(115200); pc.printf("\r\n Start! \r\n"); + + memset(txBuffer, 0, RF_BUFLEN); + memset(rxBuffer, 0, RF_BUFLEN); + //Start Timer timer.start(); //Scale Joystick Values, range[-100, 100] jstick.setScale(-100, 100); - + + + pc.attach(&serial_getchar); + while(1) { //(1) Read Joystick Values, round to int8_t presision - jstick_h = (int8_t)jstick.horizontal(); - jstick_v = (int8_t)jstick.vertical(); - //pc.printf("H: %0.2f V:%0.2f \r\n", jstick.horizontal(), jstick.vertical()); + jstick_h = jstick.horizontal(); + jstick_v = jstick.vertical(); //(2) Read Pot Values, Scale, and round to precision - knob1 = (uint8_t)(pot1.read() * 100); //rounded to uint8_t + knob1 = (pot1.read() * 100); //rounded to uint8_t knob2 = (pot2.read() * 100); - knob3 = (pot3.read()); - knob4 = (int)(pot4.read() * 100); //rounded to float - + knob3 = (pot3.read() * 100); + knob4 = (pot4.read() * 100); //rounded to float + + //pc.printf("H: %0.2f V:%0.2f K1: %0.2f K2: %0.2f K3: %0.2f K4: %0.2f \r\n", jstick.horizontal(), jstick.vertical(), knob1, knob2, knob3, knob4); + //(3)Read Button Val, Add to buffer button = !Button.read(); //button is active low + reading_input(); + + /*if(button==1){ + sending = 1; + } + if(index < 50){ + waypoints[2*index] = theta; + waypoints[2*index+1] = dis; + pc.printf("Theta is %f\n", theta); + index++; + }*/ + //button = 1; + /*if(i>100){ + reading_input(); + }*/ #ifdef RECEIVE //RECEIVE DATA: Try to receive some data - rxLen = rf_receive(rxBuffer, 128); + rxLen = rf_receive(rxBuffer, RF_BUFLEN); if(rxLen > 0) { //Toggle the Led led1 = led1^1; //(4) Process data with our protocal communication_protocal(rxLen); - //Print values once we recieve and process data - pc.printf("Received| "); - pc.printf(COMMUNICATION_FORMAT, jstick_h, jstick_v, knob1, knob2, knob3, knob4, button); - pc.printf("\r\n"); + //pc.printf("Received| "); + //pc.printf(COMMUNICATION_FORMAT, jstick_h, jstick_v, button); + //pc.printf("\r\n"); }//main if #endif #ifdef SEND //SEND DATA: Send some data every 1/2 second - if(timer.read_ms() >= 500) { + if((timer.read_ms() >= 50)) { //Reset the timer to 0 timer.reset(); // Toggle LED 2. led2 = led2^1; - + + /*theta_send = waypoints[2*sending_index]; + dis_send = waypoints[2*sending_index+1]; //(5) Add all values to buffer to be sent - sprintf(txBuffer, COMMUNICATION_FORMAT, jstick_h, jstick_v, knob1, knob2, knob3, knob4, button); + if((theta_send == 0)&&(dis_send == 0)){ + sending = 0; + } + sending_index++;*/ + sprintf(txBuffer, COMMUNICATION_FORMAT, jstick_h, jstick_v, button, theta, dis); + //button = 0; + //pc.printf("Theta sent: %f\r\n", theta); + //pc.printf("Dis sent: %f\r\n", dis); //(6) Send the buffer rf_send(txBuffer, strlen(txBuffer) + 1); - pc.printf("Sent| %s\r\n", txBuffer); + //rf_send(txBuffer, RF_BUFLEN); + //pc.printf("Sent| %s\r\n", txBuffer); } #endif