Control#

Compiled Controller#

class opensourceleg.control.compiled_controller.CompiledController(library_name, library_path, main_function_name, initialization_function_name=None, cleanup_function_name=None)#

Controller class to handle using compiled controllers. This class expects that your function has the form: myFunction(*inputs, *outputs) where *inputs is a pointer to an inputs structure and *outputs is a pointer to an outputs structure. You can define these input and output structures however you please. See examples folder of repo for examples.

Parameters
  • library_name (string) – The name of the compiled library file, without the *.so

  • library_path (string) – The path to the directory containing the library. See examples for how to get working directory of parent script.

  • main_function_name (string) – Name of the main function to call within the library. This is the function that will get called via the run() method

  • initialization_function_name (string) – Name of an initialization function for your library. This gets called only once when the library is loaded. If you don’t have an initialization function, pass None.

  • cleanup_function_name (string) – Name of a cleanup function for your library. This gets called when the CompiledController class has gone out of scope and is garbage collected. Again, pass None if you don’t need this functionality.

Authors:

Kevin Best, Senthur Raj Ayyappan Neurobionics Lab Robotics Department University of Michigan October 2023

define_inputs(input_list: list[Any]) None#

This method defines the input structure to your function. See example folder and tutorials for help on using this method.

Parameters

input_list (Input parameters given as a list of [('field_name', field_type)...]) –

field_name is a string you choose as the title of the field. field_type is a type either given by a native c_types value or

a custom type defined via the define_type() method. All types can be accessed as CompiledController.types.(type_name)

define_outputs(output_list: list[Any]) None#

This method defines the output structure to your function. See example folder and tutorials for help on using this method.

Parameters

output_list (Output parameters given as a list of [('field_name', field_type)...]) –

field_name is a string you choose as the title of the field. field_type is a type either given by a native c_types value or

a custom type defined via the define_type() method. All types can be accessed as CompiledController.types.(type_name)

define_type(type_name: str, parameter_list: list[Any])#

This method defines a new type to be used in the compiled controller. After calling this method, the datatype with name type_name will be available in my_controller.types.type_name for use. See example folder and tutorials for help on using this method.

Parameters
  • type_name (A string defining the name of your new datatype) –

  • parameter_list (A list of [('field_name', field_type)...]) –

    field_name is a string you choose as the title of the field. field_type is a type either given by a native c_types value or

    a custom type defined via the define_type() method. All types can be accessed as CompiledController.types.(type_name)

  • Usage (Example) –

  • ------------

    my_controller.DefineType(‘vector3D’, [(‘x’, my_controller.types.c_double),

    (‘y’, my_controller.types.c_double), (‘z’, my_controller.types.c_double)])

run()#

This method calls the main controller function of the library. Under the hood, it calls library_name.main_function_name(*inputs, *outputs), where library_name and main_function_name were given in the constructor.

Parameters -> None

Returns

The output structure as defined by the define_outputs() method.

Raises

ValueError – If define_inputs() or define_outputs() have not been called.

State Machine#

class opensourceleg.control.state_machine.Event(name)#

Event class

class opensourceleg.control.state_machine.FromToTransition(event: opensourceleg.control.state_machine.Event, source: opensourceleg.control.state_machine.State, destination: opensourceleg.control.state_machine.State, callback: Optional[Callable[[Any], bool]] = None)#
class opensourceleg.control.state_machine.Idle#
class opensourceleg.control.state_machine.State(name: str = 'state', is_knee_active: bool = False, knee_stiffness: float = 0.0, knee_damping: float = 0.0, knee_equilibrium_angle: float = 0.0, is_ankle_active: bool = False, ankle_stiffness: float = 0.0, ankle_damping: float = 0.0, ankle_equilibrium_angle: float = 0.0, minimum_time_in_state: float = 2.0)#

A class to represent a state in a finite state machine.

