0 | |
#include <Messenger.h>
|
1 | |
|
|
0 |
#define BAD_REQUEST "BADREQUEST"
|
|
1 |
#define HEARTBEAT_PULSE "PULSE"
|
|
2 |
#define MESSAGE_TIMEOUT_CYCLES 100
|
|
3 |
#define MOTOR_COUNT 4
|
|
4 |
#define SPEED_HEADER "NEWSPEED"
|
2 | 5 |
#define STATUS_LED 13
|
3 | |
#define MOTOR_COUNT 4
|
4 | 6 |
#define STATUS_LED_CYCLES 30000
|
5 | |
#define DEBUG_RESPONSE 1
|
6 | |
|
7 | |
Messenger serial = Messenger();
|
8 | |
unsigned int led_cycles = 0;
|
|
7 |
#define WORD_BUFFER_LENGTH 4
|
|
8 |
#define WORD_LENGTH 3
|
9 | 9 |
|
10 | 10 |
struct motor {
|
11 | 11 |
unsigned char pin;
|
|
16 | 16 |
struct motor motors[MOTOR_COUNT] = {
|
17 | 17 |
{6, 0, 0}, {9, 0, 0}, {10, 0, 0}, {11, 0, 0}
|
18 | 18 |
};
|
|
19 |
|
|
20 |
char buffer[WORD_BUFFER_LENGTH];
|
|
21 |
unsigned int led_cycles = 0;
|
|
22 |
|
19 | 23 |
|
20 | 24 |
/**
|
21 | 25 |
* Updates the speeds of the motors.
|
|
28 | 32 |
}
|
29 | 33 |
|
30 | 34 |
void print_speeds(void) {
|
31 | |
Serial.print("New speeds: ");
|
|
35 |
Serial.print(SPEED_HEADER);
|
32 | 36 |
for (int i = 0; i < MOTOR_COUNT; i++) {
|
|
37 |
Serial.print(":"
|
|
38 |
);
|
33 | 39 |
Serial.print(motors[i].current_speed);
|
34 | |
Serial.print(' ');
|
35 | 40 |
}
|
36 | 41 |
Serial.println();
|
37 | |
}
|
38 | |
|
39 | |
/**
|
40 | |
* Handle a message from the controller. Sets the speeds then
|
41 | |
* requests a speed update.
|
42 | |
*/
|
43 | |
void handle_message(void) {
|
44 | |
for (int i = 0; i < MOTOR_COUNT; i++) {
|
45 | |
if (! serial.available()) {
|
46 | |
Serial.println("You didn't send me a correctly formatted message.");
|
47 | |
#ifdef DEBUG_RESPONSE
|
48 | |
print_speeds();
|
49 | |
#endif
|
50 | |
return;
|
51 | |
} else {
|
52 | |
motors[i].next_speed = serial.readInt() % 256;
|
53 | |
}
|
54 | |
}
|
55 | |
|
56 | |
write_speeds();
|
57 | |
#ifdef DEBUG_RESPONSE
|
58 | |
print_speeds();
|
59 | |
#endif
|
60 | 42 |
}
|
61 | 43 |
|
62 | 44 |
/**
|
|
74 | 56 |
|
75 | 57 |
/* Initialize serial monitor. */
|
76 | 58 |
Serial.begin(115200);
|
77 | |
serial.attach(handle_message);
|
78 | 59 |
}
|
79 | 60 |
|
80 | 61 |
/**
|
81 | 62 |
* Wait for serial information to come in.
|
82 | 63 |
*/
|
83 | 64 |
void loop(void) {
|
84 | |
while (Serial.available() > 0) {
|
|
65 |
if (Serial.available() >= WORD_LENGTH) {
|
|
66 |
int i, j, timeout;
|
85 | 67 |
led_cycles = 0;
|
86 | 68 |
digitalWrite(STATUS_LED, HIGH);
|
87 | 69 |
|
88 | |
serial.process(Serial.read());
|
|
70 |
for (i=0; i<MOTOR_COUNT; i++) {
|
|
71 |
for (timeout=0; timeout < MESSAGE_TIMEOUT_CYCLES; timeout++) {
|
|
72 |
if (Serial.available() >= WORD_LENGTH) break;
|
|
73 |
}
|
|
74 |
|
|
75 |
if (Serial.available() >= WORD_LENGTH) {
|
|
76 |
for (j=0; j<WORD_LENGTH; j++) {
|
|
77 |
buffer[j] = Serial.read();
|
|
78 |
}
|
|
79 |
|
|
80 |
buffer[WORD_LENGTH] = '\0';
|
|
81 |
motors[i].next_speed = atoi(buffer);
|
|
82 |
} else {
|
|
83 |
Serial.println(BAD_REQUEST);
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
/* Flush serial buffer after reading a line. */
|
|
88 |
while (Serial.available()) {
|
|
89 |
Serial.print("");
|
|
90 |
Serial.read();
|
|
91 |
}
|
|
92 |
|
|
93 |
write_speeds();
|
|
94 |
print_speeds();
|
89 | 95 |
}
|
90 | 96 |
|
91 | 97 |
led_cycles++;
|
92 | 98 |
if (led_cycles >= STATUS_LED_CYCLES) {
|
93 | 99 |
digitalWrite(STATUS_LED, LOW);
|
94 | 100 |
}
|
|
101 |
|
|
102 |
if (led_cycles % 10000 == 0) {
|
|
103 |
// Serial.println(HEARTBEAT_PULSE);
|
|
104 |
}
|
|
105 |
|
95 | 106 |
}
|
|
107 |
|