BowlerKernel
ServoOutputScheduleChannel.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.dyio.sequencer;
2 
3 import java.util.ArrayList;
4 
5 import com.neuronrobotics.sdk.dyio.peripherals.AnalogInputChannel;
6 import com.neuronrobotics.sdk.dyio.peripherals.IAnalogInputListener;
7 import com.neuronrobotics.sdk.dyio.peripherals.IServoPositionUpdateListener;
8 import com.neuronrobotics.sdk.dyio.peripherals.ServoChannel;
9 
10 // TODO: Auto-generated Javadoc
15 
18 
20  AnalogInputChannel input;
21 
23  private double inputScale=.25;
24 
26  private int inputCenter = 128;
27 
29  private int inputValue;
30 
32  private boolean recording=false;
33 
35  private double interval;
36 
38  private int currentValue;
39 
41  private ArrayList<MapData> data = new ArrayList<MapData>();
42 
44  private int outputMax=255;
45 
47  private int outputMin=0;
48 
50  private int index=0;
51 
54 
56  private int analogInputChannelNumber=8;
57 
64  output=srv;
66 
67  }
68 
74  public int getChannelNumber(){
76  }
77 
81  public void pauseRecording(){
82  System.out.println("pausing recording");
83  if(input != null)
84  input.removeAnalogInputListener(this);
85  setRecording(false);
86  }
87 
91  public void resumeRecording(){
92  if(input==null)
93  initInput();
94  System.out.println("resuming recording");
95  setRecording(true);
96  }
97 
98 
99 
106  input.addAnalogInputListener(l);
107  input.setAsync(true);
108  input.configAdvancedAsyncNotEqual(10);
109  }
110 
114  private void initInput() {
115  if(input==null || (input.getChannel().getChannelNumber() != getAnalogInputChannelNumber())){
117  }
118 
120  System.out.println("Re-Setting analog input channel: "+analogInputChannelNumber);
123  }
125  }
126 
130  public void startRecording(){
131  initInput();
132  resumeRecording();
133  }
134 
135  /* (non-Javadoc)
136  * @see com.neuronrobotics.sdk.dyio.sequencer.ISchedulerListener#onTimeUpdate(double)
137  */
138  @Override
139  public void onTimeUpdate(double ms) {
140  index = (int) (ms/getInterval());
141  while(index>=data.size()){
142  data.add(new MapData(getCurrentValue()));
143  }
144 
145  if(isRecording())
146  data.get(index).input=getCurrentTargetValue();
147 
148  setCurrentValue(data.get(index).input);
149 
150 
151  //output.SetPosition(data.get(index).input);
152  //System.out.println("Setting servo "+getChannelNumber()+" value="+getCurrentValue());
153  }
154 
155 
156  /* (non-Javadoc)
157  * @see com.neuronrobotics.sdk.dyio.sequencer.ISchedulerListener#setIntervalTime(int, int)
158  */
159  @Override
160  public void setIntervalTime(int msInterval, int totalTime) {
161  setInterval(msInterval);
162  int slices = totalTime/msInterval;
163  if(data.size()==0){
164  System.out.println("Setting up sample data: "+msInterval+"ms for: "+totalTime);
165  data = new ArrayList<MapData>();
169  }
172  }
174  for(int i=0;i<slices;i++){
175  data.add(new MapData(getCurrentValue()));
176  }
177  data.add(new MapData(getCurrentValue()));
178  }
179 
180  }
181 
182  /* (non-Javadoc)
183  * @see com.neuronrobotics.sdk.dyio.peripherals.IAnalogInputListener#onAnalogValueChange(com.neuronrobotics.sdk.dyio.peripherals.AnalogInputChannel, double)
184  */
185  @Override
186  public void onAnalogValueChange(AnalogInputChannel chan, double value) {
187 
188  double centerOffset =getInputCenter()-(512*getInputScale());
189  //System.out.println("Center Offset="+centerOffset);
190 
191  double scaled = (value*getInputScale());
192  double recentered = (scaled+centerOffset);
193 
194 
195  setCurrentTargetValue((int) recentered );
196  //System.out.println("Analog value="+(int)value+" scaled="+(int)scaled +" recentered="+(int)recentered);
199  }
202  }
203  }
204 
211  this.output = output;
212  }
213 
220  return output;
221  }
222 
228  public void setInputCenter(int inputCenter) {
229  this.inputCenter =inputCenter;
230  }
231 
237  public int getInputCenter() {
238  return inputCenter;
239  }
240 
246  public void setInputScale(double inputScale) {
247  this.inputScale = inputScale;
248  }
249 
255  public double getInputScale() {
256  return inputScale;
257  }
258 
265  public void setOutputMinMax(int outputMin,int outputMax) {
266  this.outputMax = outputMax;
267  this.outputMin = outputMin;
268  }
269 
275  public int getOutputMax() {
276  return outputMax;
277  }
278 
284  public int getOutputMin() {
285  return outputMin;
286  }
287 
291  private class MapData{
292 
294  public int input;
295 
301  public MapData(int i){
302  input=i;
303  }
304  }
305 
311  public boolean isRecording() {
312  return recording;
313  }
314 
322  }
323 
331  }
332 
338  public String getXml(){
339  String s="";
340  s+="\t<ServoOutputSequence>\n";
341  s+="\t\t<outputMax>"+outputMax+"</outputMax>\n";
342  s+="\t\t<outputMin>"+outputMin+"</outputMin>\n";
343  s+="\t\t<outputChannel>"+getChannelNumber()+"</outputChannel>\n";
344  s+="\t\t<inputEnabled>"+isRecording()+"</inputEnabled>\n";
345  s+="\t\t<inputScale>"+inputScale+"</inputScale>\n";
346  s+="\t\t<outputCenter>"+inputCenter+"</outputCenter>\n";
347  s+="\t\t<inputChannel>"+getAnalogInputChannelNumber()+"</inputChannel>\n";
348  s+="\t\t<data>";
349  for(int i=0;i<data.size();i++){
350  s+=data.get(i).input;
351  if(i<data.size()-1)
352  s+=",";
353  }
354  s+= "</data>\n";
355  s+="\t</ServoOutputSequence>\n";
356  return s;
357  }
358 
364  public int getInputChannelNumber() {
365  if(input!= null)
366  return input.getChannel().getChannelNumber();
368  }
369 
375  public void setData(int[] data2) {
376  data = new ArrayList<MapData>();
377  for(int i=0;i<data2.length;i++){
378  data.add(new MapData(data2[i]));
379  }
380  }
381 
385  public void startTest() {
386  System.out.println("Starting test for output: "+getChannelNumber());
387  initInput();
388  directTester = new Tester();
389  directTester.start();
390  }
391 
395  public void stopTest() {
396  if(directTester!=null) {
397  directTester.kill();
398  if(input != null)
399  input.removeAnalogInputListener(this);
400  }
401  directTester=null;
402  }
403 
409  public boolean isTesting() {
410  return directTester!=null;
411  }
412 
418  public void setInterval(double interval) {
419  this.interval = interval;
420  }
421 
427  public double getInterval() {
428  return interval;
429  }
430 
437  try{
438 
439  }catch( Exception e){}
440  this.inputValue = inputValue;
441  }
442 
448  public int getCurrentTargetValue() {
449  return inputValue;
450  }
451 
455  public void flush(){
457  output.flush();
458  }
459 
466  //System.out.println("Setting analog input number: "+analogInputChannelNumber);
467  this.analogInputChannelNumber = analogInputChannelNumber;
468  }
469 
476  if(input != null)
477  return input.getChannel().getChannelNumber();
479  }
480 
486  public void setRecording(boolean recording) {
487  this.recording = recording;
488  }
489 
493  private class Tester extends Thread {
494 
496  private boolean running=true;
497 
498  /* (non-Javadoc)
499  * @see java.lang.Thread#run()
500  */
501  public void run() {
502  //System.out.println("Starting Test");
503  while(running) {
504  flush();
505  try {Thread.sleep((long) getInterval());} catch (InterruptedException e) {}
506  }
507  //System.out.println("Test Done");
508  }
509 
513  public void kill() {
514  running=false;
515  }
516  }
517 
518  /* (non-Javadoc)
519  * @see com.neuronrobotics.sdk.dyio.sequencer.ISchedulerListener#onReset()
520  */
521  @Override
522  public void onReset() {
523  // TODO Auto-generated method stub
524 
525  }
526 
527  /* (non-Javadoc)
528  * @see com.neuronrobotics.sdk.dyio.sequencer.ISchedulerListener#onPlay()
529  */
530  @Override
531  public void onPlay() {
532  // TODO Auto-generated method stub
533 
534  }
535 
536  /* (non-Javadoc)
537  * @see com.neuronrobotics.sdk.dyio.sequencer.ISchedulerListener#onPause()
538  */
539  @Override
540  public void onPause() {
541  // TODO Auto-generated method stub
542 
543  }
544 
550  public int getCurrentValue() {
551  return currentValue;
552  }
553 
559  public void setCurrentValue(int currentValue) {
560  this.currentValue = currentValue;
561  }
562 
568  public void sync(int loopTime) {
569 
570  getOutput().SetPosition(getCurrentValue(), loopTime);
571  }
572 
573 }
DyIOChannel getChannel(int channel)
Definition: DyIO.java:160
void removeIServoPositionUpdateListener(IServoPositionUpdateListener l)
void addIServoPositionUpdateListener(IServoPositionUpdateListener l)