segway_self balancing robot 4180 project

Dependencies:   mbed mbed-rtos LSM9DS1_Library

Revision:
11:c669b4dc1f9f
Parent:
9:28821420f7a4
Child:
12:980c06d63425
--- a/main.cpp	Sat Apr 18 20:03:58 2020 +0000
+++ b/main.cpp	Sat Apr 18 20:35:58 2020 +0000
@@ -1,10 +1,12 @@
 #include "mbed.h"
+#include "rtos.h"
 
 DigitalOut myled(LED2);
 
 Ticker bluetooth;
 Serial pc(USBTX, USBRX);
 
+Mutex parametersmutex;
 
 //Control system variables
 float rp = 50;
@@ -16,14 +18,13 @@
 void bluetooth_update() {
     char bnum=0;
     char bhit=0;
-    
-    while (blue.getc()!='!') {
-        
-        }
+    while(1){
+    if (blue.getc()=='!') {
         if (blue.getc()=='B') { //button data packet
             bnum = blue.getc(); //button number
-            pc.printf("%d",bnum);
+            //pc.printf("%d",bnum);
             bhit = blue.getc(); //1=hit, 0=release
+            parametersmutex.lock();
             if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
                 myled = bnum - '0'; //current button number will appear on LEDs
                 switch (bnum) {
@@ -87,17 +88,24 @@
                         break;
                 }
             }
+            parametersmutex.unlock();
         }
-    //}
+    }
+    Thread::wait(100);
+    }
 }
 
 int main() {
     pc.printf("this is running");
+    Thread bluetooth;
+    bluetooth.start(bluetooth_update);
     //bluetooth.attach(&bluetooth_update, 0.1);
     while(1) {
-        bluetooth_update();
+        //bluetooth_update();
+        parametersmutex.lock();
         pc.printf("rp: %f, rd: %f, ri: %f, desired_angle: %f\n", rp, rd, ri, desired_angle);
-        wait(0.1);
+        parametersmutex.unlock();
+        Thread::wait(100);
     }
 }