m3pi for proj2

Dependencies:   RemoteIR m3pi mbed-rtos mbed

Committer:
gimohd
Date:
Wed May 17 07:14:01 2017 +0000
Revision:
12:f55b31b5bc4a
Parent:
10:7eaaa891ab81
changed some things

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 1 //
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 2 // Created by Kristof on 5/9/2017.
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 3 //
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 4
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 5 #include "Controller.h"
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 6
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 7 Controller::Controller() {
Kristof@LAPTOP-FT09DA9V 7:1d77c7e0208c 8
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 9 current_left = 0;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 10 current_right = 0;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 11 //MAKES A NEW LIGHTCOMMUNICATION (starts receiving on serial)
Kristof@LAPTOP-FT09DA9V 7:1d77c7e0208c 12 lightCommunication = new LightCommunication();
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 13 car = new m3pi();
gimohd 12:f55b31b5bc4a 14 car->stop();
gimohd 12:f55b31b5bc4a 15 receiveThread.start(callback(this,&Controller::runThread));
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 16 runningThread.start(callback(this, &Controller::run));
gimohd 12:f55b31b5bc4a 17 receiveThread.join();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 18 runningThread.join();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 19
Kristof@LAPTOP-FT09DA9V 7:1d77c7e0208c 20
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 21 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 22
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 23 Controller::~Controller() {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 24 delete car;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 25 delete lightCommunication;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 26 }
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 27
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 28 /**
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 29 *
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 30 */
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 31 void Controller::runThread() {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 32 while (true) {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 33 lightCommunication->receiveData();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 34 }
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 35 }
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 36
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 37 void Controller::run() {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 38
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 39 while (true) {
gimohd 12:f55b31b5bc4a 40 /*if (lightCommunication->needsToStop()){
gimohd 12:f55b31b5bc4a 41 car->stop();
gimohd 12:f55b31b5bc4a 42 }else {
gimohd 12:f55b31b5bc4a 43 current_left = lightCommunication->getLeft(current_left);
gimohd 12:f55b31b5bc4a 44 current_right = lightCommunication->getRight(current_right);
gimohd 12:f55b31b5bc4a 45 car->left_motor(current_left) ;
gimohd 12:f55b31b5bc4a 46 car->right_motor(current_right) ;
gimohd 12:f55b31b5bc4a 47 }*/
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 48 float left = lightCommunication->getLeft(current_left);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 49 float right = lightCommunication->getRight(current_right);
gimohd 12:f55b31b5bc4a 50 //car->cls();
gimohd 12:f55b31b5bc4a 51 //car->locate(0, 1);
gimohd 12:f55b31b5bc4a 52 //car->printf("Left: %0.3f Right: %0.3f", left, right);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 53 if (current_right == right && current_left == left) {
gimohd 12:f55b31b5bc4a 54 current_left = left;
gimohd 12:f55b31b5bc4a 55 current_right = right;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 56 ;
gimohd 12:f55b31b5bc4a 57 } else if (right == 0 && left == 0) {
gimohd 12:f55b31b5bc4a 58 current_left = left;
gimohd 12:f55b31b5bc4a 59 current_right = right;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 60 car->stop();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 61 } else {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 62 current_left = left;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 63 current_right = right;
gimohd 12:f55b31b5bc4a 64
gimohd 12:f55b31b5bc4a 65 printf("cur_left = %f cur_right = %f \r\n", current_left, current_right);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 66 car->left_motor(current_left);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 67 car->right_motor(current_right);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 68 }
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 69 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 70
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 71 }