CreateBaseDevice.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Creativa77.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package com.github.c77.base_driver.create;
00018 
00023 import com.github.c77.base_driver.BaseDevice;
00024 import com.github.c77.base_driver.BaseStatus;
00025 import com.github.c77.base_driver.OdometryStatus;
00026 import com.hoho.android.usbserial.driver.UsbSerialDriver;
00027 
00028 import org.apache.commons.logging.Log;
00029 import org.apache.commons.logging.LogFactory;
00030 
00031 import java.io.IOException;
00032 
00033 public class CreateBaseDevice implements BaseDevice {
00034 
00035     private Double createBaseDiameter = 0.33; //In meters
00036     private Double createWheelRadius = 0.04;  //In meters
00037     private static UsbSerialDriver device = null;
00038 
00039     private final byte SetBaudrate57600 = (byte) 5;
00040     // iRobot Create low level commands.
00041     private final byte Start = (byte) 128;
00042     private final byte FullMode = (byte) 132;
00043     private final byte DirectDrive = (byte) 145;
00044 
00045     private class BaseSpeedValues {
00046         private final int rightWheelVel;
00047         private final int leftWheelVel;
00048 
00049         private BaseSpeedValues(int rightWheelVel, int leftWheelVel) {
00050             this.rightWheelVel = rightWheelVel;
00051             this.leftWheelVel = leftWheelVel;
00052         }
00053 
00054         public int getLeftWheelVel() {
00055             return leftWheelVel;
00056         }
00057 
00058         public int getRightWheelVel() {
00059             return rightWheelVel;
00060         }
00061     }
00062 
00063     private static final Log log = LogFactory.getLog(CreateBaseDevice.class);
00064 
00065     public CreateBaseDevice(UsbSerialDriver driver) {
00066         device = driver;
00067         try {
00068             device.open();
00069             device.setParameters(9600, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
00070         } catch (IOException e) {
00071             log.info("Error setting up device: " + e.getMessage(), e);
00072             e.printStackTrace();
00073             try {
00074                 device.close();
00075             } catch (IOException e1) {
00076                 e1.printStackTrace();
00077             }
00078             device = null;
00079         }
00080     }
00081 
00082     public void initialize() {
00083         byte[] baud = new byte[] {SetBaudrate57600};
00084         write(baud); // configure base baudrate
00085         byte[] setup = new byte[] {Start, FullMode};
00086         write(setup); // configure create base.
00087     }
00088 
00089     public void move(double linearVelX, double angVelZ) {
00090         BaseSpeedValues speeds = twistToBase(linearVelX, angVelZ);
00091         sendMovementPackage(speeds);
00092     }
00093 
00094     private BaseSpeedValues twistToBase(double linearVelX, double angVelZ) {
00095         int rightWheelVel;
00096         int leftWheelVel;
00097 
00098         if (linearVelX < 0) {
00099             angVelZ = -angVelZ;
00100         }
00101 
00102         double normalizer = (angVelZ * createBaseDiameter) / createWheelRadius;
00103         rightWheelVel = (int) (0.5 * (((2 * linearVelX) / createWheelRadius) - normalizer));
00104         leftWheelVel = (int) (0.5 * (((2 * linearVelX) / createWheelRadius) + normalizer));
00105 
00106         if ((leftWheelVel * rightWheelVel) < 0) {
00107             leftWheelVel = leftWheelVel * 2;
00108             rightWheelVel = rightWheelVel * 2;
00109         }
00110         return new BaseSpeedValues(leftWheelVel, rightWheelVel);
00111     }
00112 
00113     private void sendMovementPackage(BaseSpeedValues speeds) {
00114         int leftWheelVel = speeds.getLeftWheelVel();
00115         int rightWheelVel = speeds.getRightWheelVel();
00116 
00117         byte[] velocitiesArray= new byte[] {
00118                 DirectDrive,
00119                 (byte) leftWheelVel,
00120                 (byte) (leftWheelVel >> 8),
00121                 (byte) rightWheelVel,
00122                 (byte) (rightWheelVel >> 8)
00123         };
00124 
00125         write(velocitiesArray);
00126     }
00127 
00128     private void write(byte[] command) {
00129         log.info("Writing a command to Device.");
00130         try {
00131             device.write(command, 1000);
00132         } catch (IOException e) {
00133             e.printStackTrace();
00134         }
00135     }
00136 
00141     public BaseStatus getBaseStatus() {
00142         return new BaseStatus();
00143     }
00144 
00145     @Override
00146     public OdometryStatus getOdometryStatus() {
00147         return null;
00148     }
00149 
00150 }


android_base_controller
Author(s):
autogenerated on Fri Aug 28 2015 10:04:47