Skip to content

Python Language

Alberto Camporredondo edited this page Feb 22, 2018 · 4 revisions

The operator can specify the tasks of a mission using Python language (based on Python 2) using certain predefined functions for mission plan execution. To be used by Aerostack, the specification must follow certain conventions that are explained below.

1. Simple example

The simplest mission one can do with the robot (taking off and landing) is the following:

import executive_engine_api as api

def runMission():
  api.executeBehavior('TAKE_OFF')
  api.executeBehavior('LAND')

The function runMission() is a special function that will get executed when the mission is started. As you can see, for this example it is necessary to import the executive_engine_api package in order to use its executeBehavior() function. This function executes any behavior and waits for it to complete. It returns a string with the termination status. The available behaviors can be found here:

2. Recurrent behaviors

Recurrent behaviors don't terminate until they are inhibited, so they mustn't be executed with the function executeBehavior(), or they will block the program with and endless loop. In order to activate a behavior without waiting for its termination, the function activateBehavior() is provided. To inhibit a recurrent behavior, use the function inhibitBehavior(). When a behavior needs to be inhibited, you should set the Unique identifier (uid) as the parameter of the function. The UID is given when a behavior is activated by using the activateBehavior() function.

import executive_engine_api as api

def runMission():
  ack, uid_localization = api.activateBehavior('SELF_LOCALIZE_BY_ODOMETRY')

  api.executeBehavior('TAKE_OFF')
  api.executeBehavior('LAND')

  api.inhibitBehavior(uid_localization)

3. Accessing the belief memory

In order to add and remove beliefs from the belief memory the functions addBelief() and removeBelief() are provided. To consult the belief memory, two functions are provided: consultBelief() and trueBelief(). The latter simply returns true or false, while the first returns a boolean and a dictionary with the variables unified if it was successful.

import executive_engine_api as api

def runMission():
  api.addBelief('my_belief(something, something_else)')
  api.addBelief('my_other_belief(something)')

  result = api.trueBelief('my_belief(?X, ?Y)') # This will return true

  success, unification = api.consultBelief('my_belief(?X, ?), my_other_belief(?X)')
  if success: # Always check if query was successful before trying to access the unification variables
    my_string = unification['X']

    api.removeBelief('my_belief(%s, something_else)' % my_string)
    api.removeBelief('my_other_belief(%s)' % my_string)

4. Complete example

This example is a complete specification of a mission in Python language.

import executive_engine_api as api
import random

def runMission():
  ack, uid_localization = api.activateBehavior('SELF_LOCALIZE_BY_ODOMETRY')
  api.executeBehavior('TAKE_OFF')

  # Memorize starting point
  success, unification = api.consultBelief('position(self, (?X, ?Y, ?Z))')
  if success:
    startingPoint = [unification['X'], unification['Y'], unification['Z']]
  else:
    startingPoint = [0, 0, 0.7]

  api.inhibitBehavior(uid_localization)

  ack, uid_localization = api.activateBehavior('SELF_LOCALIZE_BY_VISUAL_MARKERS')
  objectFound = False
  # Start searching for object
  while not objectFound:
    nextPoint = [10*random.random(), 10*random.random(), 0.7]
    api.executeBehavior('GO_TO_POINT', coordinates=nextPoint)
    objectFound = api.trueBelief('position(object_1, ?)')

  success, unification = api.consultBelief('position(object_1, (?X, ?Y, ?Z))')
  if success:
    objectPosition = [unification['X'], unification['Y'], unification['Z']]

    # Set target position to 1m above object
    targetPosition = objectPosition
    targetPosition[2] += 1

    api.executeBehavior('GO_TO_POINT', coordinates=targetPosition)

    # Set object status to picked up
    api.addBelief('picked_up(object_1)')

    # Retrieve object
    api.executeBehavior('GO_TO_POINT', coordinates=startingPoint)

    # Drop object
    api.removeBelief('picked_up(object_1)')

  api.inhibitBehavior(uid_localization)

  api.executeBehavior('LAND')
Clone this wiki locally
You can’t perform that action at this time.