BowlerKernel
LegacyPidNamespaceImp.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.namespace.bcs.pid;
2 
3 import com.neuronrobotics.sdk.commands.bcs.pid.ConfigurePDVelocityCommand;
4 import com.neuronrobotics.sdk.commands.bcs.pid.ConfigurePIDCommand;
5 import com.neuronrobotics.sdk.commands.bcs.pid.ControlAllPIDCommand;
6 import com.neuronrobotics.sdk.commands.bcs.pid.ControlPIDCommand;
7 import com.neuronrobotics.sdk.commands.bcs.pid.GetPIDChannelCountCommand;
8 import com.neuronrobotics.sdk.commands.bcs.pid.KillAllPIDCommand;
9 import com.neuronrobotics.sdk.commands.bcs.pid.PDVelocityCommand;
10 import com.neuronrobotics.sdk.commands.bcs.pid.ResetPIDCommand;
11 import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
12 import com.neuronrobotics.sdk.common.BowlerDatagram;
13 import com.neuronrobotics.sdk.common.ByteList;
14 import com.neuronrobotics.sdk.common.Log;
15 import com.neuronrobotics.sdk.pid.PDVelocityConfiguration;
16 import com.neuronrobotics.sdk.pid.PIDChannel;
17 import com.neuronrobotics.sdk.pid.PIDCommandException;
18 import com.neuronrobotics.sdk.pid.PIDConfiguration;
19 import com.neuronrobotics.sdk.pid.PIDEvent;
20 import com.neuronrobotics.sdk.pid.PIDLimitEvent;
21 
22 // TODO: Auto-generated Javadoc
27 
28 
35  super(device);
36  }
37 
38  /* (non-Javadoc)
39  * @see com.neuronrobotics.sdk.namespace.bcs.pid.AbstractPidNamespaceImp#onAsyncResponse(com.neuronrobotics.sdk.common.BowlerDatagram)
40  */
41  public void onAsyncResponse(BowlerDatagram data) {
42  //Log.debug("\nPID ASYNC<<"+data);
43  if(data.getRPC().contains("_pid")){
44 
45  PIDEvent e =new PIDEvent(data);
46 
47  firePIDEvent(e);
48  }
49  if(data.getRPC().contains("apid")){
50  int [] pos = new int[getNumberOfChannels()];
51  for(int i=0;i<getNumberOfChannels();i++) {
52  pos[i] = ByteList.convertToInt( data.getData().getBytes(i*4, 4),true);
53  PIDEvent e =new PIDEvent(i,pos[i],System.currentTimeMillis(),0);
54  firePIDEvent(e);
55  }
56  }
57  if(data.getRPC().contains("pidl")){
59  }
60 
61  }
62 
63 
64  /* (non-Javadoc)
65  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#ResetPIDChannel
66  */
67  @Override
68  public boolean ResetPIDChannel(int group, float valueToSetCurrentTo) {
69  BowlerDatagram rst = getDevice().send(new ResetPIDCommand((char) group,valueToSetCurrentTo));
70  if(rst==null)
71  return false;
72  float val = GetPIDPosition(group);
73  firePIDResetEvent(group,val);
74  return true;
75  }
76 
77 
78  /* (non-Javadoc)
79  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#ConfigurePIDController
80  */
81  @Override
82  public boolean ConfigurePIDController(PIDConfiguration config) {
83  return getDevice().send(new ConfigurePIDCommand(config))!=null;
84  }
85 
86  @Override
87  /* (non-Javadoc)
88  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#getPIDConfiguration
89  */
91  BowlerDatagram conf = getDevice().send(new ConfigurePIDCommand( (char) group) );
92  PIDConfiguration back=new PIDConfiguration (conf);
93  return back;
94  }
95 
96  /* (non-Javadoc)
97  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#ConfigurePDVelovityController(com.neuronrobotics.sdk.pid.PDVelocityConfiguration)
98  */
99  @Override
101  return getDevice().send(new ConfigurePDVelocityCommand(config))!=null;
102  }
103 
104  /* (non-Javadoc)
105  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#getPDVelocityConfiguration(int)
106  */
107  @Override
109  // TODO Auto-generated method stub
110  return new PDVelocityConfiguration(getDevice().send(new ConfigurePDVelocityCommand(group)));
111  }
112 
113  /* (non-Javadoc)
114  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#getPIDChannelCount()
115  */
116  @Override
117  public int getPIDChannelCount() {
118  if(getChannelCount()==null){
121  }
122  return getChannelCount();
123  }
124 
125  @Override
126  /* (non-Javadoc)
127  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#SetPIDSetPoint
128  */
129  public boolean SetPIDSetPoint(int group,float setpoint,double seconds){
130  getPIDChannel(group).setCachedTargetValue(setpoint);
131  Log.info("Setting PID position group="+group+", setpoint="+setpoint+" ticks, time="+seconds+" sec.");
132  return getDevice().send(new ControlPIDCommand((char) group,setpoint, seconds))!=null;
133  }
134 
135  @Override
136  /* (non-Javadoc)
137  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#SetAllPIDSetPoint
138  */
139  public boolean SetAllPIDSetPoint(float []setpoints,double seconds){
140  float[] sp;
141  if(setpoints.length<getNumberOfChannels()) {
142  sp = new float[getNumberOfChannels()];
143  for(int i=0;i<sp.length;i++) {
145  }
146  for(int i=0;i<setpoints.length;i++) {
147  sp[i]=setpoints[i];
148  }
149  }else {
150  sp=setpoints;
151  }
152  for(int i=0;i<getNumberOfChannels();i++){
154  }
155  return getDevice().send(new ControlAllPIDCommand(sp, seconds))!=null;
156  }
157 
158  @Override
159  /* (non-Javadoc)
160  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#GetPIDPosition
161  */
162  public float GetPIDPosition(int group) {
163  BowlerDatagram b = getDevice().send(new ControlPIDCommand((char) group));
164  return ByteList.convertToInt(b.getData().getBytes( 1,//Starting index
165  4),//number of bytes
166  true);//True for signed data
167  }
168  @Override
169  /* (non-Javadoc)
170  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#GetAllPIDPosition
171  */
172  public float [] GetAllPIDPosition() {
173  Log.debug("Getting All PID Positions");
175  ByteList data = b.getData();
176  float [] back = new float[data.size()/4];
177  for(int i=0;i<back.length;i++) {
178  int start = i*4;
179  byte [] tmp = data.getBytes(start, 4);
180  back[i] = ByteList.convertToInt( tmp,true);
181  }
182  if(back.length != getNumberOfChannels()){
183  lastPacketTime = new long[back.length];
184  for(int i=0;i<back.length;i++){
185  PIDChannel c =new PIDChannel(this,i);
186  c.setCachedTargetValue(back[i]);
187  getChannels().add(c);
188  }
189  }
190  return back;
191  }
192 
193 
194  /* (non-Javadoc)
195  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#SetPDVelocity
196  */
197  @Override
198  public boolean SetPDVelocity(int group, int unitsPerSecond, double seconds)throws PIDCommandException {
199  try{
200  Log.debug("Setting hardware velocity control");
201  return getDevice().send(new PDVelocityCommand(group, unitsPerSecond, seconds))!=null;
202  }catch (Exception ex){
203  Log.error("Failed! Setting interpolated velocity control..");
204  return SetPIDInterpolatedVelocity( group, unitsPerSecond, seconds);
205  }
206  }
207 
208 
209  /* (non-Javadoc)
210  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IPidControlNamespace#illAllPidGroups
211  */
212  @Override
213  public boolean killAllPidGroups() {
215  return getDevice().send(new KillAllPIDCommand())==null;
216  }
217 
218  /* (non-Javadoc)
219  * @see com.neuronrobotics.sdk.namespace.bcs.pid.IExtendedPIDControl#runOutputHysteresisCalibration(int)
220  */
221  @Override
222  public boolean runOutputHysteresisCalibration(int group) {
223  throw new RuntimeException("This method is not implemented in this version of the namespace");
224  }
225 
226 
227 
228 }
BowlerDatagram send(BowlerAbstractCommand command)
static final int convertToInt(byte[] b)
Definition: ByteList.java:911
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92
static void debug(String message)
Definition: Log.java:128
boolean SetPIDInterpolatedVelocity(int group, int unitsPerSecond, double seconds)
boolean SetPIDSetPoint(int group, float setpoint, double seconds)
boolean SetPDVelocity(int group, int unitsPerSecond, double seconds)
boolean ResetPIDChannel(int group, float valueToSetCurrentTo)
void setCachedTargetValue(float targetValue)