primitive Package

primitive Module

class pypot.primitive.primitive.Primitive(robot)[source]

Bases: pypot.utils.stoppablethread.StoppableThread

A Primitive is an elementary behavior that can easily be combined to create more complex behaviors.

A primitive is basically a thread with access to a “fake” robot to ensure a sort of sandboxing. More precisely, it means that the primitives will be able to:

  • request values from the real robot (motor values, sensors or attached primitives)
  • request modification of motor values (those calls will automatically be combined among all primitives by the PrimitiveManager).

The syntax of those requests directly match the equivalent code that you could write from the Robot. For instance you can write:

class MyPrimitive(Primitive):
    def run(self):
        while True:
            for m in self.robot.motors:
                m.goal_position = m.present_position + 10

        time.sleep(1)

Warning

In the example above, while it seems that you are setting a new goal_position, you are only requesting it. In particular, another primitive could request another goal_position and the result will be the combination of both request. For example, if you have two primitives: one setting the goal_position to 10 and the other setting the goal_position to -20, the real goal_position will be set to -5 (by default the mean of all request is used, see the PrimitiveManager class for details).

Primitives were developed to allow for the creation of complex behaviors such as walking. You could imagine - and this is what is actually done on the Poppy robot - having one primitive for the walking gait, another for the balance and another for handling falls.

Note

This class should always be extended to define your particular behavior in the run() method.

At instanciation, it automatically transforms the Robot into a MockupRobot.

Warning

You should not directly pass motors as argument to the primitive. If you need to, use the method get_mockup_motor() to transform them into “fake” motors. See the Writing your own primitive section for details.

methods = ['start', 'stop', 'pause', 'resume']
properties = []
setup()[source]

Setup methods called before the run loop.

You can override this method to setup the environment needed by your primitive before the run loop. This method will be called every time the primitive is started/restarted.

run()[source]

Run method of the primitive thread. You should always overwrite this method.

Warning

You are responsible of handling the should_stop(), should_pause() and wait_to_resume() methods correctly so the code inside your run function matches the desired behavior. You can refer to the code of the run() method of the LoopPrimitive as an example.

After termination of the run function, the primitive will automatically be removed from the list of active primitives of the PrimitiveManager.

teardown()[source]

Tear down methods called after the run loop.

You can override this method to clean up the environment needed by your primitive. This method will be called every time the primitive is stopped.

elapsed_time

Elapsed time (in seconds) since the primitive runs.

start()[source]

Start or restart (the stop() method will automatically be called) the primitive.

stop(wait=True)[source]

Requests the primitive to stop.

is_alive()[source]

Determines whether the primitive is running or not.

The value will be true only when the run() function is executed.

get_mockup_motor(motor)[source]

Gets the equivalent MockupMotor.

being_synced
affect_once(motor, register, value)[source]
class pypot.primitive.primitive.LoopPrimitive(robot, freq)[source]

Bases: pypot.primitive.primitive.Primitive

Simple primitive that call an update method at a predefined frequency.

You should write your own subclass where you only defined the update() method.

recent_update_frequencies

Returns the 10 most recent update frequencies.

The given frequencies are computed as short-term frequencies! The 0th element of the list corresponds to the most recent frequency.

run()[source]

Calls the update() method at a predefined frequency (runs until stopped).

update()[source]

Update methods that will be called at a predefined frequency.

class pypot.primitive.primitive.MockupRobot(robot)[source]

Bases: object

Fake Robot used by the Primitive to ensure sandboxing.

goto_position(position_for_motors, duration, control=None, wait=False)[source]
motors

List of all attached MockupMotor.

power_max()[source]
class pypot.primitive.primitive.MockupMotor(motor)[source]

Bases: object

Fake Motor used by the primitive to ensure sandboxing:

  • the read instructions are directly delegate to the real motor
  • the write instructions are stored as request waiting to be combined by the primitive manager.
goto_position(position, duration, control=None, wait=False)[source]

Automatically sets the goal position and the moving speed to reach the desired position within the duration.

goal_speed

Goal speed (in degrees per second) of the motor.

This property can be used to control your motor in speed. Setting a goal speed will automatically change the moving speed and sets the goal position as the angle limit.

Note

The motor will turn until reaching the angle limit. But this is not a wheel mode, so the motor will stop at its limits.

manager Module

class pypot.primitive.manager.PrimitiveManager(motors, freq=50, filter=<functools.partial object>)[source]

Bases: pypot.utils.stoppablethread.StoppableLoopThread

Combines all Primitive orders and affect them to the real motors.

At a predefined frequency, the manager gathers all the orders sent by the primitive to the “fake” motors, combined them thanks to the filter function and affect them to the “real” motors.

Note

The primitives are automatically added (resp. removed) to the manager when they are started (resp. stopped).

