-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkilobot.h
169 lines (137 loc) · 3.75 KB
/
kilobot.h
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
/*
* Kilobot.h
*
* Created on: 16 Sep 2016
* Author: marshall
*/
#ifndef KILOBOT_H_
#define KILOBOT_H_
#include <stdint.h>
#include <QPointF>
#include <QVector>
enum lightColour {
OFF,
RED,
GREEN,
BLUE
};
typedef uint16_t kilobot_id;
typedef unsigned char kilobot_channel_colour;
//typedef unsigned char kilobot_message_type;
typedef struct {
uint8_t type :4;
kilobot_id id :10;
uint16_t data :10;
} kilobot_message;
struct kilobot_broadcast {
uint8_t type;
QVector < uint8_t > data;
};
#define uint8_t_LENGTH 10 // in bits
#define KILOBOT_MESSAGE_TYPE_LENGTH 4 // in bits
#define KILOBOT_MESSAGE_DATA_LENGTH 10 // in bits
#define KILOBOT_MAX_COLOUR 255 // colour channel
#define KILOBOT_REFRESH_DELAY 3 // in milliseconds
#define KILOBOT_MAX_ID 1024 //
#define UNASSIGNED_ID INT16_MAX
/*enum tracking_type {
POS_ONLY,
LED_ONLY,
ADAPTIVE_LED_ONLY,
POS_LED,
POS_ADAPTIVE_LED,
};*/
enum tracking_flags {
POS = 1 << 0,
LED = 1 << 1,
ADAPTIVE_LED = 1 << 2,
ROT = 1 << 3
};
#include <QObject>
/*struct kilobot_colour
{
kilobot_channel_colour r;
kilobot_channel_colour g;
kilobot_channel_colour b;
};*/
typedef lightColour kilobot_colour;
class ColourBuffer{
public:
ColourBuffer() : ColourBuffer(1) {}
~ColourBuffer() {}
ColourBuffer(int size);
void addColour(lightColour newColour);
lightColour getAvgColour();
lightColour getLastColour() {return buffer.at(buffer.size()-1);}
private:
int buffer_size;
QVector < lightColour > buffer;
};
class OrientationBuffer{
public:
OrientationBuffer() : OrientationBuffer(1) {}
~OrientationBuffer() {}
OrientationBuffer(int size) : buffer_size(size) {}
void addOrientation(QPointF newOrientation);
QPointF getAvgOrientation();
QPointF getLastOrientation() {return buffer.at(buffer.size()-1);}
private:
int buffer_size;
QVector < QPointF > buffer;
};
class PositionBuffer{
public:
PositionBuffer() : PositionBuffer(1) {}
~PositionBuffer() {}
PositionBuffer(int size) : buffer_size(size) {}
void addPosition(QPointF newPosition);
QPointF getOrientationFromPositions();
QPointF getLastPosition() {return buffer.at(buffer.size()-1);}
private:
int buffer_size;
QVector < QPointF > buffer;
};
class Kilobot : public QObject {
Q_OBJECT
public:
Kilobot(kilobot_id identifier, QPointF position, QPointF velocity, kilobot_colour colourValues);
Kilobot() {}
~Kilobot();
// copy constructor
Kilobot(const Kilobot& other);
kilobot_id getID();
void setID(kilobot_id);
QPointF getPosition();
QPointF getVelocity();
kilobot_colour getLedColour();
//kilobot_colour resolveKilobotState(stateColours);
void updateState(QPointF position, QPointF velocity, kilobot_colour colourValues);
/*!
* \brief updateHardware
* Copy the Kilobot (for thread safety) and signal
* the environment to update the hardware using that
* copy
*/
void updateHardware();
/*!
* \brief updateExperiment
* Copy the Kilobot (for thread safety) and signal
* the experiment to update the hardware using that
* copy, as well as a pointer for remapping signal /
* slot connections (should NOT be de-referenced)
*/
void updateExperiment();
int lightThreshold = 230;
ColourBuffer colBuffer = ColourBuffer(5);
OrientationBuffer velocityBuffer = OrientationBuffer(5);
PositionBuffer posBuffer = PositionBuffer(6);
signals:
void sendUpdateToHardware(Kilobot);
void sendUpdateToExperiment(Kilobot*,Kilobot);
private:
kilobot_id id = UNASSIGNED_ID;
QPointF pos = QPointF(0,0);
QPointF vel = QPointF(1,1);
kilobot_colour col = OFF;
};
#endif // KILOBOT_H