Example Guide

Introduction

Example code corresponding to this guide can be found in the file example.py in the git repository. For reference, the hierarchy of SimGrasp has the following structure:

_images/diagram.png

To start, import the SimGrasp module into your Python 2 program, called simgrasp. If it cannot be found, make sure you have run sudo python setup.py install first to install SimGrasp. In order to run a simulation, you must override GraspSimulation.GraspStateMachine and GraspSimulation.BatchSim with your own simulation code.

Creating a State Machine

The state machine controls which state the simulation is in and is responsible for determining when the simulation is complete. To create your own state machine, override the class GraspSimulation.GraspStateMachine. The state is stored as a string in the curState member variable and can be set by using the GraspSimulation.GraspStateMachine.enterState() method. All states must first be added to a list stored in the states member variable or else the state will not be able to be entered. The states are managed in the GraspSimulation.GraspStateMachine.runStateMachine() method along with logic detailing what to do under each state. For example, in example.py, the method contains a series of if/elif statements to check for which state the state machine is in. Within each if statement, different torques and forces are applied to the Design.Fingers and Design.GraspObject and the state is changed if certain conditions are met. When the simulation is complete, the variable simIsDone is set to True so that the next simulation can begin. No further cleanup is necessary, as it is all managed by the GraspSimulation.GraspSimulation class. The method also uses a few helper methods that determine wether or not conditions for changing states have been met. Using helper methods like these are optional, but may be helpful for code reuse.

Creating a Batch Simulation

Batch simulations allow you to run multiple simulations with slightly different hand parameters in each simulation. To create a batch simulation, override the class GraspSimulation.BatchSimulation. The batch simulation must define a finger, hand, graspObject, handController, and graspStateMachine for the GraspSimulation.GraspSimulation to use. The method GraspSimulation.BatchSim.importNewSim() is used to generate new simulations. In the example code, the method generates new hand and finger files along with a new HandController. It keeps track of which simulation number is running with the iterNum variable and changes the orientation of the fingers in each simulation. The batch simulation must also define a GraspSimulation.BatchSim.deleteOldSim() and GraspSimulation.BatchSim.endBatchSim() method. GraspSimulation.BatchSim.deleteOldSim() method should delete the finger, hand, graspObject, handController, and graspStateMachine objects. The method GraspSimulation.BatchSim.endBatchSim() is called once all of the simulations have finished. In the example code, this method is used to write the results of all of the simulations to a file. To run a batch simulation, first create a new instance of your GraspSimulation.BatchSim. Then create a new instance of a GraspSimulation.GraspSimulation and pass the batch simulation object as an argument and call GraspSimulation.GraspSimulation.run().

Table Of Contents

Previous topic

Installation Guide

Next topic

SimGrasp Modules

This Page