self-balancing-robot

Dependencies:   mbed mbed-rtos Motor

Revision:
39:80b565a355f3
Parent:
34:d6c0cf60cecd
Child:
41:b9c8d527dd2b
--- a/main.cpp	Thu Apr 23 21:04:22 2020 +0000
+++ b/main.cpp	Mon Apr 27 16:28:16 2020 +0000
@@ -26,7 +26,8 @@
 float ri = 20;
 float desired_angle = 0;
 
-float speed = 0;
+volatile float speed = 0;
+volatile float turnSpeed = 0;
 
 float pAngle = 0;
 float dAngle = 0;
@@ -41,6 +42,8 @@
 Motor leftWheel(p21, p24, p23); // pwm, fwd, rev leftWheel(p21);
 Motor rightWheel(p22, p25, p26); // pwm, fwd, rev rightWheel(p22);
 
+bool isTurning = false;
+
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////// Bluetooth Section ///////////////////////////////
@@ -101,14 +104,18 @@
                         break;
                     case '7': //button 7 left arrow
                         if (bhit == '1') {
-                            desired_angle -= 1;
+//                            desired_angle -= 1;
+                            turnSpeed = .3;
+                            isTurning = !isTurning;
                         } else {
                             //add release code here
                         }
                         break;
                     case '8': //button 8 right arrow
                         if (bhit == '1') {
-                            desired_angle += 1;
+//                            desired_angle += 1;
+                            turnSpeed = .3;
+                            isTurning = !isTurning;
                         } else {
                             //add release code here
                         }
@@ -128,10 +135,15 @@
 ///////////////////////////// update motor speeds///////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 void set_wheel_speed(float speed) {
-    if(speed > 1) speed = 1;
-    if (speed < -1) speed = -1;
-    leftWheel.speed(speed*(-1));
-    rightWheel.speed(speed*(-1));
+    if (isTurning) {
+        leftWheel.speed(turnSpeed*(1));
+        rightWheel.speed(turnSpeed*(-1));
+    } else {
+        if(speed > 1) speed = 1;
+        if (speed < -1) speed = -1;
+        leftWheel.speed(speed*(-1));
+        rightWheel.speed(speed*(-1));
+    }
 }