forked from NeuronRobotics/java-bowler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateArm.java
More file actions
554 lines (489 loc) · 11.7 KB
/
CreateArm.java
File metadata and controls
554 lines (489 loc) · 11.7 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*******************************************************************************
* Copyright 2010 Neuron Robotics, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.neuronrobotics.sdk.addons.irobot;
import com.neuronrobotics.sdk.common.Log;
import com.neuronrobotics.sdk.dyio.peripherals.DyIOPeripheralException;
import com.neuronrobotics.sdk.dyio.peripherals.ServoChannel;
// Auto-generated Javadoc
/**
* The Class CreateArm.
*/
public class CreateArm {
/** The links. */
ServoChannel [] links;
/** The Constant l1. */
private static final double l1 = 6.0;
/** The Constant l2. */
private static final double l2 = 3.93;
/** The Constant l3. */
private static final double l3 = 4.75;
/** The Constant M_PI. */
//private static final double l3 = .0001;
private static final double M_PI = Math.PI;
/** The scale. */
private double scale[]={1.55,1.50,-1.76};
/** The positions. */
private double [] positions=new double[4];
/** The angles. */
private double [] angles=new double[3];
/** The pose. */
private double [] pose=new double[3];
/** The centers. */
private double [] centers={134,136,128,48};
/** The blocking. */
private boolean blocking = false;
/** The xy thresh hold. */
private double xyThreshHold = .1;
/** The orient thresh hold. */
private double orientThreshHold = 1;
/**
* Instantiates a new creates the arm.
*
* @param links this is an array of servo channel links.
* 0th element is shoulder
* 1th element is elbow
* 2th element is is wrist
* 3th element is the gripper
*/
public CreateArm(ServoChannel [] links){
check(links);
home();
pose=getCartesianPose();
}
/**
* Home.
*/
public void home(){
setAngles(90,-90,0);
setCartesianPose(getCartesianPose());
gripOpen();
}
/**
* Rest.
*/
public void rest(){
setAngles(110,-110,0);
setCartesianPose(getCartesianPose());
gripOpen();
}
/**
* Grip close.
*/
public void gripClose(){
links[3].SetPosition((int) centers[3]+75,(float).5);
if (isBlocking()){
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// ignore
}
}
}
/**
* Grip open.
*/
public void gripOpen(){
links[3].SetPosition((int) centers[3],(float) .5);
if (isBlocking()){
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// ignore
}
}
}
/**
* Check.
*
* @param links the links
*/
private void check(ServoChannel [] links){
if(links.length != 4){
throw new DyIOPeripheralException("This perpheral needs 4 links.");
}
for(int i=0;i<4;i++){
if(links[i]==null){
throw new DyIOPeripheralException("All links must already be instantiated.");
}
links[i].SetPosition((int) getCenters()[i],2);
positions[i]=(double)getCenters()[i];
}
this.links = links;
}
/**
* Sets the shouler.
*
* @param angle set just the shoulder angle
*/
public void setShouler(double angle){
setAngles(angle,angles[1],angles[2]);
}
/**
* Sets the elbow.
*
* @param angle set just the elbow angle
*/
public void setElbow(double angle){
setAngles(angles[0],angle,angles[2]);
}
/**
* Sets the wrist.
*
* @param angle set just the wrist angle
*/
public void setWrist(double angle){
setAngles(angles[0],angles[1],angle);
}
/**
* This takes angles in degrees and converts them to servo positions; Note:
* All zeros would be the arm completely horizontal, all links pointing out.
*
* @param shoulder the shoulder
* @param elbow the elbow
* @param wrist the wrist
*/
public void setAngles(double shoulder, double elbow, double wrist){
setAngles(shoulder, elbow, wrist,(float) 1.0);
}
/**
* This takes angles in degrees and converts them to servo positions; Note:
* All zeros would be the arm completely horizontal, all links pointing out.
*
* @param shoulder the shoulder
* @param elbow the elbow
* @param wrist the wrist
* @param time the time it should take for the transition to take
*/
public void setAngles(double shoulder, double elbow, double wrist,float time) {
angles[0]=shoulder;
angles[1]=elbow;
angles[2]=wrist;
double s,e,w;
s =shoulder-90;
s *=getScale()[0];
positions[0]=getCenters()[0]+s;
e =(elbow+90);
e *=getScale()[1];
positions[1]=getCenters()[1]+e;
double interference = centers[0]-45;
double limit = 255 - (1.1*(positions[0]-interference));
if(positions[0]>interference){
Log.info("In interference zone: "+interference+" with limit: "+limit );
if (positions[1]>limit){
System.err.print("\nAttempting to set angle that interferes, fixing. Was: "+positions[0]+","+positions[1]);
positions[1]=limit;
System.err.print(" Is: "+positions[0]+" , "+positions[1]+"\n");
}
Log.info("\n");
}
if(positions[1]<(centers[1]-83)){
positions[1]=(centers[1]-83);
}
double [] an= getAngles();
w =(int) ((pose[2]-an[0]-an[1])*getScale()[2]);
positions[2]=getCenters()[2]+w;
for (int i=0;i<3;i++){
if (positions[i]>255){
positions[i]=255;
}
if (positions[i]<0){
positions[i]=0;
}
links[i].SetPosition((int) positions[i],time);
}
Log.info("Set positions Shoulder: "+positions[0]+" Elbow: "+positions[1]+" Wrist: "+positions[2]);
if (isBlocking()){
try {
Thread.sleep((long) (time*1000));
} catch (InterruptedException e1) {
// ignore
}
}
}
/**
* Gets the angles.
*
* @return an array of angles in degrees. This should correspond to pose of the arm.
*/
public double [] getAngles(){
double [] a=new double [3];
for (int i=0;i<3;i++){
a[i]=(positions[i]-getCenters()[i])/getScale()[i];
}
a[0]+=90;
a[1]-=90;
return a;
}
/**
* Gets the cartesian pose.
*
* @return pose vector, X,Y,Orientation
*/
public double [] getCartesianPose(){
double [] angles =getAngles();
pose[2] = GetOrientation();
pose[0]=(l1* cos(ToRadians(angles[0]))+l2* cos(ToRadians(angles[0])+ToRadians(angles[1]))+(l3* cos(ToRadians(GetOrientation()))));
pose[1]=(l1* sin(ToRadians(angles[0]))+l2* sin(ToRadians(angles[0])+ToRadians(angles[1]))+(l3* sin(ToRadians(GetOrientation()))));
double [] p = new double [3];
for ( int i = 0; i<3; i++){
p[i]=pose[i];
}
return p;
}
/**
* Gets the cartesian pose string.
*
* @return the cartesian pose string
*/
public String getCartesianPoseString(){
getCartesianPose();
String s="[";
for(int i=0;i <pose.length;i++){
s+=pose[i];
if(i<pose.length-1)
s+=",";
}
return s+"]";
}
/**
* Sets the cartesian pose.
*
* @param p the new cartesian pose
*/
public void setCartesianPose(double [] p){
setCartesianPose(p,(float) 1.0);
}
/**
* Sets the cartesian pose.
*
* @param p the p
* @param time the time
*/
public void setCartesianPose(double [] p, float time){
setCartesianPose(p[0],p[1], p[2],time);
}
/**
* Sets the cartesian pose.
*
* @param x the x
* @param y the y
* @param orientation the orientation
*/
public void setCartesianPose(double x, double y, double orientation){
setCartesianPose(x,y, orientation,(float).2);
}
/**
* Sets the cartesian pose.
*
* @param x the x
* @param y the y
* @param orientation the orientation
* @param time the time
*/
public void setCartesianPose(double x, double y, double orientation, float time){
if(orientation<-35)
orientation=-35;
if(orientation>35)
orientation=35;
if (!updateCartesian(x,y,orientation)){
return;
}
pose[0]=x;
pose[1]=y;
pose[2]=orientation;
Log.info("Setting Pose X: "+x+" Y: "+y+" Orientation: "+orientation );
x -= (l3*cos(orientation*M_PI/180));
y -= (l3*sin(orientation*M_PI/180));
if (sqrt(x*x+y*y) > l1+l2) {
com.neuronrobotics.sdk.common.Log.error("Hypotenus too long"+x+" "+y+"\r\n");
return;
}
double elbow = 0;
elbow =(-1*acos(((x*x+y*y)-(l1*l1+l2*l2))/(2*l1*l2)));
elbow *=(180.0/M_PI);
double shoulder =0;
shoulder =(atan2(y,x)+acos((x*x+y*y+l1*l1-l2*l2)/(2*l1*sqrt(x*x+y*y))));
shoulder *=(180.0/M_PI);
double wrist = orientation-elbow-shoulder;
setAngles(shoulder,elbow,wrist,time);
}
/**
* Update cartesian.
*
* @param x the x
* @param y the y
* @param orientation the orientation
* @return true, if successful
*/
private boolean updateCartesian(double x, double y, double orientation) {
if(((x>(pose[0]+xyThreshHold))) || (x<(pose[0]-xyThreshHold))){
Log.info("X changed");
return true;
}else{
Log.info("X set: "+x+" was: "+pose[0]);
}
if(((y>(pose[1]+xyThreshHold))) || (y<(pose[1]-xyThreshHold))){
Log.info("Y changed");
return true;
}else{
Log.info("Y set: "+y+" was: "+pose[1]);
}
if((orientation>pose[2]+orientThreshHold) || (orientation<pose[2]-orientThreshHold)){
Log.info("Orientation changed");
return true;
}
Log.info("No signifigant change");
return false;
}
/**
* Sets the cartesian x.
*
* @param x the new cartesian x
*/
public void setCartesianX(double x){
setCartesianPose(x, pose[1], pose[2]);
}
/**
* Sets the cartesian y.
*
* @param y the new cartesian y
*/
public void setCartesianY(double y){
setCartesianPose(pose[0], y, pose[2]);
}
/**
* Sets the cartesian orientation.
*
* @param o the new cartesian orientation
*/
public void setCartesianOrientation(double o){
setCartesianPose(pose[0], pose[1], o);
}
/**
* Sqrt.
*
* @param d the d
* @return the double
*/
/*
* Math wrappers for direct compatibility with C code
*/
private double sqrt(double d) {
return Math.sqrt(d);
}
/**
* Atan2.
*
* @param y the y
* @param x the x
* @return the double
*/
private double atan2(double y, double x) {
return Math.atan2(y, x);
}
/**
* Acos.
*
* @param d the d
* @return the double
*/
private double acos(double d) {
return Math.acos(d);
}
/**
* Sin.
*
* @param angle the angle
* @return the double
*/
private double sin(double angle) {
return Math.sin(angle);
}
/**
* Cos.
*
* @param angle the angle
* @return the double
*/
private double cos(double angle) {
return Math.cos(angle);
}
/**
* To radians.
*
* @param degrees the degrees
* @return the double
*/
private double ToRadians(double degrees){
return degrees*M_PI/180.0;
}
/**
* Gets the orientation.
*
* @return the current approach orientation of the wrist
*/
public double GetOrientation() {
double [] angles =getAngles();
return angles[0]+angles[1]+angles[2];
}
/**
* Sets the centers.
*
* @param centers the new centers
*/
public void setCenters(double [] centers) {
this.centers = centers;
}
/**
* Gets the centers.
*
* @return the centers
*/
public double [] getCenters() {
return centers;
}
/**
* Sets the blocking.
*
* @param blocking the new blocking
*/
public void setBlocking(boolean blocking) {
this.blocking = blocking;
}
/**
* Checks if is blocking.
*
* @return true, if is blocking
*/
public boolean isBlocking() {
return blocking;
}
/**
* Sets the scale.
*
* @param scale the new scale
*/
public void setScale(double scale[]) {
this.scale = scale;
}
/**
* Gets the scale.
*
* @return the scale
*/
public double[] getScale() {
return scale;
}
}