-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunication.cpp
More file actions
46 lines (35 loc) · 1.41 KB
/
Copy pathcommunication.cpp
File metadata and controls
46 lines (35 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "communication.h"
void* send_pwm(void *pwm){
int arduino_bus = 1; // Bus to communicate with Arduino
char str[8] = {'*', '0', '0', '0', '*', '0', '0', '0'}; // The ones left with '*' can be either a letter or a number
uint8_t leftMotor, rightMotor;
// Casting
uint8_t* pwms = (uint8_t*)pwm;
if(rc_enable_signal_handler() == -1){
return NULL;
}
// disable canonical (0), 1 stop bit (1), disable parity (0)
if (rc_uart_init(arduino_bus, BAUDRATE, TIMEOUT_S, 0, 1, 0)){
return NULL;
}
// Start string to send, the one that is zero is the first to be sent
str[0] = 'e'*((INDEX_LEFT + 1)%2) + 'd'*((INDEX_RIGHT + 1)%2);
str[4] = 'e'*INDEX_LEFT + 'd'*INDEX_RIGHT;
for(;;){
leftMotor = pwms[INDEX_LEFT];
rightMotor = pwms[INDEX_RIGHT];
str[4*INDEX_LEFT + 1] = '0' + (leftMotor/100);
str[4*INDEX_LEFT + 3] = '0' + (leftMotor%10);
str[4*INDEX_LEFT + 2] = '0' + (leftMotor%100)/10;
str[4*INDEX_RIGHT + 1] = '0' + (rightMotor/100);
str[4*INDEX_RIGHT + 2] = '0' + (rightMotor%100)/10;
str[4*INDEX_RIGHT + 3] = '0' + (rightMotor%10);
rc_uart_flush(arduino_bus); // Flush because we do not want trash into the communication line
rc_uart_write(arduino_bus, (uint8_t *)str, 8);
if(rc_get_state() == EXITING)
break;
}
// Close cleanly
rc_uart_close(arduino_bus);
return NULL;
}