The following example solves a qp problem expressed at the torque level such that:
\begin{equation}\begin{array}{ccc}\boldsymbol{\tau}^{opt} = & \underset{\boldsymbol{\tau}}{\mathrm{argmin}} & ||\boldsymbol{\dot{v}}(\boldsymbol{\tau}) - \boldsymbol{\dot{v}}^{target} || + \omega || \boldsymbol{\tau} - (\boldsymbol{g} - \boldsymbol{\dot{q}})||^2_{M^{-1}}\\& \textrm{s.t.} & \boldsymbol{\tau^{min}} \leq \boldsymbol{\tau} \leq \boldsymbol{\tau^{max}}. \\ & & \boldsymbol{q}^{min} \leq \boldsymbol{q}(\boldsymbol\tau) \leq \boldsymbol{q}^{max} \\ & & \boldsymbol{\dot{q}}^{min} \leq \boldsymbol{\dot{q}}(\boldsymbol\tau) \leq \boldsymbol{\dot{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:
./torqueQontrol robot_name
where robot_name can be either panda or ur5
Full code
15 #include "mujoco/mujoco_sim.h"
16 #include "Qontrol/Qontrol.hpp"
17 #include "trajectory_generation/trajectory_generation.h"
19 using namespace Qontrol;
21 class MujocoQontrol :
public MujocoSim
25 std::shared_ptr<Model::RobotModel<ModelImpl::PINOCCHIO>> model;
26 std::shared_ptr<JointTorqueProblem> torque_problem;
27 std::shared_ptr<Task::CartesianAcceleration<ControlOutput::JointTorque>> main_task;
28 std::shared_ptr<Task::JointTorque<ControlOutput::JointTorque>> regularisation_task;
31 TrajectoryGeneration* traj;
32 std::string resource_path;
34 void initController()
override
39 const int ndof = model->getNrOfDegreesOfFreedom();
41 torque_problem = std::make_shared<Qontrol::JointTorqueProblem>(model);
43 regularisation_task = torque_problem->task_set->add<
Task::JointTorque>(
"RegularisationTask",1e-5);
46 auto joint_velocity_constraint = torque_problem->constraint_set->add<
Constraint::JointVelocity>(
"JointVelocityConstraint");
47 auto joint_torque_constraint = torque_problem->constraint_set->add<
Constraint::JointTorque>(
"JointTorqueConstraint");
49 mju_copy(d->qpos, m->key_qpos, m->nu);
50 robot_state.joint_position.resize(ndof);
51 robot_state.joint_velocity.resize(ndof);
53 traj =
new TrajectoryGeneration(resource_path+
"trajectory.csv", m->opt.timestep);
56 void updateController()
override
58 const int ndof = model->getNrOfDegreesOfFreedom();
60 for (
int i=0; i<ndof ; ++i)
62 robot_state.joint_position[i] = d->qpos[i];
63 robot_state.joint_velocity[i] = d->qvel[i];
65 model->setRobotState(robot_state);
68 pinocchio::SE3 traj_pose(traj->pose.matrix());
70 pinocchio::SE3 current_pose(model->getFramePose(model->getTipFrameName()).matrix());
71 const pinocchio::SE3 tipMdes = current_pose.actInv(traj_pose);
72 auto err = pinocchio::log6(tipMdes).toVector();
74 Eigen::Matrix<double, 6, 1> p_gains;
75 p_gains << 1000, 1000, 1000, 1000, 1000, 1000;
77 Eigen::Matrix<double, 6, 1> d_gains = 2.0 * p_gains.cwiseSqrt();
78 Eigen::Matrix<double, 6, 1> xdd_star =
79 p_gains.cwiseProduct(err) +
80 d_gains.cwiseProduct(traj->velocity - model->getFrameVelocity(model->getTipFrameName())) + traj->acceleration;
82 main_task->setTargetAcceleration(xdd_star);
83 regularisation_task->setTargetTorque(model->getJointGravityTorques() -
84 robot_state.joint_velocity);
85 regularisation_task->setWeightingMatrix(
86 model->getInverseJointInertiaMatrix());
87 torque_problem->update(m->opt.timestep);
89 if (torque_problem->solutionFound())
91 sendJointTorque(torque_problem->getJointTorqueCommand());
97 int main(
int argc,
const char** argv) {
98 MujocoQontrol mujoco_qontrol;
99 Qontrol::Log::Logger::parseArgv(argc, argv);
102 mjv_defaultCamera(&cam);
105 mjv_defaultOption(&opt);
108 mjv_defaultPerturb(&pert);
111 auto sim = std::make_unique<mj::Simulate>(
112 std::make_unique<mj::GlfwAdapter>(),
113 &cam, &opt, &pert,
false
116 std::string robot = argv[1];
117 std::string mujoco_scene =
"./resources/"+robot+
"/scene.xml";
118 mujoco_qontrol.resource_path =
"./resources/"+robot+
"/";
121 std::thread physicsthreadhandle( &MujocoQontrol::PhysicsThread, mujoco_qontrol, sim.get(), mujoco_scene.c_str());
125 physicsthreadhandle.join();
Explanation of the code
Declaration
First we declare all the objects that will be used to define our problem.
0 std::shared_ptr<Model::RobotModel<ModelImpl::PINOCCHIO>> model;
We use pinocchio for our model library.
25 std::shared_ptr<JointTorqueProblem> torque_problem;
The output of our qp controller is at the torque level.
26 std::shared_ptr<Task::CartesianAcceleration<ControlOutput::JointTorque>> main_task;
We then declare two tasks that will be updated every milliseconds.
The main task is expressed as a Cartesian Acceleration task.
27 std::shared_ptr<Task::JointTorque<ControlOutput::JointTorque>> regularisation_task;
And we add a regularisation task (also at the torque level).
Initialization
34 void initController()
override
37 Model::RobotModel<ModelImpl::PINOCCHIO>::loadModelFromFile(resource_path+
"robot.urdf");
During initialization we instantiate the model with the robot urdf.
We initialize the problem by giving it the model. By default, the qpmad library is used.
39 const int ndof = model->getNrOfDegreesOfFreedom();
41 torque_problem = std::make_shared<Qontrol::JointTorqueProblem>(model);
42 main_task = torque_problem->task_set->add<Task::CartesianAcceleration>(
"MainTask");
43 regularisation_task = torque_problem->task_set->add<Task::JointTorque>(
"RegularisationTask",1e-5);
We then fill the task set of torque_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.
45 auto joint_configuration_constraint = torque_problem->constraint_set->add<Constraint::JointConfiguration>(
"JointConfigurationConstraint");
46 auto joint_velocity_constraint = torque_problem->constraint_set->add<Constraint::JointVelocity>(
"JointVelocityConstraint");
47 auto joint_torque_constraint = torque_problem->constraint_set->add<Constraint::JointTorque>(
"JointTorqueConstraint");
We then fill the constraint set of torque_problem with the three pre-implemented constraints. Each constraint is given a name. These constraints will automatically be updated during the update of Qontrol.
49 mju_copy(d->qpos, m->key_qpos, m->nu);
50 robot_state.joint_position.resize(ndof);
51 robot_state.joint_velocity.resize(ndof);
53 traj =
new TrajectoryGeneration(resource_path+
"trajectory.csv", m->opt.timestep);
56 void updateController()
override
58 const int ndof = model->getNrOfDegreesOfFreedom();
60 for (
int i=0; i<ndof ; ++i)
62 robot_state.joint_position[i] = d->qpos[i];
63 robot_state.joint_velocity[i] = d->qvel[i];
65 model->setRobotState(robot_state);
We also create the robot state and fill it with the simulated robot current state.
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 acceleration using a simple PD 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 and d_gains variables are the gains of the PD controller.
The desired Cartesian acceleration is then fed to the main task. The regularisation task is also updated so that it compensate for gravity plus a damping term. The resulting regularisation task would be written : \( || \boldsymbol{\tau} - (\boldsymbol{g} - \boldsymbol{\dot{q}})||^2_{M^{-1}}\)
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.