orbotix.robot.base
Class DriveControl

java.lang.Object
  extended by orbotix.robot.base.DriveControl
All Implemented Interfaces:
android.hardware.SensorEventListener, java.util.Observer

public class DriveControl
extends java.lang.Object
implements android.hardware.SensorEventListener, java.util.Observer

Singleton to manage driving a ball in touch(joystick) or with accelerometer data.

This class simplifies driving a robot by setting up input sensors and maintaining driving state through a simple interface.


Nested Class Summary
static interface DriveControl.ConnectionListener
          Interface to update a listener of connection events.
 
Field Summary
static DriveControl INSTANCE
          Singleton instance of the class
static int JOY_STICK
          Constant for driving with a touch(joystick) algorithm
static int RC
          Constant for driving with an RC Car like interface
static int TILT
          Constant for driving with accelerometer data
 
Method Summary
 void connectRobot()
          Start game play.
 void disableDirectionalDrive(android.content.Context context)
          Disables any active automatic heading correction based on the device's sensors.
 void disconnectRobot()
          Stop game play.
 DriveAlgorithm driveAlgorithm()
          Accessor to the DriveAlgorithm object used for driving the robot with the accelerometer or a touch based joy stick.
 void driveJoyStick(double x, double y)
          Method to update the position of a joy stick control and motion of a robot based on the new position.
 void driveRc(double x, double y)
          Method to update the motion of a robot based on the new gas and steering values.
 void enableDirectionalDrive(android.content.Context context)
          Attempts to enable the automatic heading correction based on the device's sensors.
 android.content.Context getBroadcastContext()
          Accessor to the context the framework uses for broadcasting events on.
 Robot getRobot()
          The Robot object that is being controlled.
 RobotControl getRobotControl()
          The RobotControl object that is being used to drive the device.
 RobotProvider getRobotProvider()
          Accessor to the RobotProvider object.
 boolean hasRobotControl()
          Test if a robot is under control.
 java.lang.Boolean isDriving()
          Test if the game is in full stop.
 void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy)
          Required by the SensorEventListener interface, however this method does nothing.
 void onSensorChanged(android.hardware.SensorEvent event)
          Implementation of the SensorEventListener interface for receiving accelerometer data when driving in tilt mode.
 void setBroadcastContext(android.content.Context context)
          Method for setting a context which is used by the framework to send broadcast messages for events.
 void setConnectionListener(DriveControl.ConnectionListener listener)
          Method to register a ConnectionListener object to listen for connection events.
 void setJoyStickPadSize(double width, double height)
          Method to set the area used by a joy stick pad, which is used when creating the JoyStickDriveAlgorithm.
 void setOnConvertListener(DriveAlgorithm.OnConvertListener listener)
          Method to register a DriveAlgorithm.OnConvertListener object that is passed to DriveAlgorithm objects that the DriveControll object creates.
 void setSpeedScale(double scale)
          Method to set the speed scale which limits the maximum speed the device can travel at.
 void startDriving(android.content.Context context, int driveMode)
          Prepares to start driving a robot.
 void stopDriving()
          Stop driving a robot by stopping the motor and accelerometer updates.
 void update(java.util.Observable observable, java.lang.Object data)
          Watches for changes in the robot state.
 void updateCalibratedHeading()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JOY_STICK

public static final int JOY_STICK
Constant for driving with a touch(joystick) algorithm

See Also:
Constant Field Values

TILT

public static final int TILT
Constant for driving with accelerometer data

See Also:
Constant Field Values

RC

public static final int RC
Constant for driving with an RC Car like interface

See Also:
Constant Field Values

INSTANCE

public static final DriveControl INSTANCE
Singleton instance of the class

Method Detail

setConnectionListener

public void setConnectionListener(DriveControl.ConnectionListener listener)
Method to register a ConnectionListener object to listen for connection events.

Parameters:
listener - The ConnectionListener object.

setBroadcastContext

public void setBroadcastContext(android.content.Context context)
Method for setting a context which is used by the framework to send broadcast messages for events.

Parameters:
context - The context to broadcast events.

getBroadcastContext

public android.content.Context getBroadcastContext()
Accessor to the context the framework uses for broadcasting events on.

Returns:
The context being used to broadcast action intents.

hasRobotControl

public boolean hasRobotControl()
Test if a robot is under control.


getRobotProvider

