1 of 29

Quaternions and E*gen!

robotics focus group #3

2 of 29

Output a real-time map of surroundings

​

Today:

  • Discussion of hardware and software parts
  • Quaternions
  • Eigen

3 of 29

4 of 29

visual odometry: movement between camera frames

+

3d rigid body motion: fundamentals of understanding how robot mathematically represents space

5 of 29

6 of 29

Why does all this rotation stuff matter?

​

  • We need to take our points from RPLidar to our software by transforming points from our laser coordinate system to our world coordinate system

rosrun tf static_transform_publisher 0 0 0 0 0 0 world laser 100

7 of 29

Robot Discussion

​

Hardware

  • Raspberry Pi Model 4B
  • RPLidar by SlamTec (2D LIDAR points)
    • Points are mapped to a 2D coordinate plane using r distance (time it takes for light beam to return back to LIDAR) and θ angle of rotation. Raw format is polar coordinate
    • We need ROS (RViz) to access the laser mapping tools
  • Chassis: Wheels, motor controllers

​

Software

  • RViz
  • ROS1 (Noetic Nimjemys)

8 of 29

Euler Angles

​

Problems:

  • Gimbal Lock
    • You lose one degree of freedom when two rotations are aligned, so you can’t represent some rotations

9 of 29

3x3 Rotation Matrix

​

Each column represents result of rotation x/y/z axes, respectively

​

0.32 = x-component of transformed x-axis in terms of original x-axis

0.46 = y-component of transformed y-axis in terms of original x-axis

-0.83 = z-component of transformed z-axis in terms of original x-axis

​

10 of 29

Alternatives to Quaternions

3x3 Rotation Matrix (X/Y/Z)

​

Euler Angles

11 of 29

Why Quaternions?

    • Computationally more efficient to calculate when combining multiple orientations
    • Avoids gimbal lock
  • What are they?
    • 3.23+9.46i+2.64j + 3.38k
  • Let’s compare them with complex numbers!
    • The product of 2 complex numbers is another complex number! That can be used to represent a rotation in a 2D plane

​

​

z1=a+bi

z2=c+di

12 of 29

what is a quaternion?

​

​

​

​

where

​

​

​

​

it’s very similar to complex numbers that you are familiar with

​

​

​

13 of 29

another representation

​

​

​

​

​

“ordered pair”

​

​

14 of 29

real quaternions

​

​

​

pure quaternions

​

​

​

​

binary form (uses unit quaternion)

​

​

​

​

​

15 of 29

onto the fun stuff! rotations

16 of 29

rotating pure quaternions orthogonal to q is ok!

​

but happens with quaternions not orthogonal to q?

we no longer get a pure quaternion :(

​

​

genius method:

we post multiply the result by the inverse of q

this gives us a pure quaternion!

​

​

but now we’ve rotated by double…

so we halve the angle, and use

17 of 29

interpolation

​

quaternions overcome gimbal lock!

​

we use something called SLERP, spherical linear interpolation

allows us to smoothly interpolate through space

​

linear interpolation

​

​

​

spherical interpolation

​

​

​

​

18 of 29

we can use exactly this, just with quaternions

​

​

​

​

​

​

​

​

we find the angle by using regular formula between vectors, just with two quaternions

​

​

​

​

​

​

19 of 29

things to note:

  • if dot-product is negative, we have the long way angle, so we just negate one of the directions
  • if angular difference is small, sin becomes 0, then dividing gives undefined result
    • so we use linear interpolation

20 of 29

conversion to and from Euler angles

Conversion_between_quaternions_and_Euler_angles

​

​

​

cameras

in 3d engines, we can encode the direction (“look at”) as a quaternion

​

we can transform world coordinates to camera coordinates and vice versa

by using quaternions!

​

​

21 of 29

trivia

​

​

22 of 29

Why Quaternions?

​

  • Let’s compare them with complex numbers!
    • The product of 2 complex numbers is another complex number! That can be used to represent a rotation in a 2D plane
  • Likewise, the product of 2 quaternion is a rotation in a 3D plane

​

​

z1=a+bi

z2=c+di

23 of 29

Eigen!

​

some helpful library links:

  • geometry module docs
    • 2 ways of representing rotations:
      • not matrix compatible: angles/axis/quaternions

​

AngleAxis<float> aa(angle_in_radian, Vector3f(ax,ay,az));

​

      • matrix compatible: projective/affine transformations

Quaternion<float> q; q = AngleAxis<float>(angle_in_radian, axis);

      • euler angles? Not too convenient on Eigen

24 of 29

Most common Eigen structures for rotations

• Rotation matrix ( 3 × 3 ): Eigen::Matrix3d.� • Rotation vector ( 3 × 1 ): Eigen::AngleAxisd.� • Euler angle ( 3 × 1 ): Eigen::Vector3d.� • Quaternion ( 4 × 1 ): Eigen::Quaterniond.� • Euclidean transformation matrix ( 4 × 4 ): Eigen::Isometry3d. • Affine transform ( 4 × 4 ): Eigen::Affine3d.� • Perspective transformation ( 4 × 4 ): Eigen::Projective3d.

25 of 29

Let’s end off with an example

  • Write a program to convert coordinates from robot 1’s coordinates to robot 2’s coordinate system
  • Robot 1 and Robot 2 are located in the world coordinate system
  • Pose of robot 1: (orientation as a quaternion & translation) q1 = [0.35, 0.2, 0.3, 0.1]T and t1 = [0.3, 0.1, 0.1]T
  • Pose of robot 2: q2 = [−0.5, 0.4, −0.1, 0.2]T , t2 = [−0.1, 0.5, 0.3]T
  • Robot 1 sees point: pR1 = [0.5, 0, 0.2]T . What are these coordinates in robot 2’s coordinate system?

transformation that preserves distances and angles

Iteratively applies the translations

26 of 29

T1W and T2W are instances of Isometry3d

​

T1W is the transformation (rotation being q1 followed by translation by t1)

​

P2 is the answer, resulting transformed point

27 of 29

Now, let’s take a look at some basic matrix things you can do with Eigen !

28 of 29

Robot Discussion

​

Hardware

  • Raspberry Pi Model 4B
  • RPLidar by SlamTec (2D LIDAR points)
    • Points are mapped to a 2D coordinate plane using r distance (time it takes for light beam to return back to LIDAR) and θ angle of rotation. Raw format is polar coordinate
    • We need ROS (RViz) to access the laser mapping tools

​

Software

  • RViz
  • ROS1 (Noetic Nimjemys)

29 of 29

Next few weeks.. the big picture

​

  • Install ROS1 Using Docker