-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThunderborgDriver.cpp
More file actions
executable file
·339 lines (285 loc) · 9.06 KB
/
ThunderborgDriver.cpp
File metadata and controls
executable file
·339 lines (285 loc) · 9.06 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "ThunderborgDriver.hpp"
#include <iostream>
#include <unistd.h> //Needed for I2C port
#include <fcntl.h> //Needed for I2C port
#include <sys/ioctl.h> //Needed for I2C port
#include <linux/i2c-dev.h> //Needed for I2C port
const uint8_t I2C_MAX_LEN = 6;
const float VOLTAGE_PIN_MAX = 36.3; // Maximum voltage from the analog voltage monitoring pin
const float VOLTAGE_PIN_CORRECTION = 0.0; // Correction value for the analog voltage monitoring pin
const uint8_t I2C_ID_THUNDERBORG = 0x15;
const uint8_t COMMAND_SET_LEDS = 5;
const uint8_t COMMAND_GET_BATT_VOLT = 21; // Get the battery voltage reading
const uint8_t COMMAND_GET_ID = 0x99; // Get the board identifier
const uint8_t COMMAND_SET_A_FWD = 8; // Set motor A PWM rate in a forwards direction
const uint8_t COMMAND_SET_A_REV = 9; // Set motor A PWM rate in a reverse direction
const uint8_t COMMAND_SET_B_FWD = 11; // Set motor B PWM rate in a forwards direction
const uint8_t COMMAND_SET_B_REV = 12; // Set motor B PWM rate in a reverse direction
const uint8_t COMMAND_ALL_OFF = 14; // Switch everything off
const uint16_t COMMAND_ANALOG_MAX = 0x3FF; //Maximum value for analog readings
const uint8_t READ_BUFFER_LENGTH = I2C_MAX_LEN + 1; //Leave room for null terminator
const char* I2C_BUS = "/dev/i2c-1";
int32_t ThunderborgDriver::initialize()
{
file_i2c = open(I2C_BUS, O_RDWR);
if (file_i2c < 0)
{
std::cerr << "Error: Failed to open the i2c bus" << std::endl;
return file_i2c;
}
int status = ioctl(file_i2c, I2C_SLAVE, I2C_ID_THUNDERBORG);
if (status < 0)
{
std::cerr << "Error: Failed to acquire bus access and/or talk to slave" << std::endl;
return status;
}
else
{
//Driver successfully initialized
initialized = true;
}
return DRIVER_SUCCESS;
}
int32_t ThunderborgDriver::rawWrite(const char* data, const uint8_t dataLength)
{
int32_t bytesWritten = 0;
if (data == nullptr)
{
std::cerr << "Write error: null pointer" << std::endl;
return DRIVER_ERROR;
}
else if (dataLength > I2C_MAX_LEN)
{
std::cerr << "Error: data exceeds maximum allowed i2c length" << std::endl;
return DRIVER_ERROR;
}
else
{
bytesWritten = write(file_i2c, data, dataLength);
if (bytesWritten < dataLength)
{
std::cerr << "Error writing to i2c" << std::endl;
return DRIVER_ERROR;
}
}
return bytesWritten;
}
int32_t ThunderborgDriver::readCommandResponse(const uint8_t command, const uint8_t length, char* buffer)
{
int32_t bytesRead = 0;
/*
* Read from i2c until either the command reply is found or we exhaust our number of retries.
*/
bool commandReplyFound = false;
uint8_t retryCount = 3;
while (!commandReplyFound && (retryCount > 0))
{
bytesRead = read(file_i2c, buffer, length);
if (bytesRead != length)
{
std::cerr << "Error reading from i2c bus" << std::endl;
return DRIVER_ERROR;
}
else
{
if (buffer[0] == command)
{
commandReplyFound = true;
break;
}
else
{
retryCount--;
}
}
}
if (!commandReplyFound)
{
std::cerr << "Unable to read command reply from i2c bus" << std::endl;
return DRIVER_ERROR;
}
return bytesRead;
}
int32_t ThunderborgDriver::rawRead(const uint8_t command, const uint8_t length, char* buffer)
{
int32_t bytesRead = 0;
/*
* First write the GET command
*/
const uint8_t GET_COMMAND_DATA_SIZE = 1;
//Leave room for null-terminator
uint8_t GET_COMMAND_BUFFER_SIZE = GET_COMMAND_DATA_SIZE + 1;
char getCommandData[GET_COMMAND_BUFFER_SIZE] = "";
getCommandData[0] = command;
int32_t writeStatus = rawWrite(getCommandData, GET_COMMAND_DATA_SIZE);
if (writeStatus == DRIVER_ERROR)
{
std::cerr << "Error writing get command to i2c bus" << std::endl;
return DRIVER_ERROR;
}
else
{
bytesRead = readCommandResponse(command, length, buffer);
}
return bytesRead;
}
bool ThunderborgDriver::checkForThunderborg()
{
bool thunderborgFound = false;
char readBuffer[READ_BUFFER_LENGTH] = "";
int32_t bytesRead = rawRead(COMMAND_GET_ID, I2C_MAX_LEN, readBuffer);
if (bytesRead != I2C_MAX_LEN)
{
std::cerr << "Could not find Thunderborg!" << std::endl;
}
else
{
if (readBuffer[1] == I2C_ID_THUNDERBORG)
{
printf("Found Thunderborg at i2c address %#02x\n", I2C_ID_THUNDERBORG);
thunderborgFound = true;
}
else
{
printf("Found device but it is not a Thunderborg! (%#02x\n instead of %#02x\n)\n", readBuffer[1], I2C_ID_THUNDERBORG);
}
}
return thunderborgFound;
}
int32_t ThunderborgDriver::setLedColor(uint8_t red, uint8_t green, uint8_t blue)
{
const uint8_t RGB_MAX_VALUE = 255;
int32_t status = DRIVER_SUCCESS;
if (!initialized)
{
std::cerr << "Error: driver not initialized" << std::endl;
return DRIVER_UNINITIALIZED_ERROR;
}
else if ( (red > RGB_MAX_VALUE) || (green > RGB_MAX_VALUE) || (blue > RGB_MAX_VALUE) )
{
std::cerr << "Error: invalid input LED color value" << std::endl;
status = DRIVER_ERROR;
}
else
{
const uint8_t SET_LED_DATA_SIZE = 4;
//Leave room at the end for null-terminator
const uint8_t LED_DATA_BUFFER_SIZE = SET_LED_DATA_SIZE + 1;
char setLedData[LED_DATA_BUFFER_SIZE] = "";
setLedData[0] = COMMAND_SET_LEDS;
setLedData[1] = red;
setLedData[2] = green;
setLedData[3] = blue;
status = rawWrite(setLedData, SET_LED_DATA_SIZE);
}
return status;
}
float ThunderborgDriver::getBatteryReading()
{
float voltage = 0;
char readBuffer[READ_BUFFER_LENGTH] = "";
int32_t bytesRead = rawRead(COMMAND_GET_BATT_VOLT, I2C_MAX_LEN, readBuffer);
if (bytesRead != I2C_MAX_LEN)
{
std::cerr << "Error: Could not read battery voltage" << std::endl;
return DRIVER_ERROR;
}
else
{
float raw = (readBuffer[1] << 8) + readBuffer[2];
float level = raw / static_cast<float>(COMMAND_ANALOG_MAX);
level *= VOLTAGE_PIN_MAX;
float correctedVoltage = level + VOLTAGE_PIN_CORRECTION;
voltage = correctedVoltage;
}
return voltage;
}
bool ThunderborgDriver::validateDirection(uint8_t direction)
{
if ( (direction != FORWARD) && (direction != REVERSE) )
{
return false;
}
return true;
}
uint8_t ThunderborgDriver::determineMotorCommand(uint8_t motorNumber, uint8_t direction)
{
uint8_t motorCommand = 0;
if (motorNumber == 1)
{
if (direction == FORWARD)
{
motorCommand = COMMAND_SET_A_FWD;
}
else
{
motorCommand = COMMAND_SET_A_REV;
}
}
else
{
if (direction == FORWARD)
{
motorCommand = COMMAND_SET_B_FWD;
}
else
{
motorCommand = COMMAND_SET_B_REV;
}
}
return motorCommand;
}
int32_t ThunderborgDriver::setMotorPower(uint8_t motorNumber, uint8_t direction, uint8_t power)
{
int32_t status = DRIVER_ERROR;
bool validDirection = validateDirection(direction);
if (!validDirection)
{
std::cerr << "Error: invalid direction" << std::endl;
return DRIVER_ERROR;
}
else if ( (motorNumber != MOTOR_ONE) && (motorNumber != MOTOR_TWO) )
{
std::cerr << "Error: invalid motor number" << std::endl;
return DRIVER_ERROR;
}
else if (!initialized)
{
std::cerr << "Error: driver not initialized" << std::endl;
return DRIVER_UNINITIALIZED_ERROR;
}
else
{
uint8_t motorCommand = determineMotorCommand(motorNumber, direction);
if (motorCommand == 0)
{
std::cerr << "Error determining motor command" << std::endl;
return DRIVER_ERROR;
}
else
{
const uint8_t SET_MOTOR_DATA_SIZE = 2;
//Leave room at the end for null-terminator
const uint8_t MOTOR_DATA_BUFFER_SIZE = SET_MOTOR_DATA_SIZE + 1;
char setMotorData[MOTOR_DATA_BUFFER_SIZE] = "";
if (power > PWM_MAX)
{
power = PWM_MAX;
}
setMotorData[0] = motorCommand;
setMotorData[1] = power;
status = rawWrite(setMotorData, SET_MOTOR_DATA_SIZE);
}
}
return status;
}
int32_t ThunderborgDriver::motorsOff()
{
const uint8_t SET_ALL_OFF_DATA_SIZE = 1;
//Leave room at the end for null-terminator
const uint8_t SET_ALL_OFF_BUFFER_SIZE = SET_ALL_OFF_DATA_SIZE + 1;
char setAllOffData[SET_ALL_OFF_BUFFER_SIZE] = "";
setAllOffData [0] = COMMAND_ALL_OFF;
int32_t status = rawWrite(setAllOffData, SET_ALL_OFF_DATA_SIZE);
return status;
}