dLife Home Page

dlife.robot.player
Class PlayerSimulation

java.lang.Object
  extended by dlife.sys.SerializationBase
      extended by dlife.robot.player.PlayerSimulation
All Implemented Interfaces:
Serializable

public class PlayerSimulation
extends SerializationBase

A PlayerSimulation object is a helpful object to create a Player simulation and all necessary configuration files. The simulation may be modified using the provided methods and you can save the configuration files. Robots are written in alphabetical order by name, not by order added. This is important when figuring out by hand which robot has which port. To run the server, run the following command replacing 'name' with the name giving in the constructor: # player name.cfg In addition, a PlayerSever object can assist with launching the server from dLife. Moreover, a file called 'name.dlp' (where name is replaced by the simulation name) is created which can be loaded and read by a PlayerConnection object.

Version:
January 14, 2010
Author:
Russell Toris, Dickinson College
See Also:
Serialized Form

Nested Class Summary
 class PlayerSimulation.PlayerSimulationPuck
          A PlayerSimulationPuck is used to keep track of the information a puck has in a player simulation.
 class PlayerSimulation.PlayerSimulationRobot
          A PlayerSimulationRobot is used to keep track of the information a robot has in a player simulation.
 
Constructor Summary
PlayerSimulation(String name, double width, double height, File map)
          Creates a PlayerSimulation object with the given simulation name, width, height, and map file.
PlayerSimulation(String name, double width, double height, File map, int startingPort)
          Creates a PlayerSimulation object with the given simulation name, width, height, and map file.
 
Method Summary
 void addDeviceToRobot(String robotName, PlayerDeviceConfiguration device)
          Adds a PlayerDeviceConfiguration to a given robot.
 void addDeviceToRobot(String robotName, PlayerDeviceConfiguration device, NoiseProfile<?> np)
          Adds a PlayerDeviceConfiguration to a given robot with a given NoiseProfile.
 void addPuck(String name, double xPos, double yPos, double dir, Color color, double width, double height)
          Add a puck to the simulation with the given name.
 void addRobot(String name, Class<? extends PlayerRobot> robotClass, double xPos, double yPos, double dir, Color color)
          Add a robot to the simulation with the given parameters.
 boolean disabled()
          If the GUI will be disabled
 void disableGUI(boolean disable)
          If the GUI will be disabled for this simulation.
 double getHeight()
          Gets the height in meters of the map for this simulation.
 BufferedImage getMapImage()
          Gets a copy of the map image.
 String getMapImageName()
          Gets the name of the map image file.
 String getName()
          Get the name of the simulation.
 int getNumPucks()
          Gets the total number of pucks in the simulation.
 int getNumRobots()
          Get the number of robots in the current simulation.
 ArrayList<String> getPuckNames()
          Get the names of all the puck types in this PlayerSimulation.
 ArrayList<PlayerSimulation.PlayerSimulationPuck> getPucks(String name)
          Get a list of all PlayerSimulationPuck objects for the puck given puck type.
 PlayerSimulation.PlayerSimulationRobot getRobot(String name)
          Get the PlayerSimulationRobot object of the robot for the robot with the given name.
 ArrayList<String> getRobotNames()
          Get the names of all the robots in this PlayerSimulation.
 int getStartingPort()
          Get the port which the robots start at in the .cfg file.
 double getWidth()
          Gets the width in meters of the map for this simulation.
 void removePuck(String name, double xPos, double yPos)
          Remove a specific puck with the given name, x position, and y position if it exits.
 void removeRobot(String name)
          Remove a robot with the given name if that name exists.
 void reset()
          Clear and remove all pucks and robots as well as reset the time interval.
 void saveConfiguration(String path)
          Write the simulation files to the given path.
 void setRealTime(boolean realTime)
          Set if the simulation should run in real time, or take 1 real-world millisecond to simulate the number of milliseconds in setSimTimeInterval (default of 100ms)
 void setSimTimeInterval(int interval)
          Set the simulation time update interval for the simulation.
 void setStartingPort(int newPort)
          Set the starting port to the given value.
 
Methods inherited from class dlife.sys.SerializationBase
read, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PlayerSimulation

