m3pi for proj2

Dependencies:   RemoteIR m3pi mbed-rtos mbed

Revision:
10:7eaaa891ab81
Parent:
9:8c5229dfab82
Child:
12:f55b31b5bc4a
--- a/Controller.cpp	Wed May 10 10:01:29 2017 +0200
+++ b/Controller.cpp	Thu May 11 15:27:12 2017 +0200
@@ -4,34 +4,58 @@
 
 #include "Controller.h"
 
-void Controller::runThread(){
-    lightCommunication->receiveData();
-}
+Controller::Controller() {
 
-Controller::Controller() {
-    //constructor once to initialise
+    current_left = 0;
+    current_right = 0;
+    //MAKES A NEW LIGHTCOMMUNICATION (starts receiving on serial)
     lightCommunication = new LightCommunication();
     car = new m3pi();
-    receiveThread.start(callback(this,Controller::runThread));
-    ir_rx = new ReceiverIR(p21);
+
+    //receiveThread.start(callback(this,&Controller::runThread));
+    runningThread.start(callback(this, &Controller::run));
+    //receiveThread.join();
+    runningThread.join();
+
 
 }
 
 Controller::~Controller() {
+    delete car;
+    delete lightCommunication;
+}
+
+/**
+ *
+ */
+void Controller::runThread() {
+    while (true) {
+        lightCommunication->receiveData();
+        printf("%s", "receiving");
+    }
+}
+
+void Controller::run() {
+
+    while (true) {
+
+        float left = lightCommunication->getLeft(current_left);
+        float right = lightCommunication->getRight(current_right);
+        car->cls();
+        car->locate(0, 1);
+        car->printf("Left: %0.3f Right: %0.3f", left, right);
+        printf("left = %f right = %f \r\n", current_left, current_right);
+        if (current_right == right && current_left == left) {
+            ;
+        } else if (current_right == 0 && current_left == 0) {
+            car->stop();
+        } else {
+            current_left = left;
+            current_right = right;
+            car->left_motor(current_left);
+            car->right_motor(current_right);
+        }
+        wait_ms(20);
+    }
 
 }
-int Controller::run() {
-
-    lightCommunication->mut->lock();
-    if (lightCommunication->needsToStop()){
-        car->stop();
-        lightCommunication->mut->unlock();
-    }else {
-        current_left = lightCommunication->getLeft(current_left);
-        current_right = lightCommunication->getRight(current_right);
-        lightCommunication->mut->unlock();
-        car->left_motor(current_left) ;
-        car->right_motor(current_right) ;
-    }
-    return 0;
-}