|
bitsim
Model, simulate, estimate and control robots in C++
|
This example accompanies the second lesson of the robotics course and builds on Frames. There we used frames and let Chrono handle the orientation. Now we open up that part. You need bitsim configured with Chrono support to run it.
A frame has a position and an orientation. A rotation matrix is one way to write the orientation. It is a \(3 \times 3\) matrix, and it has a very direct meaning: its columns are the axes of the frame, seen from the parent.
We keep the robot and the sensor of the previous lesson, and the same notation. R_AB is the orientation of frame \(B\) expressed in frame \(A\), so the inner letters still cancel: R_WR * R_RS gives R_WS and R_WR * v_R gives v_W.
Any rotation can be built from rotations around the three coordinate axes:
\[R_x(\theta) = \begin{bmatrix} 1 & 0 & 0 \\ 0 & c & -s \\ 0 & s & c \end{bmatrix} \quad R_y(\theta) = \begin{bmatrix} c & 0 & s \\ 0 & 1 & 0 \\ -s & 0 & c \end{bmatrix} \quad R_z(\theta) = \begin{bmatrix} c & -s & 0 \\ s & c & 0 \\ 0 & 0 & 1 \end{bmatrix} \]
with \(c = \cos\theta\) and \(s = \sin\theta\). A positive angle turns counterclockwise when the axis points at you. Notice that each matrix leaves its own axis unchanged, and that the signs in \(R_y\) are the other way around compared to the other two. The example writes these out by hand and checks them against Chrono, which builds the same matrices from an angle and an axis:
Not every matrix is a rotation. A rotation keeps lengths and angles, and does not mirror things. That gives two conditions:
\[R^T R = I \quad \text{and} \quad \det(R) = +1 \]
The first says that the columns are unit vectors at right angles to each other. The second says that they form a right-handed frame. The first condition also gives us the most useful property of all:
\[R^{-1} = R^T \]
Inverting a rotation is just a transpose, and R_RW is R_WR.transpose(). bitsim checks a matrix with bitsim::geometry::is_rotation; the elementary rotations and the yaw, pitch and roll angles of this example are in bitsim/geometry/rotations.h too.
The two counter-examples below fail for different reasons. Flipping an axis passes the first condition but gives \(\det(R) = -1\), a mirror image. Scaling fails the first condition since it changes lengths.
A rotation matrix re-expresses a vector in another frame. Take a robot that drives forward: in its own frame the velocity is always along \(x\), but in the world it depends on where the robot is facing.
\[v_W = R_{WR} \, v_R \]
It is the same physical vector written in different coordinates, so its length does not change. Unlike the points of the previous lesson there is no translation here: velocities are directions, and only the rotation applies.
This is the most important section of the lesson. The order of rotations matters.
We want the sensor to look to the left of the robot and 30 degrees down, so we need one turn and one tilt. Reading a product from left to right, each rotation is applied around the axes of the frame left by the previous one:
The same two rotations give a different result. Chaining rotations works exactly as for frames:
The beam is the \(x\) axis of the sensor. Knowing its direction in the world, and the sensor position from the previous lesson, we can work out where it hits the floor. Try this by hand: the sensor is 0.1 m high and the beam drops 0.5 m per metre travelled along it, so the range is 0.2 m.
Nine numbers are a lot for three degrees of freedom. A common compact description is yaw \(\psi\), pitch \(\theta\) and roll \(\phi\):
\[R = R_z(\psi) \, R_y(\theta) \, R_x(\phi) \]
Reading from the left: turn around \(z\), then pitch around the new \(y\), then roll around the new \(x\). The angles can be recovered from the matrix:
\[\psi = \text{atan2}(R_{10}, R_{00}) \quad \theta = -\arcsin(R_{20}) \quad \phi = \text{atan2}(R_{21}, R_{22}) \]
These formulas break down at \(\theta = \pm 90^\circ\). This is gimbal lock. With the pitch at \(90^\circ\) the yaw and the roll turn around the same axis and only their difference matters, so the angles \((30, 90, 0)\) and \((50, 90, 20)\) give exactly the same matrix. The rotation itself is fine, it is the three-angle description that fails. That is one reason why simulators store orientation as a quaternion, as Chrono does.
CHRONO_RobotPose::rotation_matrix() gives the current R_WR of a body. It changes as the simulation advances, but R_RS does not because the sensor is rigidly mounted. After turning the robot by \(90^\circ\) the beam points somewhere else, and the matrix is still a proper rotation.
With --render an Irrlicht window shows the two orders of section 4 side by side, and repeats the motion. Both start from the same pose and both end up with the same two rotations applied, but only one of them reaches the floor. The panels show the matrix \(R_{RS}\) as it is built, so you can check that its first column is the beam.
In this scene the sensor sits on a 0.6 m mast, since at 0.1 m the beam hits the floor too close to the robot to see. The robots face along the world \(x\) axis, so what you see is \(R_{RS}\) alone.
With --gimbal the window shows gimbal lock on a real gimbal: three nested rings, each spinning around the axis of its colour. The blue ring is the yaw around \(z\), the green ring is the pitch around \(y\) and the red ring is the roll around \(x\). The body in the middle is the small frame.
Both gimbals start from a yaw of \(30^\circ\). The left one pitches to \(30^\circ\) and the right one to \(90^\circ\). Then both add \(20^\circ\) of yaw, and finally \(20^\circ\) of roll, which takes the right one from \((30, 90, 0)\) to \((50, 90, 20)\) as in section 5.