dLife Home Page

dlife.robot.player
Class PlayerRobot

java.lang.Object
  extended by java.util.Observable
      extended by dlife.robot.Robot
          extended by dlife.robot.player.PlayerRobot
Direct Known Subclasses:
HemissonPlayer, Khepera2Player, Khepera3Player, PioneerPlayer

public abstract class PlayerRobot
extends Robot

This is an abstract class to represent a PlayerRobot. Any implementation of this class should make calls to player using the protected playerClient field. Any class that decides to override the startUp or shutDown methods should make a call to super. When a PlayerRobot is constructed, fields are populated with Player interfaces if they exist. If they do not exist, they will be initialized to null. Note that once a PlayerRobot has been shut down using the shutDown method, it can no longer be started up again. Instead, a new PlayerRobot must be constructed. In order to use a PlayerRobot in the PlayerSimulation class, the static getPlayerConfigBody MUST be overwritten. An exception will be thrown otherwise. In addition, any devices for the PlayerSimulation class should be made static and final using a PlayerDeviceConfiguration object.

Version:
Feb. 15, 2010
Author:
Russell Toris, Dickinson College

Field Summary
static PlayerDeviceConfiguration PLAYER_POSITION_CONFIG
          The configuration data for player for the position2d interface.
static int TIMEOUT
          The time allowed to wait for player to start in milliseconds.
 
Constructor Summary
PlayerRobot()
          Displays a PlayerConnectionDialog to help connect to player.
PlayerRobot(File dlpFile, int port, boolean noise)
          Connect to a PlayerRobot with the given DLP configuration file, port to attempt to connect to, and if we should load and use the noise profile for this robot.
PlayerRobot(File dlpFile, int port, boolean noise, String playerCmd)
          Connect to a PlayerRobot with the given DLP configuration file, port to attempt to connect to, if we should load and use the noise profile for this robot, and absolute player command..
PlayerRobot(javaclient2.PlayerClient pc)
          Create a PlayerRobot using the given PlayerClient which should already be connected to Player.
PlayerRobot(PlayerSimulation sim, int port, boolean noise)
          Connect to a PlayerRobot using the configuration contained in the PlayerSimuation object.
PlayerRobot(PlayerSimulation sim, String playerCmd, int port, boolean noise)
          Connect to a PlayerRobot using the configuration contained in the PlayerSimuation object.
PlayerRobot(String address, int port)
          Attempts to connect to a robot in player with the given address and port.
 
Method Summary
 void addDevice(Device newDevice)
          The Device is first checked to make sure it is a valid Device for the PlayerRobot.
 String getAddress()
          Get the address Player is running on
protected  float getAngle()
          Returns the position's angle in radians relative to the x-axis or wherever the robot was facing when resetPositionData was last called.
protected  int[] getGripperData()
          Attempt to get the gripper data for the GripperInterface if it exists.
 javaclient2.PlayerClient getPlayerClient()
          Get a reference to the PlayerClient.
static String[] getPlayerConfigBody()
          This method must be overridden or a PlayerConfigurationDataException is thrown.
static PlayerRobotIconInfo getPlayerSimulationBuilderIcon()
          This method must be overridden or a PlayerConfigurationDataException is thrown.
 int getPort()
          Get the port this robot is connected to.
protected  float getRotation()
          Return the rotation speed in radians per second.
protected  float[] getSonarValues(int numSensors)
          Attempt to get the sonar data for the SonarInterface with the given number of sensors.
protected  float getTranslation()
          Return the translation speed in meters per second.
protected  float getXPos()
          Returns the position's x value in the units set in setPositionUnits relative to the center of the map or wherever resetPositionData was last called.
protected  float getYPos()
          Returns the position's y value in the units set in setPositionUnits relative to the center of the map or wherever resetPositionData was last called.
protected  boolean hasGripper()
          Returns true if a gripper device was found on this PlayerRobot on the Player server.
protected abstract  boolean isValidDevice(Device newDevice)
          Should return if the device is valid and supported for this PlayerRobot.
protected  void resetPositionData()
          Reset the position information for this robot.