public RobotProvider getRobotProvider()
Accessor to the RobotProvider object.

Returns:
The RobotProvider object.

getRobotControl

public RobotControl getRobotControl()
The RobotControl object that is being used to drive the device.

Returns:
A RobotControl object.

getRobot

public Robot getRobot()
The Robot object that is being controlled.

Returns:
A Robot object.

setOnConvertListener

public void setOnConvertListener(DriveAlgorithm.OnConvertListener listener)
Method to register a DriveAlgorithm.OnConvertListener object that is passed to DriveAlgorithm objects that the DriveControll object creates.

Parameters:
listener - A DriveAlgorithm.OnConvertListener object.

setJoyStickPadSize

public void setJoyStickPadSize(double width,
                               double height)
Method to set the area used by a joy stick pad, which is used when creating the JoyStickDriveAlgorithm. The values are used to calculate heading and speed based on the position of the joy stick in the area defined by these values.

Parameters:
width - The width of the joy stick area.
height - The height of the joy stick area.

setSpeedScale

public void setSpeedScale(double scale)
Method to set the speed scale which limits the maximum speed the device can travel at.

Parameters:
scale - A value from 0.0 to 1.0 which is multiplied with the driving speed.

driveAlgorithm

public DriveAlgorithm driveAlgorithm()
Accessor to the DriveAlgorithm object used for driving the robot with the accelerometer or a touch based joy stick.

Returns:
A DriveAlgorithm object used to convert input coordinates to roll command coordinates.

connectRobot

public void connectRobot()
Start game play. Connects to the controlled robots. The method does not block, but connecting can take awhile, so set a callback to be informed that the setup is complete.


disconnectRobot

public void disconnectRobot()
Stop game play. Stops driving the robots and disconnects from them.


startDriving

public void startDriving(android.content.Context context,
                         int driveMode)
Prepares to start driving a robot. This includes starting to monitor sensors, like the accelerometer.

Parameters:
context - The context for obtaining input resources from.

enableDirectionalDrive

public void enableDirectionalDrive(android.content.Context context)
Attempts to enable the automatic heading correction based on the device's sensors. The gyroscope will be used alone if it is available. If the gyroscope is not available this will attempt to use the accelerometer along with the magnetometer to obtain orientations using SensorManager.getOrientation(float[], float[]). If there is an accelerometer but no magnetometer then a static heading will be set for the magnetometer data which will allow for rough heading changes. As of June 2011, almost every Android device released with a magnetometer also has at least an accelerometer, so a compass-only method of heading correction is not currently implemented. If nothing is present, automatic heading correction will be turned off.

Parameters:
context - the context from which to get the sensors.

disableDirectionalDrive

public void disableDirectionalDrive(android.content.Context context)
Disables any active automatic heading correction based on the device's sensors. Directional drive is guaranteed to be off after calling this method.

Parameters:
context - the context from which to get the sensors.

driveJoyStick

public void driveJoyStick(double x,
                          double y)
Method to update the position of a joy stick control and motion of a robot based on the new position.

Parameters:
x - The x value inside a joy stick control's view area.
y - The y value inside a joy stick control's view area.

driveRc

public void driveRc(double x,
                    double y)
Method to update the motion of a robot based on the new gas and steering values.

Parameters:
x - The gas value from 0.0 to 1.0.
y - The steering value from -1.0 to 1.0.

stopDriving

public void stopDriving()
Stop driving a robot by stopping the motor and accelerometer updates.


isDriving

public java.lang.Boolean isDriving()
Test if the game is in full stop.


onSensorChanged

public void onSensorChanged(android.hardware.SensorEvent event)
Implementation of the SensorEventListener interface for receiving accelerometer data when driving in tilt mode.

Specified by:
onSensorChanged in interface android.hardware.SensorEventListener

updateCalibratedHeading

public void updateCalibratedHeading()

onAccuracyChanged

public void onAccuracyChanged(android.hardware.Sensor sensor,
                              int accuracy)
Required by the SensorEventListener interface, however this method does nothing.

Specified by:
onAccuracyChanged in interface android.hardware.SensorEventListener

update

public void update(java.util.Observable observable,
                   java.lang.Object data)
Watches for changes in the robot state. DON'T CALL THIS FROM CLIENT CODE.

Specified by:
update in interface java.util.Observer
Parameters:
observable - A robot whose state has changed.
data - not used