BowlerKernel
Create.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.addons.irobot;
16 
17 import java.util.ArrayList;
18 
19 import com.neuronrobotics.sdk.common.ByteList;
20 import com.neuronrobotics.sdk.common.Log;
21 import com.neuronrobotics.sdk.dyio.DyIOChannelEvent;
22 import com.neuronrobotics.sdk.dyio.peripherals.DyIOPeripheralException;
23 import com.neuronrobotics.sdk.dyio.peripherals.IUARTStreamListener;
24 import com.neuronrobotics.sdk.dyio.peripherals.UARTChannel;
25 // TODO: Auto-generated Javadoc
26 
30 public class Create implements IUARTStreamListener{
31 
33  private short myAngle;
34 
36  private short myDistance;
37  //private boolean packetRecieved;
38 
40  private short previousRad=0;
41 
43  private short previousVel=0;
44 
47 
49  private byte []ledState={(byte) 139,0,0,0};
50 
52  private byte []sensor = new byte[26];
53 
56 
58  private ArrayList<ICreateSensorListener> listeners = new ArrayList<ICreateSensorListener>();
59 
65  public Create(UARTChannel chan){
66  channel= chan;
67  channel.setUARTBaudrate(57600);
69  byte [] init = {(byte) 128,(byte) 131};
70  try {
71  send(init);
72  } catch (Exception e) {
73  throw new DyIOPeripheralException("Failed to initialize iRobot Create");
74  }
76  }
77 
81  public void setFullMode(){
82  byte [] init = {(byte) 128,(byte) 132};
83  try {
84  send(init);
85  } catch (Exception e) {
86  throw new DyIOPeripheralException("Failed to initialize iRobot Create in Full Mode");
87  }
88  }
89 
93  public void InitCreate(){
94  byte [] init = {(byte) 128,(byte) 131};
95  try {
96  send(init);
97  } catch (Exception e) {
98  throw new DyIOPeripheralException("Failed to initialize iRobot Create in Full Mode");
99  }
100  }
101 
107  public void InitCreateBlocking(int timeout){
108  byte [] init = {(byte) 128,(byte) 131};
109  while(true){
110  try {
111  Thread.sleep(500);
112  Log.info("Initializing Create..");
113  send(init);
114  break;
115  } catch (Exception e) {
116  throw new DyIOPeripheralException("Failed to initialize iRobot Create in Full Mode");
117  }
118  }
119  }
120 
132  public void move(short velocity,short radius){
133  if((velocity == previousVel) &&(radius == previousRad) )
134  return;
135  previousRad = radius;
136  previousVel = velocity;
137  byte[] drv = {(byte) 137,(byte) (velocity>>8),(byte) velocity,(byte) (radius>>8),(byte) radius};
138  try {
139  send(drv);
140  } catch (Exception e) {
141  throw new DyIOPeripheralException("Failed to send drive command");
142  }
143  }
144 
150  public void driveStraight(short distance){
151  driveStraight((short)0x7fff,distance);
152  }
158  public void driveStraight(short velocity,short distance){
159  if((distance > 0 && velocity < 0) || (distance < 0 && velocity > 0)){
160  velocity *= -1;
161  }
162  byte[] drv = {(byte) 152,13,
163  (byte) 137,(byte) (velocity>>8),(byte) velocity,(byte) (128),0,
164  (byte) 156,(byte) (distance>>8),(byte) (distance),
165  (byte) 137,0,0,0,0,
166  (byte) 153};
167  try {
168  send(drv);
169  } catch (Exception e) {
170  throw new DyIOPeripheralException("Failed to send drive command");
171 
172  }
173  }
174 
184  public boolean driveStraightBlocking(int timeout,short velocity,short distance) throws InterruptedException{
185  int tries=0;
186  Log.info("Driving...");
187  try {
188  driveStraight(velocity,distance);
189  } catch (Exception e) {
190  Log.error(e.toString());
191  }
192  myDistance=0;
193  while (myDistance==0) {
194  tries++;
195  // try to get a good sensor reading. break on sucsess
196  while(true){
197  try{
198  Log.info("Trying to get a good sensor reading");
199  Thread.sleep(1000);
200  this.requestSensors();
201  break;
202  } catch (Exception e) {
203  Log.error(e.toString());
204  }
205  }
206  Log.info("Driving Attempt "+Integer.toBinaryString(tries));
207  if (tries==10){
208  Log.info("Re attempting to send command");
209  tries=0;
210  try{
211  driveStraight(velocity,distance);
212  } catch (Exception e) {
213  Log.error(e.toString());
214  }
215  Thread.sleep(1000);
216  }
217  }
218 
219  return false;
220 
221  }
222 
228  public void turn(short angle){
229  turn((short)0x7fff,angle);
230  }
236  public void turn(short velocity,short angle){
237  if(velocity<0){
238  velocity *= -1;
239  }
240  short turn = (short) ((angle>0)? 1:-1);
241  byte[] drv = {(byte) 152,13,
242  (byte) 137,(byte) (velocity>>8),(byte) velocity,(byte) (turn>>8),(byte) (turn),
243  (byte) 157,(byte) (angle>>8),(byte) (angle),
244  (byte) 137,0,0,0,0,
245  (byte) 153};
246  //Log.info("Turning: "+new ByteList(drv));
247  try {
248  send(drv);
249  } catch (Exception e) {
250  throw new DyIOPeripheralException("Failed to send drive command");
251  }
252  }
253 
263  public boolean turnBlocking(int timeout,short velocity,short angle) throws InterruptedException{
264  int tries=0;
265  Log.info("Driving...");
266  try {
267  turn(velocity,angle);
268  } catch (Exception e) {
269  Log.error(e.toString());
270  }
271  myAngle=0;
272  while (myAngle==0) {
273  tries++;
274  // try to get a good sensor reading. break on sucsess
275  while(true){
276  try{
277  Log.info("Trying to get a good sensor reading");
278  Thread.sleep(1000);
279  this.requestSensors();
280  break;
281  } catch (Exception e) {
282  Log.error(e.toString());
283  }
284  Thread.sleep(1000);
285  }
286  Log.info("Driving Attempt "+Integer.toBinaryString(tries));
287  if (tries==10){
288  Log.info("Re attempting to send command");
289  tries=0;
290  try{
291  turn(velocity,angle);
292  } catch (Exception e) {
293  Log.error(e.toString());
294  }
295  Thread.sleep(1000);
296  }
297  }
298 
299  return false;
300  }
301 
308  public void setLed(boolean max,boolean spot){
309  int led=0;
310  led+=max? (1<<1):0;
311  led+=spot? (1<<3):0;
312  ledState[1]=(byte)led;
313  setLed();
314  }
315 
322  public void setStatusLed(int color,int intensity){
323  ledState[2]=(byte)color;
324  ledState[3]=(byte)intensity;
325  setLed();
326  }
327 
331  public void requestSensors(){
333  }
334 
341  senReq=req;
342  byte []all={(byte)142,req.getValue()};
343  try {
344  send(all);
345  } catch (Exception e) {
346  throw new DyIOPeripheralException("Failed to send sensor request");
347  }
348  }
349 
353  private void setLed(){
354  try {
355  send(ledState);
356  } catch (Exception e) {
357  // ignore
358  }
359  }
360 
367  private void send(byte[]b) throws Exception{
368  channel.sendBytes(new ByteList(b));
369  }
370 
371  /* (non-Javadoc)
372  * @see com.neuronrobotics.sdk.dyio.IChannelEventListener#onChannelEvent(com.neuronrobotics.sdk.dyio.DyIOChannelEvent)
373  */
375  try {
376  Thread.sleep(100);
377  } catch (InterruptedException e1) {
378  // ignore
379  }
380  byte [] in = channel.getBytes();
381  switch(senReq){
382  case ALL:
383  if(in.length==26){
384  //Log.info("Got ALL packet from Create");
385  for (int i=0;i<26;i++){
386  sensor[i]=in[i];
387  }
388  }else{
389  Log.error("malformed ALL packet from Create"+new ByteList(in));
390  return;
391  }
392  break;
393  case IO:
394  if(in.length==10){
395  //Log.info("Got IO packet from Create");
396  for (int i=0;i<10;i++){
397  sensor[i]=in[i];
398  }
399  }else{
400  Log.error("malformed IO packet from Create"+new ByteList(in));
401  return;
402  }
403 
404  break;
405  case DRIVE:
406  if(in.length==6){
407  //Log.info("Got DRIVE packet from Create");
408  for (int i=0;i<6;i++){
409  sensor[i+10]=in[i];
410  }
411  }else{
412  Log.error("malformed DRIVE packet from Create"+new ByteList(in));
413  return;
414  }
415  break;
416  case BATTERY:
417  if(in.length==10){
418  //Log.info("Got BATTERY packet from Create");
419  for (int i=0;i<10;i++){
420  sensor[i+16]=in[i];
421  }
422  }else{
423  Log.error("malformed BATTERY packet from Create"+new ByteList(in));
424  return;
425  }
426  break;
427  case NONE:
428  Log.error("Create sent packet upstream unexpectedally: "+new ByteList(in));
429  }
432  }
433 
438  listeners.clear();
439  }
440 
448  if(!listeners.contains(l)) {
449  return;
450  }
451 
452  listeners.remove(l);
453  }
454 
462  if(listeners.contains(l)) {
463  return;
464  }
465  listeners.add(l);
466  }
467 
473  private void fireCreatePacket(CreateSensors packet) {
474  // for the blocking drive funcs
475  myAngle=packet.angle;
476  myDistance = packet.distance;
477  //packetRecieved = true;
478 
480  l.onCreateSensor(packet);
481  }
482  }
483 
484 }
void removeCreateSensorListener(ICreateSensorListener l)
Definition: Create.java:447
void requestSensors(CreateSensorRequest req)
Definition: Create.java:340
void move(short velocity, short radius)
Definition: Create.java:132
void driveStraight(short velocity, short distance)
Definition: Create.java:158
void fireCreatePacket(CreateSensors packet)
Definition: Create.java:473
void addCreateSensorListener(ICreateSensorListener l)
Definition: Create.java:461
void setStatusLed(int color, int intensity)
Definition: Create.java:322
boolean driveStraightBlocking(int timeout, short velocity, short distance)
Definition: Create.java:184
ArrayList< ICreateSensorListener > listeners
Definition: Create.java:58
void onChannelEvent(DyIOChannelEvent e)
Definition: Create.java:374
boolean turnBlocking(int timeout, short velocity, short angle)
Definition: Create.java:263
void setLed(boolean max, boolean spot)
Definition: Create.java:308
void turn(short velocity, short angle)
Definition: Create.java:236
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92