BowlerKernel
DyIOAbstractPeripheral.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.dyio.peripherals;
16 
17 import com.neuronrobotics.sdk.commands.bcs.io.AsyncThreshholdEdgeType;
18 import com.neuronrobotics.sdk.commands.bcs.io.GetDyIOChannelCountCommand;
19 import com.neuronrobotics.sdk.commands.bcs.io.SetChannelValueCommand;
20 import com.neuronrobotics.sdk.common.BowlerMethod;
21 import com.neuronrobotics.sdk.common.ByteList;
22 import com.neuronrobotics.sdk.common.InvalidResponseException;
23 import com.neuronrobotics.sdk.dyio.DyIOChannel;
24 import com.neuronrobotics.sdk.dyio.DyIOChannelMode;
25 import com.neuronrobotics.sdk.dyio.IDyIOChannel;
26 
27 // TODO: Auto-generated Javadoc
31 public abstract class DyIOAbstractPeripheral implements IDyIOChannel {
32 
35 
37  private boolean enabled = false;
38 
40  private int configuration=0;
41 
49  public DyIOAbstractPeripheral(DyIOChannel channel, DyIOChannelMode myMode, boolean async) {
50  this.channel = channel;
51  this.enabled = true;
52  if(channel.getMode() != myMode)
53  channel.setMode(myMode, async);
55  //channel.removeAllChannelEventListeners();
56  //Always notify any listeners of mode changes
57  //channel.removeAllChannelModeChangeListener();
58  }
59 
66  if(getChannel().getDevice().isLegacyParser()){
68  return 128;
69  return 0;
70  }else{
71  Object[] args = getChannel().getDevice().send("bcs.io.*;0.3;;",
73  "cchn",
74  new Object[]{ 255,
75  false,
76  new Integer[]{}
77  });
78  Integer [] configs = (Integer[]) args[0];
79  return configs[getChannel().getChannelNumber()];
80  }
81 
82  }
83 
90  return channel.getMode();
91  }
92 
98  public boolean isEnabled() {
99  return enabled;
100  }
101 
107  public boolean setMode(){
108  return channel.setMode(getClassMode());
109  }
110 
117  public boolean setMode( boolean async) {
118  return channel.setMode(getClassMode(), async);
119  }
120 
129  public boolean setMode(DyIOChannelMode mode, boolean async) {
130  if(mode != getClassMode())
131  throw new RuntimeException("The mode being set does not match the defined channel mode: "+mode+" is not"+getClassMode());
132  return channel.setMode(getClassMode(), async);
133  }
134 
142  return channel.getChannel();
143  }
144 
152  return channel.getMode();
153  }
154 
163  public boolean setValue(int value) throws InvalidResponseException {
164  return channel.setValue(value);
165  }
166 
175  public boolean setValue(ByteList value)throws InvalidResponseException {
176  return channel.setValue(value);
177  }
178 
186  public int getValue()throws InvalidResponseException {
187  //new RuntimeException().printStackTrace();
188  return channel.getValue();
189  }
190 
191 
199  public boolean SavePosition(int pos){
200  try {
201  DyIOChannelMode mode = getChannel().getMode();
202  switch(mode){
203  case SERVO_OUT:
204  case PWM_OUT :
205  configuration = pos;
206  if(getChannel().getDevice().isLegacyParser()){
207  getChannel().send(new SetChannelValueCommand(getChannel().getChannelNumber(), pos , getMode(), true));
208  }else{
209 
210  getChannel().getDevice().send("bcs.io.*;0.3;;",
212  "cchn",
213  new Object[]{ getChannel().getChannelNumber(),
214  true,
215  new Integer[]{pos}
216  });
217  getChannel().setValue(pos);
218  try {
219  Thread.sleep(30);
220  } catch (InterruptedException e) {
221  // TODO Auto-generated catch block
222  e.printStackTrace();
223  }
224  }
225  return true;
226  default:
227  return false;
228  }
229 
230  } catch (InvalidResponseException e) {
231  return false;
232  }
233  }
234 
239  abstract public boolean hasAsync();
240 
246  public boolean configAdvancedAsyncNotEqual(){
247  return getChannel().configAdvancedAsyncNotEqual(100);
248  }
249 
257  public boolean configAdvancedAsyncDeadBand(int deadbandSize){
258  return getChannel().configAdvancedAsyncDeadBand(100,deadbandSize);
259  }
260 
269  public boolean configAdvancedAsyncTreshhold(int threshholdValue,AsyncThreshholdEdgeType edgeType){
270  return getChannel().configAdvancedAsyncTreshhold(100, threshholdValue, edgeType);
271  }
272 
281  return getChannel().configAdvancedAsyncAutoSample(100);
282  }
288  public boolean configAdvancedAsyncNotEqual(int msTime){
289  return getChannel().configAdvancedAsyncNotEqual(msTime);
290  }
291 
299  public boolean configAdvancedAsyncDeadBand(int msTime,int deadbandSize){
300  return getChannel().configAdvancedAsyncDeadBand(msTime,deadbandSize);
301  }
302 
311  public boolean configAdvancedAsyncTreshhold(int msTime,int threshholdValue, AsyncThreshholdEdgeType edgeType){
312  return getChannel().configAdvancedAsyncTreshhold(msTime, threshholdValue, edgeType);
313  }
314 
322  public boolean configAdvancedAsyncAutoSample(int msTime){
323  return getChannel().configAdvancedAsyncAutoSample(msTime);
324  }
325 
331  public boolean flush() {
332  return getChannel().flush();
333  }
334 
340  public void setAsync(boolean b) {
341  channel.setMode(getClassMode(), b);
342  }
343 
349  public int getConfiguration() {
350  return configuration;
351  }
352 
358  private void setConfiguration(int configuration) {
359  this.configuration = configuration;
360  }
361 }
boolean setMode(DyIOChannelMode mode)
DyIOChannelMode getMode(boolean resync)
Object[] send(String NS, BowlerMethod method, String rpcString, Object[] arguments)
Definition: DyIO.java:168
DyIOAbstractPeripheral(DyIOChannel channel, DyIOChannelMode myMode, boolean async)
boolean configAdvancedAsyncTreshhold(int msTime, int threshholdValue, AsyncThreshholdEdgeType edgeType)
boolean configAdvancedAsyncTreshhold(int threshholdValue, AsyncThreshholdEdgeType edgeType)