diff --git a/IOIOTruck/project.properties b/IOIOTruck/project.properties index 9b8a819..21bd5c2 100644 --- a/IOIOTruck/project.properties +++ b/IOIOTruck/project.properties @@ -9,7 +9,7 @@ # Project target. target=Google Inc.:Google APIs:15 -android.library.reference.3=../LocationLib -android.library.reference.4=../../git/ioiolib -android.library.reference.1=../ExceptionHandlerLib -android.library.reference.2=../Joystick +android.library.reference.3=../../../workspace/LocationLib +android.library.reference.4=../../ioiolib/IOIOLib +android.library.reference.1=../../../workspace/ExceptionHandlerLib +android.library.reference.2=../../../workspace/Joystick diff --git a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckManager.java b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckManager.java index 4e96d0a..20c1932 100644 --- a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckManager.java +++ b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckManager.java @@ -6,13 +6,12 @@ */ package com.TwentyCodes.android.IOIOTruck; +import ioio.lib.api.DigitalOutput; import ioio.lib.api.IOIO; import ioio.lib.api.PwmOutput; import ioio.lib.api.exception.ConnectionLostException; import android.app.Activity; -import android.util.Log; -import com.TwentyCodes.android.IOIOTruck.R; import com.TwentyCodes.android.ioio.IOIOManager; /** @@ -32,14 +31,14 @@ public class IOIOTruckManager extends IOIOManager { private static final String TAG = "IOIOTruckThread"; private Activity mActivity; private IOIOTruckThreadListener mListener; - private PwmOutput mDrive; - private PwmOutput mSteer; - private PwmOutput mShifter; - private int mDriveValue; - private int mSteerValue; + private int mLeftDriveValue; + private int mRightDriveValue; private int mShifterValue; private boolean mStatLedValue; - private boolean isTank = false; + private TB6612FNGMotorDriver mLeftMotor; + private TB6612FNGMotorDriver mRightMotor; + private PwmOutput mShifter; + private DigitalOutput mMotorDriverStandBy; /** * Creates a new IOIOTruckThread @@ -54,21 +53,25 @@ public class IOIOTruckManager extends IOIOManager { updateLog(R.string.wait_ioio); } - private void arcadeDrive() { + /** + * Drives the robot based on the drive and steering values + * @throws ConnectionLostException + * @author ricky barrette + */ + private void arcadeDrive() throws ConnectionLostException { int left, right; - left = (mDriveValue + mSteerValue) /2; - right = (mDriveValue - mSteerValue) / 2; + left = (mLeftDriveValue + mRightDriveValue) /2; + right = (mLeftDriveValue - mRightDriveValue) / 2; - mDriveValue = left; - mSteerValue = right + 1500; - Log.d(TAG, "left: "+ mDriveValue + " right: "+ mSteerValue); + mLeftMotor.setSpeed(left); + mRightMotor.setSpeed(right + 1500); } /** * @return the mDriveValue */ public synchronized int getDriveValue() { - return mDriveValue; + return mLeftDriveValue; } /** @@ -82,7 +85,7 @@ public class IOIOTruckManager extends IOIOManager { * @return the mSteerValue */ public synchronized int getSteerValue() { - return mSteerValue; + return mRightDriveValue; } /** @@ -102,9 +105,12 @@ public class IOIOTruckManager extends IOIOManager { public void onConnected(IOIO ioio) throws ConnectionLostException { updateLog(R.string.ioio_connected); - mDrive = ioio.openPwmOutput(IOIOTruckValues.DRIVE_PORT, IOIOTruckValues.RC_PWM_FRQ); - mSteer = ioio.openPwmOutput(IOIOTruckValues.STEER_PORT, IOIOTruckValues.RC_PWM_FRQ); mShifter = ioio.openPwmOutput(IOIOTruckValues.SHIFTER_PORT, IOIOTruckValues.RC_PWM_FRQ); + mLeftMotor = new TB6612FNGMotorDriver(ioio, IOIOTruckValues.MOTOR_DRIVER_PWMA, IOIOTruckValues.MOTOR_DRIVER_A1, IOIOTruckValues.MOTOR_DRIVER_A2); + mRightMotor = new TB6612FNGMotorDriver(ioio, IOIOTruckValues.MOTOR_DRIVER_PWMB, IOIOTruckValues.MOTOR_DRIVER_B1, IOIOTruckValues.MOTOR_DRIVER_B2); + + //enable the motor driver + mMotorDriverStandBy = ioio.openDigitalOutput(IOIOTruckValues.MOTOR_DRIVER_STANDBY, true); } /** @@ -126,33 +132,30 @@ public class IOIOTruckManager extends IOIOManager { this.setStatLedEnabled(mStatLedValue); - if(isTank){ - arcadeDrive(); - mSteer.setPulseWidth(mSteerValue); - mDrive.setPulseWidth(mDriveValue); - } - else { - /* - * if the autonomous routine is running - * then drive the truck - * else stop the truck - */ - if(mStatLedValue) - mDrive.setPulseWidth(mDriveValue); - else - mDrive.setPulseWidth(IOIOTruckValues.DRIVE_STOP); - - } - mSteer.setPulseWidth(mSteerValue); mShifter.setPulseWidth(mShifterValue); + + mMotorDriverStandBy.write(mStatLedValue); + /* + * if the autonomous routine is running + * then drive the truck + * else stop the truck + */ + if(mStatLedValue){ + arcadeDrive(); + } + else{ + mLeftMotor.setSpeed(0); + mRightMotor.setSpeed(0); + } + } /** * @param mDriveValue the mDriveValue to set */ public synchronized void setDriveValue(int mDriveValue) { - this.mDriveValue = mDriveValue; + this.mLeftDriveValue = mDriveValue; } /** @@ -173,17 +176,9 @@ public class IOIOTruckManager extends IOIOManager { * @param mSteerValue the mSteerValue to set */ public synchronized void setSteerValue(int mSteerValue) { - this.mSteerValue = mSteerValue; + this.mRightDriveValue = mSteerValue; } - /** - * @param isTankDrive - * @author ricky barrette - */ - public synchronized void setTankDrive(boolean isTankDrive){ - isTank = isTankDrive; - } - /** * updates the log listener in the UI thread * @param resId @@ -197,7 +192,5 @@ public class IOIOTruckManager extends IOIOManager { mListener.onLogUpdate(mActivity.getString(resId)); } }); - } - -} +} \ No newline at end of file diff --git a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckValues.java b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckValues.java index fe172b4..50e96ff 100644 --- a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckValues.java +++ b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/IOIOTruckValues.java @@ -14,11 +14,46 @@ import com.TwentyCodes.android.ioio.IOIOValues; */ public class IOIOTruckValues extends IOIOValues { + /** + * IOIO port for the motor driver pwma port + */ + public static final int MOTOR_DRIVER_PWMA = 4; + + /** + * IOIO port for the motor driver a2 port + */ + public static final int MOTOR_DRIVER_A2 = 5; + + /** + * IOIO port for the motor driver a1 port + */ + public static final int MOTOR_DRIVER_A1 = 6; + + /** + * IOIO port for the motor driver stby port + */ + public static final int MOTOR_DRIVER_STANDBY = 7; + + /** + * IOIO port for the motor driver b1 port + */ + public static final int MOTOR_DRIVER_B1 = 8; + + /** + * IOIO port for the motor driver b2 port + */ + public static final int MOTOR_DRIVER_B2 = 9; + + /** + * IOIO port for the motor driver pwmb port + */ + public final static int MOTOR_DRIVER_PWMB = 10; + /** * stop the truck * PWM value */ - public static final int DRIVE_STOP = 1500; + public static final int DRIVE_STOP = RC_PWM_PULSE_WIDTH_NEUTRAL; /** * drive the truck forward @@ -33,56 +68,24 @@ public class IOIOTruckValues extends IOIOValues { public static final int DRIVE_REVERSE = 1700; /** - * IOIO port to drive the speed controller + * IOIO port for the "shifter" */ - public static final int DRIVE_PORT = 3; - - /** - * IOIO port to drive the shifter servo - */ - public static final int SHIFTER_PORT = 4; - - /** - * IOIO port to drive the steering servo - */ - public static final int STEER_PORT = 5; + public static final int SHIFTER_PORT = 3; /** - * steers the truck straight - * PWM value + * Steering value to drive the robot straight */ - public static final int STEER_STRAIGHT = 1500; - + public static final int STEER_STRAIGHT = RC_PWM_PULSE_WIDTH_NEUTRAL; + /** - * steers the truck left - * PWM value + * Steering value to drive the robot right */ - public static final int STEER_LEFT = 1400; - + public static final int STEER_RIGHT = 1700; + /** - * steers the truck right - * PWM value + * Steering value to drive the robot left */ - public static final int STEER_RIGHT = 1600; - - /** - * - * shifts truck into first - * PWM value - */ - public static final int SHIFT_FIRST = 1500; - - /** - * shifts the truck into second - * TODO verify value - * PWM value - */ - public static final int SHIFT_SECOND = 1000; - - /** - * shifts the truck into 3rd - * PWM value - */ - public static final int SHIFT_THRID = 2000; + public static final int STEER_LEFT = 1300; + } diff --git a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TB6612FNGMotorDriver.java b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TB6612FNGMotorDriver.java new file mode 100644 index 0000000..a216a69 --- /dev/null +++ b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TB6612FNGMotorDriver.java @@ -0,0 +1,86 @@ +/** + * IOIOMotorDriver.java + * @date Jan 25, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + */ +package com.TwentyCodes.android.IOIOTruck; + +import android.util.Log; +import ioio.lib.api.DigitalOutput; +import ioio.lib.api.IOIO; +import ioio.lib.api.PwmOutput; +import ioio.lib.api.exception.ConnectionLostException; + +/** + * This class is used to drive one of the (2) motors via the TB6612FNG motor driver from sparkfun. + * @author ricky barrette + */ +public class TB6612FNGMotorDriver { + + private static final String TAG = "TB6612FNGMotorDriver"; + + private PwmOutput mPWM; + private DigitalOutput mIn1; + private DigitalOutput mIn2; + + private final int PWM_FREQUENCY = 100000; + + /** + * Creates a new IOIOMotorDiver. + * @param ioio that is connected + * @param pwmPin IOIO port number for the motor driver pwm pin + * @param in1Pin IOIO port number for the motor driver in1 pin + * @param in2Pin IOIO port number for the motor driver in2 pin + * @throws ConnectionLostException + * @author ricky barrette + */ + public TB6612FNGMotorDriver(IOIO ioio, int pwmPin, int in1Pin, int in2Pin) throws ConnectionLostException { + mPWM = ioio.openPwmOutput(pwmPin, PWM_FREQUENCY); + mPWM.setDutyCycle(0); + mIn1 = ioio.openDigitalOutput(in1Pin, false); + mIn2 = ioio.openDigitalOutput(in2Pin, false); + } + + /** + * Sets the speed of this motor + * @param speed +/- 0.0 - 1.0 + * @throws ConnectionLostException + * @author ricky barrette + */ + public void setSpeed(float speed) throws ConnectionLostException { + if(Debug.DEBUG) + Log.v(TAG, "setSpeed(float): "+speed); + if(speed == 0) { + //stop + mIn1.write(false); + mIn2.write(false); + } else if (speed < 0) { + //reverse + mIn1.write(true); + mIn2.write(false); + } else if (speed > 0) { + //forward + mIn1.write(false); + mIn2.write(true); + } + mPWM.setDutyCycle(Math.abs(speed)); + } + + /** + * Sets the speed of this motor + * @param speed 1000 - 2000 + * @throws ConnectionLostException + * @author ricky barrette + */ + public void setSpeed(int speed) throws ConnectionLostException { + if(Debug.DEBUG) + Log.v(TAG, "setSpeed(int): "+speed); + if(speed == 1500) + setSpeed(0f); + else if(speed < 1500) + setSpeed((((float) speed - 1000) / 1000) - 1f); + else + setSpeed(((float) speed - 1000) / 1000); + } +} \ No newline at end of file diff --git a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TestActivity.java b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TestActivity.java index 7a6750c..9acaee1 100755 --- a/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TestActivity.java +++ b/IOIOTruck/src/com/TwentyCodes/android/IOIOTruck/TestActivity.java @@ -24,6 +24,7 @@ import com.TwentyCodes.android.exception.ExceptionHandler; * @author ricky barrette */ public class TestActivity extends Activity implements JoystickMovedListener, OnSeekBarChangeListener, OnCheckedChangeListener, IOIOTruckThreadListener { + private static final String TAG = "TestActivity"; private TextView mStatusTextView; private TextView mDriveTextView; private TextView mSteerTextView; @@ -86,7 +87,6 @@ public class TestActivity extends Activity implements JoystickMovedListener, OnS try { mIOIOManager.abort(); } catch (InterruptedException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } @@ -135,9 +135,10 @@ public class TestActivity extends Activity implements JoystickMovedListener, OnS */ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - int shifter = progress + 1000; - mIOIOManager.setShifterValue(shifter); - mShifterTextView.setText(getString(R.string.shifter)+shifter); + float shifter = progress + 1000; + mIOIOManager.setShifterValue(progress + 1000); + + mShifterTextView.setText(getString(R.string.shifter)+ shifter); } @Override