-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_arm.ino
More file actions
489 lines (421 loc) · 14.1 KB
/
Copy pathrobot_arm.ino
File metadata and controls
489 lines (421 loc) · 14.1 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
* Fixed pick-and-place demo sequence.
*
* setup() moves the robot to HOME/ZERO, opens the gripper, executes the
* TARGETS[] sequence with the open-loop Jacobian IK trajectory generator, then
* returns to HOME through the same controlled motion path. loop() stays idle.
*/
#include <Wire.h>
#include <math.h>
#include "kinematics.h"
#include "pca9685_servo_driver.h"
#include "robot_calibration.h"
enum class GripperCommand {
Closed,
Open,
};
struct ArmTarget {
TcpPose tcp_pose;
float j3_rad;
float j4_rad;
GripperCommand gripper;
};
struct JacobianIkResult {
KinematicsStatus status;
uint16_t iterations;
bool missed_deadline;
JacobianIkStepResult last_step;
ArmJointPwmUs last_pwm;
};
const ArmTarget TARGET1 = {
{0.140f, 0.360f, 0.0f},
0.900f,
0.0f,
GripperCommand::Open,
};
const ArmTarget TARGET2 = {
{0.140f, 0.360f, -0.95f},
0.900f,
-0.7f,
GripperCommand::Open,
};
const ArmTarget TARGET3 = {
{0.160f, 0.350f, -0.95f},
0.900f,
-0.7f,
GripperCommand::Open,
};
const ArmTarget TARGET4 = {
{0.160f, 0.350f, -0.95f},
0.900f,
-0.7f,
GripperCommand::Closed,
};
const ArmTarget TARGET5 = {
{0.140f, 0.360f, -0.95f},
0.900f,
-0.7f,
GripperCommand::Closed,
};
const ArmTarget TARGET6 = {
{0.150f, 0.360f, 0.0f},
0.900f,
0.0f,
GripperCommand::Closed,
};
const ArmTarget TARGET7 = {
{0.140f, 0.360f, 0.95f},
0.900f,
0.7f,
GripperCommand::Closed,
};
const ArmTarget TARGET8 = {
{0.160f, 0.350f, 0.95f},
0.900f,
0.7f,
GripperCommand::Closed,
};
const ArmTarget TARGET9 = {
{0.160f, 0.350f, 0.95f},
0.900f,
0.7f,
GripperCommand::Open,
};
const ArmTarget TARGET10 = {
{0.140f, 0.360f, 0.95f},
0.900f,
0.7f,
GripperCommand::Open,
};
const ArmTarget TARGET11 = {
{0.0f, 0.489f, 0.0f},
0.0f,
0.0f,
GripperCommand::Open,
};
const ArmTarget TARGETS[] = {
TARGET1,
TARGET2,
TARGET3,
TARGET4,
TARGET5,
TARGET6,
TARGET7,
TARGET8,
TARGET9,
TARGET10,
TARGET11,
};
constexpr unsigned long STEP_DELAY_MS = 500;
Pca9685ServoDriver servo_driver;
ArmJointAngles commanded_angles;
bool moveToArmTarget(const char *name, const ArmTarget &target, const JacobianIkConfig &config);
JacobianIkResult moveToTargetJacobianIk(
ArmJointAngles &commanded_angles,
const JacobianIkTarget &target,
const JacobianIkConfig &config);
JacobianIkTarget toJacobianIkTarget(const ArmTarget &target);
void sendZeroCommand();
bool sendKinematicJointCommand(const ArmJointAngles &angles, ArmJointPwmUs &pwm);
void sendGripperCommand(GripperCommand command);
void waitUntilNextPeriod(unsigned long period_start_ms, unsigned long period_ms, bool &missed_deadline);
bool moveExplicitJointTargets(
ArmJointAngles &commanded_angles,
const JacobianIkTarget &target,
const JacobianIkConfig &config,
JacobianIkResult &result);
float limitedAngleStep(float error_rad, float velocity_limit_rad_s, float dt_s);
float normalizedTaskError(const JacobianIkStepResult &step, const JacobianIkConfig &config);
void printTargetDiagnostics(
const ArmTarget &target,
const JacobianIkResult &result,
const ArmJointAngles &angles);
void printArmJointAngles(const ArmJointAngles &angles);
uint16_t gripperCommandToPwmUs(GripperCommand command);
void setup() {
Serial.begin(115200);
if (!servo_driver.begin()) {
Serial.println(F("PCA9685 init failed"));
return;
}
delay(100);
commanded_angles = robotHomeJointAngles();
const JacobianIkConfig ik_config = defaultJacobianIkConfig();
sendZeroCommand();
sendGripperCommand(GripperCommand::Open);
Serial.println(F("ZERO command sent"));
delay(STEP_DELAY_MS);
for (uint8_t i = 0; i < sizeof(TARGETS) / sizeof(TARGETS[0]); ++i) {
if (!moveToArmTarget("TARGET", TARGETS[i], ik_config)) {
Serial.println(F("Target command failed; sequence stopped"));
return;
}
delay(STEP_DELAY_MS);
}
Serial.println(F("Target sequence complete"));
}
void loop() {
}
bool moveToArmTarget(const char *name, const ArmTarget &target, const JacobianIkConfig &config) {
Serial.print(F("Moving to "));
Serial.println(name);
const JacobianIkResult result = moveToTargetJacobianIk(
commanded_angles,
toJacobianIkTarget(target),
config);
printTargetDiagnostics(target, result, commanded_angles);
if (result.status != KinematicsStatus::Ok) {
return false;
}
sendGripperCommand(target.gripper);
Serial.print(name);
Serial.println(F(" motion complete"));
return true;
}
JacobianIkResult moveToTargetJacobianIk(
ArmJointAngles &commanded_angles,
const JacobianIkTarget &target,
const JacobianIkConfig &config) {
JacobianIkResult result = {
KinematicsStatus::OutOfReach,
0,
false,
{},
{0, 0, 0, 0, 0},
};
const unsigned long period_ms = max(1UL, static_cast<unsigned long>(config.dt_s * 1000.0f + 0.5f));
float best_error = 3.402823466e+38f;
uint8_t no_progress_count = 0;
if (!moveExplicitJointTargets(commanded_angles, target, config, result)) {
return result;
}
for (uint16_t iteration = result.iterations; iteration < config.max_iterations; ++iteration) {
const unsigned long period_start_ms = millis();
const JacobianIkStepResult step = computeJacobianIkStep(
commanded_angles,
target,
config);
result.iterations = iteration + 1;
result.last_step = step;
if (step.status != KinematicsStatus::Ok) {
result.status = step.status;
return result;
}
if (step.converged) {
result.status = KinematicsStatus::Ok;
return result;
}
ArmJointPwmUs pwm = {0, 0, 0, 0, 0};
if (!sendKinematicJointCommand(step.next_angles, pwm)) {
result.status = KinematicsStatus::JointLimitViolation;
return result;
}
commanded_angles = step.next_angles;
result.last_pwm = pwm;
const float error = normalizedTaskError(step, config);
if (best_error - error > config.min_error_progress) {
best_error = error;
no_progress_count = 0;
} else if (++no_progress_count >= config.no_progress_iteration_limit) {
result.status = KinematicsStatus::NoProgress;
waitUntilNextPeriod(period_start_ms, period_ms, result.missed_deadline);
return result;
}
waitUntilNextPeriod(period_start_ms, period_ms, result.missed_deadline);
}
result.status = KinematicsStatus::OutOfReach;
return result;
}
JacobianIkTarget toJacobianIkTarget(const ArmTarget &target) {
return {
target.tcp_pose,
target.j3_rad,
target.j4_rad,
};
}
void sendZeroCommand() {
Serial.println(F("Sending ZERO command"));
for (uint8_t i = 0; i < ROBOT_TOTAL_JOINT_COUNT; ++i) {
const JointCalibration &joint = JOINT_CALIBRATIONS[i];
servo_driver.setServoUs(joint.channel, joint.pwm_zero_us);
}
commanded_angles = robotHomeJointAngles();
}
bool sendKinematicJointCommand(const ArmJointAngles &angles, ArmJointPwmUs &pwm) {
const KinematicsResult<ArmJointPwmUs> pwm_result = jointAnglesToPwmUs(angles, JOINT_CALIBRATIONS);
if (pwm_result.status != KinematicsStatus::Ok) {
Serial.print(F("Target PWM failed status="));
Serial.println(kinematicsStatusName(pwm_result.status));
return false;
}
pwm = pwm_result.value;
const uint16_t pwm_values[ROBOT_KINEMATIC_JOINT_COUNT] = {
pwm.j0_us,
pwm.j1_us,
pwm.j2_us,
pwm.j3_us,
pwm.j4_us,
};
for (uint8_t i = 0; i < ROBOT_KINEMATIC_JOINT_COUNT; ++i) {
servo_driver.setServoUs(
JOINT_CALIBRATIONS[i].channel,
pwm_values[i]);
}
return true;
}
void sendGripperCommand(GripperCommand command) {
const JointCalibration &gripper = JOINT_CALIBRATIONS[ROBOT_GRIPPER_JOINT_INDEX];
servo_driver.setServoUs(gripper.channel, gripperCommandToPwmUs(command));
}
void waitUntilNextPeriod(unsigned long period_start_ms, unsigned long period_ms, bool &missed_deadline) {
const unsigned long elapsed_ms = millis() - period_start_ms;
if (elapsed_ms < period_ms) {
delay(period_ms - elapsed_ms);
} else {
missed_deadline = true;
}
}
bool moveExplicitJointTargets(
ArmJointAngles &commanded_angles,
const JacobianIkTarget &target,
const JacobianIkConfig &config,
JacobianIkResult &result) {
const unsigned long period_ms = max(1UL, static_cast<unsigned long>(config.dt_s * 1000.0f + 0.5f));
while (result.iterations < config.max_iterations) {
const float yaw_error_rad = target.tcp_pose.yaw_rad - commanded_angles.j0_rad;
const float j3_error_rad = target.j3_rad - commanded_angles.j3_rad;
const float j4_error_rad = target.j4_rad - commanded_angles.j4_rad;
if (fabs(yaw_error_rad) <= config.angle_tolerance_rad &&
fabs(j3_error_rad) <= config.angle_tolerance_rad &&
fabs(j4_error_rad) <= config.angle_tolerance_rad) {
return true;
}
const unsigned long period_start_ms = millis();
ArmJointAngles next_angles = commanded_angles;
next_angles.j0_rad += limitedAngleStep(
yaw_error_rad,
config.joint_velocity_limit_rad_s[0],
config.dt_s);
next_angles.j3_rad += limitedAngleStep(
j3_error_rad,
config.joint_velocity_limit_rad_s[3],
config.dt_s);
next_angles.j4_rad += limitedAngleStep(
j4_error_rad,
config.joint_velocity_limit_rad_s[4],
config.dt_s);
ArmJointPwmUs pwm = {0, 0, 0, 0, 0};
if (!sendKinematicJointCommand(next_angles, pwm)) {
result.status = KinematicsStatus::JointLimitViolation;
return false;
}
commanded_angles = next_angles;
result.last_pwm = pwm;
result.iterations++;
waitUntilNextPeriod(period_start_ms, period_ms, result.missed_deadline);
}
result.status = KinematicsStatus::OutOfReach;
return false;
}
float limitedAngleStep(float error_rad, float velocity_limit_rad_s, float dt_s) {
const float max_step_rad = velocity_limit_rad_s * dt_s;
if (error_rad > max_step_rad) {
return max_step_rad;
}
if (error_rad < -max_step_rad) {
return -max_step_rad;
}
return error_rad;
}
float normalizedTaskError(const JacobianIkStepResult &step, const JacobianIkConfig &config) {
float result = fabs(step.reach_error_m) / config.position_tolerance_m;
const float z_error = fabs(step.z_error_m) / config.position_tolerance_m;
if (z_error > result) {
result = z_error;
}
const float yaw_error = fabs(step.yaw_error_rad) / config.angle_tolerance_rad;
if (yaw_error > result) {
result = yaw_error;
}
const float j3_error = fabs(step.j3_error_rad) / config.angle_tolerance_rad;
if (j3_error > result) {
result = j3_error;
}
const float j4_error = fabs(step.j4_error_rad) / config.angle_tolerance_rad;
if (j4_error > result) {
result = j4_error;
}
return result;
}
void printTargetDiagnostics(
const ArmTarget &target,
const JacobianIkResult &result,
const ArmJointAngles &angles) {
Serial.print(F("Target TCP reach_x_m="));
Serial.print(target.tcp_pose.reach_x_m, 6);
Serial.print(F(" reach_z_m="));
Serial.print(target.tcp_pose.reach_z_m, 6);
Serial.print(F(" yaw_rad="));
Serial.print(target.tcp_pose.yaw_rad, 6);
Serial.print(F(" requested_j3_rad="));
Serial.print(target.j3_rad, 6);
Serial.print(F(" requested_j4_rad="));
Serial.println(target.j4_rad, 6);
Serial.print(F("Jacobian IK status="));
Serial.print(kinematicsStatusName(result.status));
Serial.print(F(" iterations="));
Serial.print(result.iterations);
Serial.print(F(" missed_deadline="));
Serial.print(result.missed_deadline ? F("yes") : F("no"));
Serial.print(F(" alpha="));
Serial.println(result.last_step.alpha, 6);
Serial.print(F("Final errors reach_m="));
Serial.print(result.last_step.reach_error_m, 6);
Serial.print(F(" z_m="));
Serial.print(result.last_step.z_error_m, 6);
Serial.print(F(" yaw_rad="));
Serial.print(result.last_step.yaw_error_rad, 6);
Serial.print(F(" j3_rad="));
Serial.print(result.last_step.j3_error_rad, 6);
Serial.print(F(" j4_rad="));
Serial.println(result.last_step.j4_error_rad, 6);
printArmJointAngles(angles);
const KinematicsResult<TcpPose> fk_result = forwardKinematics(angles);
if (fk_result.status == KinematicsStatus::Ok) {
Serial.print(F("Model FK reach_x_m="));
Serial.print(fk_result.value.reach_x_m, 6);
Serial.print(F(" reach_z_m="));
Serial.print(fk_result.value.reach_z_m, 6);
Serial.print(F(" yaw_rad="));
Serial.println(fk_result.value.yaw_rad, 6);
} else {
Serial.print(F("Model FK failed status="));
Serial.println(kinematicsStatusName(fk_result.status));
}
Serial.print(F("Last commanded PWM j0_us="));
Serial.print(result.last_pwm.j0_us);
Serial.print(F(" j1_us="));
Serial.print(result.last_pwm.j1_us);
Serial.print(F(" j2_us="));
Serial.print(result.last_pwm.j2_us);
Serial.print(F(" j3_us="));
Serial.print(result.last_pwm.j3_us);
Serial.print(F(" j4_us="));
Serial.println(result.last_pwm.j4_us);
}
void printArmJointAngles(const ArmJointAngles &angles) {
Serial.print(F("Commanded angles j0_rad="));
Serial.print(angles.j0_rad, 6);
Serial.print(F(" j1_rad="));
Serial.print(angles.j1_rad, 6);
Serial.print(F(" j2_rad="));
Serial.print(angles.j2_rad, 6);
Serial.print(F(" j3_rad="));
Serial.print(angles.j3_rad, 6);
Serial.print(F(" j4_rad="));
Serial.println(angles.j4_rad, 6);
}
uint16_t gripperCommandToPwmUs(GripperCommand command) {
const JointCalibration &gripper = JOINT_CALIBRATIONS[ROBOT_GRIPPER_JOINT_INDEX];
return command == GripperCommand::Open ? gripper.pwm_min_us : gripper.pwm_max_us;
}