-
Notifications
You must be signed in to change notification settings - Fork 8
/
kilobotoverheadcontroller.cpp
357 lines (299 loc) · 10.7 KB
/
kilobotoverheadcontroller.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* KilobotOverheadController
*
* Created: 3rd Nov 2016
* Alex Cope
*
* Mainly adapted from the KiloGUI example code, but allowing integration with the Smart Arena
* infrastructure
*
*/
#include "kilobotoverheadcontroller.h"
#include "ohc/packet.h"
#include <QThread>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QDebug>
// OHC data structures & defs
typedef struct {
const char *name;
const unsigned char type;
} kilo_cmd_t;
#define COMMAND_STOP 250
#define COMMAND_LEDTOGGLE 251
static const kilo_cmd_t KILO_COMMANDS[] = {
{"Reset", RESET},
{"Run", RUN},
{"Pause", WAKEUP},
{"Sleep", SLEEP},
{"Voltage", VOLTAGE},
{"LedToggle", COMMAND_LEDTOGGLE},
{"Charging", CHARGE}
};
static const int NUM_KILO_COMMANDS = sizeof(KILO_COMMANDS)/sizeof(kilo_cmd_t);
KilobotOverheadController::KilobotOverheadController(QObject *parent) : QObject(parent), device(2), sending(false), connected(false)
{
lastMsgTime.start();
// OHC link setup
serial_conn = new SerialConnection();
connect(serial_conn, SIGNAL(error(QString)), this, SLOT(showError(QString)));
connect(serial_conn, SIGNAL(status(QString)), this, SLOT(serialUpdateStatus(QString)));
connect(serial_conn, SIGNAL(SendMsgsQueueState(bool)), this, SLOT(SendMsgsQueueState(bool)));
// Create thread
QThread *thread = new QThread();
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
// Move connection to thread
serial_conn->moveToThread(thread);
// Start thread and open connections
thread->start();
serial_conn->open();
timer.setInterval(20);
connect(&timer,SIGNAL(timeout()), this, SLOT(sendBatch()));
timer.start();
qDebug() << serial_conn->enumerate();
}
KilobotOverheadController::~KilobotOverheadController()
{
// nothing doing here...
}
void KilobotOverheadController::identifyKilobot(uint8_t id)
{
assert(id <= pow(2, uint8_t_LENGTH) - 1);
// TEMPORARY SIGNALLING:
uint8_t type = (uint8_t) 4;
uint8_t data_ohc[9] = {(uint8_t) id,0,0,0,0,0,0,0,0};
this->sendDataMessage(data_ohc, type);
}
void KilobotOverheadController::signalKilobot(kilobot_message message)
{
// NOTES: -type goes from 0-127 for user defined types and is 8bit unsigned int (128+ reserved for system types)
// -data must be an array of 9 unsigned 8bit ints
// push message onto queue for sending (QTimer will send)
this->message_q.push_back(message);
// qDebug() << "Queued message id" << message.id << "type" << message.type << "data" << message.data;
}
void KilobotOverheadController::sendBatch()
{
//qDebug() << "Running sendBatch()" << this->lastMsgTime.currentTime();
//bool enoughTimeOrEmptyList = (serial_conn->cmdQueueSize() > 2 && this->lastMsgTime.elapsed() > 100) || this->lastMsgTime.elapsed() > 30;
if (message_q.empty() && !sending && this->lastMsgTime.elapsed() > TIMEPERMSG_ms) {
this->stopSending();
}
while (message_q.size() > 0) {
if (message_q.size() > 2) {
this->lastMsgTime.restart();
uint8_t type = 0; // reserved for three-in-one messages
uint8_t data[9] = {0,0,0,0,0,0,0,0,0}; // intialise to zero
// pack data into buffer for each of the three messages (3 bytes for each message, 9 bytes total)
for (int i = 0; i < 3; ++i) {
data[i*3] = data[i*3] | (this->message_q.front().id >> 2);
data[1+i*3] = data[1+i*3] | (this->message_q.front().id << 6);
data[1+i*3] = data[1+i*3] | (this->message_q.front().type << 2);
data[1+i*3] = data[1+i*3] | (this->message_q.front().data >> 8);
data[2+i*3] = data[2+i*3] | this->message_q.front().data;
// remove message from queue
this->message_q.pop_front();
}
// send message
this->sendDataMessage(data, type);
} else if (message_q.size() > 0 && this->lastMsgTime.elapsed() > TIMEPERMSG_ms) {
this->lastMsgTime.restart();
uint8_t type = 0; // reserved for three-in-one messages
uint8_t data[9] = {0,0,0,0,0,0,0,0,0}; // intialise to zero
for (int i = 0; i < 3; ++i) {
if (message_q.size() > 0) {
data[i*3] = data[i*3] | (this->message_q.front().id >> 2);
data[1+i*3] = data[1+i*3] | (this->message_q.front().id << 6);
data[1+i*3] = data[1+i*3] | (this->message_q.front().type << 2);
data[1+i*3] = data[1+i*3] | (this->message_q.front().data >> 8);
data[2+i*3] = data[2+i*3] | this->message_q.front().data;
// remove message from queue
this->message_q.pop_front();
} else {
kilobot_message msg;
msg.id = 1023;
msg.type = 0;
msg.data = 0;
data[i*3] = data[i*3] | (msg.id >> 2);
data[1+i*3] = data[1+i*3] | (msg.id << 6);
data[1+i*3] = data[1+i*3] | (msg.type << 2);
data[1+i*3] = data[1+i*3] | (msg.data >> 8);
data[2+i*3] = data[2+i*3] | msg.data;
}
}
// send message
this->sendDataMessage(data, type);
}
}
}
void KilobotOverheadController::broadcastMessage(kilobot_broadcast message)
{
// NOTES: -type goes from 0-127 for user defined types and is 8bit unsigned int (128+ reserved for system types)
// -data must be an array of 9 unsigned 8bit ints
if (message.type == 0) {
qDebug() << "Warning - tried to send a user broadcast message with type 0 reserved for composite messages";
return;
}
if (message.type > 127 && message.type != COMMAND_STOP) {
qDebug() << "Warning - tried to send a user broadcast message with type in the system message range > 127";
return;
}
if (message.type == COMMAND_STOP) {
this->stopSending();
//qDebug() << "STOP!";
return;
}
if (message.data.isEmpty()) {
uint8_t data[9] = {0,0,0,0,0,0,0,0,0};
sending=true;
this->sendDataMessage(data, message.type);
} else {
sending=true;
this->sendDataMessage(&message.data[0], message.type);
}
//qDebug() << "Broadcasting" << message.type << " content" << message.data;
}
void KilobotOverheadController::serialUpdateStatus(QString str)
{
serial_status = str;
updateStatus();
}
void KilobotOverheadController::updateStatus()
{
QString str = serial_status;
if (str.startsWith("connect")) {
connected = true;
emit errorMessage("OHC connected");
// enable stuff for when connected
} else {
connected = false;
// disable stuff for when not connected
emit errorMessage("OHC disconnected");
}
}
void KilobotOverheadController::toggleConnection() {
if (serial_status.startsWith("connect")) {
serial_conn->close();
emit setStopButton(true);
}
else
serial_conn->open();
}
void KilobotOverheadController::stopSending() {
sendMessage(COMMAND_STOP);
}
void KilobotOverheadController::sendMessage(int type_int) {
unsigned char type = (unsigned char)type_int;
QByteArray packet(PACKET_SIZE, 0);
if (type == COMMAND_STOP) {
sending = false;
packet[0] = PACKET_HEADER;
packet[1] = PACKET_STOP;
packet[PACKET_SIZE-1]=PACKET_HEADER^PACKET_STOP;
} else {
if (sending) {
this->stopSending();
//return;
}
if (type == COMMAND_LEDTOGGLE) {
sending = false;
packet[0] = PACKET_HEADER;
packet[1] = PACKET_LEDTOGGLE;
packet[PACKET_SIZE-1]=PACKET_HEADER^PACKET_LEDTOGGLE;
} else {
sending = true;
packet[0] = PACKET_HEADER;
packet[1] = PACKET_FORWARDMSG;
packet[11] = type;
packet[PACKET_SIZE-1]=PACKET_HEADER^PACKET_FORWARDMSG^type;
}
}
serial_conn->resetDelay();
serial_conn->sendCommand(packet);
}
void KilobotOverheadController::sendDataMessage(uint8_t *payload, uint8_t type) {
//if (sending) {
emit setStopButton(true);
//stopSending();
//}
QByteArray packet(PACKET_SIZE, 0);
uint8_t checksum = PACKET_HEADER^PACKET_FORWARDMSG^type;
packet[0] = PACKET_HEADER;
packet[1] = PACKET_FORWARDMSG;
for (int i = 0; i < 9; i++) {
packet[2+i] = payload[i];
checksum ^= payload[i];
}
packet[11] = type;
packet[PACKET_SIZE-1] = checksum;
//sending = true; //added recently
serial_conn->queueCommand(packet);
}
void KilobotOverheadController::chooseProgramFile() {
QSettings settings;
QString lastDir = settings.value("progLastDir", QDir::homePath()).toString();
QString filename = QFileDialog::getOpenFileName((QWidget *) sender(), "Open Program File", lastDir, "Program Hex File (*.hex)"); //launches File Selector
program_file = filename;
//upload_button->setEnabled(false);
if (filename.isEmpty()) {
((QPushButton *)sender())->setText("[select file]");
} else {
QFileInfo info(filename);
if (info.isReadable()) {
((QPushButton *)sender())->setText(info.fileName());
//if (connected)
// upload_button->setEnabled(true);
QDir dirName (filename);
dirName.cdUp();
settings.setValue ("progLastDir", dirName.absolutePath());
}
else {
QMessageBox::critical((QWidget *) sender(), "Kilobots Toolkit", "Unable to open program file for reading.");
((QPushButton *)sender())->setText("[select file]");
program_file = "";
}
}
}
void KilobotOverheadController::uploadProgram() {
if (sending) {
this->stopSending();
emit setStopButton(true);
}
if (program_file.isEmpty()) {
QMessageBox::critical((QWidget *) sender(), "Kilobots Toolkit", "You must select a program file to upload.");
}
else {
// set to boot
this->sendMessage(BOOT);
sending = true;
serial_conn->sendProgram(program_file);
}
}
void KilobotOverheadController::showError(QString str)
{
emit errorMessage(str);
}
void KilobotOverheadController::resetKilobots()
{
sendMessage(RESET);
}
void KilobotOverheadController::sleepKilobots()
{
sendMessage(SLEEP);
}
void KilobotOverheadController::runKilobots()
{
sendMessage(RUN);
}
void KilobotOverheadController::checkVoltage()
{
sendMessage(VOLTAGE);
}
//void KilobotOverheadController::setSerial()
//{
//this->device = 2;
/*QVector<QString> ports = SerialConnection::enumerate();
if (ports.size()>0) {
}*/
//}