With this program the buttons on the biorobotics shield can be used to move the arms. both buttons pressed means shoot. Also SW3 can be pressed to adjust controller constants via Serial connection

Dependencies:   HIDScope QEI controlandadjust mbed

Revision:
1:41a9e3340c96
Parent:
0:24628832f21d
Child:
2:91bf9f1765ef
--- a/main.cpp	Thu Oct 01 14:33:20 2015 +0000
+++ b/main.cpp	Fri Oct 02 09:02:08 2015 +0000
@@ -5,6 +5,7 @@
 //////////////////////////////////////CONSTANTS, I/O, FREQUENCIES AND TICKERS//////////////////////////////////////
 //info uit
 HIDScope scope(5);
+Serial pc(USBTX, USBRX);
 
 //encoders
 QEI encoder1 (D12,D13,NC,32); // first b then a for clockwise +
@@ -13,6 +14,7 @@
 //ingangen
 DigitalIn butR(D2);
 DigitalIn butL(D3);
+InterruptIn changecontrollervalues(PTA4);
 
 //uitgangen
 DigitalOut motor1_direction(D7);// draairichting motor 1 (1 is CW encoder als je daar op kijkt en CW shaft als je daar op kijkt)
@@ -83,17 +85,17 @@
 float pi_kp=0.5;
 float pi_ki=0.01;
 float Ts=1.0/pi_control_frequency;
-double e[2]= {0,0};
+double control_error[2]= {0,0};
 double motor_error_int[2]= {0,0};
 
 // Reusable PI controller
 void pi_control()
 {
-    motor_error_int[0] = motor_error_int[0] + Ts * e[0]; // e_int is changed globally because it’s ’by reference’ (&)
-    signal_motor[0]=pi_kp*e[0]+pi_ki*motor_error_int[0];
+    motor_error_int[0] = motor_error_int[0] + Ts * control_error[0]; // e_int is changed globally because it’s ’by reference’ (&)
+    signal_motor[0]=pi_kp*control_error[0]+pi_ki*motor_error_int[0];
 
-    motor_error_int[1] = motor_error_int[1] + Ts * e[1]; // e_int is changed globally because it’s ’by reference’ (&)
-    signal_motor[1]=pi_kp*e[1]+pi_ki*motor_error_int[1];
+    motor_error_int[1] = motor_error_int[1] + Ts * control_error[1]; // e_int is changed globally because it’s ’by reference’ (&)
+    signal_motor[1]=pi_kp*control_error[1]+pi_ki*motor_error_int[1];
 }
 
 //////////////////////////////////////////////////MAIN///////////////////////////////////
@@ -129,8 +131,8 @@
         }
 
         if (pi_control_go==true) {
-            e[0]=desiredangle[0]-(counttorad*encoder1.getPulses());
-            e[1]=desiredangle[1]-(counttorad*encoder2.getPulses());
+            control_error[0]=desiredangle[0]-(counttorad*encoder1.getPulses());
+            control_error[1]=desiredangle[1]-(counttorad*encoder2.getPulses());
             pi_control();
             //motor1
             if (signal_motor[0]>=0) {//determine CW or CCW rotation
@@ -170,6 +172,16 @@
             scopedata();
             scopedata_go=false;
         }
+        //unit om controllervalues aan te passen
+        if (changecontrollervalues.read()==0) {
+            motor1_speed_control.write(0);
+            motor2_speed_control.write(0);
+            pc.printf("KP is now %f, enter new value\n",pi_kp);
+            pc.scanf("%f", &pi_kp);
+
+            pc.printf("KI is now %f, enter new value\n",pi_ki);
+            pc.scanf("%f", &pi_ki);
+        }
     }
     return 0;
 }