shady.sim
Class ShadySim

java.lang.Object
  extended by shady.common.ShadyCommon
      extended by shady.sim.ShadySim
All Implemented Interfaces:
ShadyCommonAPI, ShadySimAPI
Direct Known Subclasses:
MemberShadySim

public class ShadySim
extends ShadyCommon
implements ShadySimAPI

Shady simulator implementation.

Copyright (C) 2006 Marsette A. Vona, III

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Author:
Marsette (Marty) A. Vona, III

Nested Class Summary
protected  class ShadySim.BarrelMotion
          Specializes ShadySim.Motion for a barrel motion.
protected  class ShadySim.GripperMotion
          A motion that moves a gripper.
protected  class ShadySim.Motion
          Generic code for performing a motion in ShadySim.Motion.step()s.
protected  class ShadySim.RotateMotion
          A motion that rotates a barrel.
protected  class ShadySim.ShadeMotion
          A motion that rotates the shade.
protected  class ShadySim.SimultaneousMotion
          A simultaneous barrel rotation and shade motion.
 
Field Summary
private static java.lang.String cvsid
          CVS id.
protected  boolean discreteMode
          Whether we're in discrete mode.
static double SLEEP_GRANULARITY
          Minimum time to sleep the sim thread in wall-clock seconds.
static double STEP_TIME
          Simulated motion step time, in sim seconds.
 
Fields inherited from class shady.common.ShadyCommon
accountingInfo, accumulatedBarrelRotation, accumulatedShadeRotation, accumulating, BARREL_NAME, bodySegment, CC, clock, DEFAULT_GRIPPER_SLOP, DEFAULT_SHADE_OPEN_ANGLE, DEFAULT_SHADE_SLOP, DEFAULT_UPDATE_TIME, ENERGY, FAULT_NAME, GRIPPER_OPERATION_NAME, name, NOMINAL_BARREL_ROTATE_POWER, NOMINAL_BARREL_ROTATION_LIMIT, NOMINAL_BARREL_ROTATION_SPEED, NOMINAL_CENTER_TO_CENTER_DISTANCE, NOMINAL_GRIPPER_CLOSE_POWER, NOMINAL_GRIPPER_CLOSE_TIME, NOMINAL_GRIPPER_OPEN_POWER, NOMINAL_GRIPPER_OPEN_TIME, NOMINAL_GRIPPER_WIDTH, NOMINAL_MIN_ENERGY, NOMINAL_QUIESCENT_POWER, NOMINAL_SHADE_ROTATION_SPEED, NOMINAL_TOTAL_BATTERY_ENERGY, NOMINAL_TOTAL_LENGTH, pose, state, TIME, updateThread, wouldDuelOverride, wouldTiltOverride
 
Fields inherited from interface shady.common.ShadyCommonAPI
CLOSED_STATE, COLLISION, ESTOP, GRIPPER_CLOSE, GRIPPER_OPEN, GRIPPER_OPERATION_TARGET_STATE, IMPLEMENTATION_FAULT, LEFT, LOW_BATTERY, MOTION_LIMIT, OPEN_STATE, OVER_CURRENT, OVER_TEMP, RIGHT, TILT, TIMEOUT, UNALIGNED, UNKNOWN, WOULD_COLLIDE, WOULD_DUEL, WOULD_MOTION_LIMIT, WOULD_TILT
 
Constructor Summary
ShadySim()
          Create a ShadySim with no pose.
ShadySim(ShadyPose pose)
          Create a ShadySim with a given pose.
 
Method Summary
 void chargeBattery(double newBatteryLevel)
          Simulate charging the battery.
protected  double doSimultaneousMotion(double leftBarrelDT, double rightBarrelDT, double shadeDT)
          Implementation of ShadyCommon.simultaneousMotion(double, double, double).
 double getAccel()
          Get the simulation time acceleration factor.
 boolean getDiscreteMode()
          Check whether this ShadySim is in discrete mode.
protected  double getGripperTime(double ds)
          Get the time in seconds it will take to actuate a gripper.
protected  double getRotateBarrelTime(double dt)
          Get the time in seconds it will take to rotate a barrel differential amount dt in degrees at ShadyCommon.NOMINAL_BARREL_ROTATION_SPEED.
protected  double getRotateShadeTime(double dt)
          Get the time in seconds it will take to rotate the shade differential amount dt in degrees at ShadyCommon.NOMINAL_SHADE_ROTATION_SPEED.
 double getShadyTime()
          Get the acceltime in accelseconds relative to an arbitrary epoch.
protected  vona.time.Clock makeClock()
          Instantiate ShadyCommon.clock.
protected  ShadySim.GripperMotion makeGripperMotion(int barrel, double ds)
          Make a GripperMotion for reallySetGripper(int, double).
