In this post, we will see how to write a ROS program (in Python) to make a robot rotate according to user input. We are going to fix an error in the code that prevents this program from working as we go on.
PS:This ROS project is part of our ROS Mini Challenge series, which gives you an opportunity to win an amazing ROS Developers T-shirt! This challenge is already solved. For updates on future challenges, please stay tuned to our Twitter channel.
Step 1: Grab a copy of the ROS Project that contains the almost-working Python code
Click here to get your own copy of the project. If you don’t have an account on the ROS Development Studio, you will be asked to create one. Once you create an account or log in, the project will be copied to your workspace. That done, open your ROSject using the Open button. This might take a few moments, please be patient.
You should now see a notebook with detailed instructions about the challenge. Please ignore the section that says “Claim your Prize!” because, well, the challenge is closed already 🙂
Step 2: Start the Simulation and run the program
Click on the Simulations menu and then Choose launch file… . In the dialog that appears, select rotw1.launch, then click the Launchbutton. You should see a Gazebo window popup showing the simulation: a robot standing in front of a very beautiful house!
Keeping the Gazebo window in view, pick a Shell from Tools > Shelland run the following commands (input the same numbers shown):
user:~$ rosrun rotw1_code rotate_robot.py
Enter desired angular speed (degrees): 60
Enter desired angle (degrees): 90
Do you want to rotate clockwise? (y/n): y
[INFO] [1580744469.184313, 1044.133000]: shutdown time! Stop the robot
Did the robot rotate? I would be surprised if it did!
Step 3: Find out what the problem is (was!)
Fire up the IDE and locate the file named rotate_robot.py.You will find in the catkin_ws/src/rotw1_code/src directory.
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
import time
class RobotControl():
def __init__(self):
rospy.init_node('robot_control_node', anonymous=True)
self.vel_publisher = rospy.Publisher('/velocity', Twist, queue_size=1)
self.cmd = Twist()
self.ctrl_c = False
self.rate = rospy.Rate(10)
rospy.on_shutdown(self.shutdownhook)
def publish_once_in_cmd_vel(self):
"""
This is because publishing in topics sometimes fails the first time you publish.
In continuous publishing systems, this is no big deal, but in systems that publish only
once, it IS very important.
"""
while not self.ctrl_c:
connections = self.vel_publisher.get_num_connections()
if connections > 0:
self.vel_publisher.publish(self.cmd)
break
else:
self.rate.sleep()
def shutdownhook(self):
self.stop_robot()
self.ctrl_c = True
def stop_robot(self):
rospy.loginfo("shutdown time! Stop the robot")
self.cmd.linear.x = 0.0
self.cmd.angular.z = 0.0
self.publish_once_in_cmd_vel()
def get_inputs_rotate(self):
self.angular_speed_d = int(
raw_input('Enter desired angular speed (degrees): '))
self.angle_d = int(raw_input('Enter desired angle (degrees): '))
clockwise_yn = raw_input('Do you want to rotate clockwise? (y/n): ')
if clockwise_yn == "y":
self.clockwise = True
if clockwise_yn == "n":
self.clockwise = False
return [self.angular_speed_d, self.angle_d]
def convert_degree_to_rad(self, speed_deg, angle_deg):
self.angular_speed_r = speed_deg * 3.14 / 180
self.angle_r = angle_deg * 3.14 / 180
return [self.angular_speed_r, self.angle_r]
def rotate(self):
# Initilize velocities
self.cmd.linear.x = 0
self.cmd.linear.y = 0
self.cmd.linear.z = 0
self.cmd.angular.x = 0
self.cmd.angular.y = 0
# Convert speed and angle to radians
speed_d, angle_d = self.get_inputs_rotate()
self.convert_degree_to_rad(speed_d, angle_d)
# Check the direction of the rotation
if self.clockwise:
self.cmd.angular.z = -abs(self.angular_speed_r)
else:
self.cmd.angular.z = abs(self.angular_speed_r)
# t0 is the current time
t0 = rospy.Time.now().secs
current_angle = 0
# loop to publish the velocity estimate, current_distance = velocity * (t1 - t0)
while (current_angle < self.angle_r):
# Publish the velocity
self.vel_publisher.publish(self.cmd)
# t1 is the current time
t1 = rospy.Time.now().secs
# Calculate current angle
current_angle = self.angular_speed_r * (t1 - t0)
self.rate.sleep()
# set velocity to zero to stop the robot
#self.stop_robot()
if __name__ == '__main__':
robotcontrol_object = RobotControl()
try:
res = robotcontrol_object.rotate()
except rospy.ROSInterruptException:
pass
The problem was in the def __init__(self) function – we are publishing to the wrong topic – /velocity – instead of /cmd_vel.
Next, let’s see how we found the problem.
Step 4: Learn how the problem was solved
If you’re familiar with ROS, you’ll know that /cmd_vel is the most common name for the topic responsible for moving the robot. It’s not always that, but at least the /velocity topic instantly became suspect! Therefore, we went ahead to examine it to see if it was a proper topic:
And our suspicions were confirmed: the topic had no subscribers. This was strange, as usually /gazebo would be one of the subscribers if it was connected to the simulation.
On the other hand, /cmd_vel was connected to /gazebo. So, maybe it’s the right topic after all, but let’s confirm that next.
Step 5: Replace /velocity with /cmd_vel and rotate the robot!
Replace /velocity with /cmd_vel in the def __init__(self) function and save.
Now run the program again:
user:~$ rosrun rotw1_code rotate_robot.py
Enter desired angular speed (degrees): 60
Enter desired angle (degrees): 90
Do you want to rotate clockwise? (y/n): y
[INFO] [1580744469.184313, 1044.133000]: shutdown time! Stop the robot
Now, the robot should rotate according to user input – try different numbers!
And that was it. Hope you found this useful.
Extra: Video of this post
We made a video showing how this challenge was solved. If you prefer “sights and sounds” to “black and white”, here you go:
Did you like this post? Do you have questions about what was explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it.
How can you test the ROS programs you are developing for a robot? If you are building a program for a robot, you have to make sure that it works properly, and for that, you need some tests.
If you think that you should test your programs directly on the robot, think again. Testing on the real robot is the last of the steps you will need to do in a testing procedure.
Why shouldn’t you test on the real robot first?:
Because you can break the robot or harm somebody. When you start testing your code, many things could be wrong in your program, which can make the robot go crazy and crash into something. We are not talking about mobile apps here!! We are talking about physical stuff that can be broken or harm somebody.
Because testing on the robot takes a lot more time than testing on your local machine. To test on a real robot, usually you will need to have a physical setup that you need to rebuild every time you test (because by doing the test, the physical robot or things in the environment will change position and need to be reset).
Testing with the real robot is mandatory for the creation of a program for a robot (at least at present). However, it is the last of the steps in the testing procedure.
The proper procedure for developing for robots works as follows:
You create the ROS program on your computer.
You test in some way on your computer that the program works.
Based on the test results, you modify the program until the local testing works properly.
Once you are sure that your program works correctly on your computer, then you test it on the real robot.
You will see that on the real robot, the results differ from your local tests. Then, you need to modify the code again, as well as the local tests, in order to reflect the points that were not working on the robot
Repeat the whole testing cycle until it works on your local machine first, and then on the real robot.
IMPORTANT: If you want to develop fast and with as little hassle as possible, you should never go and try your code on the robot until it works properly in the local test environment. If the code doesn’t work in your local tests, it is never going to work on the real robot!
Then the question is, how do you test your programs on your local machine?
Well, you have several options for how to test your ROS programs without having to use a robot.
LEVELS OF TESTING
(This section’s information was extracted from ROS wiki)
Level 1. Library unit test (unittest, gtest): A library unit test should test your code sans ROS (if you are having a hard time testing sans ROS, you probably need to refactor your library). Make sure that your underlying functions are separated out and well tested with both common, edge, and error inputs. Make sure that the pre- and post- conditions of your methods are well tested. Also, make sure that the behavior of your code under these various conditions is well documented.
Level 2. ROS node unit test (rostest + unittest/gtest): Node unit tests start up your node and test its external API, i.e. published topics, subscribed topics, and services.
Level 3. ROS nodes integration/regression test (rostest + unittest/gtest): Integration tests start up multiple nodes and test that they all work together as expected.
Level 4. Functional testing: At this level, you should test the full robot application as a whole. Check that the robot is able to navigate, to grasp, to recognize people, etc… You will need to devise some tests that allow you to check that your code is still working on those functionalities. Usually, the tests are done using simulations or bag files.
CREATING TESTS FOR LEVELS 1,2, AND 3
In order to create tests for those levels, you will need to use the following libraries and packages:
Specifically for Level 1 tests:
gtest: Also known as Google Test, it is used to build unit tests in C++. You can get the full documentation about using gtest with ROS at this link.
unittest: The framework provided for doing unit tests in Python. You can get a full documentation about using unittest with ROS at this link.
Additionally, for Level 2 and 3 tests:
rostest: It is a package provided by ROS that allows the creation of nodes with specific configurations to test that your whole ROS program is working properly inside the ROS framework by executing unit tests from within. That is why, for the creation of tests for those two levels, we need to combine code>rostest with either code>gtest (C++ case) or unittest (Python case). You can get a full documentation about using code>rostest at this link.
CREATING TESTS FOR LEVEL 4
Level 4 tests are the ones that allow us to test if the program is actually doing what it is supposed to do. For testing that, we have three different options, from furthest to closest to reality.
Using mocks for testing
If you are a developer, you already know what mocks are. You can create your own mocks that emulate the connection to the different parts of the ROS API of your robot. On this page, you will find information about how to use mocks in ROS.
Working with mocks in ROS is not an easy option since it requires a lot of preparation work. Also, it is of very limited usefulness since it can only produce what you have put on it previously. Using mocks is an option that I don’t really recommend for developing. Use it only if you cannot use any of the other options below or if you are creating unit tests for your code. In my personal experience, I have never used them, having always used one of the next two options.
Anyway, if you want to use mocks for your testing, then I recommend that you use the Google Test environment: https://github.com/google/googletest.
Using ROS bags for testing
ROS provides a way to record in a log file the full ROS API of a robot while it is running in a real-life situation, and then run it back later on another computer. This log file is called a ROS bag. When you run a ROS bag on another computer, the computer will be showing your programs the same ROS API that it had when it was recorded.
ROS bags are a limited system in the sense that you can only use them when you want to create an algorithm that does something from sensor data. This means that you cannot generate commands to the robot because it is only a reproduction of data, so you can get the same data as the robot got when recorded, but you cannot decide new actions for the robot.
Using simulations for testing
So, if you want to go pro without having to use a real robot, you should use simulations of robots. Simulations are the next step in software development. This is like having the real robot on your side, but without having to care for the electronics, hardware, and physical space. Roboticists consider simulations to be the ugly brother of robotics. Roboticists usually hate to use a simulation because it is not the real robot. Roboticists like contact with the real thing. But fortunately, we are talking with the opposite kind of people here, those who want to keep their hands off the hardware. For them, simulations are key.
In the case of simulations, you have a simulation of a robot running on your computer that can run and act like the real robot. Once the simulation is running, your computer will present to your programs the same ROS API that you would have had if you were in the computer of the real robot. That is exactly the same as in the case of the ROS bags, with the advantage that now you can actually send commands to the robot and the simulated robot will reply accordingly to your commands. That is awesome!
To use robot simulations, ROS provides the Gazebo simulator (http://gazebosim.org/) already installed. You only need to have the simulation of the robot you want to program for running. Usually, the company creators of the robot already provide the simulation of their robot, which you can download and run on the Gazebo simulator.
Installing simulations and making them run could be a little more of a hassle. In case you want to avoid all that work, I recommend you use our ROS Development Studio, which contains the simulations ready to be launched with a single click, as well as having ROS, IDE, and other useful tools.
DEBUGGING
You will also need to debug the code. Most of the IDEs we have presented provide integrated debugging tools on them. However, you can also use all the standard debugging tools used for typical C++ code, including but not limited to:
If you are a ROS beginner, check out the course ROS BASICS IN 5 DAYS to learn how to debug ROS programs, and you will learn the following by using robot simulations:
Introduction to the main Debugging Tools: Logs, RQT, ROSBag…
In this post, we are going step-by-step to install ROS Melodic in a fresh Ubuntu 18.04. I’m using a regular desktop computer, the one I use to support me on ROS development.
Let’s do it!
Step 1 – Configuration
The first step is adding the ROS repository to your ubuntu sources.list
Executing: /tmp/apt-key-gpghome.WudTyznLyJ/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
gpg: key F42ED6FBAB17C654: public key "Open Robotics <info@osrfoundation.org>" imported
gpg: Total number processed: 1
gpg: imported: 1
And finally, update your packages list:
sudo apt update
Hit:1 http://eu-west-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://eu-west-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://eu-west-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:5 http://packages.ros.org/ros/ubuntu bionic InRelease [4669 B]
Get:6 http://packages.ros.org/ros/ubuntu bionic/main amd64 Packages [569 kB]
Fetched 662 kB in 2s (406 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.
Great!
Step 2 – ROS Installation
We have some options at this point. They are:
a. ROS Base
It installs ROS package, build and communication libraries.
It is recommended for embedded computers, where you don’t have much hardware for graphical tools or real robots (in the production stage) to run their specific tasks, without the need of debugging.
sudo apt install ros-melodic-ros-base
b. ROS Desktop
It installs ROS Base, rqt, rviz and robot-generic libraries
These are the basic packages for ROS development. It’s recommended for beginners to follow tutorials about ROS without having to install too much. RQT and RVIZ provides graphical interfaces for visualizing what is happening behind the scenes. With this option, you don’t have to work only using shell.
sudo apt install ros-melodic-desktop
c. ROS Desktop Full
It installs everything of ROS Desktop option plus 2D/3D simulators and 2D/3D perception.
If you want to simulate using ROS default Gazebo version, that’s your option. The Gazebo versions that comes with ROS Melodic is 9.0. (Related post: All about Gazebo 9)
sudo apt install ros-melodic-desktop-full
—
For convenience, I’m installing the ROS Base option, because I want to have a faster installation. After running the command, you must have a confirmation at the end of the output in your terminal:
0 upgraded, 485 newly installed, 0 to remove and 3 not upgraded.
Need to get 234 MB of archives.
After this operation, 1132 MB of additional disk space will be used.
Do you want to continue? [Y/n]
As you can see, ~1 GB will be installed.
Just press enter to confirm. It may take a while. A progress bar must be shown at the bottom:
Go and have a coffee =)
…
In the end, just a regular message about the last packages installed:
Setting up ros-melodic-nodelet-topic-tools (1.9.16-0bionic.20190601.015001) ...
Setting up ros-melodic-nodelet-core (1.9.16-0bionic.20190601.015433) ...
Setting up ros-melodic-roswtf (1.14.3-0bionic.20190601.014658) ...
Setting up ros-melodic-ros-comm (1.14.3-0bionic.20190601.015500) ...
Setting up ros-melodic-ros-core (1.4.1-0bionic.20190601.015718) ...
Setting up ros-melodic-ros-base (1.4.1-0bionic.20190808.193524) ...
Processing triggers for libgdk-pixbuf2.0-0:amd64 (2.36.11-2) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
And you must have a free terminal ready to use again:
user@computer:~$
Step 3 – ROS Dependencies
We are almost there! ROS commands Client can also manage dependencies for you and rosdep is the one in charge of it.
That’s why we have to initialize rosdep. It goes like this:
sudo rosdep init
Wrote /etc/ros/rosdep/sources.list.d/20-default.list
Recommended: please run
rosdep update
Similarly to ubuntu repositories, you need to update rosdep sometimes:
rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml
Query rosdistro index https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml
Skip end-of-life distro "ardent"
Skip end-of-life distro "bouncy"
Add distro "crystal"
Add distro "dashing"
Add distro "eloquent"
Skip end-of-life distro "groovy"
Skip end-of-life distro "hydro"
Skip end-of-life distro "indigo"
Skip end-of-life distro "jade"
Add distro "kinetic"
Skip end-of-life distro "lunar"
Add distro "melodic"
Add distro "noetic"
updated cache in /home/ubuntu/.ros/rosdep/sources.cache
Step 4 – Configuring environment
We have ROS and the dependencies manager installed. Let’s configure our environment. This is a very important step, once we have it done, working with ROS will be smooth.
ROS is installed at /opt/ros/<distro> (in this case /opt/ros/melodic). In order to have ROS commands available, it’s needed to source the shell file inside of the installation folder. This is done like the following:
source /opt/ros/melodic/setup.bash
But.. considering we want to have it available in every terminal we open, we use to have a “shortcut”, which is adding this command to the file "/home/<user>/.bashrc". The .bashrc file is called every time a new terminal is opened, therefore, we won’t need to source ROS setup, since we have the instruction in this file. In order to add the command to the file, you can edit it manually using an editor of your preference or just execute the command below:
At this point, we must have everything in place. Let’s try some ROS commands to make sure the installation has finished successfully.
If you have the same terminal opened from the beginning, consequently, you have to execute the source for your .bashrc file to have ROS commands available.
source ~/.bashrc
And if you have added it to your .bashrc file, that’s the last time you will execute it =)
The first thing we should try it running roscore. In other words, we will run the process in charge of communicating everything ROS-related in a ROS environment.
roscore
... logging to /home/ubuntu/.ros/log/c70b2cce-e773-11e9-9f15-027a087bcd00/roslaunch-ip-172-31-31-24-15073.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
started roslaunch server http://ip-172-31-31-24:45551/
ros_comm version 1.14.3
SUMMARY
========
PARAMETERS
* /rosdistro: melodic
* /rosversion: 1.14.3
NODES
auto-starting new master
process[master]: started with pid [15096]
ROS_MASTER_URI=http://ip-172-31-31-24:11311/
setting /run_id to c70b2cce-e773-11e9-9f15-027a087bcd00
process[rosout-1]: started with pid [15107]
started core service [/rosout]
Your terminal must be stuck here, therefore, you can’t execute anything else while you have roscore process there. roscore is running and ready to serve to other ROS processes!
Open a new terminal (your .bashrc will do the necessary source) and execute:
rosnode list
/rosout
Step 6 – Conclusion
That’s it for today! You have ROS Melodic installed and ready to use!
If you like this kind of content, don’t forget to share it with your colleagues.
Either you like it or not, please leave a comment so we can keep improving our material!
Head to http://rosds.online and create a project with a similar configuration as the one shown below. You can change the details as you like, but please make sure you select “Ubuntu 16.04 + ROS Kinetic + Gazebo 7” under “Configuration”.
Once done with that, open your ROSject. This might take a few moments, please be patient.
Step 2: Create a ROS package with a Python program
Pick a Shell from the Tools menu and create a Python package.
user:~$ cd catkin_ws/
user:~/catkin_ws$ cd src
user:~/catkin_ws/src$ catkin_create_pkg missing_permission
Created file missing_permission/package.xml
Created file missing_permission/CMakeLists.txt
Successfully created files in /home/user/catkin_ws/src/missing_permission. Please adjust the values in package.xml.
user:~/catkin_ws/src$ cd missing_permission/
user:~/catkin_ws/src/missing_permission$ mkdir -p src/
user:~/catkin_ws/src/missing_permission$ cd src
user:~/catkin_ws/src/missing_permission/src$ touch missing_perm.py
user:~/catkin_ws/src/missing_permission/src$
Fire up the IDE from the Tools menu, find the missing_perm.py file in the missing_permission package, open the file and paste the following code into it.
#! /usr/bin/env python
import rospy
rospy.init_node("Obiwan")
rate = rospy.Rate(2)
while not rospy.is_shutdown():
print "Help me Obi-Wan Kenobi, you're my only hope"
rate.sleep()
Save().
Step 3: Compile and source the workspace.
cd ~/catkin_ws
catkin_make
source devel/setup.bash
Step 4: Run the package with rosrun and roslaunch.
First, we check that the package has been recognized, with rospack list. Then we run it with rosrun.
user:~/catkin_ws$ rospack list | grep missing
missing_permission /home/user/catkin_ws/src/missing_permission
user:~/catkin_ws$ rosrun missing_permission missing_perm.py
[rosrun] Couldn't find executable named missing_perm.py below /home/user/catkin_ws/src/missing_permission
[rosrun] Found the following, but they're either not files,
[rosrun] or not executable:
[rosrun] /home/user/catkin_ws/src/missing_permission/src/missing_perm.py
user:~/catkin_ws$
The program did not run! What’s that error? Surely that not what we expected. Maybe rosrun does not like us – let’s try roslaunch!
Create a launch file and use it to launch the python program:
user:~/catkin_ws$ cd src/missing_permission/
user:~/catkin_ws/src/missing_permission$ mkdir -p launch
user:~/catkin_ws/src/missing_permission$ cd launch
user:~/catkin_ws/src/missing_permission/launch$ touch missing_perm.launch
user:~/catkin_ws/src/missing_permission$
Open the launch file in the IDE and paste in the following code:
user:~/catkin_ws/src/missing_permission$ cd ~/catkin_ws
user:~/catkin_ws$ source devel/setup.bash
user:~/catkin_ws$ roslaunch missing_permission missing_perm.launch
... logging to /home/user/.ros/log/e3188a2e-e921-11e9-8ac1-025ee6e69cec/roslaunch-rosdscomputer-11105.log
...
NODES
/
missing_permission_ex (missing_permission/missing_perm.py)
auto-starting new master
process[master]: started with pid [11147]
ROS_MASTER_URI=http://master:11311
setting /run_id to e3188a2e-e921-11e9-8ac1-025ee6e69cec
process[rosout-1]: started with pid [11170]
started core service [/rosout]
ERROR: cannot launch node of type [missing_permission/missing_perm.py]: can't locate node [missing_perm.py] in package [missing_permission]
Oops! It didn’t run again. Now we have another error screaming:
ERROR: cannot launch node of type [missing_permission/missing_perm.py]: can't locate node [missing_perm.py] in package [missing_permission]
What do we do now?
Step 5: Fix the missing execute permission on the Python file and be happy!
user:~/catkin_ws$ cd src/missing_permission/src/
user:~/catkin_ws/src/missing_permission/src$ chmod +x missing_perm.py
user:~/catkin_ws/src/missing_permission/src$
Now let’s try again with both roslaunch and rosrun:
user:~/catkin_ws/src/missing_permission/src$ roslaunch missing_permission missing_perm.launch
...
NODES
/
missing_permission_ex (missing_permission/missing_perm.py)
auto-starting new master
process[master]: started with pid [13206]
ROS_MASTER_URI=http://master:11311
setting /run_id to eec6e306-e922-11e9-b72a-025ee6e69cec
process[rosout-1]: started with pid [13229]
started core service [/rosout]
process[missing_permission_ex-2]: started with pid [13242]
Help me Obi-Wan Kenobi, you're my only hope
Help me Obi-Wan Kenobi, you're my only hope
Help me Obi-Wan Kenobi, you're my only hope
...
Running fine with roslaunch. Press Ctrl + C on the roslaunch program and try rosrun:
user:~/catkin_ws/src/missing_permission/src$ rosrun missing_permission missing_perm.py
Unable to register with master node [http://master:11311]: master may not be running yet. Will keep trying.
If you get the error above, please spin another Shell from the Tools menu, and run the following to start the ROS master:
user:~$ roscore
After this, the roscore command should resume and you’ll get:
user:~/catkin_ws/src/missing_permission/src$ rosrun missing_permission missing_perm.py
Unable to register with master node [http://master:11311]: master may not be running yet. Will keep trying.
Help me Obi-Wan Kenobi, you're my only hope
Help me Obi-Wan Kenobi, you're my only hope
Help me Obi-Wan Kenobi, you're my only hope
...
And that was it. As it turned out, roscore had no ill-feeling towards us!
Did you like this post? Do you have questions about what was explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it 🙂
In order to create ROS programs, you will need a C++ or Python code editor. In this chapter, we are going to show you a list of integrated environments for programming ROS with those languages. Many others do exist, but we are putting the most complete and easy to start with ones here.
Note: since ROS only works on Linux (at least at present), all the IDEs included here are Linux environments, even if Windows versions may exist.
If you don’t already know Linux, here’s a free course for getting started: Linux for Robotics
What follows is a description of two of the best editors for each language. The rest of the information in this chapter has been copied from wiki.ros.org. We are putting it here for your convenience.
CLOUD IDES
Using the ROS Development Studio as your IDE
You can simplify your life when working with ROS by using the ROS Development Studio. The ROSDS comes off-the-shelf with ROS, Gazebo, and IDE already installed. You need no installation on your computer, just a web browser. The main advantage of the ROSDS is that it allows you to develop on ANY operating system. It also allows programming in both C++ or Python with autocomplete functionality.
Additionally, ROSDS provides running simulations of the robots, so you can quickly test your programs in realistic simulations of robots and check that your program is actually doing what it is supposed to do. This is a very important step while developing for robots (as you will see in the next chapter). You have a full set of tutorials about how to use ROSDS here.
ROSDS allows you to share your already-working ROS projects with your peers with a single click. By sharing your projects, your mates will receive an exact copy of what you shared. This implies that your peers will be able to run it the exact same way as you. This is interesting for replicating research results, for providing exercises to students, for robot competitions based on simulations, or for just sharing code with non-experts.
Finally, let us mention that ROSDS can deploy into your real robot. You can switch from testing in the simulation to running in the robot with a couple of clicks. If you choose to use the ROSDS, then you can skip the rest of this chapter.
ROS Development Studio developing a reinforcement learning ROS program for Fetch
LOCAL IDES
What follows is a list of some of the best editors for each language (Python and C++) that you can install on your local computer.
Run (as root) the downloaded file (install script and embedded archive)
$ sudo sh netbeans-6.9.1-ml-cpp-linux.sh
Getting ROS environment variables in NetBeans
NetBeans can “import” project from existing sources. It can use Makefiles, and even run configure script or CMake to generate one. But, that kind of automatic project configuration and build would overwrite stub makefile that every ROS package has. That makefile in essence runs ROS build system for a package, and that is what we want NetBeans to do.
In order to use rosmake from NetBeans, we need to set ROS environment variables in NetBeans. Since NetBeans is started with a shell script on Linux, we can include the variables in the script.
Since from recent versions rosinstall generates setup.sh, setup.bash and setup.zsh (as opposed to just setup.sh which was actually a bash script), there is no need for all the steps that are described in the next section.
The following will suffice to get ROS environment variables to NetBeans:
We’ll try to set up project for Microstrain 3DM-GX2 IMU driver package, so note it’s path:
$ roscd microstrain_3dmgx2_imu
$ pwd
Start “C/C++ Project with Existing Sources” wizard
Use the above path to “Specify the folder that contains existing sources”
Select “Custom” Configuration Mode (note that Automatic Mode recognizes cmake)
Proceed with the wizard accepting defaults
You should get a NetBeans project for microstrain_3dmgx2_imu package with automatically configured Code Assistance (and almost working for all dependencies). E.g. you can see that the bullet library headers weren’t parsed.
We will configure Code Assistance manually. That means entering paths to all header files the package uses and all preprocessor definitions.
To get the paths to include files for the package we will use rospack. Further, we’ll use sed to format them for easier input to NetBeans.
$ rospack cflags-only-I microstrain_3dmgx2_imu | sed 's/ /:/g'-
Open the project properties. Go to Code Assistance -> C++ Compiler and paste the output of the above command to Include Directories field.
Use rospack to find the preprocessor definitions and enter them manually.
The following file netbeans-ros-code_style.zip is prepared to enable auto-formatting of C++ code in NetBeans as defined in CppStyleGuide. In order to use it, you should import it to Netbeans (Tools -> Options -> Import).
With this, the example given in CppStyleGuide#Formatting will be identically formatted, except for extra blank lines before function or class definitions. For a discussion see Google C++ style guide Vertical Whitespace.
2. PYCHARM
PyCharm is an IDE for Python. In order to run and debug ROS functionality, you need to modify the desktop file for PyCharm (the same procedure as for other IDE’s). In Ubuntu 16.04 (and possibly earlier versions), you can edit the launcher file in either /usr/share/ applications or ~/.local/share/applications (depending on whether or not you installed PyCharm for all users). The launcher file may be called pycharm-community.desktop or jetbrains-pycharm- ce.desktop. Change the line that reads
In order to work with packages just create a new project in the parent folder for all your packages or in a particular package. Please note that folder .idea with project files will be created. You can add it to .gitignore file in case if you are using Git and do not want to commit this file to repository. PyCharm will parse all files in the packages and allow you quick navigation, fast code completions, run and debug Python code, unitest run and debug.
Code
Code can be run using roslaunch or rosrun command from the command line. Simple Python files can be run using run context menu.
In order to debug Python node do the following changes:
Comment node in the launch file my_nodes.launch
If the node has any parameters specified inside put them into <group> tag with ns attribute equal to node name
Launch my_nodes.launch using roslaunch command
Run Python node from PyCharm IDE in debug mode
Unittest
Unittest can be simply run using content menu on the file in the project tree or on a particular method in the code. Results would be shown in UI.
Unittest can be normally debug using start debug menu. In case of integration test (rostest):
Comment <test> tag in the my_file.test launch file
launch my_file.test using roslaunch not rostest
launch test using PyCharm unittest debug from IDE
Custom Messages/Services
Define and build your messages/services as usual. In order Pycharm to recognize them (for autocompletion, etc.):
Open File – Settings
Select Project: projectname – Project Structure
Select src folder and click on Mark as Sources
Click OK
IDES FOR C++
1. Eclipse
Eclipse’s built-in C++ indexing capabilities have gotten quite good in recent versions.
Installing Eclipse
To use this tutorial, users should not “sudo apt-get install eclipse”. Instead:
CMake can produce Eclipse project files automatically. These project files then set up all include paths correctly, so that auto-completion and code browsing will work out of the box.
However, currently, a small hack is required for generating these project files in the right folder. The problem is that ROS creates the Makefiles and all other build artifacts in the build/ folder, but Eclipse expects the source files to be located within (or below) the folder where its project files are stored.
Fortunately, there is now a make target using a small script that circumvents this problem, by forcing CMake to create the project files in the directory of your package (and not the build/ folder). Proceed as follows:
Open a terminal, roscd into the folder of your package, and execute:make eclipse-project
You will now find two Eclipse files in your package. It is not recommended to add them to the repository, as they contain absolute links and will be different for different users checking out your software.
Note that if you change anything to your manifest.xml, you will have to run this script again, which will overwrite your Eclipse project file and thereby reverting all manual changes to the project settings.
Note: Simply running the cmake Eclipse generator like
cmake -G"Eclipse CDT4 - Unix Makefiles"
will overwrite the Makefile. This can also happen if make eclipse-project does not complete successfully. If you already did this, you will want to restore the original Makefile, which should contain the following line:
include $(shell rospack find mk)/cmake.mk
2. Creating eclipse files for multiple packages/stacks
Go to the directory where your packages reside (which may be a stack-folder or just a simple folder) and run:
If you need to convert deeper nested packages or multiple stacks at once be encouraged to use this eclipse projects bash script (https://gist.github.com/1098960) for subdirectories.
3. Catkin-y approach
If you are using catkin, you do not have the possibility to use make eclipse-project. You need to execute:
to pass the current shell environment into the make process in Eclipse.
After executing this command you will find the project files in the build/ folder. Now you can import your project as an existing project into the workspace.
Maybe you will need to execute the following if you would like to debug your program. To execute this command cd to the build/ folder. You should do so if you e.g. get an error like “No source available for main()”.
For me, the above procedure didn’t generate a .pydevproject file, like make eclipse-project ever did. Clicking Set as PyDev Project would create a config but without any Paths, so coding would be a hassle.
Workaround: From within the package you want to program run:
python $(rospack find mk)/make_pydev_project.py
Now copy the created file .pydevproject (which has all dependency package paths included) to /build and import your catkin-project into eclipse or set it as PyDev Project if already imported.
5. catkin tools
With the new catkin_tools, there are few changed from the Catkin-y method described above. To generate eclipse-project you need to execute:
to generate the .project files for each package and then run: the following script
$ ROOT=$PWD
$ cd build
$ for PROJECT in `find $PWD -name .project`; do
DIR=`dirname $PROJECT`
echo $DIR
cd $DIR
awk -f $(rospack find mk)/eclipse.awk .project > .project_with_env && mv .project_with_env .project
$ done
$ cd $ROOT
To debug use the following command and you can mention the name of the package to configure that specific project for debug instead of the entire workspace. Remember to run the script to modify .project to pass the current shell environment into the make process in Eclipse.
Now start Eclipse, select File –> Import –> Existing projects into workspace, hit next, then browse for your package’s directory (select root directory). Do NOT select Copy projects into workspace. Then finish.
You should now be able to browse through the code (hold down CTRL while clicking on a function/class name), get auto-completion (automatically appears, or press CTRL-SPACE) et cetera.
7. Fixing unresolved includes
There are many possible reasons why indexing cannot resolve all includes, functions, methods, variables, etc. Often, fixing the resolving of includes solves these errors. If you have any problems, these might be fixed by:
If the dependencies of your project have changed since first adding them to Eclipse, regenerate the project files and reimport the project into your workspace.
Making sure to load your .bashrc environment with Eclipse, by launching it using bash -i – c “eclipse” (see Reusing your shell’s environment).
In Eclipse, right-click the project, click properties -> C/C++ general -> Preprocessor Include Paths, Macros etc. Click the tab “Providers” and check the box next to “CDT GCC Built-in Compiler Settings [ Shared ]”.
Afterward, right-click the project, select Index -> Rebuild. This will rebuild your index. Once this is done, usually all includes will resolve.
As a last resort, you can also manually add folders that will be searched for headers, using right-click project -> properties -> C/C++ Include Paths and Symbols. This is usually not necessary though.
8. Building the project inside Eclipse
The eclipse-project makes the target automatically tries to set the environment variables correctly such that building within Eclipse should work out-of-the-box. Especially if you follow Reusing your shell’s environment from above.
If not, this is where you need to start looking: Right-click on the project, select Properties — > C/C++ Make Project –> Environment, and check whether the following environment variables are assigned to the correct values:
ROS_ROOT
ROS_PACKAGE_PATH
PYTHONPATH
PATH
The easiest way to obtain the correct values for your installation is to open a terminal and run
9. Running and debugging your executables within Eclipse
As for building within Eclipse, the crucial step here is to set the required environment variables correctly in the launch configuration. As the same for building, this should work out-of-the-box, especially if you follow Reusing your shell’s environment from above.
Create a new launch configuration, right-click on the project, select Run –> Run configurations… –> C/C++ Application (double click or click on New). Select the correct binary on the main tab (Search project should work when your binary was already built). Then in the environment tab, add (at least)
ROS_ROOT
ROS_MASTER_URI
again with the values of your installation. If you are unsure about them, open a terminal and run
$ echo $ROS_ROOT
$ echo $ROS_MASTER_URI
Finally, if you cannot save the configuration, remove the @ character in the name of the new run configuration.
This should now allow you to run and debug your programs within Eclipse. The output directly goes into the Console of Eclipse. Note that the ROS_INFO macros use ANSI escape sequences, which are not parsed by Eclipse; therefore, the output might look similar to this one (from Writing a Publisher/Subscriber (C++)):
[0m[ INFO] [1276011369.925053237]: I published [Hello there! This is message [0]][0m
[0m[ INFO] [1276011370.125082573]: I published [Hello there! This is message [1]][0m
[0m[ INFO] [1276011370.325025148]: I published [Hello there! This is message [2]][0m
[0m[ INFO] [1276011370.525034947]: I published [Hello there! This is message [3]][0m
Setup a file template that pre-formats whenever a new source or header file is created. The template could, for example, contain the license header, author name, and include guards (in case of a header file). To set up the templates, choose in the Preferences C/C++->Code Style->Code Templates. For example, to add the license header choose Files->C++ Source File->Default C++ source template and click on Edit… Paste the license header and click OK. From now on, all source files while automatically contain the license header.
Enable Doxygen with the project properties clicking on C/C++ General, enabling project-specific settings and selecting Doxygen as Documentation tool. This option automatically completes Doxygen style comments highlights them.
People that are used to the emacs key bindings can select emacs-style bindings in Windows- >Preferences General->Keys and selecting the emacs Scheme. Also, other useful key bindings (e.g. make project) can easily be added.
To also index files that live outside the ROS tree (e.g. the boost include files) you can add them in project properties C/C++ General->Paths and Symbols.
The generated eclipse project also works great for Python code. Just install the PyDev plugin for syntax highlighting and code completion.
11. Auto Formatting
Eclipse also has extensive formatting configuration capabilities. To add the ROS formatting profile to Eclipse, perform the following steps:
Download ROS_Format.xml to some location (versions: Indigo Kepler)
Start Eclipse
Select Window->Preferences->C/C++->Code Style
Click Import…
Select ROS_format.xml from the location used in step 1
Click OK
As you edit a file, Eclipse should use this new profile to format your code following the ROS conventions. To reformat an entire file, select Edit->Format.
2. QTCreator
As QtCreator supports opening CMake projects out of the box, it does not require a setup procedure if started from a terminal. Note that this is absolutely crucial because otherwise the environment will not be set correctly and functionality related to rosbuild or catkin will fail when running cmake.
Note that instead of starting QtCreator from a terminal, you can use the following modification to the desktop file which normally in Ubuntu resides in /usr/share/ applications if you did a system-wide installation or in ~/.local/share/applications if you installed it only for your user:
In Ubuntu 13.04 and later, the third line must read:
Icon=QtProject-qtcreator
You should not try to generate this file yourself, but rather modify the file that was created when you installed QtCreator. Add bash -i -c in the Exec line and use it in your launcher. This will run your QtCreator in a shell which should source all required setup.bash files of your ROS installation and workspace. More about desktop files and their locations for Ubuntu can be found here. Note also that the same trick can be used with eclipse.
If you are experiencing issues with the qtcreator package shipped by Ubuntu (or want to use a more up to date version of QtCreator) when opening the CMakeLists, then try installing QtCreator from Nokia’s installer.
1. catkin_make
To open a catkin code as a project, use “Open File or Project” and select the top-level CMakeLists.txt of the catkin workspace (e.g. “catkin_ws/src/CMakeLists.txt“). Select the catkin build folder (e.g. “catkin_ws/build“) as the build directory and ‘Run CMake‘ (in order to enable debugging add following line into arguments edit box:
-DCMAKE_BUILD_TYPE=Debug).
Recently this has started to fail with errors like “CMake Error: The source directory “/opt/ ros/lunar/share/catkin/cmake” does not appear to contain CMakeLists.txt.”, because the main CMakeLists is a symlink to a non-writable location. The workaround is to make a copy of toplevel.cmake instead of using a symlink:
To be able to modify all the files in the workspace add those lines in “src/CMakeLists.txt” :
#Add custom (non compiling) targets so launch scripts and python files show up in QT Creator's project view.
file(GLOB_RECURSE EXTRA_FILES */*)
add_custom_target(${PROJECT_NAME}_OTHER_FILES ALL WORKING_DIRECTORY $ {PROJECT_SOURCE_DIR} SOURCES ${EXTRA_FILES})
If you want the project to be named something else than “Project” then add a line at the top with project(!MyProjectName).
You may specify the correct catkin devel and install spaces at Projects->Build Settings by providing the following CMake arguments:
With the new catkin_tools, there is no longer a top-level make file for the whole workspace. Instead, open each package as an individual project in QtCreator. Make sure, that the build folder is set to ws/build/your_package instead of ws/build.
Before opening a package with QtCreator, though, make sure to build the package once with catkin build. If your build artifacts (binaries, libraries, …) end up in the wrong directory, you built the package first with QtCreator. You can check, whether you have this problem by simply doing a rosrun of your package’s node, change code, recompile with QtCreator and do a rosrun again — if you don’t see your changes in the executable’s behavior, it is probably installed into the wrong directory. To resolve this issue, just clean the package (catkin clean <pkg> for linked layout, for newer catkins, remove the build/<pkg> folder) and rebuild it with catkin build.
With QtCreator of version 4 and higher, you can (and actually have to) configure your compiler etc. in a Kit.
Go to Tools — Options — Build & Run — Kits. In the Default kit (or create a new kit for ROS)
Select the compiler you want to use in catkin
Select CodeBlocks — Unix Makfiles
Change the CMake Configuration to only contain QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable} (i.e. remove the define for CMAKE_CXX_COMPILER)
where you substitute /usr/bin/g++ with the compiler you actually want to use (and which is the same that you selected in the kit above). See this discussion about this issue.
You can configure some default paths in QtCreator:
The default build path (what you normally have to set manually once you open a new project): In Tools — Options — Build & Run — General set the Default build directory to
The path in which to look for projects (i.e. where to start the search when you want to open a new project): Tools — Options — Build & Run — General — Projects directory set Directory to /home//workspace/src (or wherever your workspace is).
3. Enable Clang Code Model
Install recent clang (version >= 3.6) and this plugin. See the link for how to enable it. Although this may slow down your computer it is a very valuable tool to give you compiler warnings on-line.
4. Troubleshooting
When running cmake in QtCreator (when you open the package), check the output for error messages — they provide a clue on what is going wrong.
To remove cached/stored information for a project, remove the CMakeLists.txt.user (possibly with some trailing numbers) in your project and re-open the project. If that does not solve your problem, repeat (remove CMakeLists.txt.user) and additionally remove (or rather rename) the QtCreator configuration in ~/.config/QtProject and ~/.local/share/ data/QtProject/qtcreator and try again.
In this post, we will see what Rviz is all about and how to use it.
We are going to use a ROS1 installation for this, but you don’t need to install ROS, as we will use the ROS Development Studio (ROSDS), an online platform that provides access to ROS1 or ROS2 computers and other powerful ROS tools within a browser!
Once you have cloned the project, open it up and give it a moment to finish loading.
Step 2: Bring up a TurtleBot2 simulation
Head to the Simulations menu and launch one of the pre-configured simulations:
Select “Simulations from the main menu”.
Under “World”, select “Empty to World”.
Under “Robot”, select TurtleBot2.
Click “Start Simulation”.
You should now have something like this:
That’s a TutleBot2 facing a wall.
Step 3: Launch the rviz program and use it to “spy” on the TurtleBot
We need two more tools here.
Minimize the Gazebo window.
From the Tools menu, pick “Shell” and “Graphical Tools”. Arrange them side-by-side for better visibility.
On the Shell tool, run the following command:
rosrun rviz rviz
You should now have something like this:
Maximize the Rviz window and:
Double-click the Title bar to bring the window into full focus. Ensure you can clearly see the “Add” button lablled 3 below.
Change the “Fixed frame” to “base_link”.
Click “Add” to add a visualization.
Select “Camera”
Click okay.
Semi-finally, select a topic for the Camera visualization. You should see the brick wall in full color now!
Finally, pick another Shell from the Tools menu and run the following command to cause the robot to rotate. You should see the wall in the Camera disappear and appear again as the front camera position changes.
# Just type rostopic pub /cmd_vel and then press TAB-TAB to complete message structure.
# Then change angular.z to 1.0
user:~$ rostopic pub /cmd_vel geometry_msgs/Twist "linear:
x: 0.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 1.0"
Interesting, isn’t it? You can try and play with other visualization tools and see how they work.
Now’s let review the theory briefly.
Step 4: So, what’s Rviz?
We have just seen Rviz in action. So what exactly is this amazing tool?
Short for ROS Visualization. It’s a 3-dimensional visualization tool for ROS.
It helps to visualize what the robot seeing and doing.
And that’s it. It’s more practical than theory 🙂 .
Extra: Video
Prefer to watch a video demonstrating the steps above? We have one for you below!
Related Resources and Further Learning
If you are a ROS beginner and want to learn ROS basics fast, we recommend you take any of the following courses on Robot Ignite Academy:
Did you like this post? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS topics, please let us know in the comments area and we will do a video or post about it