Qontrol
Velocity control

The following example solves a qp problem expressed at the joint velocity level such that:

\begin{equation}\begin{array}{ccc}\boldsymbol{\dot{q}}^{opt} = & \underset{\boldsymbol{\dot{q}}}{\mathrm{argmin}} & ||J(\boldsymbol{q})\boldsymbol{\dot{q}} - \boldsymbol{v}^{target} || + \omega || \boldsymbol{\dot{q}} ||^2\\& \textrm{s.t.} & \boldsymbol{\dot{q}^{min}} \leq \boldsymbol{\dot{q}} \leq \boldsymbol{\dot{q}^{max}}. \\ & & \boldsymbol{q}^{min} \leq \boldsymbol{q}(\boldsymbol{\dot{q}}) \leq \boldsymbol{q}^{max} \end{array} \end{equation}

.

The robot main tasks consists in following a simple trajectory defined in Cartesian space. The mujoco library is used to simulate the robot behaviour.

Simulation

To run this example run the following command from the build/examples directory:

./velocityQontrol robot_name

where robot_name can be either panda or ur5

Full code

1 // Copyright 2021 DeepMind Technologies Limited
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "mujoco/mujoco_sim.h"
16 #include "Qontrol/Qontrol.hpp"
17 #include "trajectory_generation/trajectory_generation.h"
18 
19 
20 using namespace Qontrol;
21 
22 class MujocoQontrol : public MujocoSim
23 {
24 public:
25 //------------------------------------------- simulation -------------------------------------------
26 std::shared_ptr<Qontrol::Model::RobotModel<Qontrol::ModelImpl::PINOCCHIO>> model;
27 std::shared_ptr<Qontrol::JointVelocityProblem> velocity_problem;
28 std::shared_ptr<Qontrol::Task::CartesianVelocity<Qontrol::ControlOutput::JointVelocity>> main_task;
29 
30 Qontrol::RobotState robot_state;
31 
32 TrajectoryGeneration* traj;
33 std::string resource_path;
34 
35 void initController( ) override
36 {
37  model =
39 
40  const int ndof = model->getNrOfDegreesOfFreedom();
41 
42  velocity_problem = std::make_shared<Qontrol::JointVelocityProblem>(model);
43  main_task = velocity_problem->task_set->add<Task::CartesianVelocity>("MainTask");
44  auto regularisation_task = velocity_problem->task_set->add<Task::JointVelocity>("RegularisationTask",1e-5);
45 
46  auto joint_configuration_constraint = velocity_problem->constraint_set->add<Constraint::JointConfiguration>("JointConfigurationConstraint");
47  auto joint_velocity_constraint = velocity_problem->constraint_set->add<Constraint::JointVelocity>("JointVelocityConstraint");
48 
49  mju_copy(d->qpos, m->key_qpos, m->nu);
50  robot_state.joint_position.resize(ndof);
51  robot_state.joint_velocity.resize(ndof);
52 
53  traj = new TrajectoryGeneration(resource_path + "trajectory.csv",
54  m->opt.timestep);
55 }
56 
57 void updateController() override
58 {
59  const int ndof = model->getNrOfDegreesOfFreedom();
60 
61  for (int i=0; i<ndof ; ++i)
62  {
63  robot_state.joint_position[i] = d->qpos[i];
64  robot_state.joint_velocity[i] = d->qvel[i];
65  }
66  model->setRobotState(robot_state);
67 
68  traj->update();
69  pinocchio::SE3 traj_pose(traj->pose.matrix());
70 
71  pinocchio::SE3 current_pose(model->getFramePose(model->getTipFrameName()).matrix());
72  const pinocchio::SE3 tipMdes = current_pose.actInv(traj_pose);
73  auto err = pinocchio::log6(tipMdes).toVector();
74  Eigen::Matrix<double,6,1> p_gains;
75  p_gains << 10,10,10,10,10,10;
76  Eigen::Matrix<double,6,1> xd_star = p_gains.cwiseProduct(err);
77 
78  main_task->setTargetVelocity(xd_star);
79 
80  velocity_problem->update(m->opt.timestep);
81 
82  if (velocity_problem->solutionFound())
83  {
84  sendJointVelocity(velocity_problem->getJointVelocityCommand());
85  }
86 }
87 
88 };
89 
90 int main(int argc, const char** argv) {
91  MujocoQontrol mujoco_qontrol;
92  Qontrol::Log::Logger::parseArgv(argc, argv);
93 
94  mjvCamera cam;
95  mjv_defaultCamera(&cam);
96 
97  mjvOption opt;
98  mjv_defaultOption(&opt);
99 
100  mjvPerturb pert;
101  mjv_defaultPerturb(&pert);
102 
103  // simulate object encapsulates the UI
104  auto sim = std::make_unique<mj::Simulate>(
105  std::make_unique<mj::GlfwAdapter>(),
106  &cam, &opt, &pert, /* is_passive = */ false
107  );
108 
109  std::string robot = argv[1];
110  std::string mujoco_scene = "./resources/"+robot+"/scene.xml";
111  mujoco_qontrol.resource_path = "./resources/"+robot+"/";
112 
113  // start physics thread
114  std::thread physicsthreadhandle( &MujocoQontrol::PhysicsThread, mujoco_qontrol, sim.get(), mujoco_scene.c_str());
115 
116  // start simulation UI loop (blocking call)
117  sim->RenderLoop();
118  physicsthreadhandle.join();
119 
120  return 0;
121 }

