BowlerKernel
AbstractLink.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.addons.kinematics;
2 
3 import java.util.ArrayList;
4 
5 
6 import com.neuronrobotics.sdk.addons.kinematics.gcodebridge.IGcodeExecuter;
7 import com.neuronrobotics.sdk.addons.kinematics.imu.IMU;
8 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
9 import com.neuronrobotics.sdk.common.IFlushable;
10 import com.neuronrobotics.sdk.common.Log;
11 import com.neuronrobotics.sdk.common.TickToc;
12 import com.neuronrobotics.sdk.pid.PIDLimitEvent;
13 import com.neuronrobotics.sdk.pid.PIDLimitEventType;
14 
15 import javafx.scene.transform.Affine;
16 
17 // TODO: Auto-generated Javadoc
21 // Kevin Shouldn't the Link's channel be kept in this level of Abstraction? The way I designg AbstractCartesianPositonDevice Requires this
22 public abstract class AbstractLink implements IFlushable{
23 
25  private double targetValue=0;
26 
28  private double targetEngineeringUnits=0;
29 
31  private ArrayList<ILinkListener> links = new ArrayList<ILinkListener>();
32 
34  private LinkConfiguration conf =null;
35  private ArrayList<LinkConfiguration> slaveLinks;
38  private boolean useLimits=true;
39 
40  private Object linksLocation=null;
41 
45  private IMU imu = new IMU();
50  public double getDeviceMaximumValue() {
52  }
57  public double getDeviceMinimumValue() {
59  }
63  public void setDeviceMaximumValue(double max) {
65  }
70  public void setDeviceMinimumValue(double min) {
72  }
78  public double getDeviceMaxEngineeringUnits() {
79  if(conf.getScale()>0)
81  else
83  }
84 
90  public double getDeviceMinEngineeringUnits() {
91  if(conf.getScale()>0)
93  else
95  }
102  this.conf=conf;
104  if(slaveLinks.size()>0)
105  System.out.println(conf.getName()+" has slaves: "+slaveLinks.size());
107  //generate the links
109  }
110  }
116  public void cacheTargetValue(){
118  //generate the links
120  link.cacheTargetValue();
121  }
123  }
124 
130  public abstract void cacheTargetValueDevice();
131 
137  public void flush(double time){
139  //generate the links
141  link.flush(time);
142  }
143  flushDevice(time);
144  }
145 
152  public void flushAll(double time){
154  //generate the links
156  link.flushAll(time);
157  }
158  flushAllDevice(time);
159  }
160 
166  public abstract void flushDevice(double time);
167 
174  public abstract void flushAllDevice(double time);
175 
182  public abstract double getCurrentPosition();
183 
190  public double toEngineeringUnits(double value){
191  return ((value-getHome())*getScale());
192  }
193 
200  public double toLinkUnits(double euValue){
201  return (euValue/getScale())+getHome();
202  }
203 
210  //Log.info("Adding link listener: "+l);
211  if(getLinks().contains(l))
212  return;
213  getLinks().add(l);
214  }
215 
222  //Log.info("Removing link listener: "+l);
223  if(getLinks().contains(l))
224  getLinks().remove(l);
225  //throw new RuntimeException();
226  }
227 
233  public void fireLinkListener(double linkUnitsValue){
234  ArrayList<ILinkListener> links2 = getLinks();
235  for (int i = 0; i < links2.size(); i++) {
236  ILinkListener l = links2.get(i);
237  //Log.info("Link Event, RAW="+linkUnitsValue);
238  try {
239  l.onLinkPositionUpdate(this,toEngineeringUnits(linkUnitsValue));
240  }catch( Throwable t) {
241  t.printStackTrace(System.out);
242  }
243  }
244  }
245 
252  for(ILinkListener l:getLinks()){
253  //Log.info("Link Event, RAW="+linkUnitsValue);
254  l.onLinkLimit(this, e);
255  }
256  }
257 
261  public void Home(){
264  }
265 
271  public void incrementEngineeringUnits(double inc){
273  }
274 
280  public void setTargetEngineeringUnits(double pos) {
281  //TickToc.tic("Nan check link "+getLinkConfiguration().getLinkIndex());
282  if(new Double(pos).isNaN()) {
283  new RuntimeException("Setpopint in setTargetEngineeringUnits can not be set to nan").printStackTrace();
284  return;
285  }
286  //TickToc.tic("Nan check link done");
288  double linkUnits = toLinkUnits(targetEngineeringUnits);
289  //TickToc.tic("to link units");
290  setPosition(linkUnits);
291  }
292 
298  public void setCurrentEngineeringUnits(double angle) {
299  double current = (double)(getCurrentPosition()-getHome());
300  if(current != 0)
301  conf.setScale(angle/current);
302  }
303 
309  public double getCurrentEngineeringUnits(){
310  double link = getCurrentPosition();
311  if(new Double(link).isNaN())
312  link=0;
313  double back = toEngineeringUnits(link);
314  //Log.info("Link space: "+link+" Joint space: "+back);
315  return back;
316  }
317 
323  public double getTargetEngineeringUnits() {
325  }
326 
332  public double getMaxEngineeringUnits() {
333  if(conf.getScale()>0)
335  else
337  }
338 
344  public double getMinEngineeringUnits() {
345  if(conf.getScale()>0)
347  else
349  }
350 
356  public void setMinEngineeringUnits(double minLimit ) {
357  if(conf.getScale()>0)
358  conf.setLowerLimit( toLinkUnits(minLimit));
359  else
360  conf.setUpperLimit( toLinkUnits(minLimit));
361  }
362 
368  public void setMaxEngineeringUnits(double maxLimit) {
369  if(conf.getScale()>0)
370  conf.setUpperLimit( toLinkUnits(maxLimit));
371  else
372  conf.setLowerLimit( toLinkUnits(maxLimit));
373  }
374 
375 
382  return Math.abs(toEngineeringUnits(conf.getUpperVelocity()));
383  }
389  public void setMaxVelocityEngineeringUnits(double max) {
391  }
392 
398  public boolean isMaxEngineeringUnits() {
399  if(getTargetValue() == getUpperLimit()) {
400  return true;
401  }
402  return false;
403  }
404 
410  public boolean isMinEngineeringUnits() {
411  if(getTargetValue() == getLowerLimit()) {
412  return true;
413  }
414  return false;
415  }
416 
422  protected void setPosition(double val) {
423  //if(getTargetValue() != val){
424  setTargetValue(val);
425  //}
427  //generate the links
429  link.cacheTargetValue();
430  }
432  }
433 
434 
435 
441  protected void setTargetValue(double val) {
442  //TickToc.tic("setTargetValue nan check");
443 
444  if(new Double(val).isNaN()) {
445  new RuntimeException("Setpopint in virtual device can not be set to nan").printStackTrace();
446  return;
447  }
448  //TickToc.tic("setTargetValue nan check done ");
449  Log.info("Setting cached value :"+val);
450  this.targetValue = val;
452  //generate the links
455  }
456  //TickToc.tic("followers set ");
457 
458  double ub = getMaxEngineeringUnits();
459  double lb = getMinEngineeringUnits();
460  boolean flip = getScale()<0;
461  boolean belowLower = targetValue<getLowerLimit();
462  boolean aboveUpper = targetValue>getUpperLimit();
463  String execpt = "Attempted="+toEngineeringUnits(targetValue)+" (engineering units) Device Units="+targetValue
464  +" \nUpper Bound="+ub+" (engineering units) Device Units="+getUpperLimit()
465  + "\nLower Bound="+lb+" (engineering units) Device Units="+getLowerLimit();
466  if(flip?belowLower:aboveUpper){
467  this.targetValue = flip?getLowerLimit():getUpperLimit();
469  //generate the links
472  }
475  new PIDLimitEvent(
477  targetValue ,
479  System.currentTimeMillis()
480  )
481  );
482  if(isUseLimits())throw new RuntimeException("Joint hit Upper software bound\n"+execpt);
483  }
484  if(flip?aboveUpper:belowLower) {
485  this.targetValue =flip?getUpperLimit():getLowerLimit();
487  //generate the links
490  }
493  new PIDLimitEvent(
495  targetValue ,
497  System.currentTimeMillis()
498  )
499  );
500  if(isUseLimits())throw new RuntimeException("Joint hit Lower software bound\n"+execpt);
501 
502  }else{
503  Log.info("Abstract Link: limits disabled");
504  }
505  //TickToc.tic("link bound set done");
506  }
507 
513  public double getTargetValue() {
514  return targetValue;
515  }
516 
522  public void setUpperLimit(int upperLimit) {
523  conf.setUpperLimit(upperLimit);
524  }
525 
531  public void setLowerLimit(int lowerLimit) {
532  conf.setLowerLimit(lowerLimit);
533  }
534 
540  public void setHome(int home) {
541  conf.setStaticOffset(home);
542  }
543 
549  public void setScale(double d) {
550  conf.setScale(d);
551  }
552 
558  public double getScale() {
559  return conf.getScale();
560  }
561 
567  public double getUpperLimit() {
568  return (double) conf.getUpperLimit();
569  }
570 
576  public double getLowerLimit() {
577  return conf.getLowerLimit();
578  }
579 
585  public double getHome() {
586  return conf.getStaticOffset();
587  }
588 
592  public void setCurrentAsUpperLimit() {
594  }
595 
599  public void setCurrentAsLowerLimit() {
601  }
602 
608  public void setUseLimits(boolean useLimits) {
609  this.useLimits = useLimits;
610  }
611 
617  public boolean isUseLimits() {
618  return useLimits;
619  }
620 
627  this.conf = conf;
628  }
629 
636  return conf;
637  }
638 
644  public ArrayList<ILinkListener> getLinks() {
645  return links;
646  }
647 
653  public void setLinks(ArrayList<ILinkListener> links) {
654  this.links = links;
655  }
656 
660  public void removeAllLinkListener() {
661  links.clear();
662  }
663 
664  public void setGlobalPositionListener(Object Object) {
665 // if(!Affine.class.isInstance(Object)) {
666 // RuntimeException runtimeException = new RuntimeException("Must be an Affine");
667 // runtimeException.printStackTrace();
668 // throw runtimeException;
669 // }
670  this.linksLocation = Object;
671  }
672 
673  public Object getGlobalPositionListener() {
674  return linksLocation;
675  }
677  return slaveFactory;
678  }
680  this.slaveFactory = slaveFactory;
681  }
682 
683  public IMU getImu() {
684  return imu;
685  }
688  }
691  }
692  public void clearChangeListener() {
694  }
695 }
void addChangeListener(ILinkConfigurationChangeListener l)
void removeChangeListener(ILinkConfigurationChangeListener l)
static void info(String message)
Definition: Log.java:110
void onLinkPositionUpdate(AbstractLink source, double engineeringUnitsValue)