Repository navigation
Expand file tree
/
Copy pathODriveArduino.cpp
More file actions
255 lines (220 loc) · 7.49 KB
/
Copy pathODriveArduino.cpp
File metadata and controls
255 lines (220 loc) · 7.49 KB
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
#include "Arduino.h"
#include "ODriveArduino.h"
ODriveArduino::ODriveArduino(Stream &serial) : serial_(serial) {}
void ODriveArduino::WaitIdle(int motor_number, float timeout)
{
unsigned long timeout_ms = (unsigned long)timeout * 1000;
unsigned long start_ms = millis();
int state0 = -1, state1 = -1;
do
{
delay(50);
if (motor_number == 0 || motor_number == -1)
serial_ << "r axis0.current_state\n";
if (motor_number == 1 || motor_number == -1)
serial_ << "r axis1.current_state\n";
if (motor_number == 0 || motor_number == -1)
state0 = readInt();
if (motor_number == 1 || motor_number == -1)
state1 = readInt();
if (motor_number == 0)
if (state0 == AXIS_STATE_IDLE)
break;
if (motor_number == 1)
if (state1 == AXIS_STATE_IDLE)
break;
if (motor_number == -1)
if (state0 == AXIS_STATE_IDLE && state1 == AXIS_STATE_IDLE)
break;
} while (millis() - start_ms < timeout_ms);
}
void ODriveArduino::WriteProperty(int motor_number, const char *property, double value)
{
serial_ << "w axis" << motor_number << "." << property << " " << value << "\n";
}
void ODriveArduino::WriteProperty(int motor_number, const char *property, float value)
{
serial_ << "w axis" << motor_number << "." << property << " " << value << "\n";
}
void ODriveArduino::WriteProperty(int motor_number, const char *property, int value)
{
serial_ << "w axis" << motor_number << "." << property << " " << value << "\n";
}
void ODriveArduino::ReadProperty(int motor_number, const char *property, double *value)
{
serial_ << "r axis" << motor_number << "." << property << "\n";
*value = readFloat();
}
void ODriveArduino::ReadProperty(int motor_number, const char *property, float *value)
{
serial_ << "r axis" << motor_number << "." << property << "\n";
*value = readFloat();
}
void ODriveArduino::ReadProperty(int motor_number, const char *property, int *value)
{
serial_ << "r axis" << motor_number << "." << property << "\n";
*value = readInt();
}
void ODriveArduino::SetPosition(int motor_number, float position)
{
SetPosition(motor_number, position, 0.0f, 0.0f);
}
void ODriveArduino::SetPosition(int motor_number, float position, float velocity_feedforward)
{
SetPosition(motor_number, position, velocity_feedforward, 0.0f);
}
void ODriveArduino::SetPosition(int motor_number, float position, float velocity_feedforward, float current_feedforward)
{
serial_ << "p " << motor_number << " " << position << " " << velocity_feedforward << " " << current_feedforward << "\n";
}
void ODriveArduino::SetVelocity(int motor_number, float velocity)
{
SetVelocity(motor_number, velocity, 0.0f);
}
void ODriveArduino::SetVelocity(int motor_number, float velocity, float current_feedforward)
{
serial_ << "v " << motor_number << " " << velocity << " " << current_feedforward << "\n";
}
void ODriveArduino::SetCurrent(int motor_number, float current)
{
serial_ << "c " << motor_number << " " << current << "\n";
}
void ODriveArduino::TrapezoidalMove(int motor_number, float position)
{
serial_ << "t " << motor_number << " " << position << "\n";
}
void ODriveArduino::SetVelocityBoth(float velocity_M0, float velocity_M1)
{
serial_ << "v 0 " << velocity_M0 << "\nv 1 " << velocity_M1 << "\n";
}
void ODriveArduino::SetVelocity_GetFeedback(int motor_number, float set_VM, float *velocity_M, float *position_M)
{
CleanSerial();
serial_ << "f " << motor_number << "\nv " << motor_number << " " << set_VM << "\n";
*position_M = readFloat();
*velocity_M = readFloat();
}
void ODriveArduino::SetVelocityBoth_GetFeedback(float set_VM0, float set_VM1, float *velocity_M0, float *position_M0, float *velocity_M1, float *position_M1)
{
CleanSerial();
serial_ << "f 0\nf 1\nv 0 " << set_VM0 << "\nv 1 " << set_VM1 << "\n";
*position_M0 = readFloat();
*velocity_M0 = readFloat();
*position_M1 = readFloat();
*velocity_M1 = readFloat();
}
void ODriveArduino::SetVelocityBoth_GetFeedback_Vbus(float set_VM0, float set_VM1, float *vbus, float *velocity_M0, float *position_M0, float *velocity_M1, float *position_M1)
{
CleanSerial();
serial_ << "f 0\nf 1\nr vbus_voltage\nv 0 " << set_VM0 << "\nv 1 " << set_VM1 << "\n";
*position_M0 = readFloat();
*velocity_M0 = readFloat();
*position_M1 = readFloat();
*velocity_M1 = readFloat();
*vbus = readFloat();
}
void ODriveArduino::SetVelocityBoth_GetVbus(float set_VM0, float set_VM1, float *vbus)
{
CleanSerial();
serial_ << "r vbus_voltage\nv 0 " << set_VM0 << "\nv 1 " << set_VM1 << "\n";
*vbus = readFloat();
}
float ODriveArduino::GetVelocity(int motor_number)
{
serial_ << "r axis" << motor_number << ".encoder.vel_estimate\n";
return readFloat();
}
float ODriveArduino::GetPosition(int motor_number)
{
serial_ << "r axis" << motor_number << ".encoder.pos_estimate\n";
return readFloat();
}
float ODriveArduino::GetVbus()
{
serial_ << "r vbus_voltage\n";
return readFloat();
}
void ODriveArduino::GetFeedback(int motor_number, float *velocity_M, float *position_M)
{
CleanSerial();
serial_ << "f " << motor_number << "\n";
*position_M = readFloat();
*velocity_M = readFloat();
}
void ODriveArduino::GetFeedbackBoth(float *velocity_M0, float *position_M0, float *velocity_M1, float *position_M1)
{
CleanSerial();
serial_ << "f 0\nf 1\n";
*position_M0 = readFloat();
*velocity_M0 = readFloat();
*position_M1 = readFloat();
*velocity_M1 = readFloat();
}
void ODriveArduino::GetFeedback_Vbus(int motor_number, float *velocity_M, float *position_M, float *vbus)
{
CleanSerial();
serial_ << "f " << motor_number << "\nr vbus_voltage\n";
*position_M = readFloat();
*velocity_M = readFloat();
*vbus = readFloat();
}
void ODriveArduino::GetFeedbackBoth_Vbus(float *velocity_M0, float *position_M0, float *velocity_M1, float *position_M1, float *vbus)
{
CleanSerial();
serial_ << "f 0\nf 1\nr vbus_voltage\n";
*position_M0 = readFloat();
*velocity_M0 = readFloat();
*position_M1 = readFloat();
*velocity_M1 = readFloat();
*vbus = readFloat();
}
ODriveAxisState ODriveArduino::read_state(int axis)
{
serial_ << "r axis" << axis << ".current_state\n";
return (ODriveAxisState)readInt();
}
bool ODriveArduino::run_state(int axis, ODriveAxisState requested_state, bool wait_for_idle, float timeout)
{
int timeout_ctr = (int)(timeout * 100.0f);
serial_ << "w axis" << axis << ".requested_state " << requested_state << '\n';
if (wait_for_idle)
{
do
{
delay(50);
serial_ << "r axis" << axis << ".current_state\n";
} while (readInt() != AXIS_STATE_IDLE && --timeout_ctr > 0);
}
return timeout_ctr > 0;
}
float ODriveArduino::readFloat(unsigned long timeout)
{
return readString(timeout).toFloat();
}
int32_t ODriveArduino::readInt(unsigned long timeout)
{
return readString(timeout).toInt();
}
String ODriveArduino::readString(unsigned long timeout)
{
String str = "";
unsigned long timeout_start = micros();
while (true)
{
while (!serial_.available())
{
if (micros() - timeout_start >= timeout)
return str;
}
char c = serial_.read();
if (c == '\n' || c == ' ')
break;
str += c;
}
return str;
}
void ODriveArduino::CleanSerial()
{
while (serial_.available())
serial_.read();
}