MChehadeh / PX4-Autopilot

BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

These instructions are used for Ubuntu 20.04. They could be used in WSL2 or as a dual boot. But it's highly recommended to be used as a dual boot.

Overview

graph TD

subgraph L1[Installation Step]
    style L1 fill:transparent,stroke-width:0px;
    subgraph PX4_Setup
    px4_setup[Install PX4 Dependencies]
    px4_clone[Clone PX4 Firmware]
    pixhawk_pa[pixhawk_setup]
    SITL_pa[Run PX4_SITL]

    px4_setup --> px4_clone
    px4_clone -->pixhawk_pa
    px4_clone -->SITL_pa

    style SITL_pa stroke-dasharray: 5,5;
    style pixhawk_pa stroke-dasharray: 5,5;
    end

    subgraph Mavros_Setup
        subgraph offboard_testing
        create_ros_workspace[Create a ROS Workspace]
        clone_packages[Clone MAVROS, MAVLINK,\nand Offboard Packages]
        create_ros_workspace --> clone_packages
        end
    install_ros[Install ROS Noetic]
    build_packages[Build the Packages]

    install_ros --> offboard_testing
    offboard_testing --> build_packages
    end
end

subgraph L2[Building/Running Step]
    style L2 fill:transparent,stroke-width:0px;
    subgraph pixhawk[pixhawk Vx]
    pixhawk_setup[pixhawk_setup]
    load_firmware[Load pre-built\nfirmware to Pixhawk]
    build_firmware[Build custom\nfirmware from source]
    pixhawk_setup --> load_firmware
    pixhawk_setup --> build_firmware

    style pixhawk_setup stroke-dasharray: 5,5;
    end

    subgraph Run_SITL
    run_sitl[Run PX4_SITL]
    launch_mavros[Launch px4.launch Node]
    run_offboard[Run offb Node]

    run_sitl --> launch_mavros
    launch_mavros --> run_offboard

    style run_sitl stroke-dasharray: 5,5;
    end
end

pixhawk_pa -.- pixhawk_setup
SITL_pa -.- run_sitl
build_packages --> launch_mavros

Table of Contents

  1. Setup of Companion Computer / PC
    1. PX4-Autopilot Installation
    2. Mavros and Offboard Installation
    3. QGroundControl
  2. Run SITL in offboard mode
  3. Setup of Pixhawk
    1. Loading pre-built firmware
    2. Building custom firmware

Setup of Companion Computer / PC

We will need to install PX4-Autopilot, Mavlink, Mavros to be able to have a communication between PX4 and ROS.

PX4-Autopilot Installation

  1. Clone PX4-Autopilot into home directory:

    cd
    git clone https://github.com/Mu99-M/PX4-Autopilot.git --recursive
  2. Run the ubuntu.sh with no arguments (in a bash shell) to install everything:

    cd
    bash ./PX4-Autopilot/Tools/setup/ubuntu.sh
  3. Restart the computer on completion.

Mavros and Offboard Installation

Installation:

  1. Install ROS Noetic if not installed following this guide.

  2. Clone and build offboard_testing repo.

Now everthing is ready to be used from companion computer side.

QGroundControl QGC (Optional)

How to run SITL in offboard mode

  1. Build px4 in sitl

    cd ~/PX4-Autopilot
    make px4_sitl_default none
  2. Launch px4.launch node

    roslaunch mavros px4.launch fcu_url:=udp://:14540@14555
  3. Launch the starting node

    roslaunch offb starting.launch

Note: You may need to change the airframe if the vehicle is not armed and there is Preflight Fail.

   cd ~/PX4-Autopilot
   make px4_sitl_default none
   param set SYS_AUTOSTART 3011

Then restart the PX4.

Setup of Pixhawk / PX4

You can either use (1) pre-buit firmware files or (2) custom build the firmware from source. Both procedures are describe below:

1. Loading pre-built firmware to Pixhawk:

