BowlerKernel
CreateArm.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.addons.irobot;
16 
17 import com.neuronrobotics.sdk.common.Log;
18 import com.neuronrobotics.sdk.dyio.peripherals.DyIOPeripheralException;
19 import com.neuronrobotics.sdk.dyio.peripherals.ServoChannel;
20 
21 // TODO: Auto-generated Javadoc
25 public class CreateArm {
26 
28  ServoChannel [] links;
29 
31  private static final double l1 = 6.0;
32 
34  private static final double l2 = 3.93;
35 
37  private static final double l3 = 4.75;
38 
40  //private static final double l3 = .0001;
41  private static final double M_PI = Math.PI;
42 
44  private double scale[]={1.55,1.50,-1.76};
45 
47  private double [] positions=new double[4];
48 
50  private double [] angles=new double[3];
51 
53  private double [] pose=new double[3];
54 
56  private double [] centers={134,136,128,48};
57 
59  private boolean blocking = false;
60 
62  private double xyThreshHold = .1;
63 
65  private double orentThreshHold = 1;
66 
76  public CreateArm(ServoChannel [] links){
77  check(links);
78  home();
80  }
81 
85  public void home(){
86  setAngles(90,-90,0);
88  gripOpen();
89  }
90 
94  public void rest(){
95  setAngles(110,-110,0);
97  gripOpen();
98  }
99 
103  public void gripClose(){
104  links[3].SetPosition((int) centers[3]+75,(float).5);
105  if (isBlocking()){
106  try {
107  Thread.sleep(500);
108  } catch (InterruptedException e1) {
109  // ignore
110  }
111  }
112  }
113 
117  public void gripOpen(){
118  links[3].SetPosition((int) centers[3],(float) .5);
119  if (isBlocking()){
120  try {
121  Thread.sleep(500);
122  } catch (InterruptedException e1) {
123  // ignore
124  }
125  }
126  }
127 
133  private void check(ServoChannel [] links){
134  if(links.length != 4){
135  throw new DyIOPeripheralException("This perpheral needs 4 links.");
136  }
137  for(int i=0;i<4;i++){
138  if(links[i]==null){
139  throw new DyIOPeripheralException("All links must already be instantiated.");
140  }
141  links[i].SetPosition((int) getCenters()[i],2);
142  positions[i]=(double)getCenters()[i];
143  }
144  this.links = links;
145  }
146 
152  public void setShouler(double angle){
153  setAngles(angle,angles[1],angles[2]);
154  }
155 
161  public void setElbow(double angle){
162  setAngles(angles[0],angle,angles[2]);
163  }
164 
170  public void setWrist(double angle){
171  setAngles(angles[0],angles[1],angle);
172  }
173 
182  public void setAngles(double shoulder, double elbow, double wrist){
183  setAngles(shoulder, elbow, wrist,(float) 1.0);
184  }
185 
195  public void setAngles(double shoulder, double elbow, double wrist,float time) {
196  angles[0]=shoulder;
197  angles[1]=elbow;
198  angles[2]=wrist;
199  double s,e,w;
200  s =shoulder-90;
201  s *=getScale()[0];
202  positions[0]=getCenters()[0]+s;
203 
204  e =(elbow+90);
205  e *=getScale()[1];
206  positions[1]=getCenters()[1]+e;
207  double interference = centers[0]-45;
208  double limit = 255 - (1.1*(positions[0]-interference));
209  if(positions[0]>interference){
210  Log.info("In interference zone: "+interference+" with limit: "+limit );
211  if (positions[1]>limit){
212  System.err.print("\nAttempting to set angle that interferes, fixing. Was: "+positions[0]+","+positions[1]);
213  positions[1]=limit;
214  System.err.print(" Is: "+positions[0]+" , "+positions[1]+"\n");
215  }
216  Log.info("\n");
217  }
218 
219  if(positions[1]<(centers[1]-83)){
220  positions[1]=(centers[1]-83);
221  }
222 
223  double [] an= getAngles();
224  w =(int) ((pose[2]-an[0]-an[1])*getScale()[2]);
225  positions[2]=getCenters()[2]+w;
226 
227 
228  for (int i=0;i<3;i++){
229  if (positions[i]>255){
230  positions[i]=255;
231  }
232  if (positions[i]<0){
233  positions[i]=0;
234  }
235  links[i].SetPosition((int) positions[i],time);
236  }
237  Log.info("Set positions Shoulder: "+positions[0]+" Elbow: "+positions[1]+" Wrist: "+positions[2]);
238  if (isBlocking()){
239  try {
240  Thread.sleep((long) (time*1000));
241  } catch (InterruptedException e1) {
242  // ignore
243  }
244  }
245 
246  }
247 
253  public double [] getAngles(){
254  double [] a=new double [3];
255  for (int i=0;i<3;i++){
256  a[i]=(positions[i]-getCenters()[i])/getScale()[i];
257  }
258  a[0]+=90;
259  a[1]-=90;
260  return a;
261  }
262 
268  public double [] getCartesianPose(){
269  double [] angles =getAngles();
270  pose[2] = GetOrentation();
271 
274  double [] p = new double [3];
275  for ( int i = 0; i<3; i++){
276  p[i]=pose[i];
277  }
278  return p;
279  }
280 
286  public String getCartesianPoseString(){
288  String s="[";
289  for(int i=0;i <pose.length;i++){
290  s+=pose[i];
291  if(i<pose.length-1)
292  s+=",";
293  }
294  return s+"]";
295  }
296 
302  public void setCartesianPose(double [] p){
303  setCartesianPose(p,(float) 1.0);
304  }
305 
312  public void setCartesianPose(double [] p, float time){
313  setCartesianPose(p[0],p[1], p[2],time);
314  }
315 
323  public void setCartesianPose(double x, double y, double orentation){
324  setCartesianPose(x,y, orentation,(float).2);
325  }
326 
335  public void setCartesianPose(double x, double y, double orentation, float time){
336  if(orentation<-35)
337  orentation=-35;
338  if(orentation>35)
339  orentation=35;
340  if (!updateCartesian(x,y,orentation)){
341  return;
342  }
343 
344  pose[0]=x;
345  pose[1]=y;
346  pose[2]=orentation;
347 
348  Log.info("Setting Pose X: "+x+" Y: "+y+" Orentation: "+orentation );
349 
350  x -= (l3*cos(orentation*M_PI/180));
351  y -= (l3*sin(orentation*M_PI/180));
352  if (sqrt(x*x+y*y) > l1+l2) {
353  System.err.println("Hypotenus too long"+x+" "+y+"\r\n");
354  return;
355  }
356  double elbow = 0;
357  elbow =(-1*acos(((x*x+y*y)-(l1*l1+l2*l2))/(2*l1*l2)));
358  elbow *=(180.0/M_PI);
359 
360  double shoulder =0;
361  shoulder =(atan2(y,x)+acos((x*x+y*y+l1*l1-l2*l2)/(2*l1*sqrt(x*x+y*y))));
362  shoulder *=(180.0/M_PI);
363 
364  double wrist = orentation-elbow-shoulder;
365  setAngles(shoulder,elbow,wrist,time);
366 
367 
368  }
369 
378  private boolean updateCartesian(double x, double y, double orentation) {
379  if(((x>(pose[0]+xyThreshHold))) || (x<(pose[0]-xyThreshHold))){
380  Log.info("X changed");
381  return true;
382  }else{
383  Log.info("X set: "+x+" was: "+pose[0]);
384  }
385  if(((y>(pose[1]+xyThreshHold))) || (y<(pose[1]-xyThreshHold))){
386  Log.info("Y changed");
387  return true;
388  }else{
389  Log.info("Y set: "+y+" was: "+pose[1]);
390  }
391  if((orentation>pose[2]+orentThreshHold) || (orentation<pose[2]-orentThreshHold)){
392  Log.info("Orentation changed");
393  return true;
394  }
395  Log.info("No signifigant change");
396  return false;
397  }
398 
404  public void setCartesianX(double x){
405  setCartesianPose(x, pose[1], pose[2]);
406  }
407 
413  public void setCartesianY(double y){
414  setCartesianPose(pose[0], y, pose[2]);
415  }
416 
422  public void setCartesianOrentation(double o){
423  setCartesianPose(pose[0], pose[1], o);
424  }
425 
432  /*
433  * Math wrappers for direct compatibility with C code
434  */
435  private double sqrt(double d) {
436  return Math.sqrt(d);
437  }
438 
446  private double atan2(double y, double x) {
447  return Math.atan2(y, x);
448  }
449 
456  private double acos(double d) {
457  return Math.acos(d);
458  }
459 
466  private double sin(double angle) {
467  return Math.sin(angle);
468  }
469 
476  private double cos(double angle) {
477  return Math.cos(angle);
478  }
479 
486  private double ToRadians(double degrees){
487  return degrees*M_PI/180.0;
488  }
489 
495  public double GetOrentation() {
496  double [] angles =getAngles();
497  return angles[0]+angles[1]+angles[2];
498  }
499 
505  public void setCenters(double [] centers) {
506  this.centers = centers;
507  }
508 
514  public double [] getCenters() {
515  return centers;
516  }
517 
523  public void setBlocking(boolean blocking) {
524  this.blocking = blocking;
525  }
526 
532  public boolean isBlocking() {
533  return blocking;
534  }
535 
541  public void setScale(double scale[]) {
542  this.scale = scale;
543  }
544 
550  public double[] getScale() {
551  return scale;
552  }
553 
554 }
void setCartesianPose(double x, double y, double orentation, float time)
Definition: CreateArm.java:335
void setCartesianPose(double[] p, float time)
Definition: CreateArm.java:312
void setAngles(double shoulder, double elbow, double wrist, float time)
Definition: CreateArm.java:195
void setCartesianPose(double x, double y, double orentation)
Definition: CreateArm.java:323
boolean updateCartesian(double x, double y, double orentation)
Definition: CreateArm.java:378
void setAngles(double shoulder, double elbow, double wrist)
Definition: CreateArm.java:182
static void info(String message)
Definition: Log.java:110