protected  ShadySim.RotateMotion makeRotateMotion(int barrel, double dt)
          Make a RotateMotion for reallyRotateBarrel(int, double).
 void pause()
          Pause this ShadySim.
protected  double reallyRotateBarrel(int barrel, double dt)
          Really rotates the barrel.
protected  double reallyRotateShade(double dt)
          Really rotates the shade.
protected  double reallySetGripper(int barrel, double targetState)
          Really actuates the gripper.
 void rechargeBattery()
          Convenience method to fully ShadySimAPI.chargeBattery(double).
 int reset()
          Attempt to reset the mechanism.
protected  void rotateBarrel(int barrel, double dt, double[] resource)
          Second part of ShadyCommon.rotateBarrel(int, double).
 int rotateShade(double dt)
          Incrementally rotate the shade.
 void setAccel(double accel)
          Set the simulation time acceleration factor.
 void setDiscreteMode(boolean discreteMode)
          Set whether this ShadySim is in discrete mode.
protected  void setGripper(int barrel, double targetState, double[] resource)
          Second part of ShadyCommon.setGripper(int, int).
 void shadySleep(double seconds)
          Sleep the current thread for the requested number of accelseconds.
 void stop(boolean estop)
          Stop all ongoing motion.
 void unPause()
          Un-ShadySimAPI.pause() this ShadySim.
 
Methods inherited from class shady.common.ShadyCommon
appendBarrelString, appendHTMLFontTag, barrelName, beginAccumulatingMotion, checkBarrel, checkEnergy, checkFault, checkGripperOperation, closeGripper, closeGripper, closeGripperPost, closeGripperPre, closeShade, disableTiltCheck, endAccumulatingMotion, faultName, getAccountingInfo, getAccountingInfo, getBarrelAngle, getBarrelRotationLimit, getCenterToCenterDistance, getClock, getCurrentPower, getCurrentTime, getFault, getFault, getFaultDetails, getGripperOperationTargetState, getGripperSlop, getGripperState, getGripperWidth, getGripQuality, getName, getNumActiveActuators, getPose, getPose, getShadeAngle, getShadeOpenAngle, getShadeSlop, getState, getState, getTotalLength, getUpdateTime, gripperOperationName, gripperStateName, gripperStateName, isAccumulatingMotion, isGripperAligned, isGripperAligned, isGripperClosed, isGripperOpen, isNameSet, isShadeClosed, isShadeOpen, makeAccountingInfo, makeState, makeUpdateThread, openGripper, openGripperPost, openGripperPre, openShade, otherBarrel, otherGripperOperation, pick, pick, reallyGetCurrentBatteryEnergy, reallyGetTotalBatteryEnergy, reestablishState, resetAccounting, rotateBarrel, rotateBarrelPost, rotateBarrelPre, rotateBarrelTo, setFault, setGripper, setGripperPost, setGripperPre, setName, setPose, setState, setWouldDuelOverride, setWouldTiltOverride, simultaneousMotion, toString, toString, toString, toString, update, updateState, wouldDuel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface shady.common.ShadyCommonAPI
beginAccumulatingMotion, closeGripper, closeGripper, closeShade, endAccumulatingMotion, faultName, getAccountingInfo, getAccountingInfo, getBarrelAngle, getBarrelRotationLimit, getCenterToCenterDistance, getClock, getCurrentPower, getFault, getFault, getFaultDetails, getGripperState, getGripperWidth, getGripQuality, getName, getNumActiveActuators, getPose, getPose, getShadeAngle, getShadeOpenAngle, getState, getState, getTotalLength, isAccumulatingMotion, isGripperAligned, isGripperAligned, isGripperClosed, isGripperOpen, isNameSet, isShadeClosed, isShadeOpen, openGripper, openShade, pick, resetAccounting, rotateBarrel, rotateBarrelTo, setName, setPose, setState, setWouldDuelOverride, setWouldTiltOverride, simultaneousMotion
 

Field Detail

cvsid

private static final java.lang.String cvsid

CVS id.

See Also:
Constant Field Values

STEP_TIME

public static final double STEP_TIME

Simulated motion step time, in sim seconds.

See Also:
Constant Field Values

SLEEP_GRANULARITY

public static final double SLEEP_GRANULARITY

Minimum time to sleep the sim thread in wall-clock seconds.

See Also:
Constant Field Values

discreteMode

protected boolean discreteMode

Whether we're in discrete mode.

See the class header doc for a description of discrete mode.

Constructor Detail

ShadySim

public ShadySim(ShadyPose pose)

Create a ShadySim with a given pose.

Parameters:
pose - the pose

ShadySim

public ShadySim()

Create a ShadySim with no pose.

Method Detail

reset

public int reset()

Attempt to reset the mechanism.