You can find the files for pre-built firmware in the PX4-Autopilot/compiled firmware directory. Upload the firmware suitabe for your FMU version to your pixhawk using QGroundControl as explained in this guide.

2. Building custom firmware from source:

Building PX4:

To build for Pixhawk 4 (FMUv5):

cd ~/PX4-Autopilot
make px4_fmu-v5_default

For any other version check this link.

Uploading firmware:

Upload the firmware to your pixhawk using QGroundControl as explained in this guide.

Setup ethernet communication with pixhawk (Optional):

You can refer to this guide to setup ethernet communication with you pixhawk if your pixhawk supports this (e.g. pixhawk 5x or 6x). If using the pixhawk cm4 baseboard with raspberry pi, you can refer to this guide. The process to setup ethernet comunication with pixhawk is briefly explained below in the report in 1. Using Ethernet subsection.

PX4 parameter configurations

To configure the PX4 parameters correctly, you can directly upload a pre-configures file to your pixhawk / SITL. You can also configure the needed parameters manually as describe later in this guide.

Loading pre-configured parameters set

You can find pre-configured parameters files on this link. You can upload the pre-configured parameter file to pixhawk using QGroundControl as described in this link

Manually configure paramaters (Optional)

You can change px4 parameters from QGroundControl as described here The major needed parameter changes are described below:

Configuring the MAVLINK instance

1. Using Ethernet

If you have already configured a static IP for your pixhawk and companion computer as explained in this guide, you can skip to step 8.

  1. Connect the pixhawk to QGC using a USB-C cable.
  2. Open QGroundcontrol > Analyze Tools > MAVLink Console.
  3. Enter the following commands: \ echo DEVICE=eth0 > /fs/microsd/net.cfg \ echo BOOTPROTO=static >> /fs/microsd/net.cfg\ echo IPADDR=192.168.144.4 >> /fs/microsd/net.cfg\ echo NETMASK=255.255.255.0 >>/fs/microsd/net.cfg\ echo ROUTER=192.168.144.1 >>/fs/microsd/net.cfg\ echo DNS=192.168.144.1 >>/fs/microsd/net.cfg\ The IPADDR could be chosen by the user. Our default configuration use IPADDR=192.168.144.4 as the IP address of the pixhawk. The pixhawk subnet should match the subnet of the companion computer.
  4. Connect the pixhawk to an external power source, and connect the ethernet cable to the companion computer / PC.
  5. If the companion computer / PC runs ubuntu: on the Companion Computer / PC, go to network settings > Wired > settings. Go to IPv4 settings and add your IPADDR of your choising (ex: 192.168.144.6) and the NETMASK 255.255.255.0 6.If the companion computer runs rapsbian (e.g. using CM4 baseboard), refer to this guide to setup a static IP for your raspberry bi.
  6. Try to ping the pixhawk ip from the companion computer by running the command ping 192.168.144.4 from the terminal.
  7. Go to params in QGC and change the following settings MAV_x_CONFIG, where x is the instance number. Our default configuration uses x=2 instance for ethernet communication.
  8. In QGroundControl, search for the MAV_x_CONFIG parameter.
  9. Set the value of the MAV_x_CONFIG parameter to ethernet.
  10. Reboot the vehicle and relaunch QGroundControl.
  11. Set the value of the MAV_x_MODE parameter to Custom.
  12. Set the value of the MAV_x_RATE parameter to 2000000 B/s.
  13. Set the value of the MAV_x_BROADCAST parameter to 1.
  14. Set the value of the MAV_x_RADIO_CTL parameter to 0.
  15. Set the value of the MAV_x_REMOTE_PRT parameter to 14540.
  16. Set the value of the MAV_x_UDP_PRT parameter to 14540.
  17. Reboot the vehicle.

For using MAVROS on the companion computer to communicate with the pixhawk over ethernet, you can run the following command:

roslaunch mavros px4.launch fcu_url:=udp://@IPADDR:14540@

