Commanding Current#

In this tutorial, we’ll show you how to use the OpenSourceLeg library to control the current of a joint.

Import the OpenSourceLeg Class#

To use the OpenSourceLeg class, we first need to import it from the opensourceleg.osl module:

from opensourceleg.osl import OpenSourceLeg

Add a Joint Object#

Once we have imported the OpenSourceLeg class, we can create an instance of the class with the desired frequency and joint configuration:

osl = OpenSourceLeg(frequency=200)  # 200 Hz

In this code, we create an OpenSourceLeg object named osl with a frequency of 200 Hz and a joint with a gear ratio of 9.0.

Controlling Joint Current#

To control the current of a joint, we can use a with block to ensure that the OpenSourceLeg object is properly opened and cleaned up after use:

with osl:

    for t in osl.clock:
        osl.knee.set_current(400)  # 400 mA

In this code, we enter a with block that sets the mode of the knee joint to “current”. We then loop over the osl.clock, which generates a sequence of timestamps at the specified frequency, and set the current of the knee joint to 400 mA. We then log the motor position of the knee joint to the console at each timestamp. We then call the osl.update() method to update the state of the OpenSourceLeg object.


This code assumes that the OpenSourceLeg object is properly configured and calibrated, and that the joint is properly connected and functioning.

Code for this tutorial:#

 1from opensourceleg.osl import OpenSourceLeg
 3osl = OpenSourceLeg(frequency=200)  # 200 Hz
 6with osl:
 7    osl.knee.set_mode(osl.knee.control_modes.current)
 9    for t in osl.clock:
10        osl.knee.set_current(400)  # 400 mA
12        osl.update()