Gazebo Math

API Reference

7.5.1
Angle example

This tutorial explains how to use the Angle class from Gazebo Math library.

C++ example

Compile the code

Go to gz-math/examples and use cmake to compile the code:

git clone https://github.com/gazebosim/gz-math/ -b gz-math7
cd gz-math/examples
mkdir build
cd build
cmake ..
make

When the code is compiled, run:

./angle_example

The ouput of the program:

The angle 'a' should be zero: 0
Pi in radians: 3.14159
Pi in degrees: 180
Pi + PI/2 in radians: 4.71239
Normalized to the range -Pi and Pi: -1.5708

Code

The code instantiates an angle class. The default constructed angle should be zero.

There are some predefined angles, such as:

By default, all values are in radians, but you can use the method Degree to convert to degrees.

std::cout << "Pi in radians: " << a << std::endl;
std::cout << "Pi in degrees: " << a.Degree() << std::endl;

The Angle class overloads the +=, and many other, math operators.

Use the method Normalized to bound the value between -PI and PI.

std::cout << "Normalized to the range -Pi and Pi: "
<< a.Normalized() << std::endl;

Ruby example

This example will only work if the Ruby interface library was compiled and installed. For example, on Ubuntu:

sudo apt install ruby-gz-math<#>

Modify the RUBYLIB environment variable to include the Gazebo Math library install path. For example, if you install to /usr:

export RUBYLIB=/usr/lib/ruby:$RUBYLIB

Move to the examples folder:

cd examples

Execute the code:

ruby angle_example.rb

Code

There are some predefined values:

printf("PI in degrees = %f\n", Gz::Math::Angle.Pi.Degree)

Create new objects:

a1 = Gz::Math::Angle.new(1.5707)
a2 = Gz::Math::Angle.new(0.7854)

Use the values in radians or degrees:

printf("a1 = %f radians, %f degrees\n", a1.Radian, a1.Degree)
printf("a2 = %f radians, %f degrees\n", a2.Radian, a2.Degree)

The Angle class overloads math operators.

printf("a1 * a2 = %f radians, %f degrees\n", (a1 * a2).Radian, (a1 * a2).Degree)
printf("a1 + a2 = %f radians, %f degrees\n", (a1 + a2).Radian, (a1 + a2).Degree)
printf("a1 - a2 = %f radians, %f degrees\n", (a1 - a2).Radian, (a1 - a2).Degree)

Normalize the value between -PI and PI.

a3 = Gz::Math::Angle.new(15.707)
printf("a3 = %f radians, %f degrees\n", a3.Radian, a3.Degree)
a3.Normalize
printf("a3.Normalize = %f radians, %f degrees\n", a3.Radian, a3.Degree)