
m3pi for proj2
Dependencies: RemoteIR m3pi mbed-rtos mbed
LightCommunication.cpp@9:8c5229dfab82, 2017-05-10 (annotated)
- Committer:
- Kristof@LAPTOP-FT09DA9V
- Date:
- Wed May 10 10:01:29 2017 +0200
- Revision:
- 9:8c5229dfab82
- Parent:
- 7:1d77c7e0208c
- Child:
- 10:7eaaa891ab81
added communication
Who changed what in which revision?
User | Revision | Line number | New 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 | 9:8c5229dfab82 | 7 | LightCommunication::LightCommunication() { |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 8 | ir_rx = new ReceiverIR(p21); |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 9 | device = new Serial(p14,p15); |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 10 | mut = new Mutex(); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 11 | left,right = 0; |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 12 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 13 | |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 14 | LightCommunication::~LightCommunication() { |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 15 | |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 16 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 17 | |
Kristof@LAPTOP-FT09DA9V | 5:5d2beac511c1 | 18 | void LightCommunication::getSerial(char * test) { |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 19 | if(device->readable()) { |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 20 | device->scanf("%s",test); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 21 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 22 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 23 | |
Kristof@LAPTOP-FT09DA9V | 5:5d2beac511c1 | 24 | void LightCommunication::getIRStyle(uint8_t * buf) { |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 25 | RemoteIR::Format format; |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 26 | if (ir_rx->getState() == ReceiverIR::Received) { |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 27 | ir_rx->getData(&format, buf, sizeof(buf) * 8); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 28 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 29 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 30 | |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 31 | void LightCommunication::receiveData() { |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 32 | mut->lock(); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 33 | //uint8_t * data = LightCommunication::getIRStyle(); |
Kristof@LAPTOP-FT09DA9V | 5:5d2beac511c1 | 34 | char data[3]; |
Kristof@LAPTOP-FT09DA9V | 5:5d2beac511c1 | 35 | LightCommunication::getSerial(data); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 36 | |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 37 | printf("%X \n",data[2]); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 38 | printf("%d \n",data[2]); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 39 | if (OWN_ID == (int) data[0]) { |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 40 | left = LightCommunication::toFloat(data[1]); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 41 | right = LightCommunication::toFloat(data[2]); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 42 | } |
Kristof@LAPTOP-FT09DA9V | 6:02f045d7695d | 43 | |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 44 | mut->unlock(); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 45 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 46 | |
Kristof@LAPTOP-FT09DA9V.home | 2:70240992e869 | 47 | float LightCommunication::getRight(float cur_right){ |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 48 | float r = right - cur_right; |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 49 | if (r>0){ |
Kristof@LAPTOP-FT09DA9V | 6:02f045d7695d | 50 | return (float)(cur_right + 0.005); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 51 | }else if (r<0){ |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 52 | |
Kristof@LAPTOP-FT09DA9V | 6:02f045d7695d | 53 | return (float)(cur_right -0.005); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 54 | }else{ |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 55 | return cur_right; |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 56 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 57 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 58 | |
Kristof@LAPTOP-FT09DA9V.home | 2:70240992e869 | 59 | float LightCommunication::getLeft(float cur_left){ |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 60 | |
Kristof@LAPTOP-FT09DA9V | 9:8c5229dfab82 | 61 | mut->lock(); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 62 | float l = left - cur_left; |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 63 | if (l>0){ |
Kristof@LAPTOP-FT09DA9V | 6:02f045d7695d | 64 | return (float)(cur_left + 0.005); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 65 | }else if (l<0){ |
Kristof@LAPTOP-FT09DA9V | 6:02f045d7695d | 66 | return (float)(cur_left -0.005); |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 67 | }else{ |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 68 | return cur_left; |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 69 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 70 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 71 | |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 72 | float LightCommunication::toFloat(int8_t a) { |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 73 | return ((float)a)/100; |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 74 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 75 | |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 76 | bool LightCommunication::needsToStop() { |
Kristof@LAPTOP-FT09DA9V | 6:02f045d7695d | 77 | return right == 0 && left == 0; |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 78 | } |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 79 | |
Kristof@LAPTOP-FT09DA9V.home | 1:76cff32344d1 | 80 |