public class RobotSimulator extends MovablePhysObject implements iRobotCommands
iRobotCreate
or some subclass of it)
that runs it is through the two files inStream
and outStream
which simulates
a serial I/O port. It uses real time as simulation time -- it has activation cycles of
15 milliseconds, just like the real robot. Modifier and Type | Class and Description |
---|---|
private class |
RobotSimulator.Sd
This is an internal class that is used to store all the information about the
various sensors of the robot.
|
iRobotCommands.Sensor
Modifier and Type | Field and Description |
---|---|
(package private) double |
angle
The current angle (in radians) that the robot is facing
|
(package private) double |
angleAccumulator
the current angle (in radians) the robot as traveled since the last time
the angle
was read.
|
(package private) double |
distanceAccumulator
the current distance (in mm) the robot as traveled since the last time the distance
was read.
|
(package private) boolean |
exit
Indicated that the robot's thread should exit if set true
|
(package private) boolean |
ffButtonOn
whether the advance (fast forward) button light is on or off
|
FileInputStream |
inStream
The input stream that this class communicates through with the agent that runs it.
|
FileOutputStream |
outStream
The output stream that this class communicates through with the agent that runs it.
|
(package private) boolean |
playButtonOn
whether the play button light is on or off
|
(package private) int |
powerButtonColor
The current color of the power button.
|
private byte[] |
queueBuf
A buffer to "put back" bytes into the input stream.
|
(package private) short |
radius
The current radius in which the robot is traveling (-2000mm to +2000mm)
|
(package private) byte[] |
script
This holds the last script loaded to the agent via the Script command.
|
(package private) double |
sensorOffset
The X,Y offset from the X,Y of the bounding box to the start of the wallSensorScanArea
The +1 is to make sure that we don't touch the robot itself
|
private RobotSimulator.Sd[] |
sensors
The array of sensors describing each of the robot's 43 sensor codes.
|
(package private) short |
velocity
The current velocity of the robot (-500mm to +500mm)
|
(package private) short |
waitAngleDegrees
This holds the angle value (in degrees) from the currently active WaitAngle
command.
|
(package private) short |
waitDistance
This holds the distance value (in mm) from the currently active WaitDistance
command.
|
(package private) boolean |
waitMode
This is true iff the robot is currently in one of it's WAIT modes (WaitTime,
WaitDistance, or WaitAngle).
|
(package private) Area |
wallSensorScanArea
The area the wall sensor detects object within.
|
env, twoPI
BytesPerPacket, C_Baud, C_Demo, C_DemoCover, C_DemoCoverAndDock, C_DemoSpot, C_Drive, C_DriveDirect, C_LEDs, C_ModeFull, C_ModeOff, C_ModeSafe, C_PlayScript, C_PlaySong, C_QueryList, C_Script, C_Sensors, C_ShowScript, C_Song, C_Start, C_WaitAngle, C_WaitDistance, C_WaitTime, chassisDiameter, chassisRadius, MAX_DEMO_NUMBER, MAX_RADIUS, MAX_SONG_LENGTH, MAX_SONG_NUMBER, MAX_VELOCITY, MIN_DEMO_NUMBER, MIN_RADIUS, MIN_SONG_NUMBER, MIN_VELOCITY, P_Straight, P_Straight2, P_TurnInPlaceClockwise, P_TurnInPlaceCounterClockwise, S_Angle, S_BatteryCapacity, S_BatteryCharge, S_BatteryTemperature, S_BumpsAndWheelDrops, S_Buttons, S_ChargingSourcesAvailable, S_ChargingState, S_CliffFrontLeft, S_CliffFrontLeftSignal, S_CliffFrontRight, S_CliffFrontRightSignal, S_CliffLeft, S_CliffLeftSignal, S_CliffRight, S_CliffRightSignal, S_Current, S_Distance, S_Group0, S_Group1, S_Group2, S_Group3, S_Group4, S_Group5, S_Group6, S_IRByte, S_LeftVelocity, S_NumberOfStreamPackets, S_OIMode, S_Overcurrents, S_Radius, S_RightVelocity, S_SongNumber, S_SongPlaying, S_UserAnalogInput, S_UserDigitalInputs, S_Velocity, S_VirtualWall, S_Voltage, S_Wall, S_WallSignal, sensorNames, SONG_bump, SONG_cliff, SONG_init, SONG_overcurrent, SONG_quit, SP_OIMode_Full, SP_OIMode_Off, SP_OIMode_Passive, SP_OIMode_Safe
Constructor and Description |
---|
RobotSimulator(String name,
String inFile,
String outFile)
Constructs and new instance with a name, and an input file and output file for communicating
with the controlling agent.
|
RobotSimulator(String name,
String inFile,
String outFile,
long x,
long y,
long angle) |
Modifier and Type | Method and Description |
---|---|
static String |
bytes2String(byte[] bytes)
Utility method to convert a byte array to a String representation.
|
protected void |
clearBumpAndOCAndCliff()
Utility method to clear the bump, overcurrent, and cliff sensors
|
protected short |
computeCliffSignal(Shape s,
PhysObject obj)
Calculates the cliff signal, ranging from 0 to 1000.
|
void |
draw(Graphics2D g)
Draw the robot, detailing it's state more than just the super's method would.
|
protected void |
encounteredCliff(PhysObject cliff)
This method will be called at least once during an update cycle to indicate
and encounter with a cliff object: once
per encounter, and if there is no encounter, once only with a null parameter.
|
protected void |
encounteredImovableObstical(Area impact)
This method is called to let this object know that it cannot move when
it encounters an immovable object.
|
protected void |
encounteredVWall(PhysObject vWall)
This method will be called at least once during an update cycle to indicate
and encounter with a virtual wall: once
per encounter, and if there is no encounter, once only with a null parameter.
|
double |
getAngle() |
private int |
getBytes(byte[] outbuf,
int startIndex,
byte[] inbuf)
Utility method to fetch outbuf.length bytes from inbuf.
|
(package private) Shape[] |
getCliffSensorShapes()
Return the 4 cliff sensor shapes.
|
(package private) static Shape |
getInitShape(long x,
long y)
Finds a shape for this instance.
|
private int |
getShorts(short[] outbuf,
int startIndex,
byte[] inbuf)
Similar to
getBytes(byte[], int, byte[]) , but interprets the inbuf
as short values instead of bytes. |
protected void |
handleInputData(byte[] buf)
This method is called whenever there in input for the robot.
|
private void |
incAngle(Double angle)
increment the angle of the robot, keeping it between 0 and 2*PI
We actually have to keep the angle with the opposite sign since
positive angles turn the actual robot counter-clockwise which is
opposite to what happens in this java world.
|
(package private) static Shape |
makeShape(double x,
double y)
Creates a robot shape a the parameter coordinates.
|
void |
move(long interval)
Calculate (and update) the robot's new position since the last move() was called.
|
private void |
pause(long time)
Utility method to wait for a certain length of time.
|
protected void |
println(String s)
Utility method to print debug data.
|
protected void |
processMouseEvent(MouseEvent e) |
(package private) void |
resetLocation()
Utility method to randomly reset the robot's position in the coordinate system
if necessary.
|
(package private) void |
returnSensorData(byte packet)
Write the appropriate sensor data out to
outStream . |
void |
run()
This is what the thread runs.
|
protected boolean |
safetyCheck(double distance)
Call stop() and return true if any of the safety conditions are violated: any cliff
sensor is triggered, any wheel drops, or the unit is charging.
|
protected void |
setBumpAndOD(double x,
double y)
Set the bump and overcurrent sensors assuming we have bumped into a corporeal
object at the parameter coordinates.
|
(package private) void |
setCliff(PhysObject obj)
Set the cliff sensors according to the parameter
PhysObject . |
protected void |
stop()
Utility method to stop the robot
|
move
allowMovement, compareTo, equals, exit, getAngleDeg, getColor, getName, getShape, getShowHeader, getWidth, getX, getY, getZOrder, hasExited, isAnimated, isCliff, isCorporeal, isLabeled, isMovable, isPaint, isPersistent, isVWall, moveTo, normalizeAngle, processMouseMoveEvent, replace, replace, setAnimated, setCliff, setColor, setCorporeal, setLabeled, setMovable, setName, setPaint, setPersistent, setShape, setVWall, setZOrder, show, toString
boolean exit
public FileInputStream inStream
public FileOutputStream outStream
Area wallSensorScanArea
double sensorOffset
short velocity
short radius
double angle
double distanceAccumulator
double angleAccumulator
int powerButtonColor
boolean playButtonOn
boolean ffButtonOn
boolean waitMode
short waitAngleDegrees
Short.MAX_VALUE
when there is no WaitAngle command
active.short waitDistance
Short.MAX_VALUE
when there is no WaitDistance command
active.byte[] script
private final RobotSimulator.Sd[] sensors
RobotSimulator.Sd
private byte[] queueBuf
public RobotSimulator(String name, String inFile, String outFile) throws FileNotFoundException, InstantiationException, Exception
name
- The name of the agent -- will be used to access this instance from the environment, so should be unique. Will also label the shape in the interface.inFile
- The input file written to by the controlling agent.outFile
- The output file to be read by the controlling agent.FileNotFoundException
- if either the input file or output file can't be found or instantiatedInstantiationException
- if the robot can't be instantiated in the environment (no room?)Exception
public RobotSimulator(String name, String inFile, String outFile, long x, long y, long angle) throws FileNotFoundException, InstantiationException, Exception
static Shape getInitShape(long x, long y)
static Shape makeShape(double x, double y)
x
- The x coordinatey
- The y coordinatevoid resetLocation()
public void run()
inStream
for new data, and calling handleInputData(byte[])
if
there is any. It's cycle time is 15 milliseconds.run
in interface Runnable
Runnable.run()
private void pause(long time)
time
- The time (in milliseconds) to waitpublic void move(long interval)
interval
- the time in milliseconds since the last move() was calledprotected void encounteredImovableObstical(Area impact)
MovablePhysObject
encounteredImovableObstical
in class MovablePhysObject
impact
- The area describing the impact with the other objectprotected void encounteredCliff(PhysObject cliff)
MovablePhysObject
encounteredCliff
in class MovablePhysObject
cliff
- Either one of the cliff objects encountered or null if none was
encountered during the update cycle.protected void encounteredVWall(PhysObject vWall)
MovablePhysObject
encounteredVWall
in class MovablePhysObject
vWall
- Either one of the cliff objects encountered or null if none was
encountered during the update cycle.protected boolean safetyCheck(double distance)
protected void clearBumpAndOCAndCliff()
protected short computeCliffSignal(Shape s, PhysObject obj)
s
- the shape of the cliff sensorobj
- void setCliff(PhysObject obj)
PhysObject
. Do not clear any
current cliff sensors.obj
- an object that is assumed to be a cliff or paint objectShape[] getCliffSensorShapes()
protected void setBumpAndOD(double x, double y)
x
- the x coordinate of a bump.y
- the x coordinate of a bump.protected void stop()
public void draw(Graphics2D g)
draw
in class PhysObject
g
- the graphics surface to draw to.PhysObject.draw(java.awt.Graphics2D)
protected void println(String s)
s
- a String to printprotected void handleInputData(byte[] buf)
buf
- the data available for processing from the robot.public static String bytes2String(byte[] bytes)
bytes
- the bytes of format as a Stringprivate int getBytes(byte[] outbuf, int startIndex, byte[] inbuf)
outbuf
- the buf to place the fetches bytes instartIndex
- the index to start in inbufinbuf
- the input bufferprivate int getShorts(short[] outbuf, int startIndex, byte[] inbuf)
getBytes(byte[], int, byte[])
, but interprets the inbuf
as short values instead of bytes.outbuf
- the buf to place the fetches shorts instartIndex
- the index to start in inbufinbuf
- the input bufferprivate void incAngle(Double angle)
angle
- the angle to setvoid returnSensorData(byte packet)
outStream
.packet
- the code (group or individual) of the packet to write out.public double getAngle()
getAngle
in class PhysObject
protected void processMouseEvent(MouseEvent e)
processMouseEvent
in class PhysObject