···
3
+
#define SPU 80 // 20 teeth pulley, 16 microstep, 2mm a tooth belt, 16*200 microsteps per turn == (16*200)/(20*2) == 80
8
+
typedef uint8_t (*CallbackFunction)(uint8_t*, int, uint8_t*);
10
+
struct EventCallback {
12
+
CallbackFunction callback;
16
+
float pos[] = {0, 0};
18
+
const int motor1StepPin = D10;
19
+
const int motor1DirPin = D9;
20
+
const int motor2StepPin = D8;
21
+
const int motor2DirPin = D7;
23
+
const int enablePin = D1;
26
+
servo.attach(PIN_SERVO);
30
+
// on("light", onLight);
32
+
on("servo", moveServo);
33
+
on("motorsOn", motorsOn);
34
+
on("motorsOff", motorsOff);
35
+
on("moveTowardsOrigin", moveTowardsOrigin);
36
+
on("setOrigin", setOrigin);
38
+
pinMode(motor1StepPin, OUTPUT);
39
+
pinMode(motor1DirPin, OUTPUT);
40
+
pinMode(motor2StepPin, OUTPUT);
41
+
pinMode(motor2DirPin, OUTPUT);
43
+
pinMode(enablePin, OUTPUT); // enable pin
45
+
pinMode(PIN_LED, OUTPUT);
47
+
startupIndicator(); // once startup is finished, indicate to user
54
+
void startupIndicator() {
55
+
// flash LED to indicate board is initialized
56
+
for (int i = 0; i < 3; i++) {
57
+
digitalWrite(PIN_LED, 0);
59
+
digitalWrite(PIN_LED, 1);
64
+
uint8_t onLight(uint8_t* payload, int length, uint8_t* reply) {
65
+
uint8_t value = payload[0];
67
+
// Serial.println("light it up");
69
+
digitalWrite(PIN_LED, value);
74
+
uint8_t motorsOn(uint8_t* payload, int length, uint8_t* reply) {
75
+
digitalWrite(enablePin, 0);
79
+
uint8_t motorsOff(uint8_t* payload, int length, uint8_t* reply) {
80
+
digitalWrite(enablePin, 1);
84
+
uint8_t moveTowardsOrigin(uint8_t* payload, int length, uint8_t* reply) {
88
+
// this ternary may not be neccesary
90
+
x + ( x < 0 ? 10 : -10),
91
+
y + ( y < 0 ? 10 : -10)
97
+
uint8_t setOrigin(uint8_t* payload, int length, uint8_t* reply) {
105
+
/* ------------------------------------------------------------ */
107
+
int bufferIndex = 0;
108
+
uint8_t msgBuffer[100];
110
+
void readSerial() {
111
+
while (Serial.available() > 0) {
112
+
uint8_t incoming = Serial.read();
114
+
msgBuffer[bufferIndex] = incoming;
116
+
if (incoming != 0) {
118
+
continue; // Proceed to the next byte if current byte is not null
121
+
// Serial.print("RECEIVED: ");
122
+
// for (int i = 0; i < bufferIndex; i++) {
123
+
// Serial.print(msgBuffer[i]);
124
+
// Serial.print(", ");
126
+
// Serial.println("DONE");
128
+
// Now we have a full message, perform COBS decoding
129
+
uint8_t decoded[bufferIndex];
130
+
cobs_decode(decoded, msgBuffer, bufferIndex);
132
+
// Serial.print("DECODED: ");
133
+
// for (int i = 0; i < bufferIndex; i++) {
134
+
// Serial.print(decoded[i]);
135
+
// Serial.print(", ");
137
+
// Serial.println("DONE");
139
+
// Parse the decoded message
142
+
uint8_t msgLength = decoded[i];
143
+
// Serial.print("MSG-LENGTH: ");
144
+
// Serial.println(msgLength);
146
+
uint8_t msgArr[msgLength];
148
+
while (i < 1 + msgLength) {
149
+
msgArr[i-1] = decoded[i];
153
+
// Serial.print("MSGARR: ");
154
+
// for (int i = 0; i < msgLength; i++) {
155
+
// Serial.print(msgArr[i]);
156
+
// Serial.print(", ");
158
+
// Serial.println("MSGARR-END");
160
+
uint8_t payloadLength = decoded[i];
161
+
uint8_t payload[payloadLength];
163
+
while (i < 1 + msgLength + 1 + payloadLength) {
164
+
payload[i-1-msgLength-1] = decoded[i];
168
+
uint8_t msgCount = decoded[i];
170
+
// String msg = String((char*)msgArr);
171
+
String msg = byteArrayToString(msgArr, msgLength);
173
+
// Serial.println(msg);
175
+
// printArray("PAYLOAD", payload, payloadLength);
177
+
// Serial.print("MSGCOUNT: ");
178
+
// Serial.println(msgCount);
180
+
bool triggered = triggerEvent(msg, payload, payloadLength, msgCount);
182
+
bufferIndex = 0; // Reset the buffer for the next message
186
+
/* ------------------------------------------------------------ */
190
+
const int MAX_EVENTS = 255; // Maximum number of events to store, adjust as needed
191
+
EventCallback eventCallbacks[MAX_EVENTS];
192
+
int eventCount = 0;
194
+
const int REPLY_PAYLOAD_LENGTH = 255;
195
+
uint8_t reply[REPLY_PAYLOAD_LENGTH];
197
+
void on(String event, CallbackFunction callback) {
198
+
if (eventCount < MAX_EVENTS) {
199
+
eventCallbacks[eventCount].event = event;
200
+
eventCallbacks[eventCount].callback = callback;
201
+
eventCount = (eventCount + 1) % MAX_EVENTS;
203
+
// Serial.println("Max number of events reached. Wrapping events.");
207
+
bool triggerEvent(String event, uint8_t* payload, int payloadLength, uint8_t msgCount) {
208
+
for (int i = 0; i < eventCount; i++) {
209
+
if (eventCallbacks[i].event == event) {
210
+
// want to pass payload and payloadLength, need to get response payload
211
+
uint8_t reply_length = eventCallbacks[i].callback(payload, payloadLength, reply);
213
+
sendAck(msgCount, reply, reply_length);
218
+
// Serial.println(" No event registered.");
222
+
const int arrayLength = 7; // + length;
223
+
uint8_t byteArray[arrayLength];
225
+
void sendAck(uint8_t msgCount, uint8_t* reply, uint8_t length) {
226
+
// Serial.println("SEND ACK");
228
+
// int arrayLength = 7; // + length;
229
+
// static uint8_t byteArray[arrayLength];
231
+
byteArray[0] = 0x03;
232
+
byteArray[1] = 0x61;
233
+
byteArray[2] = 0x63;
234
+
byteArray[3] = 0x6B;
235
+
byteArray[4] = 0x00;
236
+
byteArray[5] = msgCount;
237
+
byteArray[6] = 0x0A;
239
+
// byteArray[4] = length;
240
+
// for (int i = 0; i < length; i++) {
241
+
// byteArray[i+5] = reply[i];
243
+
// byteArray[5+length] = msgCount;
244
+
// byteArray[6+length] = 0x0A;
246
+
// Serial.println(msgCount);
247
+
// printArray("ACK", byteArray, 5+length);
249
+
Serial.write(byteArray, arrayLength);
251
+
// uint8_t byteArrayEncoded[arrayLength + 2]; // +2 for possible COBS overhead
252
+
// cobs_encode(byteArrayEncoded, byteArray, arrayLength);
253
+
// Serial.write(byteArrayEncoded, arrayLength + 2);
255
+
// printArray("ENCODED-ACK", byteArray, 5+length);
259
+
/* ------------------------------------------------------------ */
261
+
void cobs_encode(uint8_t *dst, const uint8_t *src, size_t len) {
262
+
size_t read_index = 0;
263
+
size_t write_index = 1;
264
+
size_t code_index = 0;
267
+
while (read_index < len) {
268
+
if (src[read_index] == 0) {
269
+
dst[code_index] = code;
271
+
code_index = write_index++;
274
+
dst[write_index++] = src[read_index++];
276
+
if (code == 0xFF) {
277
+
dst[code_index] = code;
279
+
code_index = write_index++;
284
+
dst[code_index] = code;
286
+
// Add trailing zero
287
+
if (write_index < len + 2) {
288
+
dst[write_index] = 0;
292
+
void cobs_decode(uint8_t *dst, const uint8_t *src, size_t len) {
293
+
size_t i, j, dst_i = 0;
294
+
for (i = 0; i < len;) {
295
+
uint8_t code = src[i++];
296
+
for (j = 1; j < code && i < len; j++) {
297
+
dst[dst_i++] = src[i++];
299
+
if (code < 0xFF && dst_i < len) {
305
+
/* ------------------------------------------------------------ */
307
+
// TODO: THINK THIS IS BUGGY
308
+
void cobs_print(const String& message) {
309
+
// Convert the message to a byte array
310
+
int length = message.length();
311
+
uint8_t byteArray[length + 1]; // +1 for the null terminator
312
+
message.getBytes(byteArray, length + 1);
314
+
// Prepare the buffer for the encoded message
315
+
uint8_t encoded[length + 2]; // +2 for possible COBS overhead
317
+
// Perform COBS encoding
318
+
cobs_encode(encoded, byteArray, length + 1);
320
+
// Send the encoded message
321
+
Serial.write(encoded, length + 2); // Write the encoded bytes
324
+
/* ------------------------------------------------------------ */
326
+
float read_float(uint8_t* buffer, int index) {
327
+
uint8_t byte0 = buffer[index];
328
+
uint8_t byte1 = buffer[index+1];
329
+
uint8_t byte2 = buffer[index+2];
330
+
uint8_t byte3 = buffer[index+3];
332
+
uint8_t byteArray[] = {byte0, byte1, byte2, byte3};
334
+
memcpy(&floatValue, &byteArray, sizeof(floatValue));
339
+
int read_int(uint8_t* buffer, int index) {
340
+
uint8_t byte0 = buffer[index];
341
+
uint8_t byte1 = buffer[index+1];
342
+
uint8_t byte2 = buffer[index+2];
343
+
uint8_t byte3 = buffer[index+3];
345
+
uint8_t byteArray[] = {byte0, byte1, byte2, byte3};
347
+
memcpy(&value, &byteArray, sizeof(value));
352
+
String byteArrayToString(byte arr[], int length) {
353
+
String result = "";
355
+
for (int i = 0; i < length; i++) {
356
+
result += (char)arr[i]; // Convert each byte to a character and append it to the string
362
+
/* ------------------------------------------------------------------------ */
364
+
#define EPSILON 0.01
366
+
void goTo(float x, float y) {
368
+
// Serial.println("START GOTO");
370
+
// Set your target distances for each motor (in steps)
371
+
float motor1Target = (x + y) - pos[0];
372
+
float motor2Target = (y - x) - pos[1];
374
+
// Set motor direction based on target values
375
+
digitalWrite(motor1DirPin, motor1Target >= 0 ? HIGH : LOW);
376
+
digitalWrite(motor2DirPin, motor2Target >= 0 ? HIGH : LOW);
378
+
// Calculate the relative speeds and maximum duration for both motors
379
+
float maxSteps = max(abs(motor1Target), abs(motor2Target));
380
+
float motor1Speed = abs(motor1Target) / maxSteps;
381
+
float motor2Speed = abs(motor2Target) / maxSteps;
383
+
unsigned long stepDuration = 500; // The time it takes to perform one step in microseconds
384
+
unsigned long motor1StepInterval = stepDuration / motor1Speed;
385
+
unsigned long motor2StepInterval = stepDuration / motor2Speed;
387
+
// Initialize variables for step timing
388
+
unsigned long motor1PrevStepTime = 0;
389
+
unsigned long motor2PrevStepTime = 0;
390
+
float motor1Step = 0;
391
+
float motor2Step = 0;
393
+
// Loop until both motors reach their target steps
394
+
while (abs(motor1Target - motor1Step) > EPSILON || abs(motor2Target - motor2Step) > EPSILON) {
397
+
unsigned long currentTime = micros();
400
+
if (abs(motor1Target - motor1Step) > EPSILON && ((currentTime - motor1PrevStepTime) >= motor1StepInterval)) {
401
+
digitalWrite(motor1StepPin, HIGH);
402
+
delayMicroseconds(1);
403
+
digitalWrite(motor1StepPin, LOW);
404
+
delayMicroseconds(1);
406
+
motor1Step += (motor1Target >= 0 ? 1.0 : -1.0)/SPU;
407
+
motor1PrevStepTime = currentTime;
411
+
if (abs(motor2Target - motor2Step) > EPSILON && ((currentTime - motor2PrevStepTime) >= motor2StepInterval)) {
412
+
digitalWrite(motor2StepPin, HIGH);
413
+
delayMicroseconds(1);
414
+
digitalWrite(motor2StepPin, LOW);
415
+
delayMicroseconds(1);
417
+
motor2Step += (motor2Target >= 0 ? 1.0 : -1.0)/SPU;
418
+
motor2PrevStepTime = currentTime;
422
+
// Serial.println("END GOTO");
424
+
pos[0] += motor1Step;
425
+
pos[1] += motor2Step;
428
+
uint8_t go(uint8_t* payload, int length, uint8_t* reply) {
429
+
float x = read_float(payload, 0);
430
+
float y = read_float(payload, 4);
437
+
uint8_t moveServo(uint8_t* payload, int length, uint8_t* reply) {
438
+
int angle = read_int(payload, 0);
440
+
servo.writeMicroseconds(angle);
447
+
void printArray(String label, uint8_t* arr, int arrSize) {
448
+
Serial.print(label);
449
+
Serial.print("-BEGIN: ");
450
+
for (int i = 0; i < arrSize; i++) {
451
+
Serial.print(arr[i]);
452
+
Serial.print(", ");
454
+
Serial.print(label);
455
+
Serial.println("-END");