Gazebo Math

API Reference

6.15.1
Angle example

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

C++ example

Compile the code

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

git clone https://github.com/ignitionrobotics/ign-math/ -b ign-math6
cd ign-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.

gz::math::Angle a;

There are some predefined angles, such as:

a = gz::math::Angle::HalfPi;
a = gz::math::Angle::Pi;

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.

a += gz::math::Angle::HalfPi;

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. Modify the RUBYLIB environment variable to include the Ignition Math library install path. For example, if you install to /usr:

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

Execute the code:

ruby angle_example.rb

Code

There are some predefined values:

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

Create new objects:

a1 = Ignition::Math::Angle.new(1.5707)
a2 = Ignition::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 = Ignition::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)
T endl(T... args)