protected  void setGripper(int cmd, int arg)
          Set the gripper for this robot if it exists.
protected  void setPositionData(float x, float y, float theta)
          Set the position information for this robot.
protected  void setPositionUnits(int unit)
          Set the units for which a the position data from Player will be returned.
protected  void setSonarUnits(int numSensors, int unit)
          Set the units for which a particular SonarInterface will return its readings in.
protected  void setSpeed(float speed, float turnrate)
          Set the wheel speeds for this robot.
 void shutDown()
          Closes the connection to the Player server and also marks the robot as not alive.
 void startUp()
          Marks the robot as alive.
 
Methods inherited from class dlife.robot.Robot
freeze, getDevices, getRunTime, isAlive, removeDevice, sendMessage, setAlive
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PLAYER_POSITION_CONFIG

public static final PlayerDeviceConfiguration PLAYER_POSITION_CONFIG
The configuration data for player for the position2d interface. This should be passed to the addSensorToRobot method of a PlayerSimulation class if noise is desired for the position data.


TIMEOUT

public static final int TIMEOUT
The time allowed to wait for player to start in milliseconds. If the time is exceed, it will stop trying.

See Also:
Constant Field Values
Constructor Detail

PlayerRobot

public PlayerRobot()
Displays a PlayerConnectionDialog to help connect to player. If the user does not cancel, a connection will be attempted. If the connection fails, an exception is thrown.

Throws:
RobotCreationCanceledException - if the user cancels the creation of the robot via the dialog box.
RobotCommunicationException - thrown if there was a problem communicating with Player during creation of the robot
UnknownHostException - thrown if the host name cannot be resolved

PlayerRobot

public PlayerRobot(String address,
                   int port)
            throws UnknownHostException
Attempts to connect to a robot in player with the given address and port. This constructor assumes Player is running.

Parameters:
address - the address of the player server
port - the port of the robot
Throws:
UnknownHostException - thrown if the host name cannot be resolved
RobotCommunicationException - thrown if there was a problem communicating with Player during creation of the robot

PlayerRobot

public PlayerRobot(javaclient2.PlayerClient pc)
Create a PlayerRobot using the given PlayerClient which should already be connected to Player. Note that this PlayerClient's runThreaded method should NOT be called prior to passing it into this constructor. This constructor is included primarily for testing purposes.

Parameters:
pc - the PlayerClient for this robot
Throws:
RobotCommunicationException - thrown if there was a problem communicating with Player during creation of the robot

PlayerRobot

public PlayerRobot(PlayerSimulation sim,
                   int port,
                   boolean noise)
Connect to a PlayerRobot using the configuration contained in the PlayerSimuation object. This configuration will be saved to a temporary location and used to load player. The PlayerRobot will attempt to connect to the port provided and attempt to load noise if 'noise' is true. This will attempt to find a valid player command on your system.

Parameters:
sim - the PlayerSimulation used to setup player
port - the port to connect to
noise - if noise should be used
Throws:
RobotCommunicationException - thrown if there was a problem communicating with Player during creation of the robot

PlayerRobot

public PlayerRobot(PlayerSimulation sim,
                   String playerCmd,
                   int port,
                   boolean noise)
Connect to a PlayerRobot using the configuration contained in the PlayerSimuation object. This configuration will be saved to a temporary location and used to load player. The PlayerRobot will attempt to connect to the port provided and attempt to load noise if 'noise' is true. This will use the absolute Player command provided.

Parameters:
sim - the PlayerSimulation used to setup player
playerCmd - the absolute system command to start player
port - the port to connect to
noise - if noise should be used
Throws:
RobotCommunicationException - thrown if there was a problem communicating with Player during creation of the robot

PlayerRobot

public PlayerRobot(File dlpFile,
                   int port,
                   boolean noise)
Connect to a PlayerRobot with the given DLP configuration file, port to attempt to connect to, and if we should load and use the noise profile for this robot. The DLP file is assumed to live in the same directory as all other player configuration files for this simulation. A valid player command will be searched for on the system in three likely locations.

Parameters:
dlpFile - the DLP file for this simulation
port - the port of the robot to connect to in Player
noise - if a noise profile should be loaded and used
Throws:
RobotCommunicationException - thrown if there was a problem communicating with Player during creation of the robot

