Skip to content

Instantly share code, notes, and snippets.

@stephane-caron
Last active November 28, 2022 09:18
Show Gist options
  • Save stephane-caron/6575ec518edd2c3d32ae9cf2498b9486 to your computer and use it in GitHub Desktop.
Save stephane-caron/6575ec518edd2c3d32ae9cf2498b9486 to your computer and use it in GitHub Desktop.
Python code for the tutorial on humanoid and wheeled-legged controllers
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2022 Stéphane Caron
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
- Tutorial: https://ytazz.github.io/vnoid/humanoids2022tutorial.html
- Slides: https://scaron.info/slides/humanoids-2022.pdf
"""
import meshcat_shapes
import numpy as np
import pink.utils
from pink import apply_configuration, solve_ik
from pink.tasks import BodyTask
from pinocchio.visualize import MeshcatVisualizer
from robot_descriptions.loaders.pinocchio import load_robot_description
# Loading a robot description
robot = load_robot_description("upkie_description")
robot.setVisualizer(MeshcatVisualizer())
robot.initViewer(open=True)
robot.loadViewerModel()
robot.display(robot.q0)
# Define tasks for inverse kinematics
tasks = [
BodyTask(f"{leg}_contact", position_cost=1.0, orientation_cost=1.0)
for leg in ("left", "right") # adapt to the robot you picked
]
# Initialize task targets
configuration = apply_configuration(robot, robot.q0)
for task in tasks:
task.set_target_from_configuration(configuration)
# Display target frames
for task in tasks:
meshcat_shapes.frame(robot.viewer[f"{task.body}_target"])
def update_targets(tasks, t, dt):
for task in tasks:
T = task.transform_target_to_world
u = 0.2 * np.array([2.0, 0.0, 1.0])
T.translation += np.sin(2 * t) * u * dt
robot.viewer[f"{task.body}_target"].set_transform(T.np)
# Closed-loop inverse kinematics
rate = pink.utils.RateLimiter(frequency=200.0)
dt = rate.period
for t in np.arange(0.0, 5.0, dt):
update_targets(tasks, t, dt)
velocity = solve_ik(configuration, tasks, dt, solver="proxqp")
q = configuration.integrate(velocity, dt)
configuration = apply_configuration(robot, q)
robot.display(q)
rate.sleep()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment