How to Move the Turtlesim Robot to Goal Locations – ROS

In this tutorial, we will build ROS nodes to move the turtlesim robot to goal locations.

Real-World Applications

This project has a number of real-world applications: 

  • Indoor Delivery Robots
  • Mapping of Underground Mines, Caves, and Hard-to-Reach Environments
  • Robot Vacuums
  • Order Fulfillment
  • Factories

You can imagine the turtlesim robot is like having an aerial view of a mobile robot moving around on a factory floor. We want to be able to navigate the robot around the floor to do its work, and in order to navigate it, we imagine the factory floor is like an x-y graph (e.g. like the image below from this tutorial). All we would need to do is enter an x-y coordinate, and the robot would move to that goal location (i.e. waypoint).

state-space-model-4JPG

Prerequisites

Run the Turtlesim Simulation

To run the turtlesim simulation, open a terminal window and launch ROS.

roscore

See what nodes are available to run by typing:

rosrun turtlesim <TAB><TAB>
1-nodes-available-to-run

Open another terminal window, and launch the turtlesim_node.

rosrun turtlesim turtlesim_node

Let’s steer the robot.

rosrun rqt_robot_steering rqt_robot_steering

You should see a window pop up. 

2-rqt-robot-steering

You’ll note that the default ROS topic is the /cmd_vel topic. This topic is where the rqt_robot_steering node will publish Twist messages. A Twist message in ROS is a special kind of topic that consists of the x, y, and z components of the linear and angular velocity.

Go back to the terminal window, and let’s see what topic the turtlesim node is subscribed to.

rostopic list

The turtlesim node gets its velocity commands from the /turtle1/cmd_vel topic.

List all the active nodes.

rosnode list

Find out what the /turtlesim node is subscribing to.

rosnode info /turtlesim
4-ros-node-info-turtlesim

You can see that the /turtlesim node is subscribing to /turtle1/cmd_vel

Let’s find out more about this topic.

rostopic info /turtle1/cmd_vel

You can see that the type of message that passes through the /turtle1/cmd_vel topic is a Twist message.

3-rostopic-list-info

Let’s change the topic name of the rqt_steering GUI to /turtle1/cmd_vel.

6-change-topic-name

If you adjust the sliders for the linear and angular velocities, you will see the turtle move accordingly because the GUI is publishing linear and angular velocity values to the /turtle1/cmd_vel topic. The /turtlesim node reads the velocity values on this topic and then moves accordingly.

In a real-world robot, we can see how the rqt_steering node is useful. We can publish velocity commands to the /cmd_vel topic. A motor controller that subscribes to the /cmd_vel topic can convert those velocity commands into motor commands to make the robot drive accordingly.

Create a Package

Now that we’ve gotten reacquainted with turtlesim, let’s see how we get into the purpose of this tutorial: create a ROS node to move the turtlesim robot to a goal location.

The first thing we need to do is create a package.

Open a new terminal window.

Go to the src folder of your catkin_ws folder.

cd ~/catkin_ws/src
catkin_create_pkg go_to_goal_turtlesim std_msgs rospy roscpp

Move inside the package.

cd go_to_goal_turtlesim 

Open the CMakeLists.txt file. 

gedit CMakeLists.txt

Enable C++11 support be removing the hashtag # on line 5 by uncommenting the add_compile_option… line.

Here is how that should look now.

7-uncomment-line-5

How to Move the Robot to a Desired X Coordinate

Create the ROS Node

Now let’s create a program (i.e. ROS Node) that will take the coordinates of the turtle as input and will output a velocity command as a Twist message. The velocity command will be calculated so as to get the turtlesim robot to move to a goal x coordinate that can be any number from 0 to 11.

In this first node, we will create a ROS node to move the turtlesim robot to a goal x coordinate.

The first thing we need to do is to open a new terminal window.

Move to the src folder of the go_to_goal_turtlesim package.

cd ~/catkin_ws/src/go_to_goal_turtlesim/src

Create a new C++ program called go_to_goal_x.cpp.

gedit go_to_goal_x.cpp

Add the following code to the file, and then click Save.

/// @file go_to_goal_x.cpp
/// @author Addison Sears-Collins
/// @date May 4, 2021
/// 
/// This program takes the coordinates of the turtlesim robot as input
/// and outputs a velocity command as a Twist message. The velocity command
/// will be calculated so as to get the turtlesim robot to move to a goal 
/// x coordinate that can be any number from 0 to 11.

#include <cstdlib> // Use for the absolute value method abs()
#include <iostream> // Enables command line input and output

#include "ros/ros.h" // Necessary header files for ROS
#include "geometry_msgs/Twist.h" // Twist messages (linear & angular velocity)
#include "geometry_msgs/Pose2D.h" // x, y position and theta orientation
#include "turtlesim/Pose.h" // x, y, theta, linear & angular velocity

// Remove the need to use std:: prefix
using namespace std;

// Key variable declarations 
geometry_msgs::Twist velCommand; // Linear and angular velocity in m/s 
geometry_msgs::Pose2D current; // Current x, y, and theta 
geometry_msgs::Pose2D desired; // Desired x, y, and theta 

// Goal x-value, which can be any number from 0 to 11 (inclusive)
const double GOAL = 1.3;

// The gain K, which is used to calculate the linear velocity
const double K_l = 1.0;

// The distance threshold in meters that will determine when 
// the turtlesim robot successfully reaches the goal.
const double distanceTolerance = 0.1;

// Initialized variables and take care of other setup tasks
void setup() {

  // Desired x goal coordinate
  desired.x = GOAL;
  
  // Initialize the Twist message.
  // Initial linear and angular velocities are 0 m/s and rad/s, respectively.
  velCommand.linear.x = 0.0;
  velCommand.linear.y = 0.0;
  velCommand.linear.z = 0.0;
  velCommand.angular.x = 0.0;
  velCommand.angular.y = 0.0;
  velCommand.angular.z = 0.0;
}

// Get the distance between the current x coordinate and 
// the desired x coordinate.
double getDistanceToGoal() {
  return desired.x - current.x;
}