public PlayerSimulation(String name,
                        double width,
                        double height,
                        File map)
Creates a PlayerSimulation object with the given simulation name, width, height, and map file. The starting port for the robots will be 7000 and increment with each additional robot. Name must be of the form [a-zA-Z][a-zA-Z\d]+.

Parameters:
name - the name of the simulation. This will be used to name all files.
width - the width in meters of the map
height - the height in meters of the map
map - the file of the map image
Throws:
InvalidParameterException - if name is not of the form [a-zA-Z][a-zA-Z\d]+

PlayerSimulation

public PlayerSimulation(String name,
                        double width,
                        double height,
                        File map,
                        int startingPort)
Creates a PlayerSimulation object with the given simulation name, width, height, and map file. This constructor allows you to specify a starting port for the robots which will increment with each additional robot. Default is 7000. Port 6665 is a reserved port for use within dLife. Name must be of the form [a-zA-Z][a-zA-Z\d]+.

Parameters:
name - the name of the simulation. This will be used to name all files.
width - the width in meters of the map
height - the height in meters of the map
map - the file of the map image
Throws:
InvalidParameterException - if name is not of the form [a-zA-Z][a-zA-Z\d]+
Method Detail

getStartingPort

public int getStartingPort()
Get the port which the robots start at in the .cfg file.

Returns:
the port which the robots start at in the .cfg file.

setStartingPort

public void setStartingPort(int newPort)
Set the starting port to the given value. Port 6665 is a reserved port for use within dLife.

Parameters:
newPort - the new starting port

getWidth

public double getWidth()
Gets the width in meters of the map for this simulation.

Returns:
the width in meters of the map

getHeight

public double getHeight()
Gets the height in meters of the map for this simulation.

Returns:
the height in meters of the map

getMapImageName

public String getMapImageName()
Gets the name of the map image file.

Returns:
the name of the map image file

getMapImage

public BufferedImage getMapImage()
                          throws IOException
Gets a copy of the map image.

Returns:
a copy of the map image
Throws:
IOException - thrown if there is a problem reading the map image.

disableGUI

public void disableGUI(boolean disable)
If the GUI will be disabled for this simulation. The GUI will NOT be disabled by default.

Parameters:
disable - if the GUI will be disabled

disabled

public boolean disabled()
If the GUI will be disabled

Returns:
if the GUI will be disabled

getNumRobots

public int getNumRobots()
Get the number of robots in the current simulation.

Returns:
the number of robots in the current simulation

getNumPucks

public int getNumPucks()
Gets the total number of pucks in the simulation.

Returns:
the total number of pucks in the simulation

getName

public String getName()
Get the name of the simulation.

Returns:
the name of the simulation

addRobot

public void addRobot(String name,
                     Class<? extends PlayerRobot> robotClass,
                     double xPos,
                     double yPos,
                     double dir,
                     Color color)
Add a robot to the simulation with the given parameters. If the type does not match any of the predefined constants, an exception will be thrown. Color will be the closest match found in the rgb.txt file provided by stage. Ports are automatically assigned when the files are created. Adding a robot with the same name as a current robot will override the old robot. A standard robot includes just the base itself (as well as its differential drive effector). Any additional sensors/effectors should be added to the robot using the addDeviceToRobot method. Name must be of the form [a-zA-Z][a-zA-Z\d]+.

Parameters:
name - the name of the robot
robotClass - the class object of the player robot you will be using
xPos - the x-position (in meters)
yPos - the y-position (in meters)
dir - the direction (in degrees)
color - the color of the robot
Throws:
InvalidPlayerRobotTypeException - thrown if type does not match one of the predefined constants from this class
InvalidParameterException - if name is not of the form [a-zA-Z][a-zA-Z\d]+

getRobotNames

public ArrayList<String> getRobotNames()
Get the names of all the robots in this PlayerSimulation.

Returns:
the names of all the robots in this PlayerSimulation

getRobot

public PlayerSimulation.PlayerSimulationRobot getRobot(String name)
Get the PlayerSimulationRobot object of the robot for the robot with the given name. If that robot name does not exist, null is returned.