PlayerRobot

public PlayerRobot(File dlpFile,
                   int port,
                   boolean noise,
                   String playerCmd)
Connect to a PlayerRobot with the given DLP configuration file, port to attempt to connect to, if we should load and use the noise profile for this robot, and absolute player command.. The DLP file is assumed to live in the same directory as all other player configuration files for this simulation.

Parameters:
dlpFile - the DLP file for this simulation
port - the port of the robot to connect to in Player
noise - if a noise profile should be loaded and used
playerCmd - the absolute system command to start player
Throws:
RobotCommunicationException - thrown if there was a problem communicating with Player during creation of the robot
Method Detail

getPort

public int getPort()
Get the port this robot is connected to.

Returns:
the port this robot is connected to

getAddress

public String getAddress()
Get the address Player is running on

Returns:
the address Player is running on

getPlayerClient

public javaclient2.PlayerClient getPlayerClient()
Get a reference to the PlayerClient. This is only used in a few test classes.

Returns:
a reference to the PlayerClient

isValidDevice

protected abstract boolean isValidDevice(Device newDevice)
Should return if the device is valid and supported for this PlayerRobot. For example, a Khepera3SonarSensor is allowed but a Khepera3BatterySensor is not. This will also check if the device was found on the Player server. This method is called by the addSensor method to confirm that the device can be added.

Parameters:
newDevice - the Device to check
Returns:
true if newDevice is a valid device for the robot and false if not.

addDevice

public void addDevice(Device newDevice)
The Device is first checked to make sure it is a valid Device for the PlayerRobot. If the sensor is invalid, an exception will be thrown. If it is valid, it is passed along to Robot.addEffector(newEffector). All implementations of PseudoDevice will be accepted as they do not require communication with the PlayerRobot.

Overrides:
addDevice in class Robot
Parameters:
newDevice - the Device to add to this PlayerRobot.
Throws:
InvalidPlayerDeviceException - thrown if the sensor is not valid for this PlayerRobot

startUp

public void startUp()
Marks the robot as alive. Any method that overrides this should make a call to super at some point. Once this robot has been shut down, it can no longer be restarted. Instead a new PlayerRobt must be constructed.

Overrides:
startUp in class Robot

shutDown

public void shutDown()
Closes the connection to the Player server and also marks the robot as not alive. Any method that overrides this should make a call to super at some point. Once this robot has been shut down, it can no longer be restarted. Instead a new PlayerRobt must be constructed.

Overrides:
shutDown in class Robot

getSonarValues

protected float[] getSonarValues(int numSensors)
Attempt to get the sonar data for the SonarInterface with the given number of sensors. If a SonarInterface is not found on Player with the given number of sensors, null is returned. Values are typically available every 100ms. If the data is not available at the time of the call, cached values are returned. The length of the array of sonar sensors is the only way to determine which SonarInterface to read from, thus, only one SonarInterface may exist for any given length of sonar sensors per robot.

Parameters:
numSensors - the number of sensors in the sonar array we want to read from
Returns:
the sonar values for the given sonar array
Throws:
RobotCommunicationException - thrown if we loose communication with Player

setSonarUnits

protected void setSonarUnits(int numSensors,
                             int unit)
Set the units for which a particular SonarInterface will return its readings in. The default will be meters. Units can be found in the PlayerConstants class. If a SonarInterface with the provided number of sensors does not exist, no effect will be seen.

Parameters:
numSensors - the number of sensors in the sonar array we want to read from
unit - a unit constant found in the PlayerConstants class
Throws:
RobotCommunicationException - thrown if we loose communication with Player

setPositionUnits

protected void setPositionUnits(int unit)
Set the units for which a the position data from Player will be returned. The default will be meters. Units can be found in the PlayerConstants class.

Parameters:
unit - a unit constant found in the PlayerConstants class
Throws:
RobotCommunicationException - thrown if we loose communication with Player

resetPositionData

protected void resetPositionData()
Reset the position information for this robot.

Throws:
RobotCommunicationException - thrown if we loose communication with Player