// If we haven't yet reached the goal, set the velocity value.
// Otherwise, stop the robot.
void setVelocity() {
  if (abs(getDistanceToGoal()) > distanceTolerance) {

    // The magnitude of the robot's velocity is directly
    // proportional to the distance the robot is from the 
    // goal.
    velCommand.linear.x = K_l * getDistanceToGoal();
  }
  else {
    cout << "Goal has been reached!" << endl << endl;
    velCommand.linear.x = 0;
  }
}

// This callback function updates the current position and 
// orientation of the robot. 
void updatePose(const turtlesim::PoseConstPtr &currentPose) {
  current.x = currentPose->x;
  current.y = currentPose->y;
  current.theta = currentPose->theta;
}

int main(int argc, char **argv) {

  setup();  

  // Initiate ROS
  ros::init(argc, argv, "go_to_goal_x");
    
  // Create the main access point to communicate with ROS
  ros::NodeHandle node;

  // Subscribe to the robot's pose
  // Hold no messages in the queue. Automatically throw away 
  // any messages that are received that are not able to be
  // processed quickly enough.
  // Every time a new pose is received, update the robot's pose.
  ros::Subscriber currentPoseSub =
    node.subscribe("turtle1/pose", 0, updatePose);

  // Publish velocity commands to a topic.
  // Hold no messages in the queue. Automatically throw away 
  // any messages that are received that are not able to be
  // processed quickly enough.
  ros::Publisher velocityPub =
    node.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 0);

  // Specify a frequency that want the while loop below to loop at
  // In this case, we want to loop 10 cycles per second
  ros::Rate loop_rate(10); 

  // Keep running the while loop below as long as the ROS Master is active. 
  while (ros::ok()) {

    // Here is where we call the callbacks that need to be called.
    ros::spinOnce();

    // After we call the callback function to update the robot's pose, we 
    // set the velocity values for the robot.
    setVelocity();

    // Publish the velocity command to the ROS topic
    velocityPub.publish(velCommand);

    // Print the output to the console
    cout << "Current x = " << current.x << endl
         << "Desired x = " << desired.x <<  endl
         << "Distance to Goal = " << getDistanceToGoal() << " m" << endl
         << "Linear Velocity (x) = " << velCommand.linear.x << " m/s" << endl
         << endl;

    // Sleep as long as we need to to make sure that we have a frequency of
    // 10Hz
    loop_rate.sleep();
  }

  return 0;
}

Now we need to add the C++ program we just wrote to the CMakeLists.txt file so that we can compile the program and launch it using ROS. Each time we write a new C++ ROS node, we have to make sure we add it to the CMakeLists.txt file.

cd ~/catkin_ws/src/go_to_goal_turtlesim
gedit CMakeLists.txt

Go to the bottom of the file.

Add the following lines.

8-add-lines-to-cmakelists-1

Save the file, and close it.

Go to the root of the workspace.

cd ~/catkin_ws

Compile the code.

catkin_make

Launch the ROS Node

Now let’s launch the ROS node we created in the last section.

Open a new terminal window.

Start ROS.

roscore

Open another terminal window, and launch the turtlesim_node.

rosrun turtlesim turtlesim_node
9-launch-turtlesim-node

We can see that the starting pose of the robot is as follows:

  • x = 5.544445
  • y = 5.544445
  • theta = 0.000000
10-starting-x-location
Sorry the font is so small.

Open another terminal window, and launch the go_to_goal_x node.

rosrun go_to_goal_turtlesim go_to_goal_x
11-launch-node
12-goal-has-been-reached
13-goal-has-been-reached

How to Move the Robot to a Desired X and Y Coordinate (i.e. Waypoint)

Create the Waypoint Publisher ROS Node

In this section, we’ll write a ROS node that can accept a waypoint (i.e. goal x and y coordinate) from the user. We want to then publish that goal location to a ROS topic named “waypoint”. 

We will later write a ROS node that can read the waypoint and navigate the turtlesim robot to that waypoint.

First, move to the src folder of the go_to_goal_turtlesim package.

cd ~/catkin_ws/src/go_to_goal_turtlesim/src

Create a new C++ program called waypoint_publisher.cpp.

gedit waypoint_publisher.cpp

Add the following code to the file, and then click Save.

/// @file waypoint_publisher.cpp
/// @author Addison Sears-Collins
/// @date May 6, 2021
/// 
/// This program takes the user's desired waypoint (i.e. goal location 
/// or x,y coordinate) as input and publishes it to a topic as a 
/// geometry_msgs/Pose2D data type.

#include <iostream> // Enables command line input and output

#include "ros/ros.h" // Necessary header files for ROS
#include "geometry_msgs/Pose2D.h" // x, y position and theta orientation

// Remove the need to use std:: prefix
using namespace std;

// Key variable declarations 
geometry_msgs::Pose2D waypoint; // Goal location ... x, y, and theta 

// Ask user for the desired waypoint...Where do you want the robot to move to?
void set_waypoint() {

  cout << "Where do you want the robot to go?" << endl; 
  cout << "Enter waypoint x: ";
  cin >> waypoint.x;
  cout << "Enter waypoint y: ";
  cin >> waypoint.y;
  cout << endl;  
}

int main(int argc, char **argv) { 

  // Initiate ROS
  ros::init(argc, argv, "waypoint_publisher");
    
  // Create the main access point to communicate with ROS
  ros::NodeHandle node;

  // Publish waypoint to a topic.
  // Hold no messages in the queue. Automatically throw away 
  // any messages that are not able to be processed quickly enough.
  ros::Publisher waypointPub =
    node.advertise<geometry_msgs::Pose2D>("waypoint", 0);

  // Keep running the while loop below as long as the ROS Master is active. 
  while (ros::ok()) {

    // Ask the user where he wants the robot to go
    set_waypoint();

    // Publish the waypoint to the ROS topic
    waypointPub.publish(waypoint);

  }

  return 0;
}

Now we need to add the C++ program we just wrote to the CMakeLists.txt file.