Parameters
  • name (str) – Name of the state

  • is_knee_active (bool) – Whether the knee is active. Default: False

  • knee_stiffness (float) – Knee stiffness in Nm/rad

  • knee_damping (float) – Knee damping in Nm/rad/sec

  • knee_equilibrium_angle (float) – Knee equilibrium angle

  • is_ankle_active (bool) – Whether the ankle is active. Default: False

  • ankle_stiffness (float) – Ankle stiffness in Nm/rad

  • ankle_damping (float) – Ankle damping in Nm/rad/sec

  • ankle_equilibrium_angle (float) – Ankle equilibrium angle

  • minimum_time_in_state (float) – Minimum time spent in the state in seconds. Default: 2.0

Note

The knee and ankle impedance parameters are only used if the corresponding joint is active. You can also set custom data using the set_custom_data method.

get_custom_data(key: str) Any#

Get custom data for the state. The custom data is a dictionary that can be used to store any data you want to associate with the state.

Parameters

key (str) – Key of the data

Returns

Value of the data

Return type

Any

make_ankle_active()#

Make the ankle active

Note

The ankle impedance parameters are only used if the ankle is active.

make_knee_active()#

Make the knee active

Note

The knee impedance parameters are only used if the knee is active.

set_ankle_impedance_paramters(theta, k, b) None#

Set the ankle impedance parameters

Parameters
  • theta (float) – Equilibrium angle of the ankle joint

  • k (float) – Stiffness of the ankle joint

  • b (float) – Damping of the ankle joint

Note

The ankle impedance parameters are only used if the ankle is active. You can make the ankle active by calling the make_ankle_active method.

set_custom_data(key: str, value: Any) None#

Set custom data for the state. The custom data is a dictionary that can be used to store any data you want to associate with the state.

Parameters
  • key (str) – Key of the data

  • value (Any) – Value of the data

set_knee_impedance_paramters(theta, k, b) None#

Set the knee impedance parameters

Parameters
  • theta (float) – Equilibrium angle of the knee joint

  • k (float) – Stiffness of the knee joint

  • b (float) – Damping of the knee joint

Note

The knee impedance parameters are only used if the knee is active. You can make the knee active by calling the make_knee_active method.

set_minimum_time_spent_in_state(time: float) None#

Set the minimum time spent in the state

Parameters

time (float) – Minimum time spent in the state in seconds

class opensourceleg.control.state_machine.StateMachine(osl=None, spoof: bool = False)#

State Machine class

Parameters
  • osl (Any) – The OpenSourceLeg object.

  • spoof (bool) – If True, the state machine will spoof the state transitions–ie, it will not check the criteria for transitioning but will instead transition after the minimum time spent in state has elapsed. This is useful for testing. Defaults to False.

current_state#

The current state of the state machine.

Type

State

states#

The list of states in the state machine.

Type

list[State]

is_spoofing#

Whether or not the state machine is spoofing the state transitions.

Type

bool

add_state(state: opensourceleg.control.state_machine.State, initial_state: bool = False) None#

Add a state to the state machine.

Parameters
  • state (State) – The state to be added.

  • initial_state (bool, optional) – Whether the state is the initial state, by default False

add_transition(source: opensourceleg.control.state_machine.State, destination: opensourceleg.control.state_machine.State, event: opensourceleg.control.state_machine.Event, callback: Optional[Callable[[Any], bool]] = None) Optional[opensourceleg.control.state_machine.Transition]#

Add a transition to the state machine.

Parameters
  • source (State) – The source state.

  • destination (State) – The destination state.

  • event (Event) – The event that triggers the transition.

  • callback (Callable[[Any], bool], optional) – A callback function that returns a boolean value, which determines whether the transition is valid, by default None

class opensourceleg.control.state_machine.Transition(event: opensourceleg.control.state_machine.Event, source: opensourceleg.control.state_machine.State, destination: opensourceleg.control.state_machine.State, callback: Optional[Callable[[Any], bool]] = None)#

Transition class