|
| 1 | +# Torque Trajectory Control Tutorial |
| 2 | + |
| 3 | +This `opensourceleg.control` module provides functiionality for doing torque control. This tutorial demonstrates how to control the Open Source Leg (OSL) using torque trajectories for both the knee and ankle joints. |
| 4 | +## Warnings: |
| 5 | + |
| 6 | +1. This example is not meant to be used as a walking controller. The goal of this example is to provide a reference for how a torque trajectory can be loaded and commanded. |
| 7 | +2. While runnig this script make sure to have load on the actuators. |
| 8 | +3. Please be cautious while changing mass parameters. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +The script implements a torque control system that: |
| 13 | + |
| 14 | +1. Loads pre-defined torque trajectories for knee and ankle joints |
| 15 | +2. Applies these trajectories in a cyclic manner |
| 16 | +3. Logs the performance data |
| 17 | +4. Generates visualization plots |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +- OpenSourceLeg hardware setup |
| 22 | +- Python environment with required dependencies: |
| 23 | + |
| 24 | + - numpy |
| 25 | + - matplotlib |
| 26 | + |
| 27 | +- Access to torque trajectory files: |
| 28 | + |
| 29 | + - `ankle.pkl` |
| 30 | + - `knee.pkl` |
| 31 | + |
| 32 | +## Command Line Arguments |
| 33 | + |
| 34 | +The script accepts the following command line arguments: |
| 35 | + |
| 36 | +- `--mass`: User mass in kg (default: 1.0) |
| 37 | +- `--stride-time`: Stride time in seconds (default: 1.0) |
| 38 | +- `--frequency`: Control loop frequency in Hz (default: 200.0) |
| 39 | + |
| 40 | +## Key Parameters |
| 41 | + |
| 42 | +- `USER_MASS`: The mass of the user in kg (configurable via command line) |
| 43 | +- `STRIDE_TIME`: The stride time in seconds (configurable via command line) |
| 44 | +- `FREQUENCY`: The control loop frequency in Hz (configurable via command line) |
| 45 | +- `TRAJECTORY_LEN`: Fixed at 150 points. This is the length of the torque trajectories in the ankle.pkl and knee.pkl files. |
| 46 | +- `GEAR_RATIO`: Set to 9 * (83/18) |
| 47 | + |
| 48 | +## Hardware Configuration |
| 49 | + |
| 50 | +The script configures two DephyActuators and two AS5048B encoders: |
| 51 | + |
| 52 | +1. **Actuators**: |
| 53 | + |
| 54 | + - Knee actuator (port=`/dev/ttyACM0`) |
| 55 | + - Ankle actuator (port=`/dev/ttyACM1`) |
| 56 | + |
| 57 | + Both configured with: |
| 58 | + |
| 59 | + - Specified gear ratio |
| 60 | + - User-defined frequency |
| 61 | + - Dephy logging disabled |
| 62 | + |
| 63 | +2. **Encoders**: |
| 64 | + |
| 65 | + - Knee joint encoder (bus=1, A1=True, A2=False) |
| 66 | + - Ankle joint encoder (bus=1, A1=False, A2=True) |
| 67 | + |
| 68 | +## Functions |
| 69 | + |
| 70 | +### Get Torque |
| 71 | + |
| 72 | +```python |
| 73 | +--8<-- "tutorials/control/torque_trajectory/torque_trajectory.py:22:42" |
| 74 | +``` |
| 75 | + |
| 76 | +Calculates the torque setpoint for a given time point: |
| 77 | + |
| 78 | +- `t`: Current time in seconds |
| 79 | +- `data`: List containing torque trajectory data points |
| 80 | +- `user_mass`: Mass of the user in kg |
| 81 | +- `stride_time`: Stride time in seconds |
| 82 | +- `trajectory_len`: Length of the trajectory |
| 83 | + |
| 84 | +Returns torque setpoint scaled by user mass |
| 85 | + |
| 86 | +### Plot Data |
| 87 | + |
| 88 | +```python |
| 89 | +--8<-- "tutorials/control/torque_trajectory/torque_trajectory.py:45:88" |
| 90 | +``` |
| 91 | + |
| 92 | +Generates three plots: |
| 93 | + |
| 94 | +1. Ankle torque (setpoint vs. actual) |
| 95 | +2. Knee torque (setpoint vs. actual) |
| 96 | +3. Joint positions (knee and ankle in degrees) |
| 97 | + |
| 98 | +Saves the plot as `plot.png` |
| 99 | + |
| 100 | +## Operation Flow |
| 101 | + |
| 102 | +1. **Initialization**: |
| 103 | + |
| 104 | + - Parses command line arguments |
| 105 | + - Sets up logging with specified frequency |
| 106 | + - Configures actuators and sensors |
| 107 | + - Loads trajectory data from pickle files |
| 108 | + |
| 109 | +2. **Control Sequence**: |
| 110 | + |
| 111 | + - Homes the OSL |
| 112 | + - Sets control mode to CURRENT for both actuators |
| 113 | + - Sets current gains |
| 114 | + - Waits for user input |
| 115 | + - Executes torque trajectory |
| 116 | + - Logs performance data |
| 117 | + |
| 118 | +3. **Visualization**: |
| 119 | + |
| 120 | + - Generates plots after completion |
| 121 | + - Saves plots to "plot.png" |
| 122 | + |
| 123 | +## Usage |
| 124 | + |
| 125 | +1. Ensure trajectory files (`ankle.pkl` and `knee.pkl`) are in the working directory. |
| 126 | + We have provided a sample trajectory file in the `tutorials/control/torque_trajectory/` directory for both the ankle and knee joints. |
| 127 | + These trajectories are output of an high-level controller (not provided here) for level walking. |
| 128 | + The torque trajectories present the joint torque setpoints for one gait cycle, and the units are in Nm/kg. |
| 129 | + |
| 130 | +2. Run the script with optional arguments: |
| 131 | + ```bash |
| 132 | + python torque_trajectory.py --mass 10 --stride-time 1 --frequency 200 |
| 133 | + ``` |
| 134 | +3. Press Enter when prompted to start the walking trajectory |
| 135 | +4. The system will execute the trajectory and generate plots upon completion |
| 136 | + |
| 137 | +## Safety Notes |
| 138 | + |
| 139 | +- This example is not meant to be used as a walking controller |
| 140 | +- Please be cautious while changing mass parameters |
| 141 | +- Ensure all connections are secure before operation |
| 142 | +- Keep emergency stop accessible during operation |
| 143 | + |
| 144 | +## Output |
| 145 | + |
| 146 | +The script generates: |
| 147 | + |
| 148 | +1. Real-time logs in the `./logs` directory with filename "torque_trajectory" |
| 149 | +2. A plot file (`plot.png`) showing: |
| 150 | + |
| 151 | + - Torque trajectories for both joints |
| 152 | + - Actual vs. commanded torques |
| 153 | + - Joint position data in degrees |
| 154 | + |
| 155 | +## Full Script for this tutorial |
| 156 | +```python |
| 157 | +--8<-- "tutorials/control/torque_trajectory/torque_trajectory.py:1:212" |
| 158 | +``` |
| 159 | +If you have any questions or need further assistance, please post on the [Open Source Leg community forum](https://opensourceleg.org/community). |
0 commit comments