blob: 2531967b3c9296a628947f2a5093630c29c91f80 [file] [log] [blame]
Austin Schuh7400da02018-01-28 19:54:58 -08001/*!
2@page rbd_tutorials ct rbd Tutorials
3
4- \subpage rbd_tut_modelling
5- \subpage rbd_tut_forward_dynamics
6- \subpage rbd_tut_types
7
8This tutorial is specific for the RBD Module of the Control Toolbox. For installation instructions and an overview see the main CT documentation.
9
10\page rbd_tut_modelling Model a Rigid Body Dynamics system
11@tableofcontents
12
13In this tutorial, we will create a simple Rigid Body Dynamics system.
14
15\section rbd_tut_modelling_robcogen Creating an new model
16
17The Rigid Body Dynamics module of the Control Toolbox relies on <a href="https://robcogenteam.bitbucket.io">RobCoGen</a>, a code-generation framework for Rigid Body Dynamics and Kinematics. This involves
18a separate code generation step for creating the dynamics and kinematics equations prior to using your model in the Control Toolbox. However, as of writing this documentation, RobCoGen is the only Rigid
19Body Dynamics framework that supports Auto-Differentiation, a major ingredient for fast Optimal Control. We plan to support more modelling frameworks in the future.
20
21\subsection rbd_tut_robcogen Step 1: Create your model using RobCoGen
22As a first step, download RobCoGen version 0.4ad.0 from the <a href="https://robcogenteam.bitbucket.io/binary.html">official website</a>. Follow the instruction on the website for creating your kindsl and
23dtdsl files. Generate all C++ outputs.
24
25\subsection rbd_tut_robcogen_import Step 2: Create a model for CT
26As a next step, we suggest to first create a new package
27
28\code{.sh}
29cd catkin_ws/src
30catkin_create_pkg my_robot ct_rbd
31\endcode
32
33As you can see, we made our package depend on ct_rbd. Next, we create the folder structure
34
35\code{.sh}
36cd my_robot
37mkdir -p include/my_robot/generated
38\endcode
39
40Copy all files generated by RobCoGen into this folder. We generally recommend adding your kindsl/dtdsl files to a subfolder called "model" as well for later reference.
41
42Next, edit the CMakeLists.txt in your package and add the following lines:
43
44\code{.cmake}
45include_directories(
46 include
47 ${catkin_INCLUDE_DIRS}
48)
49\endcode
50
51Next, create a header file for your robot in include/my_robot/MyRobot.h with the following content
52
53
54\code{.cpp}
55
56#pragma once
57
58#include <Eigen/Core>
59#include <Eigen/StdVector>
60
61#include "generated/declarations.h"
62#include "generated/jsim.h"
63#include "generated/jacobians.h"
64#include "generated/traits.h"
65#include "generated/forward_dynamics.h"
66#include "generated/inertia_properties.h"
67#include "generated/inverse_dynamics.h"
68#include "generated/transforms.h"
69#include "generated/link_data_map.h"
70
71// define namespace and base
72#define ROBCOGEN_NS ct_my_robot
73#define TARGET_NS MyRobot
74
75// define the links
76#define CT_BASE fr_MyRobotBase
77#define CT_L0 fr_Shoulder_AA
78#define CT_L1 fr_Shoulder_FE
79#define CT_L2 fr_Humerus_R
80#define CT_L3 fr_Elbow_FE
81#define CT_L4 fr_Wrist_R
82#define CT_L5 fr_Wrist_FE
83
84// define single end effector (could also be multiple)
85#define CT_N_EE 1
86#define CT_EE0 fr_ee
87#define CT_EE0_IS_ON_LINK 5
88#define CT_EE0_FIRST_JOINT 0
89#define CT_EE0_LAST_JOINT 5
90
91#include <ct/rbd/robot/robcogen/robcogenHelpers.h>
92
93\endcode
94
95You need to adjust a few variables. First, set the namespace (ROBCOGEN_NS) which the RobCoGen files have been created in. Also, you can define a target namespace (MyRobot). Next, you need to define your base link (CT_BASE, which is either floating or fixed, depending on your kindsl). Afterwards, define all links (CT_L0, CT_L1, CT_L2, ...) by using the frames you defined in RobCoGen. Last but not least, you can but do not have to specify endeffectors. Endeffectors can be useful for contacts or optimal control. If in doubt, skip these for now.
96
97Once you need end-effectors, specify their number (CT_N_EE). Then, for each end-effector, specify its name in RobCoGen (CT_EEx) and which link it is rigidly attached to (CT_EEx_IS_ON_LINK). Then specify the first joint in the kinematic chain leading up to the end-effector (CT_EEx_FIRST_JOINT, often but not necessarily 0) as well as the last joint (CT_EEx_LAST_JOINT). The Control Toolbox assumes that all joint numbers in between those two also influence the end-effector.
98
99Above steps automatically create the following useful types:
100
101\code{.cpp}
102ct::rbd::MyRobot::Dynamics myRobotDynamics; // an implementation of ct::rbd::Dynamics for your robot
103ct::rbd::MyRobot::tpl::Dynamics<SCALAR> myRobotDynamicsTpl; // an implementation of ct::rbd::Dynamics for your robot templated on the scalar type!
104
105ct::rbd::MyRobot::Kinematics myRobotKinematics; // an implementation of ct::rbd::Kinematics for your robot
106\endcode
107
108The Dynamics and Kinematics class have some useful functions including computing forward and inverse dynamics as well as transforms and Jacobians. Please refer to the documentation for more details.
109
110That's it! You can verify your setup by looking into the ct models package, e.g. at the HyA robot. Feel free to check out our tutorials on how to \ref rbd_tut_forward_dynamics as well as \ref rbd_tut_types.
111
112
113
114
115
116\page rbd_tut_forward_dynamics Simulate your robot and create a controller
117@tableofcontents
118
119Do you still remember the \ref tut_basics? If not, refresh your mind really quickly now. Great! As you remember, to forward simulate a system or apply any control to it, we first have to make it a ct::core::System or if controllable a ct::core::ControlledSystem.
120
121\section rbd_tut_create_system Creating a ControlledSystem for our robot
122Effectively, we want to turn our robot into a Ordinary Differential Equation (ODE) of the form \f[ \dot(x) = f(x(t),u(t)) \f] . However, this raises the question what our state x and control u are. For a Rigid Body Dynamics system there are multiple choices. If we assume a kinematic model, the state consists of joint positions and control inputs are the joint velocities. If we use a dynamic model, the state will most likely be joint positions and velocities while the control input are joint forces/torques. This is the case that we will look at in this example. However, keep in mind there are multiple choices for formulating the ODE.
123
124The Control Toolbox provides two handy wrappers, that transform the forward dynamics of a Rigid Body system into a ct::core::ControlledSystem. For fixed-base system, this is the class ct::rbd::FixBaseFDSystem and for floating-base system this is ct::rbd::FloatingBaseFDSystem. Take a look at those files to see the implementation details.
125
126You can simply instantiate your dynamics using those classes
127
128\code{.cpp}
129 // obtain the state dimension
130 const size_t STATE_DIM = FixBaseFDSystem<MyRobot::Dynamics>::STATE_DIM;
131
132 // create an instance of the system
133 std::shared_ptr<core::System<STATE_DIM>> dynamics(new FixBaseFDSystem<MyRobot::Dynamics>);
134\endcode
135
136Please look at the documentation to find other ct::rbd::Dynamics methods that you can potentially use.
137
138
139
140\section rbd_tut_simulate_system Simulate the forward dynamics
141
142In order to forward simulate the dynamics, we can simply call an integrator
143
144\code{.cpp}
145 core::Integrator<STATE_DIM> integrator(dynamics, ct::core::RK4);
146
147 core::StateVector<12> state;
148 state.setZero();
149
150 integrator.integrate_n_steps(state, 0, 1000, 0.001);
151\endcode
152
153While we are using a regular integrator in the example above, all ct/rbd models can also be used with our ct::core::SymplecticIntegrator which allows for bigger step sizes.
154
155
156\section rbd_tut_control Control and Linearization
157
158Now that our robot is a ct::core::ControlledSystem we can throw all sorts of great control algorithms at it! If we want to compute an LQR controller for the forward dynamics, you can simply follow our tutorial on \ref optcon_tut_lqr. Or maybe even want throw an MPC controller at it (see \ref optcon_tut_mpc)? Do not forget to check out ct::rbd::FixBaseNLOC and ct::rbd::FloatingBaseNLOCContactModel to quickly generate a Nonlinear Optimal Control setup for your robot!
159
160\warn In most cases we need the linearization for our system. Here we have the same choices as indicated in \ref core_tut_linearization. The same characteristics apply and for best accuracy and speed we recommend using Auto-Differentiation with code generation. However, for quick prototyping numerical differentiation can be appealing. Due to the special structure of Rigid Body Dynamics systems, we can compute a slightly better linearization which mixes analytic and numerical derivatives to improve accuracy and speed over pure numerical derivatives. This class is called ct::rbd::RbdLinearizer.
161
162
163
164\page rbd_tut_types Useful RBD types
165There are several useful types in the ct RBD package. This is just a loose collection of a few useful classes and types. Please refer to the individual documentation for more information.
166
167* ct::rbd::Dynamics and ct::rbd::Kinematics - forward/inverse dynamics, transforms and Jacobians
168* ct::rbd::RBDState - the state of a Rigid Body Dynamics system in minimal coordinates
169* ct::rbd::RigidBodyState - the state of a single 6-DoF Rigid Body
170* ct::rbd::IDControllerFB - a floating base inverse dynamics controller
171
172*/