Parameters:
name - the name of the robot to get the PlayerSimulationRobot object of
Returns:
the PlayerSimulationRobot object of the robot or null if the name does not exist

addDeviceToRobot

public void addDeviceToRobot(String robotName,
                             PlayerDeviceConfiguration device)
Adds a PlayerDeviceConfiguration to a given robot. If the robot name does not exist, no changes will be made. The PlayerDeviceConfiguration object is defined in each PlayerRobot class.

Parameters:
robotName - the name of the robot to add the device to
device - the PlayerDeviceConfiguration provided from that robots PlayerRobot class.

addDeviceToRobot

public void addDeviceToRobot(String robotName,
                             PlayerDeviceConfiguration device,
                             NoiseProfile<?> np)
Adds a PlayerDeviceConfiguration to a given robot with a given NoiseProfile. If the robot name does not exist, no changes will be made. The NoiseProfile should have the same return type as defined in the PlayerDeviceConfiguration. This will be checked before being added. If the NoiseProfile is not compatible with the PlayerDeviceConfiguration, an IncompatibleNoiseProfileException is thrown. The PlayerDeviceConfiguration object is defined in each PlayerRobot class. NoiseProfiles can only be added to devices of type PlayerDeviceConfiguration.RANGER and PlayerDeviceConfiguration.POSITION2D.

Parameters:
robotName - the name of the robot to add the device to
device - the PlayerDeviceConfiguration provided from that robots PlayerRobot class.
np - the noise profile for this device

removeRobot

public void removeRobot(String name)
Remove a robot with the given name if that name exists.

Parameters:
name - the name of the robot to remove

addPuck

public void addPuck(String name,
                    double xPos,
                    double yPos,
                    double dir,
                    Color color,
                    double width,
                    double height)
Add a puck to the simulation with the given name. Pucks with the same name are stored together and should have the same dimensions. They may have different positions and colors. If pucks have the same name and different dimensions, only the dimensions of the first puck with that name will be used. Color will be the closest match found in the rgb.txt file provided by stage. Name must be of the form [a-zA-Z][a-zA-Z\d]+.

Parameters:
name - the name of the puck type
xPos - the x-position (in meters)
yPos - the y-position (in meters)
dir - the direction (in degrees)
color - the color
width - the width in the x direction in meters
height - the hight in the y direction in meters
Throws:
InvalidParameterException - if name is not of the form [a-zA-Z][a-zA-Z\d]+

getPuckNames

public ArrayList<String> getPuckNames()
Get the names of all the puck types in this PlayerSimulation.

Returns:
the names of all the puck types in this PlayerSimulation

getPucks

public ArrayList<PlayerSimulation.PlayerSimulationPuck> getPucks(String name)
Get a list of all PlayerSimulationPuck objects for the puck given puck type. If that puck type name does not exist, null is returned.

Parameters:
name - the name of the puck type to get the PlayerSimulationPuck object list of
Returns:
the PlayerSimulationPuck object list of the pucks or null if the puck type does not exist

removePuck

public void removePuck(String name,
                       double xPos,
                       double yPos)
Remove a specific puck with the given name, x position, and y position if it exits. If no such puck exists, no changes will be made.

Parameters:
name - name of the puck to remove
xPos - the x position of the puck
yPos - the y position of the puck

setSimTimeInterval

public void setSimTimeInterval(int interval)
Set the simulation time update interval for the simulation. This is the number of milliseconds between each update of the simulation information. The default time is 100 milliseconds.

Parameters:
interval -

setRealTime

public void setRealTime(boolean realTime)
Set if the simulation should run in real time, or take 1 real-world millisecond to simulate the number of milliseconds in setSimTimeInterval (default of 100ms)

Parameters:
realTime - if the simulation should run in real time

reset

public void reset()
Clear and remove all pucks and robots as well as reset the time interval.


saveConfiguration

public void saveConfiguration(String path)
                       throws IOException
Write the simulation files to the given path.

Parameters:
path - the path (not including the file name) where the files will be saved
Throws:
FileNotFoundException - thrown if it cannot write the files.
IOException

dLife Home Page