-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoComm.cpp
More file actions
132 lines (103 loc) · 2.98 KB
/
Copy pathArduinoComm.cpp
File metadata and controls
132 lines (103 loc) · 2.98 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class ArduinoComm{
public:
int fd, n, i, c = 0;
struct termios toptions;
ArduinoComm(){
}
bool init(){
/* open serial port */
//fd = open("/dev/ttySIM0", O_RDWR | O_NOCTTY);
fd = open( "/dev/ttyACM0", O_RDWR| O_NOCTTY );
if(DEBUG) printf("fd opened as %i\n", fd);
usleep(3500000);
/* get current serial port settings */
tcgetattr(fd, &toptions);
/* set 9600 baud both ways */
cfsetispeed(&toptions, B9600);
cfsetospeed(&toptions, B9600);
/* 8 bits, no parity, no stop bits */
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
/* Canonical mode */
toptions.c_lflag |= ICANON;
/* commit the serial port settings */
tcsetattr(fd, TCSANOW, &toptions);
if(fd > 0)return true;
return false;
}
void startCallibration(){
write(fd, "1", 1);
}
void readDataTask(){
int n = 0,
spot = 0;
char buf = '\0';
/* Whole response*/
char response[1024];
memset(response, '\0', sizeof response);
if(DEBUG)cout << "Start read" << std::endl;
do {
n = read( fd, &buf, 1 );
sprintf( &response[spot], "%c", buf );
spot += n;
if(DEBUG) cout << "Spot: " << spot << std::endl;
} while( buf != '\n' && n > 0);
if(DEBUG){
if (n < 0) {
std::cout << "Error reading: " << strerror(errno) << std::endl;
}
else if (n == 0) {
std::cout << "Read nothing!" << std::endl;
}
else {
std::cout << c << " --> Response: " << response << std::endl;
}
}
c++;
if(spot != 10){
if(DEBUG)printf("BAD BYTEES!!!!!!!\n");
return;
}
aProtocol.pacmanDirection = response[0] - '0';
aProtocol.pacmanVision = response[6] - '0';
aProtocol.pacmanVelocity = response[2] - '0';
aProtocol.pacmanAmbient = response[4] - '0';
aProtocol.state = true;
}
void readData(){
//write(fd, "1", 1);
while(true){
readDataTask();
/*printf("Srart thread\n");
thread t1(&ArduinoComm::readDataTask,this);
t1.join();
printf("End thread\n");*/
}
//}
//t1.join();
/*arduinoProtocol aP;
printf("Getting values......\n");
write(fd, "1", 1);
n = read(fd, buf, 64);
// insert terminating zero in the string
buf[n] = 0;
printf("%i bytes read, buffer contains: %s\n\n", n, buf);
// usleep(100000);
if(n != 11){
aP.state = false;
return aP;
}
aP.state = true;
aP.pacmanDirection = buf[0] - '0';
aP.pacmanVision = buf[6] - '0';
aP.pacmanVelocity = buf[2] - '0';
aP.pacmanAmbient = buf[4] - '0';
return aP;*/
}
void startProcess(){
thread t1(&ArduinoComm::readData,this);
t1.detach();
}
};