Explanation of the code

Declaration

First we declare all the objects that will be used to define our problem.

0 std::shared_ptr<Qontrol::Model::RobotModel<Qontrol::ModelImpl::PINOCCHIO>> model;

We use pinocchio for our model library.

26 std::shared_ptr<Qontrol::JointVelocityProblem> velocity_problem;

The output of our qp controller is at the velocity level.

27 std::shared_ptr<Qontrol::Task::CartesianVelocity<Qontrol::ControlOutput::JointVelocity>> main_task;

The main task is expressed as a Cartesian velocity task.

Initialization

35 void initController( ) override
36 {
37  model =
38  Model::RobotModel<ModelImpl::PINOCCHIO>::loadModelFromFile(resource_path+"robot.urdf");

During initialization we instantiate the model with the robot urdf.

39  velocity_problem = std::make_shared<Qontrol::JointVelocityProblem>(model);

We initialize the problem by giving it the model. By default, the qpmad library is used.

43  main_task = velocity_problem->task_set->add<Task::CartesianVelocity>("MainTask");
44  auto regularisation_task = velocity_problem->task_set->add<Task::JointVelocity>("RegularisationTask",1e-5);

We then fill the task set of velocity_problem with the main task and the regularisation task. Each tasks is given a name and a relative weight \( \omega \). This weight can be modified at any time. In this example, the regularisation tasks is defined as a joint veloicty task. Its means that this task will minimize the overall robot joint veloicty.

45 
46  auto joint_configuration_constraint = velocity_problem->constraint_set->add<Constraint::JointConfiguration>("JointConfigurationConstraint");
47  auto joint_velocity_constraint = velocity_problem->constraint_set->add<Constraint::JointVelocity>("JointVelocityConstraint");

We then fill the constraint set of velocity_problem with the two pre-implemented constraints. Each constraint is given a name. These constraints will automatically be updated during the update of Qontrol.

48 
49  mju_copy(d->qpos, m->key_qpos, m->nu);
50  robot_state.joint_position.resize(ndof);
51  robot_state.joint_velocity.resize(ndof);
52 
53  traj = new TrajectoryGeneration(resource_path + "trajectory.csv",
54  m->opt.timestep);
55 }
56 
57 void updateController() override
58 {
59  const int ndof = model->getNrOfDegreesOfFreedom();
60 
61  for (int i=0; i<ndof ; ++i)
62  {
63  robot_state.joint_position[i] = d->qpos[i];
64  robot_state.joint_velocity[i] = d->qvel[i];
65  }
66  model->setRobotState(robot_state);

We create the robot state and fill it with the simulated robot current state.

67 
68  traj->update();

We create a simple trajectory that has been precalculated and store in a csv file. This trajectory start at the robot current Cartesian pose and does a translation of (-0.1, -0,1, -0.1) m.

Update

The update function is called every milliseconds. At the beginning of each update we fill the new robot state according to the simulated robot.

We also update the trajectory so that it gives the next Cartesian pose to reach in 1 ms.

We then compute the desired Cartesian velocity using a simple proportionnal controller. Pinocchio is used to compute the error between the desired Cartesian pose and the current Cartesian pose. This is done by the log6 function. The p_gains are the proportionnal gains of the controller.

The desired Cartesian velocity is then fed to the main task.

Once we updated the necassary tasks and constraints we can update the whole problem. If a solution to the problem exist we can then get it and send it to the simulated robot.

Main function

The main function function fetches the robot name given in argv and starts the Mujoco simulation.

Qontrol::RobotState
Robot state.
Definition: GenericModel.hpp:35
Qontrol::Constraint::JointVelocity
Implemtentation of a joint velocity constraint.
Definition: JointVelocity.hpp:35
Qontrol::Task::CartesianVelocity
Implementation of a Cartesian velocity task.
Definition: CartesianVelocity.hpp:35
Qontrol::Model::RobotModel
Decalaration of a template specialization for the model library.
Definition: GenericModel.hpp:488
Qontrol::Task::JointVelocity
Implemtentation of a joint velocity task.
Definition: JointVelocity.hpp:35
Qontrol::Constraint::JointConfiguration
Implemtentation of a joint configuration constraint.
Definition: JointConfiguration.hpp:35