-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArm.cs
More file actions
322 lines (279 loc) · 12.6 KB
/
Arm.cs
File metadata and controls
322 lines (279 loc) · 12.6 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
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
namespace UseArm
{
//Base 6.8 in -4pi to 4pi (yaw) Stow: 0
//First Joint 28.0 in -76 degrees to 100 degrees (Pitch) Stow: -76
//2nd Joint 28.0 in 11.59 degrees to 170 degrees (Pitch) Stow: 11.59
//Hand 12.75 in -90 degrees to 90 degrees (Pitch) Stow: 0
// -2pi to 2pi (Roll) Stow: 0
/*
* Basic arm construction:
* Arm a = new Arm((0, 0, -1.5f, 1.5f, 0, 0, 3), (0.78f, 3), (0, 0, -0.5f, 0.5f, 0, 0, 3), (0, 3));
* Creates an arm with 4 parts.
* First part: Cannot roll and cannot yaw. Goes between about -Pi / 2 and Pi / 2 in pitch. Length 3.
* Second part: Cannot roll and cannot yaw. Fixed at about Pi / 4 to the horizontal. Length 3.
* Third part: Cannot roll and cannot yaw. Goes between about -Pi / 6 and Pi / 6 in pitch. Length 3.
* Fourth part: Cannot roll and cannot yaw. Fixed parallel to the horizontal. Length 3.
*
* To move the arm somewhere. call MoveTo(X, Y, Z).
* To see what arm segment i should be turned to, check CurrentRolls[i], CurrentPitches[i],
* and CurrentYaws[i].
*/
public struct ArmPart
{
/*
* I'm breaking conventions here a bit:
* The front of the rover is along the X Axis
* The Y Axis is vertical, parallel to gravity
* The Z Axis is the cross product of the X and Y Axis
*
* Here is how I'm defining the angles to be:
* Roll - Rotation about the X Axis
* Pitch - Rotation about the Z Axis
* Yaw - Rotation about the Y Axis
* This is contrary to norm. Usually Yaw is about the Z Axis
* and the Z Axis is parallel to gravity. Not here!
*
* Construction:
* - Either provide MinRoll, MaxRoll, MinPitch, MaxPitch,
* MinYaw, MaxYaw, and Length
* - Or provide Fixed Pitch angle and Length, in which case
* the arm will ensure that that ArmPart will always be at
* that angle with respect to the ground.
*/
public float MinRoll;
public float MaxRoll;
public float MinPitch;
public float MaxPitch;
public float MinYaw;
public float MaxYaw;
public float Length;
public bool Fixed;
public float FixedAngle;
public ArmPart(float MinRoll, float MaxRoll,
float MinPitch, float MaxPitch,
float MinYaw, float MaxYaw,
float L)
{
this.MinRoll = MinRoll;
this.MaxRoll = MaxRoll;
this.MinPitch = MinPitch;
this.MaxPitch = MaxPitch;
this.MinYaw = MinYaw;
this.MaxYaw = MaxYaw;
Length = L;
Fixed = false;
FixedAngle = 0.0f;
}
// FixedAngle makes Yaw constant
public ArmPart(float FixedAngle, float Length, float MinPitch = 0.0f, float MaxPitch = 2.0f * (float)Math.PI)
{
this.MinRoll = 0.0f;
this.MaxRoll = 0.0f;
this.MinPitch = MinPitch;
this.MaxPitch = MaxPitch;
this.MinYaw = 0.0f;
this.MaxYaw = 0.0f;
this.FixedAngle = FixedAngle;
this.Fixed = true;
this.Length = Length;
}
public static implicit operator ArmPart((int, int) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2);
}
public static implicit operator ArmPart((float, float) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2);
}
public static implicit operator ArmPart((double, double) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2);
}
public static implicit operator ArmPart((int, int, int, int) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2, (float)t.Item3, (float)t.Item4);
}
public static implicit operator ArmPart((float, float, float, float) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2, (float)t.Item3, (float)t.Item4);
}
public static implicit operator ArmPart((double, double, double, double) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2, (float)t.Item3, (float)t.Item4);
}
public static implicit operator ArmPart((int, int, int, int, int, int, int) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2, (float)t.Item3, (float)t.Item4, (float)t.Item5,
(float)t.Item6, (float)t.Item7);
}
public static implicit operator ArmPart((double, double, double, double, double, double, double) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2, (float)t.Item3, (float)t.Item4, (float)t.Item5,
(float)t.Item6, (float)t.Item7);
}
public static implicit operator ArmPart((float, float, float, float, float, float, float) t)
{
return new ArmPart((float)t.Item1, (float)t.Item2, (float)t.Item3, (float)t.Item4, (float)t.Item5,
(float)t.Item6, (float)t.Item7);
}
}
public class Arm
{
public float[] CurrentRolls { get; private set; }
public float[] CurrentPitches { get; private set; }
public float[] CurrentYaws { get; private set; }
private ArmPart[] Params;
private float[] TempRolls;
private float[] TempPitches;
private float[] TempYaws;
private float[] RollSums;
private float[] PitchSums;
private float[] YawSums;
//Takes an array of ArmParts (which can be conveniently constructed
//using tuples) and initializes the Arm. Following this constructor,
//CurrentPitches, CurrentRolls, and CurrentYaws will be initialized
//to random values close to 0. Randomization is needed because of
//everything is 0, gradients will be 0 and nothing will be updated.
//This is a common technique in machine learning, particularly
//neural networks.
public Arm(params ArmPart[] Parameters)
{
this.Params = Parameters;
this.CurrentRolls = new float[Parameters.Length];
this.CurrentPitches = new float[Parameters.Length];
this.CurrentYaws = new float[Parameters.Length];
Random r = new Random();
for (int i = 0; i < Params.Length; i++)
{
CurrentRolls[i] = (float)r.NextDouble() * 0.00001f;
CurrentPitches[i] = (float)r.NextDouble() * 0.00001f;
CurrentYaws[i] = (float)r.NextDouble() * 0.00001f;
}
this.RollSums = new float[Parameters.Length];
this.PitchSums = new float[Parameters.Length];
this.YawSums = new float[Parameters.Length];
this.TempRolls = new float[Parameters.Length];
this.TempPitches = new float[Parameters.Length];
this.TempYaws = new float[Parameters.Length];
}
//Calculates the current position of the endpoint of the arm. Fills in
//the angle sum arrays which will be used for calculating gradient.
public (float X, float Y, float Z) CalculatePosition()
{
float X = 0.0f;
float Y = 0.0f;
float Z = 0.0f;
float PitchSum = 0.0f;
float RollSum = 0.0f;
float YawSum = 0.0f;
for (int i = 0; i < CurrentPitches.Length; i++)
{
RollSum += CurrentRolls[i];
PitchSum += CurrentPitches[i];
YawSum += CurrentYaws[i];
RollSums[i] = RollSum;
PitchSums[i] = PitchSum;
YawSums[i] = YawSum;
X += (float)(Params[i].Length * Math.Cos(PitchSum) * Math.Cos(YawSum));
if (Math.Abs(RollSum) > 1e-8)
{
Y += (float)(Params[i].Length * Math.Sin(RollSum) * Math.Sin(PitchSum));
Z += (float)(Params[i].Length * Math.Cos(RollSum) * Math.Sin(YawSum));
}
else
{
Y += (float)(Params[i].Length * Math.Sin(PitchSum));
Z += (float)(Params[i].Length * Math.Sin(YawSum));
}
}
return (X, Y, Z);
}
//Takes the target position, the current position, and the arm part
//whose gradient should be calculated.
private (float, float, float) CalcGrad(float TX, float TY, float TZ, float X, float Y, float Z, int i)
{
float RollY = 0.0f;
float RollZ = 0.0f;
float PitchX = 0.0f;
float PitchY = 0.0f;
float YawX = 0.0f;
float YawZ = 0.0f;
for (int a = i; a < Params.Length; a++)
{
if (Math.Abs(RollSums[a]) > 1e-8)
{
RollZ += (float)(Params[a].Length * Math.Sin(RollSums[a]) * Math.Sin(YawSums[a]));
RollY += (float)(Params[a].Length * Math.Sin(RollSums[a]) * Math.Sin(PitchSums[a]));
YawZ += (float)(Params[a].Length * Math.Cos(YawSums[a]) * Math.Sin(RollSums[a]));
}
else
{
RollZ += (float)(Params[a].Length * Math.Sin(YawSums[a]));
RollY += (float)(Params[a].Length * Math.Sin(PitchSums[a]));
YawZ += (float)(Params[a].Length * Math.Cos(YawSums[a]));
}
PitchX += (float)(Params[a].Length * Math.Sin(PitchSums[a]) * Math.Cos(YawSums[a]));
PitchY += (float)(Params[a].Length * Math.Cos(PitchSums[a]) * Math.Cos(RollSums[a]));
YawX += (float)(Params[a].Length * Math.Sin(YawSums[a]) * Math.Cos(PitchSums[a]));
}
return (2 * RollZ * (TZ - Z) - 2 * RollY * (TY - Y),
2 * PitchX * (TX - X) - 2 * PitchY * (TY - Y),
2 * YawX * (TX - X) - 2 * YawZ * (TZ - Z));
}
//Takes an angle in radians and ensures it is between
//0 and 2 * PI
private static float EnsureStandardForm(float Angle)
{
return Angle % (float)(2 * Math.PI);
}
//Moves the arm to the target X, Y, and Z. The angles of each
//arm part will be stored in CurrentRoll, CurrentPitch, and
//CurrentYaw.
public void MoveTo(float X, float Y, float Z)
{
const float LearningRate = 0.00005f;
const int Iterations = 40;
for (int i = 0; i < Iterations; i++)
{
var (CurX, CurY, CurZ) = CalculatePosition();
Console.WriteLine("Z: " + CurZ);
for (int j = 0; j < Params.Length; j++)
{
if (Params[j].Fixed)
{
float PrevSum = j > 0 ? PitchSums[j - 1] : 0;
CurrentPitches[j] = EnsureStandardForm((Params[j].FixedAngle - PrevSum));
CurrentPitches[j] = Math.Max(Math.Min(CurrentPitches[j], Params[j].MaxPitch), Params[j].MinPitch);
continue;
}
//float Cost = (CurX - X) * (CurX - X) + (CurY - Y) * (CurY - Y) + (CurZ - Z) * (CurZ - Z);
var (GradRoll, GradPitch, GradYaw) = CalcGrad(X, Y, Z, CurX, CurY, CurZ, j);
if (j == 0)
Console.WriteLine("GradYaw:" + GradYaw);
GradRoll *= LearningRate;
GradPitch *= LearningRate;
GradYaw *= LearningRate;
TempRolls[j] = CurrentRolls[j] - GradRoll;
TempPitches[j] = CurrentPitches[j] - GradPitch;
TempYaws[j] = CurrentYaws[j] - GradYaw;
float[] tmp = CurrentRolls;
CurrentRolls = TempRolls;
TempRolls = tmp;
tmp = CurrentPitches;
CurrentPitches = TempPitches;
TempPitches = tmp;
tmp = CurrentYaws;
CurrentYaws = TempYaws;
TempYaws = tmp;
CurrentRolls[j] = Math.Max(Math.Min(CurrentRolls[j], Params[j].MaxRoll), Params[j].MinRoll);
CurrentPitches[j] = Math.Max(Math.Min(CurrentPitches[j], Params[j].MaxPitch), Params[j].MinPitch);
CurrentYaws[j] = Math.Max(Math.Min(CurrentYaws[j], Params[j].MaxYaw), Params[j].MinYaw);
}
}
}
}
}