You are here:-----PX4 Research Log [12] – PX4 Off-board Control with MAVROS (1)

PX4 Research Log [12] – PX4 Off-board Control with MAVROS (1)

Companion computer is a very efficient way to control your PX4 vehicle in off-board mode. We have tested various methods and we have concluded Robot Operating System (ROS) is the best way. Here is a series of tutorials guiding you to achieve off-board control.

What you need:

  • A Pixhawk or a Pixracer
  • A Raspberry Pi 3 Model B (this is the only one we have tested), running Ubuntu Mate

Install ROS


Install ROS Kinetic on your Raspberry Pi, (alternatively install it on a Debian computer to test it our first),  Kinetic is the only ROS distro we have tested, on Ubuntu MATE

Set up source.list

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

Set up keys

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116

Install

sudo apt-get update

sudo apt-get install ros-kinetic-desktop-full

Initialise ROS dependencies

sudo rosdep init

rosdep update

Setup ROS environment

echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc

Install dependencies for building packages

sudo apt-get install python-rosinstall python-rosinstall-generator python-wstool build-essential

Check if your environment is setup properly

printenv | grep ROS

Install rqt_graph

sudo apt-get install ros-kinetic-rqt

sudo apt-get install ros-kinetic-rqt-common-plugins

 

Create ROS workspace


First we create the catkin workspace. Catkin is a build tool made for ROS.

mkdir -p ~/catkin_ws/src

cd ~/catkin_ws/

catkin_make

echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc

source ~/.bashrc

 

Useful ROS filesystem tools

rospack – gives information about packages

roscd – change directory to the package

rosls – lists files in the package

 

Install MAVROS and create/build MAVROS package


sudo apt-get install ros-kinetic-mavros ros-kinetic-mavros-extras

The command to create a package is:

catkin_create_pkg <package_name> [dependency1] [dependency2]

So to create our MAVROS package, let’s call it px4_mavros, change directory to the ROS workspace source folder (e.g. ~/catkin_ws/src), and:

catkin_create_pkg px4_mavros std_msgs rospy roscpp

Now change directory to the new px4_mavros folder, and edit the package.xml file. Add message_generation to the build_depend field, and add message_runtime to the run_depend field. Now go to the root directory of the workspace, run

catkin_make

The package we have created will be used in the second tutorial.

 

Pixhawk configuration and wiring


The Pixhawk should be configured as:

  • Telemetry 2 should be configured to companion link baudrate 921600
  • The telemetry 2 port should be physically connected to your computer, using a usb to ttl adapter

 

Launch mavros


ROS core is required in the background anytime you want to use ROS, so first

roscore (edit: roslaunch will actually launch a ROS core as well, this step is not needed).

Change the launch file in /opt/ros/kinetic/share/mavros/launch/px4.launch according to your system, the device is typically /dev/ttyUSB0 on Ubuntu, and the baudrate should be set to 921600. Now we give permission to use this device and launch mavros (do this in a new terminal)

sudo chmod 666 /dev/ttyUSB0 (note, if you do sudo usermod -a -G dialout $USER then log out and log in again, you shouldn’t need to chmod 666 every time)

roslaunch mavros px4.launch

This creates a mavros node that talks to your pixhawk running PX4. To see all the ROS nodes, go to a new terminal and:

rosnode list

you should see mavros as one of the nodes. You can also see it visually by

rosrun rqt_graph rqt_graph

Now let us listen to what PX4 is talking to ROS

rostopic echo /mavros/imu/data

This will print the imu data on the pixhawk to the terminal, you may move your controller around to see the changes being reflected in the terminal.

If you are using a real vehicle, make sure the propeller is not installed, you can try arm the vehicle using:

rosrun mavros mavsafety arm

I highly recommend you run your Pixhawk in a simulated environment such as the jMAVsim I introduced in tutorial 8. You can disarm the vehicle by using:

rosrun mavros mavsafety disarm

 

In the next post I will run a simple example and plot the data on the screen.

By |2017-08-15T05:41:44+00:00August 15th, 2017|Categories: Flight Control, PX4, Software, Tutorials|Tags: , , |0 Comments

About the Author:

Leave A Comment