where IPADDR is the ip address of the pixhawk (by default 192.168.144.4).

2. Using UART
  1. Choose the MAVLink instance to work on MAV_x_CONFIG, where x is the instance number. Our default configuration uses x=1 instance for UART communication.
  2. In QGroundControl, search for the MAV_x_CONFIG parameter.
  3. Set the value of the MAV_x_CONFIG parameter to the port you are using (e.g., TELEM 2).
  4. Reboot the vehicle and relaunch QGroundControl.
  5. Set the value of the MAV_x_MODE parameter to Custom.
  6. Set the value of the MAV_x_RATE parameter to 2000000 B/s.
  7. Reboot the vehicle.

After completing these steps, your MAVLink instance should be configured and ready to use.

Disablig landing detector

The landing detector of the PX4 can disarm the drone if not configured properly. As such, it is recommended to disable this module by setting the below parameters in QGroundControl:

  1. Set the value of the COM_DISARM_LAND parameter to -1.
  2. Set the value of the MPC_THR_HOVER parameter to a low value (e.g. 0.25).

Disabling autodisarming at delayed takeoff

Set the value of the 'COM_DISARM_PRFLT' parameter to '1000.0'.

Disarming PX4 when connection is lost

Set the value of the 'COM_OBL_RC_ACT' parameter to '7'.

Change Frequency for navigation topics

We are interested in 3 topics for now (VEHICLE_ANGULAR_VELOCITY, VEHICLE_ATTITUDE, and VEHICLE_LOCAL_POSITION).

1. Change the Publishing Rate in the Mavlink

First, run uorb top -1 in the Mavlink shell to check the publishing rate of each topic. Each topic of the three mentioned above should be at least 200 Hz.

To change the rate of VEHICLE_ATTITUDE and VEHICLE_LOCAL_POSITION:

  1. Set the parameter IMU_INTEG_RATE to be 400 Hz.
  2. Set the parameter EKF2_PREDICT_US to be 2500.

Note: The parameter IMU_INTEG_RATE is to change the rate of the VEHICLE_ATTITUDE topic.

Note: The EKF2_PREDICT_US is to change the rate of the VEHICLE_LOCAL_POSITION topic, however, the topic rate will be limited to the IMU_INTEG_RATE parameter value.

Run uorb top -1 in the Mavlink shell to re-check the publishing rate of the topics after applying the changes.

2. Change the Stream Rate in MAVLINK_MODE_CUSTOM
  1. Open ~/PX4-Autopilot/src/modules/mavlink/mavlink_main.cpp

  2. Change the three lines of the topics to be:

    configure_stream_local("VEHICLE_LOCAL_POSITION", 200.0f);
    configure_stream_local("VEHICLE_ATTITUDE", 200.0f);
    configure_stream_local("VEHICLE_ANGULAR_VELOCITY", 200.0f);
  3. Save, build, and upload to the pixhawk 6x. To build PX4 for pixhawk 6x:

cd ~/PX4-Autopilot/
make px4_fmu-v6x_default upload

A successful run will end with this output:

Erase  : [====================] 100.0%
Program: [====================] 100.0%
Verify : [====================] 100.0%
Rebooting.

[100%] Built target upload
3. Check the Topics Frequency in Mavros
  1. Close QGroundControl if open.
  2. Unplug and re-plug the pixhawk.
  3. run:
    roslaunch mavros px4.launch
  4. Then run each topic in a separate terminal:
    rostopic hz /mavros/vehicle_angular_velocity/vehicle_angular_velocity
    rostopic hz /mavros/vehicle_attitude/vehicle_attitude
    rostopic hz /mavros/vehicle_local_position/vehicle_local_position

    The output should be like this:

    average rate: 200.00
        min: 0.002s max: 0.019s std dev: 0.00572s window: 100

    If the rate is successfully 200 Hz, please start from step 2 again but with 400.0f Hz.