cd ~/catkin_ws/src/go_to_goal_turtlesim
gedit CMakeLists.txt

Go to the bottom of the file.

Add the following lines.

14-add-lines-to-cmakelists

Save the file, and close it.

Go to the root of the workspace.

cd ~/catkin_ws

Compile the code.

catkin_make

Launch the ROS Node

Now let’s launch the ROS node we created in the last section.

Open a new terminal window.

Start ROS.

roscore

Open another terminal window, and launch the waypoint_publisher node.

rosrun go_to_goal_turtlesim waypoint_publisher

Here is what you will see in the terminal.

15-run-ros-node-1

Open a new terminal, and check out the active topics.

rostopic list

We can see one of the topics is /waypoint.

Let’s see what messages are passing through this topic.

rostopic echo /waypoint

You can see the pose of the robot (i.e. x, y, and theta values) is being published to the topic named /waypoint.

16-rostopic-echo-1
17-rostopic-echo-2

Create a ROS Node to Move the Robot to the Waypoint (i.e. X-Y Coordinate)

Now we need to create a ROS node that subscribes to the /waypoint topic and publishes a velocity command to move the turtle to the user’s desired waypoint (i.e. x-y coordinate).

Open a new terminal window.

Move to the src folder of the go_to_goal_turtlesim package.

cd ~/catkin_ws/src/go_to_goal_turtlesim/src

Create a new C++ program called go_to_goal_x_y.cpp.

gedit go_to_goal_x_y.cpp

Add the following code to the file, and then click Save.

/// @file go_to_goal_x_y.cpp
/// @author Addison Sears-Collins
/// @date May 7, 2021
/// 
/// This program takes the coordinates of the turtlesim robot and a goal 
/// waypoint (published by waypoint_publisher.cpp) as input and outputs 
/// a velocity command as a Twist message. The velocity command
/// will be calculated so as to get the turtlesim robot to move to the user's
/// desired goal waypoint (x, y coordinate). The x and y values can be any 
/// number from 0 to 11.

#include <cstdlib> // Use for the absolute value method abs()
#include <iostream> // Enables command line input and output

#include "ros/ros.h" // Necessary header files for ROS
#include "geometry_msgs/Twist.h" // Twist messages (linear & angular velocity)
#include "geometry_msgs/Pose2D.h" // x, y position and theta orientation
#include "turtlesim/Pose.h" // x, y, theta, linear & angular velocity

// Remove the need to use std:: prefix
using namespace std;

// Key variable declarations 
geometry_msgs::Twist velCommand; // Linear and angular velocity in m/s 
geometry_msgs::Pose2D current; // Current x, y, and theta 
geometry_msgs::Pose2D waypointGoal; // Waypoint x, y, and theta (the waypoint)
ros::Publisher velocityPub; // Object used for publishing velocity command

const double PI = 3.141592654;

// The gain K, which is used to calculate the linear velocity
const double K_l = 0.5;

// The gain K, which is used to calculate the angular velocity
const double K_a = 0.5;

// The distance threshold in meters that will determine when 
// the turtlesim robot successfully reaches the goal.
const double distanceTolerance = 0.1;

// The angle threshold in radians that will determine when 
// the turtlesim robot successfully reaches the goal.
const double angleTolerance = 0.1;

// This flag determines when the robot needs to either 
// move towards a waypoint or stop.
bool goToWaypoint = false;

// Initialized variables and take care of other setup tasks
void setup() {

  // We initialize with the default starting coordinate 
  // for the turtlesim simulator
  waypointGoal.x = 5.544445;
  waypointGoal.y = 5.544445;
  
  // Initialize the Twist message.
  // Initial linear and angular velocities are 0 m/s and rad/s, respectively.
  velCommand.linear.x = 0.0;
  velCommand.linear.y = 0.0;
  velCommand.linear.z = 0.0;
  velCommand.angular.x = 0.0;
  velCommand.angular.y = 0.0;
  velCommand.angular.z = 0.0;
}

// Get the distance between the current x,y coordinate and 
// the desired waypoint x,y coordinate.
double getDistanceToWaypoint() {
  return sqrt(pow(waypointGoal.x - current.x, 2) + pow(
    waypointGoal.y - current.y, 2));
}

// Get the heading error
// i.e. how many radians does the robot need 
// to turn to head towards the waypoint  
double getHeadingError() {

  double deltaX = waypointGoal.x - current.x;
  double deltaY = waypointGoal.y - current.y;
  double waypointHeading = atan2(deltaY, deltaX);
  double headingError = waypointHeading - current.theta;   
  
  // Make sure heading error falls within -PI to PI range
  if (headingError > PI) {
    headingError = headingError - (2 * PI);
  } 
  if (headingError < -PI) {
    headingError = headingError + (2 * PI);
  } 
  
  return headingError;
}

// If we haven't yet reached the goal, set the velocity value.
// Otherwise, stop the robot.
void setVelocity() {

  double distanceToWaypoint = getDistanceToWaypoint();
  double headingError = getHeadingError();

  // If we are not yet at the waypoint
  if (goToWaypoint == true && (abs(distanceToWaypoint) > distanceTolerance)) {
   
    // If the robot's heading is off, fix it.
    if (abs(headingError) > angleTolerance) {
      velCommand.linear.x = 0.0;
      velCommand.angular.z = K_a  * headingError;
    }
    // Just fix the distance gap between current pose and waypoint.
    // The magnitude of the robot's velocity is directly
    // proportional to the distance the robot is from the 
    // goal.
    else {
      velCommand.linear.x = K_l * distanceToWaypoint;
      velCommand.angular.z = 0.0;    
    }
  }
  else {
    cout << "Goal has been reached!" << endl << endl;
    velCommand.linear.x = 0.0;
    velCommand.angular.z = 0.0; 
    goToWaypoint == false;
  }
}

// This callback function updates the current position and 
// orientation of the robot. 
void updatePose(const turtlesim::PoseConstPtr &currentPose) {
  current.x = currentPose->x;
  current.y = currentPose->y;
  current.theta = currentPose->theta;
}

