Graphical State Space Programming (GSSP) Tutorials
Home -> GSSP Tutorials -> Section 2.8 Writing Code in GSSP

Section 2.8 Writing Code in GSSP

This section describes the built-in GSSP variables and functions that you can use when attaching code to regions and waypoints.

Variables

region

During exeuction, each region is represented by a Region object. region is a reference to the Region object to which the current executing code is attached to. You can also use this variable in the code attached to a waypoint. In this case, region will refer to the region that the current waypoint set inherited priority from.

The following is a list of properties of the region class that you can access.

  • region.name - the name of the region.
  • region.priority - the priority of the region.
  • region.constraints - A dictionary containing the constraints of the region. Keys are the names of the state-variables for which this region has constraints. The values are two-element arrays, where the first element is the lower bound, and the second element is the upper bound.

You should not try to modify these variables during runtime. More properties may become available in future releases of GSSP.

Example

R = region
print R.name
print R.priority

# Print out constraints of the region and their values
for name in R.constraints:
    # Print: lower bound <= state-var name <= upper bound
    print R.constraints[name][0], '<=', name, '<=', R.constraints[name][1]

global

global is a python dictionary that allows you to share data between the code attached to various regions.

Example

global['share value'] = 5

# In the code attached to some other region or waypoint
print global['share value']

lock

This is the object that is returned by the python function threading.RLock(). Code attached to each region or waypoint is run in a different thread. Use this variable to synchronize between all the code blocks. Refer to the python API to learn how to use the RLock object.

Example

lock.acquire()
global['a'] = 1
global['b'] = 2
lock.release()

state

state is a dictionary that contains the current state of the robot. The keys are the names of the state-variables, as specified in the state-variable set.

Example

# Suppose we are using the Generic Robot state-variable set
print "Robot's Position X is", state['Position X']
print "Robot's Velocity is", state['Velocity']

stateVars

stateVars is a reference to config.Robots.stateVarSets[VAR_SET]['stateVars'], where VAR_SET is the state-variable set used. Refer to the previous section for details on working with config.Robots.

Example

# Suppose we are using the Generic Robot state-variable set
print stateVars['Position X']['error']

Functions

setBlocking(blocking)

Set the default blocking behavior of the blocking funtions. The argument blocking can be True or False. The default blocking behavior is True. See startWaypoint for more details.

Example

setBlocking(True)

startWaypoint(wptSetName, blocking=None)

Given the name of a waypoint set, this function will execute the corresponding waypoint set. See Section 2.5 Creating Waypoints for details on how this function works.

blocking specifies whether this function should block until it has finished executing. blocking can take three values. blocking=True means that this function will block. blocking=False means that this function will not block. blocking=None is the default value, in which case the default value is used. The default value is set to True, but can be modified using the setBlocking function.

Example

startWaypoint('my waypoint set')

goTo({stateVar1 : val1, stateVar2 : val2 ...... stateVarN: valN}, name='', blocking=None)

goTo takes as argument a dictionary that describes a position in the state space. This function will internally create a waypoint at the specified position and then execute it. See Section 2.2 Creating Regions for a detailed on how this function works.

name is an optional argument specifying the name of the goTo operation. The name of a goTo is useful for cancelling it. see cancelGoTo below.

See startWaypoint for a description of the blocking argument.

Example

goTo({'Position X':5, 'Position Y':10}}

cancelWaypoint(wptSetName)

cancelWaypoint cancels the currently operating waypoint set with the name wptSetName. By cancelling a waypoint set operation, the task thread responsible for the waypoint set operation is killed. If the specified waypoint set is not currently operating, or if no such waypoint set exists, nothing will happen.

Priority does not play a role here. A call to this function in the code of a low priority task thread can cancel a waypoint initiated by a high priority task thread.

Example

startWaypoint('w1')

# Some time later, perhaps in the code of some other region or waypoint...
cancelWaypoint('w1')

cancelGoTo(name)

cancelGoTo cancels the goTo operation with the given name. By cancelling a goTo operation, the task thread responsible for the goTo is killed. If the specified goTo is no longer operating, or if no such goTo operation exists, nothing will happen.

Priority does not play a role here. A call to this function in the code of a low priority task thread can cancel a goTo initiated by a high priority task thread.

Example

goTo({'Position X':5, 'Position Y':10}, name='g1'}

# Some time later, perhaps in the code of some other region or waypoint...
cancelGoTo('g1')

startTCP(port, blocking=None)

Executing this function will cause GSSP to listen at the specified port for external TCP controllers. See Section 3.4 Creating TCP Controllers for details.

See startWaypoint for a description of the blocking argument.

Example

startTCP(9559)

sleep(secs)

This is the same thing as time.sleep(), which allows you to pause the execution of the current thread. The code attached to each region runs in its own thread. Each call to goTo, startWaypoint, and startTCP also run in their own threads. The code attached to a waypoint runs as part of the thread responsible for the corresponding waypoint set.

Example

goTo({'Position X':5, 'Position Y':10}}
sleep(0.5) # Sleep for half a second
goTo({'Position X':-6, 'Position Y':-50}}

Previous: Section 2.7 Customizing State-variable Sets   |   Next: Chapter 3 Running Robot Plans