-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOdometry.cpp
197 lines (160 loc) · 4.23 KB
/
Odometry.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* This file is part of the RPI Robot Framework distribution (https://github.com/Elektropioneer/RPI_Robot_Framework).
* Copyright (c) 2020 Miloš Zivlak (milos.zivlak@sensa-group.net).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver/Odometry.h"
#include "core/Serial.h"
#define _ODOMETRY_COMMAND_1 'o'
#define _ODOMETRY_COMMAND_2 'd'
#define _ODOMETRY_CMD _ODOMETRY_COMMAND_1, _ODOMETRY_COMMAND_2
Odometry::Odometry(void)
{
}
Odometry::Odometry::~Odometry(void)
{
}
OdometryStatus Odometry::getInfo(void)
{
}
bool Odometry::setMaxSpeed(int speed)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'V';
msg << (uint8_t)(speed & 0xFF);
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::setPosition(int x, int y, int orientation)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'I';
msg << (uint8_t)((x >> 8) & 0xFF);
msg << (uint8_t)(x & 0xFF);
msg << (uint8_t)((y >> 8) & 0xFF);
msg << (uint8_t)(y & 0xFF);
msg << (uint8_t)((angle >> 8) & 0xFF);
msg << (uint8_t)(angle & 0xFF);
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::hardStop(void)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'S';
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::softStop(void)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 's'
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::moveForward(int distance)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'D';
msg << (uint8_t)((distance >> 8) & 0xFF);
msg << (uint8_t)(distance & 0xFF);
msg << (uint8_t)(DirectionForward);
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::moveBackward(int distance)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'D';
msg << (uint8_t)((distance >> 8) & 0xFF);
msg << (uint8_t)(distance & 0xFF);
msg << (uint8_t)(DirectionBackward);
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::rotate(int angle)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'T';
msg << (uint8_t)((angle >> 8) & 0xFF);
msg << (uint8_t)(angle & 0xFF);
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::rotateAbsolute(int angle)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'A';
msg << (uint8_t)((angle >> 8) & 0xFF);
msg << (uint8_t)(angle & 0xFF);
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::gotoXY(int x, int y, int speed, Direction direction)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'Q';
msg << (uint8_t)((x >> 8) & 0xFF);
msg << (uint8_t)(x & 0xFF);
msg << (uint8_t)((y >> 8) & 0xFF);
msg << (uint8_t)(y & 0xFF);
msg << (uint8_t)(speed & 0xFF);
msg << (uint8_t)(direction & 0xFF);
if (!serial.send(msg))
{
return false;
}
}
bool Odometry::arc(int xc, int yc, int fi, Direction direction)
{
Serial &serial = Serial::serial();
SerialMessage msg{ _ODOMETRY_CMD };
msg << 'Q';
msg << (uint8_t)((xc >> 8) & 0xFF);
msg << (uint8_t)(xc & 0xFF);
msg << (uint8_t)((yc >> 8) & 0xFF);
msg << (uint8_t)(yc & 0xFF);
msg << (uint8_t)((fi >> 8) & 0xFF);
msg << (uint8_t)(fi & 0xFF);
msg << (uint8_t)(direction & 0xFF);
if (!serial.send(msg))
{
return false;
}
}