m3pi for proj2

Dependencies:   RemoteIR m3pi mbed-rtos mbed

Committer:
Kristof@LAPTOP-FT09DA9V
Date:
Thu May 11 15:27:12 2017 +0200
Revision:
10:7eaaa891ab81
Parent:
9:8c5229dfab82
Child:
11:e9faca228d30
added comments

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 "LightCommunication.h"
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 6
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 7
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 8
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 9 LightCommunication::LightCommunication()
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 10 {
Kristof@LAPTOP-FT09DA9V 9:8c5229dfab82 11 ir_rx = new ReceiverIR(p21);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 12 device = new Serial(p13,p14);
Kristof@LAPTOP-FT09DA9V 9:8c5229dfab82 13 mut = new Mutex();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 14 left= 0;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 15 right = 0;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 16 device->baud(19200);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 17 device->attach(callback(this,&LightCommunication::receiveData), Serial::RxIrq);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 18 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 19
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 20 LightCommunication::~LightCommunication()
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 21 {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 22 delete mut;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 23 delete device;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 24 delete ir_rx;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 25 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 26
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 27 void LightCommunication::getIRStyle(uint8_t * buf)
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 28 {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 29 RemoteIR::Format format;
Kristof@LAPTOP-FT09DA9V 9:8c5229dfab82 30 if (ir_rx->getState() == ReceiverIR::Received) {
Kristof@LAPTOP-FT09DA9V 9:8c5229dfab82 31 ir_rx->getData(&format, buf, sizeof(buf) * 8);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 32 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 33 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 34
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 35 void LightCommunication::receiveData()
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 36 {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 37 //SERIAL RECEIVE
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 38 if(device->readable()) {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 39 uint8_t data [128];
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 40 device->scanf("%s",data);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 41 printf("Received data: client:%d left: %d right:%d \r\n",data[0],data[1],data[2]);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 42 if (OWN_ID == (int) data[0]) {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 43 mut->lock();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 44 left = LightCommunication::toFloat(data[1]);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 45 right = LightCommunication::toFloat(data[2]);
Kristof@LAPTOP-FT09DA9V 6:02f045d7695d 46
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 47 printf("DATA left = %f right = %f \r\n",left,right);
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 48 mut->unlock();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 49 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 50 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 51 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 52
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 53 float LightCommunication::getRight(float cur_right)
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 54 {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 55
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 56 float r = right;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 57 mut->unlock();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 58 /*
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 59 if (r>0) {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 60 return (float)(cur_right + (0.005*SPEED));
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 61 } else if (r<0) {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 62
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 63 return (float)(cur_right - (0.005*SPEED));
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 64 } else {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 65 return cur_right;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 66 }*/
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 67 return r;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 68 }
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 69
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 70 float LightCommunication::getLeft(float cur_left)
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 71 {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 72
Kristof@LAPTOP-FT09DA9V 9:8c5229dfab82 73 mut->lock();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 74 /*float l = left - cur_left;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 75 if (l>0) {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 76 return (float)(cur_left + (0.005*SPEED));
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 77 } else if (l<0) {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 78 return (float)(cur_left - (0.005*SPEED));
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 79 } else {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 80 return cur_left;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 81 }*/
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 82 return left;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 83 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 84
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 85 float LightCommunication::toFloat(int8_t a)
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 86 {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 87 return (((float)a)/100.0)*SPEED;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 88 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 89
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 90 bool LightCommunication::needsToStop()
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 91 {
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 92 mut->lock();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 93 bool stop = right == 0 && left == 0;
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 94 mut->unlock();
Kristof@LAPTOP-FT09DA9V 10:7eaaa891ab81 95 return stop;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 96 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 97
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 98