-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWalbi_record.ino
192 lines (156 loc) · 4.92 KB
/
Walbi_record.ino
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
//
// Walbi, the walking biped
//
// a project by The Inner Geek / Pedro Tavares Silva
//
// Tutorial at https://releasetheinnergeek.wordpress.com/
//
// Hardware: - Arduino Nano
// - LewanSoul LX16-A serial servos x10
// - LewanSoul Debug Board
//
// GNU General Public License v2.0
//
// This program reads and displays the angle of each servo of the robot
//
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // 10 = Arduino soft RX, 11 = Arduino soft TX
String message = "";
class CommandFeedback
{
public:
byte servoId;
byte command;
byte parameter[7];
byte numPar;
boolean messageComplete = false;
int angle(void) {
return (int(this->parameter[1]) * 256 + int(this->parameter[0]));
};
};
CommandFeedback response;
void setup() {
Serial.begin(115200); // hardware serial - USB cable from Arduino to PC running Arduino IDE Serial Monitor
while (!Serial) {}
Serial.println("\n\nHardware serial ready");
mySerial.begin(115200); // SoftwareSerial - connects Arduino to Debug Board serial pins (RX->TX, TX->RX, GND->GND)
Serial.println("PROGRAM VERSION 1.1.5\n\nSoftware serial ready\n\nSend 'R' to read servos");
}
// ========== SEND COMMAND =====================================
// =============================================================
void sendReadCommandToServos(byte servoID)
{
byte command[6];
command [0] = 0x55;
command [1] = 0x55;
command [2] = servoID;
command [3] = 3;
command [4] = 28; // SERVO_POS_READ
command [5] = (~(servoID + 3 + 28)) & 0xff;
mySerial.write(command, sizeof(command));
}
void process() {
if (sizeof(message)) {
switch (message.length()) {
case 1: // discard message if first byte is invalid
if (message[0] != 85) {
message = "";
// Serial.println("Discarder invalid byte");
} else
// Serial.println("First byte ok");
break;
case 2: // discard message if second byte is invalid
if (message[1] != 85) {
message = "";
// Serial.println("Discarded two invalid bytes");
} else
// Serial.println("Second byte ok");
break;
case 3:
case 4:
case 5:
case 6: // not enough data, wait for more data
// Serial.println("Waiting for more data");
break;
default: // 7 and above
// Serial.println("Analyzing...");
if (message.length() == (byte(message[3]) + 3)) { // servo message complete
// Serial.println("Got enough data.");
if (false) {// will check checksum in next version
// checksum = (~(id + length + cmd + [5..len-1])) & 0xff
message = "";
Serial.println("Bad checksum. Discarded data.");
} else {
response.servoId = int(message[2]);
response.command = int(message[4]);
for (int i = 0; i < (int(message[3]) - 3); i++)
response.parameter[i] = message[5 + i];
response.numPar = message[3] - 3;
response.messageComplete = true;
message = "";
// Serial.println("Good data. Loaded into object.");
}
} else {
// Serial.print("Not enough data. Needed ");
// Serial.print(int(message[3]) + 3);
// Serial.print(" bytes and got ");
// Serial.println(message.length());
}
}
}
}
boolean listen(int timeOut)
{
int wait = 0;
char incomming;
do {
if (mySerial.available()) {
incomming = mySerial.read();
if (incomming == char(-1))
Serial.println("ERROR reading SoftwareSerial");
else {
message += incomming;
// Serial.print("\nmessage is now ");
// Serial.print(message.length());
// Serial.println(" chars long\n");
// Serial.print("Going to process string <");
// Serial.print(message);
// Serial.println(">");
process();
}
}
wait++;
delay(1);
} while ((wait < timeOut) and (!response.messageComplete));
if (wait == timeOut) {
Serial.println("ERROR: timed-out");
return (false);
} else
return (true);
}
void loop() {
byte incomingByte;
if (Serial.available() > 0) { // wait for command R
incomingByte = Serial.read();
if (char(incomingByte) == 'R') {
for (int i = 1; i <= 10; i++) {
for (int a = 1; a <= 10; a++) {
sendReadCommandToServos(i); //read servo 1
if (listen(20))
break;
}
if (response.messageComplete) {
Serial.print("Servo ");
Serial.print(response.servoId);
Serial.print(" angle: ");
Serial.println(response.angle());
response.messageComplete = false;
}
}
}
else {
Serial.print("\n\nInvalid command received: ");
Serial.print("(" + String(char(incomingByte)) + ")");
}
}
}