// This callback function updates the desired waypoint when a waypoint
// message is published to the /waypoint topic
void updateWaypoint(const geometry_msgs::Pose2D &waypointPose) {
  waypointGoal.x = waypointPose.x;
  waypointGoal.y = waypointPose.y;
  goToWaypoint = true;  
}

int main(int argc, char **argv) {

  setup();  

  // Initiate ROS
  ros::init(argc, argv, "go_to_goal_x_y");
    
  // Create the main access point to communicate with ROS
  ros::NodeHandle node;

  // Subscribe to the robot's pose
  // Hold no messages in the queue. Automatically throw away 
  // any messages that are received that are not able to be
  // processed quickly enough.
  // Every time a new pose is received, update the robot's pose.
  ros::Subscriber currentPoseSub =
    node.subscribe("turtle1/pose", 0, updatePose);
    
  // Subscribe to the user's desired waypoint
  // Hold no messages in the queue. Automatically throw away 
  // any messages that are received that are not able to be
  // processed quickly enough.
  // Every time a new waypoint is received, update the robot's 
  // desired waypoint.
  // The tcpNoDelay is to reduce latency between nodes and to make sure we are
  // not missing any critical waypoint messages.
  ros::Subscriber waypointPoseSub =
    node.subscribe("waypoint", 0, updateWaypoint, 
    ros::TransportHints().tcpNoDelay());

  // Publish velocity commands to a topic.
  // Hold no messages in the queue. Automatically throw away 
  // any messages that are received that are not able to be
  // processed quickly enough.
  velocityPub =
    node.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 0);

  // Specify a frequency that want the while loop below to loop at
  // In this case, we want to loop 10 cycles per second
  ros::Rate loop_rate(10); 

  // Keep running the while loop below as long as the ROS Master is active. 
  while (ros::ok()) {

    // Here is where we call the callbacks that need to be called.
    ros::spinOnce();

    // After we call the callback function to update the robot's pose, we 
    // set the velocity values for the robot.
    setVelocity();

    // Publish the velocity command to the ROS topic
    velocityPub.publish(velCommand);

    // Print the output to the console
    cout << "Current (x,y) = " << "(" << current.x << "," << current.y << ")" 
         << endl
         << "Waypoint (x,y) = " << "(" << waypointGoal.x << "," 
         << waypointGoal.y << ")" 
         << endl
         << "Distance to Waypoint = " << getDistanceToWaypoint() << " m" 
         << endl
         << "Linear Velocity (x) = " << velCommand.linear.x << " m/s" 
         << endl << endl;

    // Sleep as long as we need to to make sure that we have a frequency of
    // 10Hz
    loop_rate.sleep();
  }

  return 0;
}

Now we need to add the C++ program we just wrote to the CMakeLists.txt file.

cd ~/catkin_ws/src/go_to_goal_turtlesim
gedit CMakeLists.txt

Go to the bottom of the file.

Add the following lines.

18-add-go-to-goal-lines-x-y

Save the file, and close it.

Go to the root of the workspace.

cd ~/catkin_ws

Compile the code.

catkin_make

Launch the ROS Node

Now let’s launch the ROS node we created in the last section.

Open a new terminal window.

Start ROS.

roscore

Open another terminal window, and launch the turtlesim_node.

cd ~/catkin_ws/
rosrun turtlesim turtlesim_node

Open another terminal window, and launch the waypoint_publisher node.

rosrun go_to_goal_turtlesim waypoint_publisher

Open another terminal window, and launch the go_to_goal_x_y node.

rosrun go_to_goal_turtlesim go_to_goal_x_y

Now, go back to the terminal window where you launched the waypoint_publisher node.

Enter any waypoint x value from 0 to 11. I will enter 1.2.

Enter any waypoint y value from 0 to 11. I will enter 2.7.

When the turtle gets close enough, you will see a message that says “Goal has been reached!”

19-close-enough

Here is what the simulation looks like. You will see the robot moved from (5.544445, 5.544445) to (1.29721, 2.71791)…which is close enough to the waypoint. Once the turtle reached the waypoint, it stopped.

20-simulation-output

Let’s try another waypoint.

Go back to the terminal window where you launched the waypoint_publisher node.

Enter any waypoint x value from 0 to 11. I will enter 11.

Enter any waypoint y value from 0 to 11. I will enter 11.

Here is the output:

21-11-11-output

Create a Launch File

So that we don’t have to keep opening up ROS nodes in the future, we will create a launch file to make our life easier.

The first thing we need to do is to open a new terminal window and go to the go_to_goal_turtlesim package.

cd ~/catkin_ws/src/go_to_goal_turtlesim

Create a folder called ‘launch’.

mkdir launch

Create a new launch file inside the launch directory you just made.

cd launch

Open up the text editor.

gedit turtlesim_waypoint_follower.launch

Type the code below into the file, and save it.

<launch>
  <node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node" />
  <node pkg="go_to_goal_turtlesim" type="waypoint_publisher" name="waypoint_publisher" output="screen" />
  <node pkg="go_to_goal_turtlesim" type="go_to_goal_x_y" name="go_to_goal_x_y" />
</launch>

Save this file to the catkin_ws/src/go_to_goal_turtlesim/launch folder. This file will run C++ executables.

Change the permissions of the launch file we just created.

cd ~/catkin_ws/src/go_to_goal_turtlesim/launch
sudo chmod +x turtlesim_waypoint_follower.launch

Run the launch file. Open a new terminal window, and type:

cd ~/catkin_ws/

This command below is all a single command on one line.

roslaunch go_to_goal_turtlesim turtlesim_waypoint_follower.launch

References

