BowlerKernel
LinkFactory.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.addons.kinematics;
2 
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 
6 import com.neuronrobotics.sdk.addons.kinematics.gcodebridge.GcodeDevice;
7 import com.neuronrobotics.sdk.addons.kinematics.gcodebridge.GcodePrismatic;
8 import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
9 import com.neuronrobotics.sdk.common.DeviceManager;
10 import com.neuronrobotics.sdk.common.IFlushable;
11 import com.neuronrobotics.sdk.common.Log;
12 import com.neuronrobotics.sdk.common.TickToc;
13 import com.neuronrobotics.sdk.dyio.DyIO;
14 import com.neuronrobotics.sdk.dyio.peripherals.AnalogInputChannel;
15 import com.neuronrobotics.sdk.dyio.peripherals.CounterOutputChannel;
16 import com.neuronrobotics.sdk.dyio.peripherals.ServoChannel;
17 import com.neuronrobotics.sdk.namespace.bcs.pid.IExtendedPIDControl;
18 import com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace;
19 import com.neuronrobotics.sdk.pid.GenericPIDDevice;
20 import com.neuronrobotics.sdk.pid.ILinkFactoryProvider;
21 import com.neuronrobotics.sdk.pid.VirtualGenericPIDDevice;
22 
23 // TODO: Auto-generated Javadoc
28  private static HashMap<String, INewLinkProvider> userLinkProviders = new HashMap<String, INewLinkProvider>();
29 
31  private ArrayList<AbstractLink> links = new ArrayList<AbstractLink>();
32 
34  private ArrayList<LinkConfiguration> linkConfigurations = null;
35 
43  public static void addLinkProvider(String typeTag, INewLinkProvider provider) {
44  userLinkProviders.put(typeTag, provider);
45  LinkType.addType(typeTag);
46  }
47 
54  public static boolean linkProviderExists(String typeTag) {
55  return userLinkProviders.get(typeTag) != null;
56  }
57 
61  public LinkFactory() {
62  this(null);
63  }
64 
71  if (bad != null)
73  }
74 
82  this(null);
83  // Log.enableInfoPrint();
84  // TODO fill in the auto link configuration
85  LinkConfiguration first = connection.requestLinkConfiguration(0);
86  first.setPidConfiguration(d);
87  getLink(first);
88 
89  for (int i = 1; i < first.getTotlaNumberOfLinks(); i++) {
90  LinkConfiguration tmp = connection.requestLinkConfiguration(i);
91  tmp.setPidConfiguration(d);
92  getLink(tmp);
93  }
94 
95  }
96 
103  public AbstractLink getLink(String name) {
104  for (AbstractLink l : links) {
105  if (l.getLinkConfiguration().getName().equalsIgnoreCase(name))
106  return l;
107  }
108  String data = "No link of name '" + name + "' exists";
109  for (AbstractLink l : links) {
110  data += "\n" + l.getLinkConfiguration().getName();
111  }
112  throw new RuntimeException(data);
113  }
114 
122  for (AbstractLink l : links) {
123  if (l.getLinkConfiguration() == c)
124  return l;
125  }
126  return getLinkLocal(c);
127  }
128 
135  // retreive the old link
136  AbstractLink oldLink = getLink(c);
137  links.remove(oldLink);
138  AbstractLink newLink = getLinkLocal(c);
139  for (ILinkListener l : oldLink.getLinks()) {
140  newLink.addLinkListener(l);
141  }
142  oldLink.removeAllLinkListener();
143 
144  }
145 
153 
154  AbstractLink tmp = null;
155  // Log.info("Loading link: "+c.getName()+" type = "+c.getTypeEnum()+" device=
156  // "+c.getDeviceScriptingName());
157  try {
158  switch (c.getTypeEnum()) {
159 
160  case ANALOG_PRISMATIC:
161  if (getDyio(c) != null) {
162  tmp = new AnalogPrismaticLink(new AnalogInputChannel(getDyio(c).getChannel(c.getHardwareIndex())),
163  c);
164  tmp.setUseLimits(false);
165  }
166  break;
167  case ANALOG_ROTORY:
168  if (getDyio(c) != null) {
169  tmp = new AnalogRotoryLink(new AnalogInputChannel(getDyio(c).getChannel(c.getHardwareIndex())), c);
170  tmp.setUseLimits(false);
171  }
172  break;
173  case PID_TOOL:
174  case PID:
175  if (getPid(c) != null) {
176  tmp = new PidRotoryLink(getPid(c).getPIDChannel(c.getHardwareIndex()), c, false);
177  }
178  break;
179  case PID_PRISMATIC:
180  if (getPid(c) != null) {
181  tmp = new PidPrismaticLink(getPid(c).getPIDChannel(c.getHardwareIndex()), c, false);
182  }
183  break;
184  case DUMMY:
185  case VIRTUAL:
186  String myVirtualDevName = c.getDeviceScriptingName();
187  tmp = new PidRotoryLink(getVirtual(myVirtualDevName).getPIDChannel(c.getHardwareIndex()), c, true);
188  break;
189  case GCODE_HEATER_TOOL:
190  if (getGCODE(c) != null) {
191  tmp = getGCODE(c).getHeater(c);
192  }
193  break;
194  case GCODE_STEPPER_PRISMATIC:
195  case GCODE_STEPPER_ROTORY:
196  case GCODE_STEPPER_TOOL:
197  if (getGCODE(c) != null) {
198  tmp = getGCODE(c).getLink(c);
199  }
200  break;
201  case USERDEFINED:
202  if (userLinkProviders.containsKey(c.getTypeString())) {
203  INewLinkProvider iNewLinkProvider = userLinkProviders.get(c.getTypeString());
204  tmp = iNewLinkProvider.generate(c);
205  if (IHardwareSyncPulseProvider.class.isInstance(iNewLinkProvider)) {
208  }
209  }
210  break;
211  default:
212  break;
213  }
214  } catch (Throwable t) {
215  t.printStackTrace();
216  }
217  if (tmp == null) {
218  String myVirtualDevName = c.getDeviceScriptingName();
219  if (!c.isPrismatic()) {
220  tmp = new PidRotoryLink(getVirtual(myVirtualDevName).getPIDChannel(c.getHardwareIndex()), c, true);
221  } else {
222  tmp = new PidPrismaticLink(getVirtual(myVirtualDevName).getPIDChannel(c.getHardwareIndex()), c, true);
223  }
224  }
225  tmp.setLinkConfiguration(c);
226  addLink(tmp);
227  return tmp;
228  }
229 
235  public void addLink(AbstractLink link) {
236  links.add(link);
237  if (!getLinkConfigurations().contains(link.getLinkConfiguration()))
239  }
240 
246  public double[] getLowerLimits() {
247  double[] up = new double[links.size()];
248  for (int i = 0; i < up.length; i++) {
249  up[i] = links.get(i).getMinEngineeringUnits();
250  }
251  return up;
252  }
253 
259  public double[] getUpperLimits() {
260  double[] up = new double[links.size()];
261  for (int i = 0; i < up.length; i++) {
262  up[i] = links.get(i).getMaxEngineeringUnits();
263  }
264  return up;
265  }
266 
273  for (AbstractLink lin : links) {
274  lin.addLinkListener(l);
275  }
276  }
277 
283  public void flush(final double seconds) {
284  HashMap<String, Boolean> flushed = new HashMap<String, Boolean>();
286  String name = c.getDeviceScriptingName();
287  // TickToc.tic("Checking "+name+" for flush ");
288  // if a device is disconnected it is removed from the device manager. the
289  // factory should check all devices
290  Object specificDevice = DeviceManager.getSpecificDevice(IFlushable.class, name);
291  if (specificDevice == null) {
292  getLink(c).flush(seconds);// links flushed directly because there is no flushable device
293  } else {
294  if (flushed.get(name) == null) {
295  flushed.put(name, true);
296  IFlushable flushDevice = (IFlushable) specificDevice;
297 
298  flushDevice.flush(seconds);
299  // TickToc.tic("Flushed "+name);
300  }
301  }
302  // TickToc.tic("Done Checking "+name+" for flush ");
303 
304  }
305  // System.out.println("Flush Took "+(System.currentTimeMillis()-time)+"ms");
306  }
307 
314 
317 
318  }
319 
326 
328 
329  }
330 
337 
339 
340  }
341 
347  public void setCachedTargets(double[] jointSpaceVect) {
348  if (jointSpaceVect.length != links.size())
349  throw new IndexOutOfBoundsException("Expected " + links.size() + " links, got " + jointSpaceVect.length);
350  int i = 0;
351  for (AbstractLink lin : links) {
352 
353  try {
354  lin.setTargetEngineeringUnits(jointSpaceVect[i]);
355  } catch (Exception ee) {
356  throw new RuntimeException("Joint " + i + " failed\n" + ee.getMessage());
357  }
358  i++;
359  }
360  }
361 
367  public boolean isConnected() {
369  // if a device is disconnected it is removed from the device manager. the
370  // factory should check all devices
371  if (DeviceManager.getSpecificDevice(null, c.getDeviceScriptingName()) == null) {
372  return false;
373  }
374  }
375  return true;
376  }
377 
383  public ArrayList<LinkConfiguration> getLinkConfigurations() {
384  if (linkConfigurations == null) {
385  linkConfigurations = new ArrayList<LinkConfiguration>();
386  }
387  return linkConfigurations;
388  }
389 
396  // TODO Auto-generated method stub
397  for (AbstractLink lin : links) {
398  lin.removeLinkListener(l);
399  }
400  }
401 
407  public void deleteLink(int i) {
408  links.remove(i);
409  getLinkConfigurations().remove(i);
410  }
411 
412  @Override
413  public void sync() {
414  doSync();
415  }
416 
417  public VirtualGenericPIDDevice getVirtual(String myVirtualDevName) {
418  return (VirtualGenericPIDDevice) DeviceManager.getSpecificDevice(myVirtualDevName, () -> {
419  VirtualGenericPIDDevice virtualGenericPIDDevice = new VirtualGenericPIDDevice(myVirtualDevName);
420  virtualGenericPIDDevice.addIHardwareSyncPulseReciver(this);
421  return virtualGenericPIDDevice;
422  });
423  }
424 
425 }
VirtualGenericPIDDevice getVirtual(String myVirtualDevName)
ArrayList< LinkConfiguration > getLinkConfigurations()
AbstractLink getLinkLocal(LinkConfiguration c)
static HashMap< String, INewLinkProvider > userLinkProviders
LinkFactory(ILinkFactoryProvider connection, IExtendedPIDControl d)
static void addLinkProvider(String typeTag, INewLinkProvider provider)
IPidControlNamespace getPid(LinkConfiguration c)
static boolean linkProviderExists(String typeTag)
ArrayList< LinkConfiguration > linkConfigurations
static void addConnection(final Object newDevice, String name)
static Object getSpecificDevice(String name, IDeviceProvider provider)
AbstractLink generate(LinkConfiguration conf)
LinkConfiguration requestLinkConfiguration(int index)