BowlerKernel
AbstractPidNamespaceImp.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.namespace.bcs.pid;
2 
3 import java.util.ArrayList;
4 
5 import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
6 import com.neuronrobotics.sdk.common.BowlerDatagram;
7 import com.neuronrobotics.sdk.common.Log;
8 import com.neuronrobotics.sdk.pid.IPIDEventListener;
9 import com.neuronrobotics.sdk.pid.PIDChannel;
10 import com.neuronrobotics.sdk.pid.PIDCommandException;
11 import com.neuronrobotics.sdk.pid.PIDEvent;
12 import com.neuronrobotics.sdk.pid.PIDLimitEvent;
13 
14 // TODO: Auto-generated Javadoc
18 public abstract class AbstractPidNamespaceImp implements IExtendedPIDControl {
19 
21  private ArrayList<IPIDEventListener> PIDEventListeners = new ArrayList<IPIDEventListener>();
22 
24  protected ArrayList<PIDChannel> channels = null;
25 
27  protected long [] lastPacketTime = null;
28 
31 
33  private Integer channelCount=null;
34 
41  this.setDevice(device);
43  public void onPIDReset(int group, float currentValue) {}
44  public void onPIDLimitEvent(PIDLimitEvent e) {}
45  public void onPIDEvent(PIDEvent e) {
47  }
48  });
49  }
50 
51 
58  public float GetCachedPosition(int group) {
60  }
61 
68  public void SetCachedPosition(int group, float value) {
69 
71  }
72 
73 
74  /* (non-Javadoc)
75  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#flushPIDChannels
76  */
77  @Override
78  public void flushPIDChannels(double time) {
79  float [] data = new float[getNumberOfChannels()];
80  for(int i=0;i<getNumberOfChannels();i++){
82  }
83  Log.info("Flushing in "+time+"ms");
84  SetAllPIDSetPoint(data, time);
85  }
86  /* (non-Javadoc)
87  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#SetPIDInterpolatedVelocity
88  */
89  @Override
90  public boolean SetPIDInterpolatedVelocity(int group, int unitsPerSecond, double seconds) throws PIDCommandException {
91  long dist = (long)unitsPerSecond*(long)seconds;
92  long delt = ((long) (GetCachedPosition(group))-dist);
93  if(delt>2147483646 || delt<-2147483646){
94  throw new PIDCommandException("(Current Position) - (Velocity * Time) too large: "+delt+"\nTry resetting the encoders");
95  }
96  return SetPIDSetPoint(group, (int) delt, seconds);
97  }
98 
105  public int getNumberOfChannels(){
106  return getChannels().size();
107  }
108 
109 
110  /* (non-Javadoc)
111  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#addPIDEventListener
112  */
114  synchronized(PIDEventListeners){
115  if(!PIDEventListeners.contains(l))
116  PIDEventListeners.add(l);
117  }
118  }
119  /* (non-Javadoc)
120  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#removePIDEventListener
121  */
123  synchronized(PIDEventListeners){
124  if(PIDEventListeners.contains(l))
125  PIDEventListeners.remove(l);
126  }
127  }
128 
135  synchronized(PIDEventListeners){
137  l.onPIDLimitEvent(e);
138  }
139  //channels.get(e.getGroup()).firePIDLimitEvent(e);
140  }
141 
147  public void firePIDEvent(PIDEvent e){
148  if(lastPacketTime != null){
149  if(lastPacketTime[e.getGroup()]>e.getTimeStamp()){
150  Log.error("This event timestamp is out of date, aborting"+e);
151  return;
152  }else{
153  //Log.info("Pid event "+e);
155  }
156  }
157 
158 
160  for (int i = 0; i < PIDEventListeners.size(); i++) {
162  l.onPIDEvent(e);
163  }
164  }
165 
172  public void firePIDResetEvent(int group,float value){
173  SetCachedPosition(group, value);
175  l.onPIDReset(group,value);
176  //channels.get(group).firePIDResetEvent(group, value);
177  }
178 
185  return device;
186  }
187 
194  this.device = device;
195  }
196  /* (non-Javadoc)
197  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#getPIDChannel
198  */
199  @Override
200  public PIDChannel getPIDChannel(int group) {
201  if(getNumberOfChannels()==0) {
202  getChannels();
203  }
204  while(!(group < getNumberOfChannels() )){
205  PIDChannel c =new PIDChannel(this,group);
206  getChannels().add(c);
207  }
208  return getChannels().get(group);
209  }
210 
211  /* (non-Javadoc)
212  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#isAvailable()
213  */
214  @Override
215  public boolean isAvailable() {
216  return device.isAvailable();
217  }
218 
224  public abstract void onAsyncResponse(BowlerDatagram data);
225 
226 
232  public ArrayList<PIDChannel> getChannels() {
233  if(channels==null){
234  channels=new ArrayList<PIDChannel>();
235  for(int i=0;i<getPIDChannelCount();i++){
236  getPIDChannel(i);
237  }
238  }
239  return channels;
240  }
241 
242 
248  public void setChannels(ArrayList<PIDChannel> channels) {
249  this.channels = channels;
250  }
251 
252 
258  public Integer getChannelCount() {
259  return channelCount;
260  }
261 
262 
268  public void setChannelCount(Integer channelCount) {
269  if(channelCount == null)
270  throw new RuntimeException("Must be set to a real value");
271  this.channelCount = channelCount;
272  }
273 
274 
275 }
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92
boolean SetPIDInterpolatedVelocity(int group, int unitsPerSecond, double seconds)
void setCurrentCachedPosition(float currentCachedPosition)
boolean SetAllPIDSetPoint(float[]setpoints, double seconds)
boolean SetPIDSetPoint(int group, float setpoint, double seconds)