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 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
sudo apt-get update
sudo apt-get install ros-kinetic-desktop-full
Initialise ROS dependencies
sudo rosdep init
Setup ROS environment
echo "source /opt/ros/kinetic/setup.bash" >> ~/.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
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
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.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
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
ROS core is required in the background anytime you want to use ROS, so first (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)
(note, if you do
sudo chmod 666 /dev/ttyUSB0
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:
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.