Mavros, ROS and Pixhawk


Im trying to control my drone (with a pixhawk and PX4) from my PC with Ubuntu 16.04.
I’ve tried this example with the drone.

I dont know what I have to do to compile this program with ROS. I tried to create a ROS package with catkin_init, paste the example in the src directory and modify the CMakeList.txt adding the dependys as follow (using the ROS tutorials):

-Create the workspace for catkin:
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ cd ~/catkin_ws/
$ catkin_make
$ source devel/setup.bash

-Creating a catkin package:
$ cd ~/catkin_ws/src
catkin_create_pkg drone_control std_msgs rospy roscpp
$ cd ~/catkin_ws
$ catkin_make
$ . ~/catkin_ws/devel/setup.bash

-Writing the publisher node:
In the source directory i create a cpp archive: offb_node.cpp and I paste the code into it.
Add the following lines to the CMakeLists.txt:

include_directories(include ${catkin_INCLUDE_DIRS})

add_executable(control src/offb_node.cpp)
target_link_libraries(control ${catkin_LIBRARIES})
add_dependencies(control drone_control_generate_messages_cpp)

But when i try to compile i get an error.

Can someone told me how to create a ros node to control a drone using Mavros step by step?

Best regards,

Hi Luis,

Your structure of the catkin workspace has to be different. You are building your packages, for example a package for your offb_node.cpp, into your src folder of your workspace. In this packages you need again a src folder where you will have your offb_node.cpp. Also in this package there have to be a package.xml and a CMakeLists.txt. I recommend you to do the beginner tutorial for ROS about packages:

There the structure of a catkin workspace is explained.

Best regards
Nils Rottmann

I would also appreaciate a walkthrough, in linux 16.04. The ROS tutorial page does not really help.
On one hand on page
“catkin build” is used, whereas in some of the examples they mention “catkin_make”, which does not work.
“catkin_make_isolated” works, but then from
the command
roslaunch mavros px4.launch fcu_url:=“udp://:14540@”
does not work.

It would be really nice to have a minimal working example.

@arta catkin_make is old. You should be fine with using only catkin build

Can you show the error you are having with roslaunch mavros px4.launch fcu_url:="udp://:14540@" ?

How are you running PX4? on SITL?

Hi @Jaeyoung-Lim,
Thanks for the prompt response. I had forgotten to update my progress. My problem was that I was trying to make the offboard example work from this page . However I found the following at first very confusing:

“Create the offb_node.cpp file in your ROS package (by also adding it to your CMakeList.txt so it is compiled), and paste the following inside it”

So I started to create a ROS package etc, and then I started to ruin into weird build issues. What I do now is that I added “offb_node.cpp” to "/home//catkin_ws/src/mavros/mavros/src/ " and then


to CMakelists.txt in "/home//catkin_ws/src/mavros/mavros/ ". Finally I do “catkin build”. And then I am able to start things by first launching PX4 + simulator, then in a separate console

roslaunch mavros px4.launch fcu_url:=“udp://:14540@”

Then in a separate console

rosrun mavros offb_node

And now everything works fine (although little Mavros learning material, but that is another issue… :slight_smile: )
If I am doing something wrong, please let me know!

hi there!
if i try it the way you did, i get the error: Couldn’t fine executable named offb_node

can you help me?

what i did:

  • offb_node.cpp in "/home//catkin_ws/src/mavros/mavros/src/ "
  • expanded the Cmakelist.
  • opend gazebo with: make px4_sitl_default_gazebo
  • tryed to run: rosrun mavros offb_node

gazebo opens with en quadrocopter.
but if i try to run the offb_node i get the error…

Did you solve this issue? I am getting the same error running ROS kinetic

Nevermind, resourcing the catkin_ws solved this issue

Can you please explain how it worked for you.
I am struggling in the “Finally I do “catkin build”.” part of @arta explanation.
Please help.