A call to this method will not change the mechanism state, however if any motion was ongoing it will be stopped and an ShadyCommonAPI.ESTOP fault will occur.

The current barrel angles and gripper states are be re-determined.

Any current fault condition will be cleared. However, implementations may subsequently check for any ongoing fault. The first such fault that is found is then set as the current fault and the return value from this method is false.

This is intended to be a relatively lightweight operation to clear a fault condition. I.e. is does not necessarily entail a reboot.

This method does not ShadyCommonAPI.resetAccounting().

Default impl clears ShadyCommon.state.fault and state.faultDetails, then ShadyCommonAPI.stop(boolean), ShadyCommon.reestablishState() and ShadyCommon.checkFault().

This impl clears thread interrupt state, then chains to super.

Specified by:
reset in interface ShadyCommonAPI
Overrides:
reset in class ShadyCommon
Returns:
0 iff reset was successful and no ongoing fault is detected, else the ongoing fault

stop

public void stop(boolean estop)

Stop all ongoing motion.

Has no effect if there is no ongoing motion.

Sets an ShadyCommonAPI.ESTOP fault iff estop is true, there was ongoing motion, and there was no prior fault condition.

This impl sets and ShadyCommonAPI.ESTOP fault only if there are any ShadyCommon.state.numActiveActuators, and always sets numActiveActuators to zero.

Specified by:
stop in interface ShadyCommonAPI
Parameters:
estop - whether to enable ESTOP fault generation

getRotateBarrelTime

protected double getRotateBarrelTime(double dt)

Get the time in seconds it will take to rotate a barrel differential amount dt in degrees at ShadyCommon.NOMINAL_BARREL_ROTATION_SPEED.

Parameters:
dt - the differential rotation in degrees
Returns:
the time in seconds it will take to rotate a barrel dt degrees at nominal speed

reallyRotateBarrel

protected double reallyRotateBarrel(int barrel,
                                    double dt)

Really rotates the barrel.

Implementations should have this method do whatever is necessary to rotate the barrel the requested amount, updating ShadyCommon.state appropriately. Motion limit checking will have already been performed (based on the prior ShadyCommon.state) in ShadyCommon.rotateBarrel(int, double). All accounting info management is also done in rotateBarrel(). This method must simply


makeRotateMotion

protected ShadySim.RotateMotion makeRotateMotion(int barrel,
                                                 double dt)

Make a RotateMotion for reallyRotateBarrel(int, double).

This is a hook so subclasses can also subclass RotateMotion.

Params same as the ShadySim.RotateMotion constr.

Returns:
a RotateMotion corresponding to barrel and dt

rotateBarrel

protected void rotateBarrel(int barrel,
                            double dt,
                            double[] resource)

Second part of ShadyCommon.rotateBarrel(int, double).

Short circuits if dt epsilon equals 0.0, otherwise executes motion and collects resource usage.

Defaults to super (which calls reallyRotateBarrel(int, double)) iff we're not in discrete mode or if motion accumulation is enabled.

See the class header doc for a description of discrete mode.

If we are in discrete mode, the motion is executed as a single discrete step. Energy is not tallied (set to -1.0) and the expired time is 0.0.

Overrides:
rotateBarrel in class ShadyCommon
Parameters:
barrel - same as for ShadyCommon.rotateBarrel(int, double)
dt - same as for ShadyCommon.rotateBarrel(int, double)
resource - a two-element array into which ShadyCommon.ENERGY and ShadyCommon.TIME are recorded on return

getGripperTime

protected double getGripperTime(double ds)

Get the time in seconds it will take to actuate a gripper.

Parameters:
ds - the differential change in state of the gripper
Returns:
the time in seconds it will take to actuate a gripper by ds

reallySetGripper

protected double reallySetGripper(int barrel,
                                  double targetState)

Really actuates the gripper.

Implementations should have this method do whatever is necessary to actuate the requested gripper, updating ShadyCommon.state appropriately. Checking if the gripper is already closed/open would have already happened in ShadyCommon.setGripper(int, int). All accounting info management is also done in setGripper(). This method must simply


makeGripperMotion

protected ShadySim.GripperMotion makeGripperMotion(int barrel,
                                                   double ds)

Make a GripperMotion for reallySetGripper(int, double).

This is a hook so subclasses can also subclass GripperMotion.

Params same as the ShadySim.GripperMotion constr.

Returns:
a GripperMotion corresponding to barrel and ds

setGripper

protected void setGripper(int barrel,
                          double targetState,
                          double[] resource)

Second part of ShadyCommon.setGripper(int, int).

Executes motion and collects resource usage.

Defaults to super iff we're not in discrete mode, which calls reallySetGripper(int, double).

See the class header doc for a description of discrete mode.

If we are in discrete mode, the motion is executed as a single discrete step. Energy is not tallied (set to -1.0) and the expired time is 0.0.