Parameters:
  • motors (list of DxlMotor) – list of real motors used by the attached primitives
  • freq (int) – update frequency
  • filter (func) – function used to combine the different request (default mean)
add(p)[source]

Add a primitive to the manager. The primitive automatically attached itself when started.

remove(p)[source]

Remove a primitive from the manager. The primitive automatically remove itself when stopped.

primitives

List of all attached Primitive.

update()[source]

Combined at a predefined frequency the request orders and affect them to the real motors.

stop()[source]

Stop the primitive manager.

move Module

class pypot.primitive.move.Move(freq)[source]

Bases: object

Simple class used to represent a movement.

This class simply wraps a sequence of positions of specified motors. The sequence must be recorded at a predefined frequency. This move can be recorded through the MoveRecorder class and played thanks to a MovePlayer.

framerate
add_position(pos, time)[source]

Add a new position to the movement sequence.

Each position is typically stored as a dict of (time, (motor_name,motor_position)).

iterpositions()[source]

Returns an iterator on the stored positions.

positions()[source]

Returns a copy of the stored positions.

plot(ax)[source]
save(file)[source]

Saves the Move to a json file.

Note

The format used to store the Move is extremely verbose and should be obviously optimized for long moves.

classmethod create(d)[source]

Create a Move from a dictionary.

classmethod load(file)[source]

Loads a Move from a json file.

classmethod loads(str)[source]

Loads a Move from a json string.

class pypot.primitive.move.MoveRecorder(robot, freq, tracked_motors)[source]

Bases: pypot.primitive.primitive.LoopPrimitive

Primitive used to record a Move.

The recording can be start() and stop() by using the LoopPrimitive methods.

Note

Re-starting the recording will create a new Move losing all the previously stored data.

setup()[source]

Setup methods called before the run loop.

You can override this method to setup the environment needed by your primitive before the run loop. This method will be called every time the primitive is started/restarted.

update()[source]

Update methods that will be called at a predefined frequency.

move

Returns the currently recorded Move.

add_tracked_motors(tracked_motors)[source]

Add new motors to the recording

class pypot.primitive.move.MovePlayer(robot, move=None, play_speed=1.0, move_filename=None, start_max_speed=50, **kwargs)[source]

Bases: pypot.primitive.primitive.LoopPrimitive

Primitive used to play a Move.

The playing can be start() and stop() by using the LoopPrimitive methods.

Warning

the primitive is run automatically the same framerate than the move record. The play_speed attribute change only time lockup/interpolation

setup()[source]

Setup methods called before the run loop.

You can override this method to setup the environment needed by your primitive before the run loop. This method will be called every time the primitive is started/restarted.

update()[source]

Update methods that will be called at a predefined frequency.

duration()[source]

utils Module

class pypot.primitive.utils.Sinus(robot, refresh_freq, motor_list, amp=1, freq=0.5, offset=0, phase=0)[source]

Bases: pypot.primitive.primitive.LoopPrimitive

Apply a sinus on the motor specified as argument. Parameters (amp, offset and phase) should be specified in degree.

properties = ['frequency', 'amplitude', 'offset', 'phase']
update()[source]

Compute the sin(t) where t is the elapsed time since the primitive has been started.

frequency
amplitude
offset
phase
class pypot.primitive.utils.Cosinus(robot, refresh_freq, motor_list, amp=1, freq=0.5, offset=0, phase=0)[source]

Bases: pypot.primitive.utils.Sinus

Apply a cosinus on the motor specified as argument. Parameters (amp, offset and phase) should be specified in degree.

class pypot.primitive.utils.PositionWatcher(robot, refresh_freq, watched_motors)[source]

Bases: pypot.primitive.primitive.LoopPrimitive

record_positions
setup()[source]

Setup methods called before the run loop.

You can override this method to setup the environment needed by your primitive before the run loop. This method will be called every time the primitive is started/restarted.

update()[source]

Update methods that will be called at a predefined frequency.

plot(ax)[source]
class pypot.primitive.utils.SimplePosture(robot, duration)[source]

Bases: pypot.primitive.primitive.Primitive

setup()[source]

Setup methods called before the run loop.

You can override this method to setup the environment needed by your primitive before the run loop. This method will be called every time the primitive is started/restarted.

run()[source]

Run method of the primitive thread. You should always overwrite this method.

Warning

You are responsible of handling the should_stop(), should_pause() and wait_to_resume() methods correctly so the code inside your run function matches the desired behavior. You can refer to the code of the run() method of the LoopPrimitive as an example.

After termination of the run function, the primitive will automatically be removed from the list of active primitives of the PrimitiveManager.

teardown()[source]

Tear down methods called after the run loop.

You can override this method to clean up the environment needed by your primitive. This method will be called every time the primitive is stopped.