Offboard Control with Mavros (Python)
Note: This tutorial is applicable for all Kerloud UAV products.
The offboard control with mavros in python language is conducted in the same fashion as the C++ case. The idea is to utilize standard topics or services in the mavros package to retrieve vehicle information or send commands. The workspace can be found in ~/src/uav_workspace/pythonuav_ros, and all necessary prerequisites are set up in factory.
1. Environment Setup
Dependencies for this tutorial are:
mavros: standard mavros package with different ROS versions, which can be installed by: sudo apt install ros-<ROS_VERSION>-mavros
mavlink: standard mavlink package with different ROS versions, which can be installed by: sudo apt install ros-<ROS_VERSION>-mavlink
Users can refer to the README file in the workspace for details.
We also maintain customized mavros & mavlink packages in github for compatibility concern, which can be referred in:
mavros (dev_kerlouduav branch): https://github.com/cloudkernel-tech/mavros
mavlink (dev_kerlouduav branch): https://github.com/cloudkernel-tech/mavlink-gdp-release
To use these packages, users have to create a new catkin workspace, and need to source the environment by source devel/setup.bash each time when they want to launch these customized nodes.
2. Code Explanation
The workspace consists of two program files: commander.py and px4_mavros_run.py. The commander.py (Commander Interface) provides a user-friendly interface for application development, and px4_mavros_run.py (Control Interface) encapsulates low level message communication with mavros.
2.1 Commander Interface
The Commander Interface implements friendly API for user programming, like move(), turn(), hover(), land() commands. The main entry of the interface is as follows:
The commander class is first instantiated as an object, and then sleeps for 2 seconds. The program will request the vehicle to go to several waypoints defined in ENU frame by default. Make sure to ajust the sleep time so the vehicle can reach the desired position. Users can edit the entry function easily by adding more UAV motions such as hover, turn, etc.
2.2 Control Interface
The Control Interface implements low level communication with the mavros gateway, and there is no need to change it for most cases. The entry function is start() in the Px4Controller class, which will be called when the px4_mavros_run.py is executed.
In the start() function, the ros node is initialized first, then the program will wait for the IMU heading data to be ready so it could construct the initial target pose for vehicle command. The program will first command the vehicle to takeoff to a desired height (3.2m by default).
Then the program will arm and set offboard control mode automatically:
Later on, the program will enter a while loop to respond to different commands from the Commander Interface, including position setpoints, turn, hover, etc.
3. How to Run for SITL Simulation
We highly recommend users to conduct SITL simulation test before real flights. The setup procedure of SITL environment can be referred in the previous C++ offboard control tutorial .
Assuming that the firmware repo is in the directory ~/src/Firmware, the gazebo-based SITL simulation can be started with:
4. How to Run for Real Tests
Users are advised to follow the procedures below exactly to run the demo:
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/uav_workspace/pythonuav_ros/commander.py, and run commands below to start the offboard control modules:
The vehicle will takeoff first and carry out the defined mission autonomously. Be sure to switch to the POSITION mode if any unexpected behavior happens.
Last updated