BowlerKernel
ServoChannel.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 import java.util.ArrayList;
17 
18 import com.neuronrobotics.sdk.common.BowlerAbstractCommand;
19 import com.neuronrobotics.sdk.common.BowlerMethod;
20 import com.neuronrobotics.sdk.common.Log;
21 import com.neuronrobotics.sdk.dyio.DyIO;
22 import com.neuronrobotics.sdk.dyio.DyIOChannel;
23 import com.neuronrobotics.sdk.dyio.DyIOChannelEvent;
24 import com.neuronrobotics.sdk.dyio.DyIOChannelMode;
25 import com.neuronrobotics.sdk.common.DeviceManager;
26 import com.neuronrobotics.sdk.dyio.IChannelEventListener;
27 
28 // TODO: Auto-generated Javadoc
33 
35  private ArrayList<IServoPositionUpdateListener >listeners = new ArrayList<IServoPositionUpdateListener >();
36 
43  public ServoChannel(int channel){
44  this(((DyIO) DeviceManager.getSpecificDevice(DyIO.class, null)).getChannel(channel));
45  }
46 
54  public ServoChannel(DyIO dyio,int channel){
55  this(dyio.getChannel(channel));
56  }
57 
64  super(channel,DyIOChannelMode.SERVO_OUT,false);
65  if(!setMode()) {
66  throw new DyIOPeripheralException("Could not set channel " + channel + " to " + DyIOChannelMode.SERVO_OUT + " mode");
67  }
68  channel.setDap(this);
70  }
71 
78  public boolean SetPosition(int pos){
79  return SetPosition(pos, 0);
80  }
88  public boolean SetPosition(int pos, double time){
89  return SetPosition( pos, (float) time);
90  }
91 
99  public boolean SetPosition(int pos, float time){
100  if(time == 0)
101  time = (float) .001;
102  if(!validate()) {
103  return false;
104  }
105  //firePositionUpdate(pos,time);
106  getChannel().setCachedValue(pos);
107  getChannel().setCachedTime(time);
108  if(getChannel().getCachedMode()) {
109  //Log.debug(getClass()+" In cached mode, NOT flushing on Set");
110  return true;
111  }
112 
113  return flush();
114  }
115 
116 
122  private boolean validate() {
123  if(!isEnabled()) {
124  //return false;
125  }
126  return getMode() == DyIOChannelMode.SERVO_OUT;
127  }
128 
129  /* (non-Javadoc)
130  * @see com.neuronrobotics.sdk.dyio.peripherals.DyIOAbstractPeripheral#hasAsync()
131  */
132  @Override
133  public boolean hasAsync() {
134  return false;
135  }
136 
143  private void firePositionUpdate(int pos,double time){
145  s.onServoPositionUpdate(this, pos,time);
146  }
147  }
148 
155  if(listeners.contains(l))
156  return;
157  listeners.add(l);
158  }
159 
166  if(listeners.contains(l))
167  listeners.remove(l);
168  }
169 
174  public void enablePowerOverride(){
176  }
177 
181  public void disablePowerOverride(){
182  getChannel().getDevice().send(new powerOverridePacket(false) );
183  }
184 
189 
195  public powerOverridePacket(boolean ovr){
197  setOpCode("povr");
198  getCallingDataStorage().add(ovr?1:0);
199  }
200  }
201 
202  /* (non-Javadoc)
203  * @see com.neuronrobotics.sdk.dyio.IChannelEventListener#onChannelEvent(com.neuronrobotics.sdk.dyio.DyIOChannelEvent)
204  */
205  @Override
207  firePositionUpdate(e.getUnsignedValue(), System.currentTimeMillis());
208  }
209 
210 }
synchronized boolean add(byte data)
Definition: ByteList.java:149
static Object getSpecificDevice(String name, IDeviceProvider provider)
void setDap(DyIOAbstractPeripheral dap)
void addChannelEventListener(IChannelEventListener l)
Object[] send(String NS, BowlerMethod method, String rpcString, Object[] arguments)
Definition: DyIO.java:168
DyIOChannel getChannel(int channel)
Definition: DyIO.java:160
void removeIServoPositionUpdateListener(IServoPositionUpdateListener l)
void addIServoPositionUpdateListener(IServoPositionUpdateListener l)
ArrayList< IServoPositionUpdateListener > listeners