Tools#

Units#

class opensourceleg.tools.units.acceleration#
opensourceleg.tools.units.convert_from_default(value: float, to_unit: float) float#

Convert a value from the default unit to an user unit.

Parameters
  • value (float) – Value to convert

  • to_unit (float) – Desired unit for the converted value.

Returns

Converted value in desired unit.

Return type

float

Example

>>> convert_from_default(2000, units.current.A)
2.0 # A
>>> convert_from_default(10000, units.voltage.V)
10.0 # V
>>> convert_from_default(0.7853981633974483, units.position.deg)
45.0 # deg
opensourceleg.tools.units.convert_to_default(value: float, from_unit: float) float#

Convert a value from an user unit to the default unit.

Parameters
  • value (float) – Value to convert

  • from_unit (float) – Unit corresponding to the value that is being converted to the default unit.

Returns

Converted value in default units.

Return type

float

Example

>>> convert_to_default(2, units.current.A)
2000 # returns value in mA, which is the default unit for current
>>> convert_to_default(10, units.voltage.V)
10000 # returns value in mV, which is the default unit for voltage
>>> convert_to_default(45, units.position.deg)
0.7853981633974483 # returns value in rad, which is the default unit for position
class opensourceleg.tools.units.current#
class opensourceleg.tools.units.damping#
class opensourceleg.tools.units.force#
class opensourceleg.tools.units.length#
class opensourceleg.tools.units.mass#
class opensourceleg.tools.units.position#
class opensourceleg.tools.units.stiffness#
class opensourceleg.tools.units.time#
class opensourceleg.tools.units.torque#
class opensourceleg.tools.units.velocity#
class opensourceleg.tools.units.voltage#

Utilities#

class opensourceleg.tools.utilities.EdgeDetector(bool_in)#

Used to calculate rising and falling edges of a digital signal in real time. Call edgeDetector.update(digitalSignal) to update the detector. Then read edgeDetector.rising_edge or falling edge to know if the event occurred.

Author: Kevin Best tkevinbest

class opensourceleg.tools.utilities.LoopKiller(fade_time=0.0)#

Soft Realtime Loop—a class designed to allow clean exits from infinite loops with the potential for post-loop cleanup operations executing.

The Loop Killer object watches for the key shutdown signals on the UNIX operating system (which runs on the PI) when it detects a shutdown signal, it sets a flag, which is used by the Soft Realtime Loop to stop iterating. Typically, it detects the CTRL-C from your keyboard, which sends a SIGTERM signal.

the function_in_loop argument to the Soft Realtime Loop’s blocking_loop method is the function to be run every loop. A typical usage would set function_in_loop to be a method of an object, so that the object could store program state. See the ‘ifmain’ for two examples.

# This library will soon be hosted as a PIP module and added as a python dependency. # UM-LoCoLab/NeuroLocoMiddleware

Author: Gray C. Thomas, Ph.D GrayThomas, https://graythomas.github.io

class opensourceleg.tools.utilities.SaturatingRamp(loop_frequency=100, ramp_time=1.0)#

Creates a signal that ramps between 0 and 1 at the specified rate. Looks like a trapezoid in the time domain Used to slowly enable joint torque for smooth switching at startup. Call saturatingRamp.update() to update the value of the ramp and return the value. Can also access saturatingRamp.value without updating.

Example usage:

ramp = saturatingRamp(100, 1.0)

# In loop

torque = torque * ramp.update(enable_ramp)

Author: Kevin Best tkevinbest

update(enable_ramp=False)#

Updates the ramp value and returns it as a float. If enable_ramp is true, ramp value increases Otherwise decreases.

Example usage:

torque = torque * ramp.update(enable_ramp)

Parameters

enable_ramp (bool, optional) – If enable_ramp is true, ramp value increases. Defaults to False.

Returns

Scalar between 0 and 1.

Return type

value (float)

class opensourceleg.tools.utilities.SoftRealtimeLoop(dt=0.001, report=False, fade=0.0)#

Soft Realtime Loop—a class designed to allow clean exits from infinite loops with the potential for post-loop cleanup operations executing.

The Loop Killer object watches for the key shutdown signals on the UNIX operating system (which runs on the PI) when it detects a shutdown signal, it sets a flag, which is used by the Soft Realtime Loop to stop iterating. Typically, it detects the CTRL-C from your keyboard, which sends a SIGTERM signal.

the function_in_loop argument to the Soft Realtime Loop’s blocking_loop method is the function to be run every loop. A typical usage would set function_in_loop to be a method of an object, so that the object could store program state. See the ‘ifmain’ for two examples.

This library will soon be hosted as a PIP module and added as a python dependency. UM-LoCoLab/NeuroLocoMiddleware

# Author: Gray C. Thomas, Ph.D # GrayThomas, https://graythomas.github.io

opensourceleg.tools.utilities.clamp_within_vector_range(input_value, input_vector)#

This function ensures that input_value remains within the range spanned by the input_vector. If the input_value falls outside the vector’s bounds, it’ll return the appropriate max or min value from the vector.

Example

clamp_within_vector_range(10, [0,1,2,3]) = 3 clamp_within_vector_range(-10, [0,1,2,3]) = 0

Author:

Kevin Best, 8/7/2023 tkevinbest

opensourceleg.tools.utilities.get_active_ports()#

Lists active serial ports.

opensourceleg.tools.utilities.get_ctype(token)#

Converts a single token from a header file into a ctypes argument.

Author: Kevin Best, 8/7/2023 tkevinbest

opensourceleg.tools.utilities.get_ctype_args(input_header: str)#

Converts a header file from C string into a list of ctypes arguments.

Keyword Arguments

inputHeader – string from header file, such as “const struct0_T *thighIMU, double Knee_joint_position, double Ankle_joint_position”

Returns

ctypes list of the appropriate types for the inputs, such as (ctypes.c_void_p, ctypes.c_double, ctypes.c_double)

Author: Kevin Best, tkevinbest

Logger#

class opensourceleg.tools.logger.Logger(file_path: str = './osl', log_format: str = '[%(asctime)s] %(levelname)s: %(message)s')#

Bases: logging.Logger

Logger class is a class that logs attributes from a class to a csv file

__init__(self, container

object, file_path: str, logger: logging.Logger = None) -> None

log(self) None#
add_attributes(container: Union[object, dict[Any, Any]], attributes: list[str]) None#

Adds class instance and attributes to log

Parameters
  • container (object, dict) – Container can either be an object (instance of a class) or a Dict containing the attributes to be logged.

  • attributes (list[str]) – List of attributes to log

close() None#

Closes the csv file

data() None#

Logs the attributes of the class instance to the csv file

set_file_level(level: str = 'DEBUG') None#

Sets the level of the logger

Parameters

level (str) – Level of the logger

set_stream_level(level: str = 'INFO') None#

Sets the level of the logger

Parameters

level (str) – Level of the logger