Offboard Control Example (ROS C++)
This tutorial covers how to write an offboard control node in ROS with the provided C++ API for the Kerloud flying rover series. The offboard example is to command the flying rover for waypoint missions in both rover and multicopter modes.
1. Environment Setup
The recommended environment for this tutorial is ROS noetic with Ubuntu 20.04, which is the default environment for the onboard Raspberry Pi model 4.
The ready-to-use ROS workspace is under the directory ~/src/flyingrover_workspace/catkinws_offboard. The workspace contains the follow packages maintained in our official repositories listed below:
mavros (dev_flyingrover branch): https://github.com/cloudkernel-tech/mavros
mavlink (dev_flyingrover branch): https://github.com/cloudkernel-tech/mavlink-gdp-release
offboard control node (dev_flyingrover branch): https://github.com/cloudkernel-tech/offboard_demo
To update to latest version, users can conduct these commands as follows:
cd ~/src/flyingrover_workspace/catkinws_offboard/src/mavros
git pull origin
git checkout dev_flyingrover
cd ~/src/flyingrover_workspace/catkinws_offboard/src/mavlink
git pull origin
git checkout dev_flyingrover
cd ~/src/flyingrover_workspace/catkinws_offboard/offboard_demo
git pull origin
git checkout dev_flyingroverDue to continuous software upgrade for Kerloud products, users can create the workspace themselves in the directory ~/src/flyingrover_workspace if they don't find the code there.
To build the workspace,
It can take up to 10 minutes for the build process to finish in a raspberry Pi computer, so be patient for the first time.
To clean the workspace, use the command:
2. Code Explanation
The code example is in a standard ROS node form, and users have to familiarize with the official ROS tutorial on writing publisher and subscriber (http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29).
To highlight, the offboard_flyingrover node contains several files:
offboard_demo/launch/off_mission.launch: a default launch file to launch the offboard control node.
offboard_demo/launch/waypoints_xyzy.yaml: waypoint mission definition file with ENU coordinates and corresponding yaw values.
offboard_demo/src/off_mission_node.cpp: the ROS node file for offboard control
The mission for this example is defined as: the flying rover will firstly operate in the rover mode and run through several waypoints. After it reaches the last waypoint, the vehicle will transit to the multicopter mode, and then takeoff to fly to the same waypoints sequentially.
Here we illustrate the main function of the off_mission_node in sequence for clarity.
(1) Declarations of subscriptions, publications and services
We usually declare ROS subscriptions, publications and services at the beginning of a main program.
To obtain the state information or send commands, various topics or services from the mavros package can be easily utilized. For example, to obtain the local position information for the UAV, the mavros/setpoint_position/local topic should be subscribed. Kindly note that the coordinates are defined in the ENU (East-North-Up) frame. The standard information for the mavros package can be referred in http://wiki.ros.org/mavros. For other topics or sevices specifically for the flying rover, please refer to the API section.
(2) Service command construction
We then construct necessary commands to call those defined services above as below. Note these commands for flying rover mode transition, and users have to understand them clearly before utilizing for real flights.
(3) Waypoint coordinate parsing for rover and multicopter modes
The waypoint coordinates for rover and multicopter modes are passed to the position setpoint topic differently. For multicopter mode, the 3D position is needed, and we also have to handle the takeoff process seperately. For rover mode, the 2D position can be directly passed to the desired topic.
(4) Mode switching
The last part of this example is to handle the mode transition for the vehicle. After it reaches the last waypoint in the rover mode, the program will call the transition service to switch to the multicopter mode. Once the transition is successful, the waypoint index is reset to zero, and then the vehicle will start takeoff and fly through the same waypoints again.
3. How to Run for Real Tests
Now it's time to take the flying rover outside for real tests. Users have to follow the orders below carefully to avoid unexpected behaviors:
Mount the battery ready with a battery cable, make sure that it will not fall down during flight.
Make sure that all switches are in their initial position and the throttle channel is lowered down.
Power on with the battery.
Wait for the GPS to be fixed: usually we can hear a reminding sound after 2-3 minutes, and the main LED in the pixhawk will turn green.
Make sure that the safety pilot is standby, and there are no people surrounding the vehicle. Wait a few minutes for the onboard computer to boot, log into it remotely via a local wifi network, confirm the waypoint mission defined in ~/src/catkinws_offboard/src/offboard_flyingrover/launch/waypoints_xyzy.yaml, and run commands below to start the offboard control modules:
Arm the vehicle by lowering down the throttle and move the yaw stick to the right, then we will hear a long beep from the machine.
Switch to the OFFBOARD mode with the specified stick (e.g. channel 7), then the machine will start the waypoint mission autonomously. Cheers!
Last updated
Was this helpful?