-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharm_controller.ino
More file actions
368 lines (280 loc) · 9.13 KB
/
Copy patharm_controller.ino
File metadata and controls
368 lines (280 loc) · 9.13 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
#include <Servo.h>
#include <Wire.h>
#include <arc.h>
class Vec2d {
public:
Vec2d();
Vec2d(float ix, float iy);
float dot(const Vec2d& other);
bool isCWRotation(const Vec2d& v); //Returns true if you need to rotate CW from v to get to this vector
float magnitude();
friend Vec2d operator+(const Vec2d& v1, const Vec2d& v2);
friend Vec2d operator-(const Vec2d& v1, const Vec2d& v2);
friend Vec2d operator*(const float& scalar, const Vec2d& v);
friend Vec2d operator*(const Vec2d& v, const float& scalar);
float x;
float y;
};
class Arm {
public:
Arm();
void updateJointAngles();
Vec2d m_joints[4];
int m_numJoints;
Vec2d m_angleVectors[3]; //Used to get rotations needed. These vectors point in the 0 angle of each bone
int m_numAngleVectors;
float m_boneLengths[3]; //Base at start
int m_numBones;
Vec2d m_rootLocation;
float m_jointAngles[3];
int m_numJointAngles;
float m_drawThickness;
};
class IKSolver {
public:
IKSolver();
void solve(Arm& arm, Vec2d target);
private:
float m_tolerance;
int m_maxLoops;
};
Vec2d::Vec2d() {
x = 0;
y = 0;
}
Vec2d::Vec2d(float ix, float iy) {
x = ix;
y = iy;
}
float Vec2d::dot(const Vec2d& other) {
return x * other.x + y * other.y;
}
bool Vec2d::isCWRotation(const Vec2d& v) {
float z_comp_cross = v.x * y - x * v.y;
return z_comp_cross < 0;
}
float Vec2d::magnitude() {
return pow(pow(x, 2) + pow(y, 2), 0.5);
}
Vec2d operator+(const Vec2d& v1, const Vec2d& v2) {
return Vec2d(v1.x + v2.x, v1.y + v2.y);
}
Vec2d operator-(const Vec2d& v1, const Vec2d& v2) {
return Vec2d(v1.x - v2.x, v1.y - v2.y);
}
Vec2d operator*(const float& scalar, const Vec2d& v) {
return Vec2d(v.x * scalar, v.y * scalar);
}
Vec2d operator*(const Vec2d& v, const float& scalar) {
return scalar * v;
}
float getSeparation(Vec2d v1, Vec2d v2) {
return pow(pow(v1.x - v2.x, 2) + pow(v1.y - v2.y, 2), 0.5);
}
Arm::Arm() {
m_rootLocation = Vec2d(0, 0);
m_boneLengths[0] = 80;
m_boneLengths[1] = 80;
m_boneLengths[2] = 80;
m_numBones = 3;
m_joints[0] = m_rootLocation; //Root joint
m_joints[1] = Vec2d(0, m_boneLengths[0]) + m_rootLocation;
m_joints[2] = Vec2d(0, m_boneLengths[1]) + m_joints[1];
m_joints[3] = Vec2d(0, m_boneLengths[2]) + m_joints[2];
m_numJoints = 4;
m_angleVectors[0] = Vec2d(0.5736, 0.8192);
m_angleVectors[1] = Vec2d(0.5735, 0.8196);
m_angleVectors[2] = Vec2d(0.5735, 0.8196);
m_numAngleVectors = 3;
m_drawThickness = 5;
m_numJointAngles = 3;
}
void Arm::updateJointAngles() {
for (int i = 0; i < m_numJoints-1; i++) {
Vec2d compareVector(0, 1);
if (i > 0) {
compareVector = m_joints[i] - m_joints[i - 1];
}
Vec2d jointVector = m_joints[i + 1] - m_joints[i];
float angle = acosf(compareVector.dot(jointVector) / (compareVector.magnitude() * jointVector.magnitude()));
if (!compareVector.isCWRotation(jointVector)) {
angle = -angle;
}
m_jointAngles[i] = angle;
}
m_jointAngles[0] *= -1.f;
//m_jointAngles[1] *= -1.f;
m_jointAngles[2] *= -1.f;
//Convert to degrees and center on 90
for (int i = 0; i < m_numJointAngles; ++i) {
m_jointAngles[i] *= 57.3;
m_jointAngles[i] += 90;
}
//Buffer
for (int i = 0; i < m_numJointAngles; ++i) {
if (m_jointAngles[i] > 175) {
m_jointAngles[i] = 175;
}
if (m_jointAngles[i] < 5) {
m_jointAngles[i] = 5;
}
}
}
IKSolver::IKSolver() {
m_tolerance = 0.01;
m_maxLoops = 10;
}
void IKSolver::solve(Arm& arm, Vec2d target) {
float disToTarget = getSeparation(arm.m_joints[0], target);
float totalArmLength = 0;
for (int i = 0; i < arm.m_numBones; ++i) {
totalArmLength += arm.m_boneLengths[i];
}
if (disToTarget > totalArmLength) {
//std::cout << "IKSolver::solve Distance to target is greater that arm reach" << std::endl;
}
else {
Vec2d initialRootPos = arm.m_joints[0];
float armEndToTargetDis = getSeparation(arm.m_joints[arm.m_numJoints - 1], target);
Vec2d newJoints[4];
for (int i = 0; i < 4; ++i) {
newJoints[i] = arm.m_joints[i];
}
int loopCounter = 0;
while (armEndToTargetDis > m_tolerance) {
newJoints[3] = target;
for (int i = 2; i >= 0; i--) {
float disToNextJoint = getSeparation(newJoints[i + 1], newJoints[i]);
float lambda = arm.m_boneLengths[i] / disToNextJoint;
Vec2d newJointPos = (1 - lambda) * newJoints[i + 1] + lambda * newJoints[i];
//2 doesn't have to undergo joint restraints
if (i <= 1) {
//Check angle
Vec2d setJoint = newJoints[i + 2] - newJoints[i + 1];
Vec2d prospectiveJoint = newJointPos - newJoints[i + 1];
float angle = setJoint.dot(prospectiveJoint) / (setJoint.magnitude() * prospectiveJoint.magnitude()); //can be optimized
if (angle < 0) {
//good
}
else {
//bad needs moving
Vec2d perpVector(-setJoint.y, setJoint.x);
if (perpVector.dot(prospectiveJoint) < 0) {
perpVector = perpVector * -1.f;
}
perpVector = perpVector * (1.f / perpVector.magnitude());
newJointPos = newJoints[i + 1] + arm.m_boneLengths[i] * perpVector;
}
}
newJoints[i] = newJointPos;
}
newJoints[0] = initialRootPos;
for (int i = 0; i < 3; ++i) {
float disToNextJoint = getSeparation(newJoints[i], newJoints[i + 1]);
float lambda = arm.m_boneLengths[i] / disToNextJoint;
Vec2d newJointPos = (1 - lambda) * newJoints[i] + lambda * newJoints[i + 1];
if (i >= 1) {
Vec2d setJoint = newJoints[i - 1] - newJoints[i];
Vec2d prospectiveJoint = newJointPos - newJoints[i];
float angle = setJoint.dot(prospectiveJoint) / (setJoint.magnitude() * prospectiveJoint.magnitude());
if (angle < 0) {
}
else {
Vec2d perpVector(-setJoint.y, setJoint.x);
if (perpVector.dot(prospectiveJoint) < 0) {
perpVector = perpVector * -1.f;
}
perpVector = perpVector * (1.f / perpVector.magnitude());
newJointPos = newJoints[i] + arm.m_boneLengths[i] * perpVector;
}
}
newJoints[i + 1] = newJointPos;
}
for (int i = 0; i < 4; ++i) {
arm.m_joints[i] = newJoints[i];
}
armEndToTargetDis = getSeparation(arm.m_joints[arm.m_numJoints - 1], target);
loopCounter++;
if (loopCounter > m_maxLoops) {
break;
//std::cout << "unable to solve" << std::endl;
}
}
}
}
Arm arm;
IKSolver ikSolver;
Servo BaseMotor;
Servo ShoulderMotor;
Servo ElbowMotor;
Servo WristMotor;
const int BaseEquilAngle = 90;
const int ShoulderEquilAngle = 90;
const int ElbowEquilAngle = 90;
const int WristEquilAngle = 90;
const int BaseOffsetAngle = -5;
const int ShoulderOffsetAngle = 5;
const int ElbowOffsetAngle = 5;
const int WristOffsetAngle = 0;
const int sg90PulseLower = 700;
const int sg90PulseHigher = 2300;
const int s3003PulseLower = 400;
const int s3003PulseHigher = 3100;
float targetPos3D[3];
float gain;
void servoWrite(Servo& servo, int angle) {
servo.write(clip(angle, 0, 180));
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
BaseMotor.attach(2, sg90PulseLower, sg90PulseHigher);
servoWrite(BaseMotor, BaseEquilAngle + BaseOffsetAngle);
ShoulderMotor.attach(3, s3003PulseLower, s3003PulseHigher);
servoWrite(ShoulderMotor, ShoulderEquilAngle + ShoulderOffsetAngle);
ElbowMotor.attach(4, sg90PulseLower, sg90PulseHigher);
servoWrite(ElbowMotor, ElbowEquilAngle + ElbowOffsetAngle);
WristMotor.attach(5, sg90PulseLower, sg90PulseHigher);
servoWrite(WristMotor, WristEquilAngle + WristOffsetAngle);
targetPos3D[0] = 50; //Along one of star dir, along axis of base motor
targetPos3D[1] = 100; //Along other star axis
targetPos3D[2] = 50; // Vertical
gain = 0.4;
}
void loop() {
float sineCounter = 0;
float sineAmplitude = 50;
while(true) {
targetPos3D[0] = 0;
targetPos3D[1] = 130 + sineAmplitude * sin(sineCounter);
targetPos3D[2] = 25;
float theta = atanf(targetPos3D[0] / targetPos3D[1]);
theta = theta * 57.3;
theta = theta + 90;
Vec2d targetPos(pow(pow(targetPos3D[0], 2) + pow(targetPos3D[1], 2), 0.5), targetPos3D[2]);
Serial.println(theta);
sineCounter += 0.01;
ikSolver.solve(arm, targetPos);
arm.updateJointAngles();
//BaseMotor.write(theta);
//ShoulderMotor.write(arm.m_jointAngles[0]);
servoWrite(ShoulderMotor, arm.m_jointAngles[0] + ShoulderOffsetAngle);
//servoWrite(ElbowMotor, arm.m_jointAngles[1] + ElbowOffsetAngle);
//servoWrite(WristMotor, arm.m_jointAngles[2] + WristOffsetAngle);
//ElbowMotor.write(arm.m_jointAngles[1]);
//WristMotor.write(arm.m_jointAngles[2]);
//testing
//servoWrite(ElbowMotor, 90);
//servoWrite(WristMotor, 90);
//Serial.println(arm.m_jointAngles[0]);
//Serial.println(arm.m_jointAngles[1]);
//Serial.println(arm.m_jointAngles[2]);
}
}