Secure Autonomous and Cyber-Physical Systems
Instructor: Prof. Sibin Mohan, Oregon State University
CS/ECE 599 | Winter 2022 Term | MW 2:00 - 3:50 PM PT | BAT 150
MP II-A: PX4 Autopilot & uORB Middleware
Administrivia
Announcement Date |
Feb. 12, 2022 |
Submission Date |
Feb. 18, 2022 |
Submission Time |
11:59PM PT |
Total Points |
8 |
Objective
There are multiple flight firmwares with different architecture/design principles but understanding one of them helps with understanding others.
In this MP, we look into PX4 autopilot, an open-source autopilot firmware.
The following objectives are designed to help you familiarize with flight controller firmware:
-
Install and use PX4 and PX4 Software-In-The-Loop simulatior to perform quadrotor flight simulation.
-
Gain familiarity with MAVLink and controlling the UAV using offboard control.
-
Understand internals of the PX4 and uORB middleware.
Prelude
The usage of drones is gaining popularity: from agricultural usage to a more safety critical usage such as firefighting and search and rescue.
Therefore, MP2 will focus on a auto-pilot flight controller.
There are two popular opensource auto-pilot flight controller: PX4 and Ardupilot.
Despite the archetectual difference between the two, they both rely on the usage of a middleware to exchange messages between their components.
In this MP, we will be using PX4 and its Software-In-The-Loop (SITL) flight controller simulation with Gazebo physics simulation.
-
PX4 is a popular opensource autopilot that controls multicopters, fixed-wing aircrafts, rovers and submarines.
As shown in the diagram, PX4 is composed of modules and the modules communicate using the uORB middleware.
-
uORB middware is a publish-subscribe middleware where a module can publish certain message topics and other modules can subscribe to the topic to get the message.
-
PX4 provides Software-In-The-Loop(SITL) simulation platform where PX4 controls a simulated vehicle in the host machine.
-
By default, SITL provides simplistic physics simulation. However, it supports Gazebo for more complex simulation.
-
PX4 also provides Hardware-In-The-Loop where the environment is simulated in the host machine but the flight controller runs in the flgiht hardware.
Setting up PX4
We prepared a VM image which has the PX4 setup.
The setup has the following system requirement:
- 13GB Storage
- At least 4GB memory
- VirtualBox/VMWare
To setup, download the image from here.
The username and password for the VM image is student
Running the PX4-SITL
Running the simulator
- Download the VM image here.
-
In side the VM, change the directory to the PX4:
cd /home/student/Desktop/MP2/PX4-Autopilot/
-
To compile and start the simulation run:
make px4_sitl gazebo
-
In PX4 command prompt, start the mp2 module
mp2 start
-
In another terminal, change the directory to MP2 base directory:
cd /home/student/Desktop/MP2/PX4-Autopilot/
-
If the run is successful, you should see the following:
-
In another terminal, change the directory to the MP2 base directory and run the offboard control:
python3 mp2_offboard_control.py
MP II-A Instructions
Mavlink and Offboard Control
- Mavlink is the messaging protocol used by UAVs and rovers which contains information regarding the sensor data, state of the vehicle, and status of the vehicle.
- The protocol also contains commands to control the vehicle.
- Offboard control is a method of controlling the UAV like a script.
- For instance, the offboard control script can command the vehicle to move up by a meter or maintain certain velocity or attitude.
- In this MP, we will be using MAVSDK-Python and the script is located in
~/Desktop/MP2/mp2_offboard_control.py
- Check out the example script to implement the missions.
- Also checkout the API documentation.
-
Implement linear mission
- Place the setpoint to 10 meters above the ground for takeoff.
- Laterally move the vehicle by placing a position setpoint anywhere in the same altitude.
- Land where the vehicle was moved to.
-
Implement circular mission
- Place the setpoint to 10 meters above the ground for takeoff.
- Move the vehicle by placing multiple setpoints (at least 16) around the place of takeoff in a circular shape with radius equal to 20 meters
- The circular mission does not need to be a perfect circle.
-
-
When the script ends, the script outputs
traj.npy
in numpy format.
-
To read the data, use
numpy.load()
.
-
The data is a matrix in following shape
(# of logs, 6)
where columns consists of (north_pos, east_pos, down_pos, north_vel, east_vel, down_vel).
-
Because the coordinates are in North, East, Down (NED) format, when the drone takes off, the down_pos value should be negative and decrease.
-
We provided a script which can run with the following command
python3 mp2_plot_traj.py
.
-
The script creates a window with a 3D plot which can be rotated using mouse can the plot can be saved using the save icon.
-
The red and blue points represent the starting point of the log and the end points of the log respectively (i.e., the vehicle moved from red point to the blue point following the line).
-
The blue point does not need to reach the ground since it only implies that the logging ended before the vehicle fully landed.
-
However, Make sure that the vehicle reaches the position before sending out the next command.
-
To read the latest position, use
async with lock:
and read from the variable velpos
.
uORB: Understanding the middleware
- We provided our skeleton module located in
PX4-Autopilot/src/modules/mp2
.
However, it needs to subscribe to the vehicle_local_position
.
You must uncomment /*
and */
.
- To subscribe to the topic, use
orb_subscribe(ORB_ID(INSERT_TOPIC_NAME_HERE))
to get the subscription id.
- Poll for the topic using
px4_poll()
.
- If the polling recieves the topic of interest, the data can be read by first creating the struct to hold the data:
struct INSERT_TOPIC_NAME_s topic_msg
.
For example, to create a struct variable to hold the sensor_gyro
message, use struct sensor_gyro_s var_name
.
- Copy the data to the struct using
orb_copy(topic id, subscription id, reference to struct)
(e.g., for sensor_gyro
, it would be orb_copy(ORB_ID(sensor_gyro), gyro_sub_id, &var_name);
.
- Check out this link for a more indepth tutorial.
Therefore for this task, subscribe to that topic and have the position information print out using
PX4_INFO()
which takes input like printf()
.
Submission Instructions
-
Plot of the trajectory of the two missions (2 points)
- Your
mp2_offboard_control
and describe the implementation details in the report (4 points)
- Your
mp2.cpp
module code (2 point)