Credit to the book Practical Robotics in C++: Build and Program Real Autonomous Robots Using Raspberry Pi (#ad) … a fantastic book, which I highly recommend … and the official ROS tutorials for Turtlesim.

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Visualize IMU Data Using the BNO055, ROS, and Jetson Nano

In this tutorial, we will learn how to use an NVIDIA Jetson Nano to read data from the BNO055 Absolute Orientation Sensor and connect that data to ROS using the ros_imu_bno055 package. Here is what you will be able to do by the end of this tutorial:

bno055-ros-in-action

Real-World Applications

IMUs like the BNO055 enable us to measure the acceleration and orientation of a robotic system. IMUs are common in both wheeled robots and aerial robots. 

Prerequisites

You Will Need

In addition to the parts listed in the article I linked to in the Prerequisites, you will need the following components (#ad).

Once you finish this project, you can mount the IMU on anything that moves (drone, robot car, etc.) so you can measure the system’s acceleration and orientation.

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Soldering (Optional)

To make sure the electrical pins are stuck to the board, you can solder them like I did in this video here with the BNO055 board.

Set Up the Hardware

First, let’s set up the hardware.

Make the following connections between the BNO055 and the USB to Serial Converter:

  • Connect BNO055 Vin to 3.3V (VCC) power.
  • Connect BNO055 GND to ground (GND).
  • Connect BNO055 SDA (UART transmitter TX) to receiver RXD.
  • Connect BNO055 SCL (UART receiver RX) to transmitter TXD.
  • Connect BNO055 PS1 to BNO055 Vin (3.3V).
2021-04-19-16.38.10
2021-04-19-16.38.18-1

Now connect the other end of the USB to Serial Converter to the Jetson Nano.

2021-04-19-17.31.37

If you have a LIDAR connected to your Jetson Nano, disconnect it from the USB port.

Install and Build the BNO055 ROS Package

Let’s get all the software set up.

Turn on your Jetson Nano.

Open a new terminal window.

Install the BNO055 ROS package.

cd ~/catkin_ws/src
git clone https://github.com/RoboticArts/ros_imu_bno055.git

Build the package.

cd ~/catkin_ws/
catkin_make --only-pkg-with-deps ros_imu_bno055

Source the environment.

source devel/setup.bash

Install the ROS IMU plugin.

sudo apt-get install ros-melodic-rviz-imu-plugin

Install pyserial.

pip3 install pyserial

Install the udev rule to identify the device as ttyUSB_IMU.

roscd ros_imu_bno055
sudo cp utils/99-bno055.rules /etc/udev/rules.
sudo udevadm control --reload-rules && sudo udevadm trigger

Reboot your computer.

sudo reboot

View the IMU Data

Open a terminal window.

Install some packages.

sudo apt-get install python-catkin-pkg
sudo apt-get install python3-catkin-pkg-modules
sudo apt-get install python3-rospkg-modules

Get a list of the connected USB devices (there should only be one, which is the BNO055).

ls -la /dev/ttyUSB*

Change permissions of the USB connection.

sudo chmod 666 /dev/ttyUSB0

Open a new terminal, and type the following command to launch visualization of the IMU data in Rviz.

roslaunch ros_imu_bno055 view_imu.launch
1-run-imu-launch

Move your BNO055 around, and you will see the axes move. The red line is the x-axis, the green line is the y-axis, and the blue line is the z-axis.

2-imu_view_launch

Let’s see the active ROS topics. Open up a new terminal and type:

rostopic list

Now let’s see the data.

rostopic echo /imu/data
rostopic-echo

Press CTRL + C in all terminal windows to close everything down.

Another way to launch the BNO055 ROS driver is by using the following command. Notice how we specified the USB port name (ttyUSB0):

roslaunch ros_imu_bno055 imu.launch serial_port:=/dev/ttyUSB0

Let’s see the active ROS topics. Open up a new terminal and type:

rostopic list

Press CTRL + C in all terminal windows to close everything down.

Other Commands

Launch the IMU again.

roslaunch ros_imu_bno055 imu.launch serial_port:=/dev/ttyUSB0

Here are some other commands. To reset the IMU, you can use the following command:

rosservice call /imu/reset_device "{}"

When you type this command, your USB to Serial converter should blink a few times, and then hold steady.

To calibrate the IMU, type the following command:

roslaunch ros_imu_bno055 imu_calibration.launch serial_port:=/dev/ttyUSB0 operation_mode:=NDOF_FMC_OFF
3-calibration-mode

Move your IMU around as explained at the bottom of this page in order to calibrate the gyroscope, accelerometer, and the magnetometer.

That’s it! Keep building!

Visualize IMU Data Using the MPU6050, ROS, and Jetson Nano

In this tutorial, we will learn how to use an NVIDIA Jetson Nano to read data from an MPU6050 IMU (Inertial measurement unit) sensor. We will also learn how to connect the MPU6050’s data to ROS, the most popular framework in the world for writing robot software. Here is what the final output will look like:

mpu_6050

Real-World Applications

IMUs like the MPU6050 enable us to measure the acceleration and orientation of a robotic system. IMUs are common in both wheeled robots and aerial robots.

Prerequisites

You Will Need

In addition to the parts listed in the article I linked to in the Prerequisites, you will need the following components (#ad).

Once you finish this project, you can mount the IMU on anything that moves (drone, robot car, etc.) so you can measure the system’s acceleration and orientation.

Disclosure (#ad): As an Amazon Associate I earn from qualifying purchases.

Set Up the Hardware

Here is the pinout diagram for the NVIDIA Jetson Nano B01.

1-pin-diagram-nvidia-jetson-nano

Make the following connections:

  • Connect VCC of the MPU6050 to pin 17 (3.3V) of the Jetson Nano.
  • Connect GND of the MPU6050 to pin 25 (GND) of the Jetson Nano.
  • Connect SCL of the MPU6050 to pin 5 (SCL) of the Jetson Nano.
  • Connect SDA of the MPU6050 to pin 3 (SDA) of the Jetson Nano.
2021-04-19-13.44.38

Set Up the Communication Protocol

Our MPU6050 will use the I2C serial communication protocol. What this means is that data will be transferred from the IMU to the Jetson Nano one bit at a time.

Turn on your Jetson Nano.

Open a terminal window.

Type the following command to verify that you can see the MPU6050.

sudo i2cdetect -r -y 1

You can see that the address of the MPU6050 is 68.

2021-04-19-10.23.51

Now let’s go through the different ways we can connect the MPU6050 to the Jetson Nano. We’ll go through one option at a time.

Write the Code

Option 1: Use the Adafruit Circuit Python Library

Install the adafruit-blinka library.

sudo apt-get update
pip3 install adafruit-blinka

It will take some time to download everything, so be patient. It might even look like it is frozen during the downloading process, but don’t worry, it is downloading.

2021-04-19-10.30.39

Test that everything is installed correctly.

Make a new directory named mpu6050_test.

mkdir mpu6050_test

Move to that directory.

cd mpu6050_test

Create a Python program called blinkatest.py.

gedit blinkatest.py

Write the following code.

import board
import busio

print("Hello blinka!")

# Try to create an I2C device
i2c = busio.I2C(board.SCL, board.SDA)
print("I2C 1 ok!")

print("done!")

Press save, and close it.

Run the code:

python3 blinkatest.py
2021-04-19-10.44.32

Now let’s work with the Adafruit MPU6050 library.

Install the MPU6050 library.

pip3 install adafruit-circuitpython-mpu6050

Create a new program called mpu6050_simpletest.py.

gedit mpu6050_simpletest.py

Write the following code inside it. Credit to Adafruit for this code.

Save the file and close it.

Run the code.

python3 mpu6050_simpletest.py

Press CTRL+C when you’re done running the program.

Option 2: Using smbus

Here is another approach to reading data from the MPU6050. Credit to ElectronicWings for this method.

Install the smbus library.

sudo apt-get install python3-smbus

Create a program called mpu6050_simpletest2.py.

gedit mpu6050_simpletest2.py

Write the following code inside it.

'''
        Read Gyro and Accelerometer by Interfacing Raspberry Pi with MPU6050 using Python
	http://www.electronicwings.com
'''
import smbus			#import SMBus module of I2C
from time import sleep          #import

#some MPU6050 Registers and their Address
PWR_MGMT_1   = 0x6B
SMPLRT_DIV   = 0x19
CONFIG       = 0x1A
GYRO_CONFIG  = 0x1B
INT_ENABLE   = 0x38
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
ACCEL_ZOUT_H = 0x3F
GYRO_XOUT_H  = 0x43
GYRO_YOUT_H  = 0x45
GYRO_ZOUT_H  = 0x47


def MPU_Init():
	#write to sample rate register
	bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)
	
	#Write to power management register
	bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)
	
	#Write to Configuration register
	bus.write_byte_data(Device_Address, CONFIG, 0)
	
	#Write to Gyro configuration register
	bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)
	
	#Write to interrupt enable register
	bus.write_byte_data(Device_Address, INT_ENABLE, 1)

def read_raw_data(addr):
	#Accelero and Gyro value are 16-bit
        high = bus.read_byte_data(Device_Address, addr)
        low = bus.read_byte_data(Device_Address, addr+1)
    
        #concatenate higher and lower value
        value = ((high << 8) | low)
        
        #to get signed value from mpu6050
        if(value > 32768):
                value = value - 65536
        return value


bus = smbus.SMBus(1) 	# or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x68   # MPU6050 device address

MPU_Init()

print (" Reading Data of Gyroscope and Accelerometer")

while True:
	
	#Read Accelerometer raw value
	acc_x = read_raw_data(ACCEL_XOUT_H)
	acc_y = read_raw_data(ACCEL_YOUT_H)
	acc_z = read_raw_data(ACCEL_ZOUT_H)
	
	#Read Gyroscope raw value
	gyro_x = read_raw_data(GYRO_XOUT_H)
	gyro_y = read_raw_data(GYRO_YOUT_H)
	gyro_z = read_raw_data(GYRO_ZOUT_H)
	
	#Full scale range +/- 250 degree/C as per sensitivity scale factor
	Ax = acc_x/16384.0
	Ay = acc_y/16384.0
	Az = acc_z/16384.0
	
	Gx = gyro_x/131.0
	Gy = gyro_y/131.0
	Gz = gyro_z/131.0
	

	print ("Gx=%.2f" %Gx, u'\u00b0'+ "/s", "\tGy=%.2f" %Gy, u'\u00b0'+ "/s", "\tGz=%.2f" %Gz, u'\u00b0'+ "/s", "\tAx=%.2f g" %Ax, "\tAy=%.2f g" %Ay, "\tAz=%.2f g" %Az) 	
	sleep(1)

Save the file and close it.

Run the code.

python3 mpu6050_simpletest2.py

Here is the output:

2021-04-19-11.12.51

Option 3: Using smbus again

Here is our third and final approach to reading data from the MPU6050. 

Create a program called mpu6050_simpletest3.py.

gedit mpu6050_simpletest3.py

Write the following code inside it.

"""This program handles the communication over I2C
between a Jetson Nano and a MPU-6050 Gyroscope / Accelerometer combo.
Made by: Dennis/TW
Released under the MIT License
Copyright 2019
"""

import smbus
from time import sleep      

class mpu6050:

    # Global Variables
    GRAVITIY_MS2 = 9.80665
    address = None
    bus = smbus.SMBus(1)

    # Scale Modifiers
    ACCEL_SCALE_MODIFIER_2G = 16384.0
    ACCEL_SCALE_MODIFIER_4G = 8192.0
    ACCEL_SCALE_MODIFIER_8G = 4096.0
    ACCEL_SCALE_MODIFIER_16G = 2048.0

    GYRO_SCALE_MODIFIER_250DEG = 131.0
    GYRO_SCALE_MODIFIER_500DEG = 65.5
    GYRO_SCALE_MODIFIER_1000DEG = 32.8
    GYRO_SCALE_MODIFIER_2000DEG = 16.4

    # Pre-defined ranges
    ACCEL_RANGE_2G = 0x00
    ACCEL_RANGE_4G = 0x08
    ACCEL_RANGE_8G = 0x10
    ACCEL_RANGE_16G = 0x18

    GYRO_RANGE_250DEG = 0x00
    GYRO_RANGE_500DEG = 0x08
    GYRO_RANGE_1000DEG = 0x10
    GYRO_RANGE_2000DEG = 0x18

    # MPU-6050 Registers
    PWR_MGMT_1 = 0x6B
    PWR_MGMT_2 = 0x6C

    SELF_TEST_X = 0x0D
    SELF_TEST_Y = 0x0E
    SELF_TEST_Z = 0x0F
    SELF_TEST_A = 0x10

    ACCEL_XOUT0 = 0x3B
    ACCEL_XOUT1 = 0x3C
    ACCEL_YOUT0 = 0x3D
    ACCEL_YOUT1 = 0x3E
    ACCEL_ZOUT0 = 0x3F
    ACCEL_ZOUT1 = 0x40

    TEMP_OUT0 = 0x41
    TEMP_OUT1 = 0x42

    GYRO_XOUT0 = 0x43
    GYRO_XOUT1 = 0x44
    GYRO_YOUT0 = 0x45
    GYRO_YOUT1 = 0x46
    GYRO_ZOUT0 = 0x47
    GYRO_ZOUT1 = 0x48

    ACCEL_CONFIG = 0x1C
    GYRO_CONFIG = 0x1B

    def __init__(self, address):
        self.address = address

        # Wake up the MPU-6050 since it starts in sleep mode
        self.bus.write_byte_data(self.address, self.PWR_MGMT_1, 0x00)

    # I2C communication methods

    def read_i2c_word(self, register):
        """Read two i2c registers and combine them.

        register -- the first register to read from.
        Returns the combined read results.
        """
        # Read the data from the registers
        high = self.bus.read_byte_data(self.address, register)
        low = self.bus.read_byte_data(self.address, register + 1)

        value = (high << 8) + low

        if (value >= 0x8000):
            return -((65535 - value) + 1)
        else:
            return value

    # MPU-6050 Methods

    def get_temp(self):
        """Reads the temperature from the onboard temperature sensor of the MPU-6050.

        Returns the temperature in degrees Celcius.
        """
        # Get the raw data
        raw_temp = self.read_i2c_word(self.TEMP_OUT0)

        # Get the actual temperature using the formule given in the
        # MPU-6050 Register Map and Descriptions revision 4.2, page 30
        actual_temp = (raw_temp / 340) + 36.53

        # Return the temperature
        return actual_temp

    def set_accel_range(self, accel_range):
        """Sets the range of the accelerometer to range.

        accel_range -- the range to set the accelerometer to. Using a
        pre-defined range is advised.
        """
        # First change it to 0x00 to make sure we write the correct value later
        self.bus.write_byte_data(self.address, self.ACCEL_CONFIG, 0x00)

        # Write the new range to the ACCEL_CONFIG register
        self.bus.write_byte_data(self.address, self.ACCEL_CONFIG, accel_range)

    def read_accel_range(self, raw = False):
        """Reads the range the accelerometer is set to.

        If raw is True, it will return the raw value from the ACCEL_CONFIG
        register
        If raw is False, it will return an integer: -1, 2, 4, 8 or 16. When it
        returns -1 something went wrong.
        """
        # Get the raw value
        raw_data = self.bus.read_byte_data(self.address, self.ACCEL_CONFIG)

        if raw is True:
            return raw_data
        elif raw is False:
            if raw_data == self.ACCEL_RANGE_2G:
                return 2
            elif raw_data == self.ACCEL_RANGE_4G:
                return 4
            elif raw_data == self.ACCEL_RANGE_8G:
                return 8
            elif raw_data == self.ACCEL_RANGE_16G:
                return 16
            else:
                return -1

    def get_accel_data(self, g = False):
        """Gets and returns the X, Y and Z values from the accelerometer.

        If g is True, it will return the data in g
        If g is False, it will return the data in m/s^2
        Returns a dictionary with the measurement results.
        """
        # Read the data from the MPU-6050
        x = self.read_i2c_word(self.ACCEL_XOUT0)
        y = self.read_i2c_word(self.ACCEL_YOUT0)
        z = self.read_i2c_word(self.ACCEL_ZOUT0)

        accel_scale_modifier = None
        accel_range = self.read_accel_range(True)

        if accel_range == self.ACCEL_RANGE_2G:
            accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_2G
        elif accel_range == self.ACCEL_RANGE_4G:
            accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_4G
        elif accel_range == self.ACCEL_RANGE_8G:
            accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_8G
        elif accel_range == self.ACCEL_RANGE_16G:
            accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_16G
        else:
            print("Unkown range - accel_scale_modifier set to self.ACCEL_SCALE_MODIFIER_2G")
            accel_scale_modifier = self.ACCEL_SCALE_MODIFIER_2G

        x = x / accel_scale_modifier
        y = y / accel_scale_modifier
        z = z / accel_scale_modifier

        if g is True:
            return {'x': x, 'y': y, 'z': z}
        elif g is False:
            x = x * self.GRAVITIY_MS2
            y = y * self.GRAVITIY_MS2
            z = z * self.GRAVITIY_MS2
            return {'x': x, 'y': y, 'z': z}

    def set_gyro_range(self, gyro_range):
        """Sets the range of the gyroscope to range.

        gyro_range -- the range to set the gyroscope to. Using a pre-defined
        range is advised.
        """
        # First change it to 0x00 to make sure we write the correct value later
        self.bus.write_byte_data(self.address, self.GYRO_CONFIG, 0x00)

        # Write the new range to the ACCEL_CONFIG register
        self.bus.write_byte_data(self.address, self.GYRO_CONFIG, gyro_range)

    def read_gyro_range(self, raw = False):
        """Reads the range the gyroscope is set to.

        If raw is True, it will return the raw value from the GYRO_CONFIG
        register.
        If raw is False, it will return 250, 500, 1000, 2000 or -1. If the
        returned value is equal to -1 something went wrong.
        """
        # Get the raw value
        raw_data = self.bus.read_byte_data(self.address, self.GYRO_CONFIG)

        if raw is True:
            return raw_data
        elif raw is False:
            if raw_data == self.GYRO_RANGE_250DEG:
                return 250
            elif raw_data == self.GYRO_RANGE_500DEG:
                return 500
            elif raw_data == self.GYRO_RANGE_1000DEG:
                return 1000
            elif raw_data == self.GYRO_RANGE_2000DEG:
                return 2000
            else:
                return -1

    def get_gyro_data(self):
        """Gets and returns the X, Y and Z values from the gyroscope.

        Returns the read values in a dictionary.
        """
        # Read the raw data from the MPU-6050
        x = self.read_i2c_word(self.GYRO_XOUT0)
        y = self.read_i2c_word(self.GYRO_YOUT0)
        z = self.read_i2c_word(self.GYRO_ZOUT0)

        gyro_scale_modifier = None
        gyro_range = self.read_gyro_range(True)

        if gyro_range == self.GYRO_RANGE_250DEG:
            gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_250DEG
        elif gyro_range == self.GYRO_RANGE_500DEG:
            gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_500DEG
        elif gyro_range == self.GYRO_RANGE_1000DEG:
            gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_1000DEG
        elif gyro_range == self.GYRO_RANGE_2000DEG:
            gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_2000DEG
        else:
            print("Unkown range - gyro_scale_modifier set to self.GYRO_SCALE_MODIFIER_250DEG")
            gyro_scale_modifier = self.GYRO_SCALE_MODIFIER_250DEG

        x = x / gyro_scale_modifier
        y = y / gyro_scale_modifier
        z = z / gyro_scale_modifier

        return {'x': x, 'y': y, 'z': z}

    def get_all_data(self):
        """Reads and returns all the available data."""
        temp = get_temp()
        accel = get_accel_data()
        gyro = get_gyro_data()

        return [accel, gyro, temp]

if __name__ == "__main__":
    while(1):
        mpu = mpu6050(0x68)
        print("Temperature (C): ", mpu.get_temp())
        accel_data = mpu.get_accel_data()
        print("Acceleration x (m/s^2): ", accel_data['x'])
        print("Acceleration y (m/s^2): ", accel_data['y'])
        print("Acceleration z (m/s^2): ", accel_data['z'])
        gyro_data = mpu.get_gyro_data()
        print("Gyroscope x (deg/s): ", gyro_data['x'])
        print("Gyroscope y (deg/s): ", gyro_data['y'])
        print("Gyroscope z (deg/s): ", gyro_data['z'])
        sleep(1)

Save the file and close it.

Run the code.

python mpu6050_simpletest3.py

Here is the output:

2021-04-19-11.34.27

Connect the MPU6050 IMU to ROS

Now let’s connect the MPU6050 to ROS. We want to publish the data we read from the MPU6050 to a ROS topic. Let’s take a look at two different ways to do this using pre-built ROS packages.

Option 1

Install and Build the Package

Go to your catkin workspace.

cd ~/catkin_ws/src

Clone the package. This package was created by roboticists at Oregon State University.

git clone https://github.com/OSUrobotics/mpu_6050_driver.git
cd mpu_6050_driver/scripts
gedit tf_broadcaster_imu.py 

Write this code.

Save the file, and close it.

Open the imu_node.py file.

gedit imu_node.py

Go down to lines 93 and 94, and add a queue_size of 10. Here is how the code should look:

2021-04-19-12.38.31

Save the file and close it.

Change permissions of both files.

chmod +x imu_node.py
chmod +x tf_broadcaster_imu.py

Move to the root.

cd ~/catkin_ws/

Build the package.

catkin_make

Open a new terminal window, and print the path to your new ROS package named mpu_6050_driver.

rospack find mpu_6050_driver
2021-04-19-12.03.43

To see if our package depends on other packages, type these commands. Ignore any error messages you see in the terminal:

rosdep update
rosdep check mpu_6050_driver

You should see a message that says “All system dependencies have been satisfied.”

Run the Node

Now we want to run the node that is inside the mpu_6050_driver package.

Open a new terminal window, and launch ROS.

cd ~/catkin_ws/
roscore

Open another terminal window, and run the IMU node.

rosrun mpu_6050_driver imu_node.py

Open another terminal window, and type the following command:

rosrun mpu_6050_driver tf_broadcaster_imu.py

Open Rviz.

rosrun rviz rviz

Change the Fixed Frame parameter under Global Options to imu_link.

Click the Add button in the bottom-left of the Displays menu.

Add TF.

Also add Imu if you have that option. Under the Imu dropdown in the Displays, change the Topic to /imu/data.

Let’s see the active ROS topics. Open up a new terminal and type:

rostopic list

Open another terminal window, and visualize the imu data.

rostopic echo /imu/data
2021-04-19-12.43.17

Press CTRL+C on all windows when you’re done.

Option 2

Install and Build the Package

For this option, we will use the IMU Complementary Filter package. Type the following commands to install this package.

cd ~/catkin_ws/src
sudo apt-get install ros-melodic-imu-tools

Install the MPU 6050 driver package created by Robotics and ROS Learning. He gives a great walkthrough about this package in this video on YouTube. Type this single command below.

git clone https://github.com/bandasaikrishna/orientations_from_IMU_MPU_6050.git

Using the file explorer on your Jetson Nano, copy all of the contents of that folder you just imported from GitHub and paste them into your mpu_6050_driver package from the previous section. When asked if you want to overwrite, yes you want to overwrite all duplicated folders.

Go to the scripts folder.

cd ~/catkin_ws/src/mpu_6050_driver/scripts

Change permissions on the imu_node.py file and tf_broadcaster_imu.py nodes.

chmod +x imu_node.py
chmod +x tf_broadcaster_imu.py

Build the package.

cd ~/catkin_ws/
catkin_make

Check dependencies. Open a new terminal window, and type:

rosdep check mpu_6050_driver

You should see a message that says “All system dependencies have been satisfied.”

Launch It

Now we want to launch the package.

Open a new terminal window, and type the following command:

roslaunch mpu_6050_driver imu_demo.launch

Let’s see the active ROS topics. Open up a new terminal and type:

rostopic list
final-output-imu-mpu6050

That’s it! If for some reason, that GitHub code is no longer available, you can download the code here from my files.

Keep building!