Andrew Olguin / Mbed 2 deprecated RTOS_Controller_v2

Dependencies:   IMU MODSERIAL Servo mbed

Fork of RTOS_Controller by Marco Rubio

Files at this revision

API Documentation at this revision

Comitter:
aolgu003
Date:
Wed Jul 27 02:45:45 2016 +0000
Parent:
5:07bbe020eb65
Child:
7:396fa2a8648d
Commit message:
Added command parsing

Changed in this revision

MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
communication.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
vessel.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Wed Jul 27 02:45:45 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/Sissors/code/MODSERIAL/#d8422efe4761
--- a/communication.h	Tue Jul 26 17:22:33 2016 +0000
+++ b/communication.h	Wed Jul 27 02:45:45 2016 +0000
@@ -1,5 +1,5 @@
 #ifndef COMMUNICATION_H
 #define COMMUNICATION_H
-Serial pc(USBTX, USBRX); // tx, rx
+MODSERIAL pc(USBTX, USBRX); // tx, rx
 
 #endif
\ No newline at end of file
--- a/main.cpp	Tue Jul 26 17:22:33 2016 +0000
+++ b/main.cpp	Wed Jul 27 02:45:45 2016 +0000
@@ -2,10 +2,10 @@
 #include "mbed.h"
 #include "vessel.h"
 
+
 int main()
 {  
     Timer t;
-    
     wait(3);
     Vessel seagoat; //Starts the seagoat
     seagoat.SetYawPID(4,0,0);  
@@ -29,7 +29,7 @@
         
         //seagoat.motorTest();
         seagoat.update();
-        
+        seagoat.updateCommand();
         wait(0.01);
     }
 }
\ No newline at end of file
--- a/vessel.h	Tue Jul 26 17:22:33 2016 +0000
+++ b/vessel.h	Wed Jul 27 02:45:45 2016 +0000
@@ -2,10 +2,12 @@
 #define VESSEL_H
 
 #include "mbed.h"
+#include "MODSERIAL.h"
 #include "MPU6050.h"
 #include "Servo.h"
 #include "IMU.h"
 #include "PID.h"
+#include <string>
 /*
             Cameras
       FL ----- F ->--- FR
@@ -28,6 +30,8 @@
       |        |       |
       6  ---<- 5 ----- 4
 */
+#define BUFFER_SIZE 255
+
 class Vessel
 {
 
@@ -58,8 +62,9 @@
     double xPoint, xIn, xOut;
     double yPoint, yIn, yOut;
     double zPoint, zIn, zOut;
+    double p_gain, i_gain, d_gain;
     PID pidy, pidr, pidp, pidX, pidY, pidZ;
-
+    char buffer[BUFFER_SIZE];
 public:
     void Start_IMU() {
         pc.printf("Starting up\n\r");
@@ -76,7 +81,8 @@
         pidp(&pitchIn, &pitchOut, &pitchPoint,1,1,1, DIRECT), 
         pidX(&xIn, &xOut, &xPoint,1,1,1, DIRECT),
         pidY(&yIn, &yOut, &yPoint,1,1,1, DIRECT),
-        pidZ(&zIn, &zOut, &zPoint,1,1,1, DIRECT){
+        pidZ(&zIn, &zOut, &zPoint,1,1,1, DIRECT)
+        {
 
         pidy.SetMode(AUTOMATIC);  //Yaw PID
         pidy.SetOutputLimits(-255,255);
@@ -258,10 +264,54 @@
         //pc.printf("%f,%f,%f,%f\n\r",accxs, yaws, yy, (accxs + yaws) / yy + 0.5);
         //pc.printf("%f,%f,%f,%f,%f \n\r",acczs, pitchs, rolls, zpr, (acczs + pitchs + rolls) / zpr + 0.5);
         //pc.printf("YAW: %f, %f, %f, %f, %f, %f, %f, %f\n\r", abs((acczs + pitchs + rolls) / zpr),abs((accys + yaws) / yy),abs((acczs + pitchs - rolls) / zpr),abs((accxs + yaws) / xy),abs((acczs - pitchs - rolls) / zpr),abs((accys + yaws) / yy),abs((acczs - pitchs + rolls) / zpr),abs((accxs + yaws) / yy));
-        // pc.printf("YAW: %f, %f, %f, %f\n\r", xOut, yawOut, yawIn, yawPoint);
-        //pc.printf("YAW: %f, %f, %f, %f\n\r", yaw, yawOut, yawIn, yawPoint);
-        //pc.printf("ACC: %f, %f, %f\n\r", ax, ay, az);
+        // pc.printf("YAW: %f,%f, %f\n\r", ax, ay, az);
         //pc.printf("YPR: %f, %f, %f\n\r", yaw, pitch, roll);
     }
+    
+void updateCommand() {
+    char c = 0;
+    string command;
+    char buffer[BUFFER_SIZE] = {' '};
+    int buffer_iter = 0;    
+    pc.printf("Checking for command\n");
+
+    // Note: you need to actually read from the serial to clear the RX interrupt
+    if (pc.readable()) {
+       pc.printf("Found command\n");
+       while (pc.readable()) {
+            c = pc.getc();
+            pc.putc(c);
+            buffer[buffer_iter] = c;
+            buffer_iter++;
+        }
+        pc.printf("Command saved to buffer\n");
+        command = strtok (buffer," ,\n");
+        
+        if (strcmp(command.c_str(), "a")) {
+            this->yawPoint = atof(strtok (NULL, " ,\n"));
+            this->pitchPoint = atof(strtok (NULL, " ,\n"));
+            this->rollPoint = atof(strtok (NULL, " ,\n"));
+            pc.printf("Received Attitude points: yawPoint: %f, pitchPoint: %f, rollPoint: %f\n", this->yawPoint, this->pitchPoint, this->rollPoint);
+        } else if (strcmp(command.c_str(), "b")) {
+            this->xPoint = atof(strtok (NULL, " ,\n"));
+            this->yPoint = atof(strtok (NULL, " ,\n"));
+            this->zPoint = atof(strtok (NULL, " ,\n"));
+            pc.printf("Received X,Y,Z points: X: %f, Y: %f, Z: %f\n", this->xPoint, this->yPoint, this->zPoint);
+        } else if (strcmp(command.c_str(), "c")) {
+            this->p_gain = atof(strtok (NULL, " ,\n"));
+            this->i_gain = atof(strtok (NULL, " ,\n"));
+            this->d_gain = atof(strtok (NULL, " ,\n"));
+            
+            this->SetYawPID(this->p_gain, this->i_gain, this->d_gain);
+            pc.printf("Received PID values P: %f, I: %f, D: %f\n", this->p_gain, this->i_gain, this->d_gain);
+
+        }
+                
+        memset(buffer, ' ', sizeof(buffer));
+        buffer_iter = 0;
+        fflush(stdout);
+    }
+}
 };
+
 #endif
\ No newline at end of file