m3pi for proj2

Dependencies:   RemoteIR m3pi mbed-rtos mbed

Committer:
Kristof@LAPTOP-FT09DA9V.home
Date:
Tue May 09 23:16:07 2017 +0200
Revision:
3:ecb3fa0406e8
Parent:
2:70240992e869
Child:
4:9580760cdd1f
added communication

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.home 3:ecb3fa0406e8 7 int LightCommunication::left = 0;
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 8 int LightCommunication::right = 0;
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 9 Serial LightCommunication::device(p14,p15);
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 10 ReceiverIR LightCommunication::ir_rx(p21);
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 11 Mutex LightCommunication::mut;
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 12
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 13
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 14 LightCommunication::LightCommunication() {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 15 left,right = 0;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 16 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 17
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 18 LightCommunication::~LightCommunication() {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 19
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 20 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 21
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 22 char * LightCommunication::getSerial() {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 23 char test[3];
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 24 if(device.readable()) {
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 25 device.scanf("%s",test);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 26 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 27 return test;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 28 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 29
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 30 uint8_t * LightCommunication::getIRStyle() {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 31 RemoteIR::Format format;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 32 uint8_t buf[32];
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 33 int bitcount;
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 34 if (ir_rx.getState() == ReceiverIR::Received) {
Kristof@LAPTOP-FT09DA9V.home 3:ecb3fa0406e8 35 bitcount = ir_rx.getData(&format, buf, sizeof(buf) * 8);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 36 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 37 return buf;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 38 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 39
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 40 void LightCommunication::receiveData() {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 41 mut.lock();
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 42 //uint8_t * data = LightCommunication::getIRStyle();
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 43 char * data = LightCommunication::getSerial();
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 44
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 45 printf("%X \n",data[2]);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 46 printf("%d \n",data[2]);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 47 if (OWN_ID == (int) data[0]) {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 48 left = LightCommunication::toFloat(data[1]);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 49 right = LightCommunication::toFloat(data[2]);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 50 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 51 mut.unlock();
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 52 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 53
Kristof@LAPTOP-FT09DA9V.home 2:70240992e869 54 float LightCommunication::getRight(float cur_right){
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 55 float r = right - cur_right;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 56 if (r>0){
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 57 return (cur_right + 0.005);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 58 }else if (r<0){
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 59
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 60 return (cur_right -0.005);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 61 }else{
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 62 return cur_right;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 63 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 64 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 65
Kristof@LAPTOP-FT09DA9V.home 2:70240992e869 66 float LightCommunication::getLeft(float cur_left){
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 67
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 68 mut.lock();
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 69 float l = left - cur_left;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 70 if (l>0){
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 71 return (cur_left + 0.005);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 72 }else if (l<0){
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 73 return (cur_left -0.005);
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 74 }else{
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 75 return cur_left;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 76 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 77 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 78
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 79 float LightCommunication::toFloat(int8_t a) {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 80 return ((float)a)/100;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 81 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 82
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 83 bool LightCommunication::needsToStop() {
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 84 if (right == 0 && left == 0){
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 85 return true;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 86 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 87 return false;
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 88 }
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 89
Kristof@LAPTOP-FT09DA9V.home 1:76cff32344d1 90