setPositionData

protected void setPositionData(float x,
                               float y,
                               float theta)
Set the position information for this robot.

Parameters:
x - the x coordinate in the units set in setPositionUnits
y - the y coordinate in the units set in setPositionUnits
theta - the angle in radians
Throws:
RobotCommunicationException - thrown if we loose communication with Player

getXPos

protected float getXPos()
Returns the position's x value in the units set in setPositionUnits relative to the center of the map or wherever resetPositionData was last called. Values are typically available every 100ms. If the data is not available at the time of the call, cached values are returned.

Returns:
the robot's x value

getYPos

protected float getYPos()
Returns the position's y value in the units set in setPositionUnits relative to the center of the map or wherever resetPositionData was last called. Values are typically available every 100ms. If the data is not available at the time of the call, cached values are returned.

Returns:
the robot's y value

getAngle

protected float getAngle()
Returns the position's angle in radians relative to the x-axis or wherever the robot was facing when resetPositionData was last called. Values are typically available every 100ms. If the data is not available at the time of the call, cached values are returned.

Returns:
the robot's angle

setSpeed

protected void setSpeed(float speed,
                        float turnrate)
Set the wheel speeds for this robot. Units for speed should be in the same units that were set in setPositionUnits (or default to meters). That is, speed should be in units/sec and turnrate in rad/sec.

Parameters:
speed - the translation speed in units/sec
turnrate - the turnrate in rad/sec
Throws:
RobotCommunicationException - thrown if we loose communication with Player

getTranslation

protected float getTranslation()
Return the translation speed in meters per second. It is important to note that regardless of the units set in setPositionUnits, the speed will return in meters per second. Values are typically available every 100ms. If the data is not available at the time of the call, cached values are returned.

Returns:
the translation speed in meters per second
Throws:
RobotCommunicationException - thrown if we loose communication with Player

getRotation

protected float getRotation()
Return the rotation speed in radians per second. It is important to note that regardless of the units set in setPositionUnits, the speed will return in meters per second. Values are typically available every 100ms. If the data is not available at the time of the call, cached values are returned.

Returns:
the rotation speed in radians per second
Throws:
RobotCommunicationException - thrown if we loose communication with Player

hasGripper

protected boolean hasGripper()
Returns true if a gripper device was found on this PlayerRobot on the Player server.

Returns:
if a gripper was found on this PlayerRobot

setGripper

protected void setGripper(int cmd,
                          int arg)
Set the gripper for this robot if it exists. More information on the command and argument parameters can be found in the JavaDoc in the javaclient2.GripperInterface class.

Parameters:
cmd - the gripper command to send
arg - the argument for the gripper command
Throws:
RobotCommunicationException - thrown if we loose communication with Player or if the gripper does not exist.

getGripperData

protected int[] getGripperData()
Attempt to get the gripper data for the GripperInterface if it exists. Values are typically available every 100ms. If the data is not available at the time of the call, cached values are returned. Data is returned as an array with the [state, beams] data. More information on these two values can be found in the javaclient2.structures.gripper.PlayerGripperData class.

Returns:
the gripper state and beam data for this robot
Throws:
RobotCommunicationException - thrown if we loose communication with Player or the gripper does not exist

getPlayerConfigBody

public static String[] getPlayerConfigBody()
This method must be overridden or a PlayerConfigurationDataException is thrown. This should return the main body configuration for player without any devices. It should end with a single ")" on a line by itself. Each line should be an element in the array. See Khepera3Player for an example.

Returns:
the main configuration for the robots body.
Throws:
PlayerConfigurationDataException - if this method was not overridden

getPlayerSimulationBuilderIcon

public static PlayerRobotIconInfo getPlayerSimulationBuilderIcon()
This method must be overridden or a PlayerConfigurationDataException is thrown. This should return a PlayerRobotIconInfo containing the icon of this robot to be used in a PlayerSimulationBuilder.

Returns:
a PlayerRobotIconInfo containing the icon of this robot to be used in a PlayerSimulationBuilder.
Throws:
PlayerConfigurationDataException - if this method was not overridden

dLife Home Page