Overrides:
setGripper in class ShadyCommon
Parameters:
barrel - same as for ShadyCommon.setGripper(int, int)
targetState - target gripper state
resource - a two-element array into which ShadyCommon.ENERGY and ShadyCommon.TIME are recorded on return

rotateShade

public int rotateShade(double dt)

Incrementally rotate the shade.

This impl chains to ShadyCommon.reallyRotateShade(double) unless there is a fault or motion accumulation is turned on.

Defaults to super (which calls reallySetGripper(int, double)) iff we're not in discrete mode or motion accumulation is enabled.

See the class header doc for a description of discrete mode.

If we are in discrete mode, the motion is executed as a single discrete step. Otherwise, makes and executes a ShadySim.ShadeMotion.

Specified by:
rotateShade in interface ShadyCommonAPI
Overrides:
rotateShade in class ShadyCommon
Parameters:
dt - a positive or negative real number giving the incremental rotation in degrees

reallyRotateShade

protected double reallyRotateShade(double dt)

Really rotates the shade.

Implementations should have this method do whatever is necessary to rotate the shade the requested amount, updating ShadyCommon.state appropriately. This method must simply


getRotateShadeTime

protected double getRotateShadeTime(double dt)

Get the time in seconds it will take to rotate the shade differential amount dt in degrees at ShadyCommon.NOMINAL_SHADE_ROTATION_SPEED.

Parameters:
dt - the differential rotation in degrees
Returns:
the time in seconds it will take to rotate the shade dt degrees at nominal speed

doSimultaneousMotion

protected double doSimultaneousMotion(double leftBarrelDT,
                                      double rightBarrelDT,
                                      double shadeDT)

Implementation of ShadyCommon.simultaneousMotion(double, double, double).

Specified by:
doSimultaneousMotion in class ShadyCommon

setDiscreteMode

public void setDiscreteMode(boolean discreteMode)

Set whether this ShadySim is in discrete mode.

See the class header doc for a description of discrete mode.

Specified by:
setDiscreteMode in interface ShadySimAPI
Parameters:
discreteMode - true iff this ShadySim should be in discrete mode

getDiscreteMode

public boolean getDiscreteMode()

Check whether this ShadySim is in discrete mode.

See the class header doc for a description of discrete mode.

Specified by:
getDiscreteMode in interface ShadySimAPI
Returns:
true iff this ShadySim is in discrete mode

rechargeBattery

public void rechargeBattery()

Convenience method to fully ShadySimAPI.chargeBattery(double).

Specified by:
rechargeBattery in interface ShadySimAPI

chargeBattery

public void chargeBattery(double newBatteryLevel)

Simulate charging the battery.

This impl sets ShadyCommon.accountingInfo.currentBatteryEnergy to newBatteryLevel.

Specified by:
chargeBattery in interface ShadySimAPI
Parameters:
newBatteryLevel - the new battery level, in Joules

setAccel

public void setAccel(double accel)

Set the simulation time acceleration factor.

A factor of 2.0 means the simulation runs twice as fast as reality would.

Specified by:
setAccel in interface ShadySimAPI
Parameters:
accel - the simulation time acceleration factor

getAccel

public double getAccel()

Get the simulation time acceleration factor.

Specified by:
getAccel in interface ShadySimAPI
Returns:
the simulation time acceleration factor

getShadyTime

public double getShadyTime()

Get the acceltime in accelseconds relative to an arbitrary epoch.

Specified by:
getShadyTime in interface ShadySimAPI
Returns:
the acceltime in accelseconds relative to an arbitrary epoch

shadySleep

public void shadySleep(double seconds)
                throws java.lang.InterruptedException

Sleep the current thread for the requested number of accelseconds.

Specified by:
shadySleep in interface ShadySimAPI
Parameters:
seconds - how long to sleep in accelseconds, non-negative
Throws:
java.lang.InterruptedException

pause

public void pause()

Pause this ShadySim.

This ShadySim will act like time has stopped until it is ShadySimAPI.unPause()d. Any ongoing non-discrete motions or ShadySimAPI.shadySleep(double)s will stop, and the threads that were blocked for them will remain blocked. Any future non-discrete motions or ShadySimAPI.shadySleep(double)s will not start and the calling threads will block until the pause is removed and the motion completes.

Specified by:
pause in interface ShadySimAPI

unPause

public void unPause()

Un-ShadySimAPI.pause() this ShadySim.

Specified by:
unPause in interface ShadySimAPI

makeClock

protected vona.time.Clock makeClock()

Instantiate ShadyCommon.clock.

This is a hook to allow subclasses to implement the clock differently.

This impl just returns a new Clock.

This impl returns a new SuperClock.

Overrides:
makeClock in class ShadyCommon
Returns:
the clock