# Companion Computer

This section introduces how to set up the (on-board) companion computer for PX4-based drones.&#x20;

### Hardware Connection

#### FTDI Devices / Logic Level Shifters

* Connect onboard computer and Pixhawk flight controller via an FTDI Chip USB-to-serial adapter.
  * use the `TELEM2` port on Pixhawk and a USB port on companion computer
  * make sure the FTDI chip or logic level shifter is compatible in our use case
    * see [this section](https://docs.px4.io/master/en/peripherals/companion_computer_peripherals.html#ftdi-devices) for recommended ones (Pixdev discontinued though)
  * make sure the pin-to-pin connection is correct
    * out of 6 pins, 3 pins must be connected (TX-RX, RX-TX, GND-GND)
    * the red power pin must not be connected
    * CTS and RTS pins are optional; for the use of so called "flow control" for Pixhawk
    * see [this section](https://docs.px4.io/master/en/companion_computer/pixhawk_companion.html#hardware-setup) for the full wiring table
* It is recommended that the FTDI chip operates at 3.3V, but not required
  * the red power pin is not connected anyway
  * according to [this section](https://docs.px4.io/master/en/companion_computer/pixhawk_companion.html#hardware-setup) in PX4 docs, all Pixhawk serial ports operate at 3.3V and are 5V level compatible.
* Example: [USB FTDI Serial to JST-GH (Basic)](https://store.mrobotics.io/USB-FTDI-Serial-to-JST-GH-p/mro-ftdi-jstgh01-mr.htm)&#x20;
  * warning: this product is not a ready-to-go adapter; need to switch RX and TX cables (cut them off and then solder together)
    * see [this video](https://www.youtube.com/watch?v=NSqPHQ1zQco) for a quick recap about soldering two wires together
    * soldering quality can also affect the performance; bad soldering will lead to high noise level in the communication
  * the connection link is: computer --- USB port --- USB to micro USB cable --- micro USB port --- FTDI chip --- 6-pin JST port --- JST cable --- 6-pin TELEM2 port --- Pixhawk

![(left) FTDI cable after soldering (right) final cable after hot-glue gun and heat-shrink tubing](/files/-MfBZWQYRRdZrZfuP0UH)

#### WiFi Router

Need to prepare a high-speed low-latency WiFi router

* edit settings to shrink DHCP range and leave a range of IP addresses unused for static IP
* in my case, I use `192.168.0.21` for onboard computer and `192.168.0.121` for gcs computer

### PX4 Firmware

* Need to enable MAVLink on a configurable serial port (typically `TELEM2`).
  * Connect the Pixhawk flight controller to QGroundControl via USB or Telemetry (TELEM1), so that you can change parameters in the parameter table and push them to the vehicle.
  * Set param MAV\_1\_CONFIG = `TELEM 2` (MAV\_1\_CONFIG is often used to map the TELEM 2 port)
  * Reboot the vehicle to make other parameters visible.&#x20;
  * Set the remaining parameters if not being set by default.
    * MAV\_1\_MODE = `Onboard`&#x20;
    * SER\_TEL2\_BAUD = `921600` (921600 or higher recommended for applications like log streaming or FastRTPS)
* Once this parameter is enabled, when Pixhawk is powered on, it will automatically broadcast messages (drone status) via TELEM2 port. The RX indicator should be flickering accordingly.&#x20;

### Onboard Computer

* Set up a static IP address for WiFi connection
  * in my case, I use `192.168.0.21` for onboard computer and `192.168.0.121` for gcs computer
  * optional: edit `hosts` file by `sudo gedit /etc/hosts` and add item `192.168.0.121 arcs-gcs`
* udev rule: create a new file at `/etc/udev/rules.d/99-pixhawk.rules` with the following content
  * double check by `lsusb` the actual vendor and product ID of the FTDI chip

```bash
# NOTE: use the actual vendor and product IDs (check it by lsusb)
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", SYMLINK+="ttyPixhawk"
#SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="ttyPixhawk"

# optional; can be omitted if Arduino is not being used
SUBSYSTEM=="tty", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="0042", SYMLINK+="ttyArduino"
```

* Add current user to the `tty` and `dialout` groups to avoid to have to execute scripts as root

```
sudo usermod -a -G tty <username>
sudo usermod -a -G dialout <username>
```

* Launch file is the same as `mavros/launch/px4.launch` except for the following modifications
  * set `fcu_url` to `/dev/ttyPixhawk:921600`&#x20;
  * set `gcs_url` to `udp://@arcs-gcs:14550`&#x20;
  * double check if `tgt_system` matches `MAV_SYS_ID` parameter in PX4 firmware (default 1)
  * double check if `tgt_component` matches `MAV_COMP_ID` parameter in PX4 firmware (default 1)&#x20;

### Ground Control Station (GSC) Computer

* Set up a static IP address for WiFi connection
  * in my case, I use `192.168.0.21` for onboard computer and `192.168.0.121` for gcs computer
  * optional: edit `hosts` file by `sudo gedit /etc/hosts`and add item `192.168.0.21 arcs-robot`
  * optional: set up password-free SSH login by `ssh-copy-id arcs@arcs-robot`
* Download QGroundControl Ubuntu amd64 build (arm architecture is not supported)
  * run `./QGroundControl` and you can see drone status
  * it will automatically listen to port 14550 for UDP connections

## Troubleshooting

#### Hardware connection

* `lsusb` check the vendor ID and product ID of FTDI chip
* `ls -l /dev/ttyPixhawk` check the read/write permission
* use a multi-meter to check voltage level&#x20;
* find a chip that has TX/RX indicators to help debugging

#### mavros connection

* `rostopic echo -n1 /diagnostics`
* `rosrun mavros checkid`

#### Issue: FCU not connected

* If TELEM2 port is indeed broadcasting messages, then `cat /dev/ttyPixhawk` should print out random symbols/streaming and `rostopic echo -n1 /diagnostics` should have RX packages/bytes received.&#x20;
* See [this issue in mavros repository](https://github.com/mavlink/mavros/issues/1592) for more details.

## References

* PX4 docs: [Companion Computer Peripherals](https://docs.px4.io/master/en/peripherals/companion_computer_peripherals.html)
* PX4 docs: [Companion Computer for Pixhawk Series](https://docs.px4.io/master/en/companion_computer/pixhawk_companion.html)
* PX4 docs: [MAVLink Peripherals (GCS/OSD/Companion)](https://docs.px4.io/master/en/peripherals/mavlink_peripherals.html)
* PX4 docs: [Serial Port Configuration](https://docs.px4.io/master/en/peripherals/serial_configuration.html)
* Holybro docs: [Pixhawk 4 Pinout Manual](http://www.holybro.com/manual/Pixhawk4-Pinouts.pdf)

## Examples for Healthy/Successful Connections

#### roslaunch mavros px4.launch

```
auto-starting new master
process[master]: started with pid [25993]
ROS_MASTER_URI=http://localhost:11311

setting /run_id to 52bef7b4-ea8d-11eb-b0f6-204ef6c5ef8f
process[rosout-1]: started with pid [26011]
started core service [/rosout]
process[mavros-2]: started with pid [26029]
[ INFO] [1626917830.064227539]: FCU URL: /dev/ttyPixhawk:921600
[ INFO] [1626917830.075512530]: serial0: device: /dev/ttyPixhawk @ 921600 bps
[ INFO] [1626917830.077993842]: GCS URL: udp://@arcs-gcs:14550
[ INFO] [1626917830.079239602]: udp1: Bind address: 0.0.0.0:14555
[ INFO] [1626917830.079788274]: udp1: Remote address: 192.168.0.121:14550
[ INFO] [1626917830.123951216]: Plugin 3dr_radio loaded
[ INFO] [1626917830.133589392]: Plugin 3dr_radio initialized
[ INFO] [1626917830.134123504]: Plugin actuator_control loaded
...
[ INFO] [1626917831.596142837]: Plugin wind_estimation loaded
[ INFO] [1626917831.599738645]: Plugin wind_estimation initialized
[ INFO] [1626917831.599962517]: Autostarting mavlink via USB on PX4
[ INFO] [1626917831.600359957]: Built-in SIMD instructions: ARM NEON
[ INFO] [1626917831.600489557]: Built-in MAVLink package version: 2021.3.3
[ INFO] [1626917831.600601205]: Known MAVLink dialects: common ardupilotmega ASLUAV all autoquad icarous matrixpilot paparazzi standard uAvionix ualberta
[ INFO] [1626917831.601232277]: MAVROS started. MY ID 1.240, TARGET ID 1.1
[ INFO] [1626917831.607340148]: IMU: High resolution IMU detected!
[ INFO] [1626917831.623283188]: IMU: Attitude quaternion IMU detected!
[ INFO] [1626917831.712271824]: CON: Got HEARTBEAT, connected. FCU: PX4 Autopilot
[ INFO] [1626917831.726061328]: IMU: High resolution IMU detected!
[ INFO] [1626917831.727046608]: IMU: Attitude quaternion IMU detected!
[ INFO] [1626917832.734570666]: GF: Using MISSION_ITEM_INT
[ INFO] [1626917832.734848618]: RP: Using MISSION_ITEM_INT
[ INFO] [1626917832.735039114]: WP: Using MISSION_ITEM_INT
[ INFO] [1626917832.735182506]: VER: 1.1: Capabilities         0x000000000000e4ef
[ INFO] [1626917832.735327146]: VER: 1.1: Flight software:     010b03ff (a6274bc5ed000000)
[ INFO] [1626917832.735467946]: VER: 1.1: Middleware software: 010b03ff (a6274bc5ed000000)
[ INFO] [1626917832.735595530]: VER: 1.1: OS software:         080200ff (ec20f2e6c5cc35b2)
[ INFO] [1626917832.735720650]: VER: 1.1: Board hardware:      00000032
[ INFO] [1626917832.735841738]: VER: 1.1: VID/PID:             26ac:0032
[ INFO] [1626917832.736006506]: VER: 1.1: UID:                 4356500d20383058
[ WARN] [1626917832.736531306]: CMD: Unexpected command 520, result 0
[ INFO] [1626917837.984168559]: udp1: Remote address: 192.168.0.121:14550
[ WARN] [1626917837.999627759]: FCU: Preflight: GPS Speed Accuracy too low
[ INFO] [1626917838.736300702]: VER: 1.1: Capabilities         0x000000000000e4ef
[ INFO] [1626917838.736565246]: VER: 1.1: Flight software:     010b03ff (a6274bc5ed000000)
[ INFO] [1626917838.736837118]: VER: 1.1: Middleware software: 010b03ff (a6274bc5ed000000)
[ INFO] [1626917838.737020766]: VER: 1.1: OS software:         080200ff (ec20f2e6c5cc35b2)
[ INFO] [1626917838.737107582]: VER: 1.1: Board hardware:      00000032
[ INFO] [1626917838.737212190]: VER: 1.1: VID/PID:             26ac:0032
[ INFO] [1626917838.737322942]: VER: 1.1: UID:                 4356500d20383058
[ INFO] [1626917838.929431802]: WP: seems GCS requesting mission
[ INFO] [1626917838.929658650]: WP: scheduling pull after GCS is done
[ INFO] [1626917838.957424154]: GF: seems GCS requesting mission
[ INFO] [1626917838.957704026]: GF: scheduling pull after GCS is done
[ INFO] [1626917838.984743641]: RP: seems GCS requesting mission
[ INFO] [1626917838.984973657]: RP: scheduling pull after GCS is done
[ INFO] [1626917841.716082666]: HP: requesting home position
[ INFO] [1626917843.932448880]: WP: mission received
[ INFO] [1626917843.961392944]: GF: mission received
[ INFO] [1626917843.987520816]: RP: mission received
[ INFO] [1626917845.005917544]: PR: parameters list received
[ INFO] [1626917851.715933397]: HP: requesting home position
[ INFO] [1626917861.715935981]: HP: requesting home position
[ INFO] [1626917871.715917420]: HP: requesting home position

```

#### rostopic echo -n1 /diagnostics

```
header: 
  seq: 461
  stamp: 
    secs: 1626918167
    nsecs: 602185190
  frame_id: ''
status: 
  - 
    level: 0
    name: "mavros: FCU connection"
    message: "connected"
    hardware_id: "/dev/ttyPixhawk:921600"
    values: 
      - 
        key: "Received packets:"
        value: "2169"
      - 
        key: "Dropped packets:"
        value: "0"
      - 
        key: "Buffer overruns:"
        value: "0"
      - 
        key: "Parse errors:"
        value: "0"
      - 
        key: "Rx sequence number:"
        value: "98"
      - 
        key: "Tx sequence number:"
        value: "0"
      - 
        key: "Rx total bytes:"
        value: "7954038"
      - 
        key: "Tx total bytes:"
        value: "194865"
      - 
        key: "Rx speed:"
        value: "36628.000000"
      - 
        key: "Tx speed:"
        value: "875.000000"
  - 
    level: 0
    name: "mavros: GPS"
    message: "3D fix"
    hardware_id: "/dev/ttyPixhawk:921600"
    values: 
      - 
        key: "Satellites visible"
        value: "9"
      - 
        key: "Fix type"
        value: "3"
      - 
        key: "EPH (m)"
        value: "0.95"
      - 
        key: "EPV (m)"
        value: "2.03"
  - 
    level: 0
    name: "mavros: Heartbeat"
    message: "Normal"
    hardware_id: "/dev/ttyPixhawk:921600"
    values: 
      - 
        key: "Heartbeats since startup"
        value: "336"
      - 
        key: "Frequency (Hz)"
        value: "1.000000"
      - 
        key: "Vehicle type"
        value: "Quadrotor"
      - 
        key: "Autopilot type"
        value: "PX4 Autopilot"
      - 
        key: "Mode"
        value: "AUTO.LOITER"
      - 
        key: "System status"
        value: "Standby"
  - 
    level: 2
    name: "mavros: System"
    message: "Sensor health"
    hardware_id: "/dev/ttyPixhawk:921600"
    values: 
      - 
        key: "Sensor present"
        value: "0x122E002F"
      - 
        key: "Sensor enabled"
        value: "0x1221002F"
      - 
        key: "Sensor health"
        value: "0x022E002F"
      - 
        key: "3D gyro"
        value: "Ok"
      - 
        key: "3D accelerometer"
        value: "Ok"
      - 
        key: "3D magnetometer"
        value: "Ok"
      - 
        key: "absolute pressure"
        value: "Ok"
      - 
        key: "GPS"
        value: "Ok"
      - 
        key: "rc receiver"
        value: "Fail"
      - 
        key: "AHRS subsystem health"
        value: "Ok"
      - 
        key: "Battery"
        value: "Ok"
      - 
        key: "pre-arm check status. Always healthy when armed"
        value: "Fail"
      - 
        key: "CPU Load (%)"
        value: "41.4"
      - 
        key: "Drop rate (%)"
        value: "0.0"
      - 
        key: "Errors comm"
        value: "0"
      - 
        key: "Errors count #1"
        value: "0"
      - 
        key: "Errors count #2"
        value: "0"
      - 
        key: "Errors count #3"
        value: "0"
      - 
        key: "Errors count #4"
        value: "0"
  - 
    level: 0
    name: "mavros: Battery"
    message: "Normal"
    hardware_id: "/dev/ttyPixhawk:921600"
    values: 
      - 
        key: "Voltage"
        value: "16.22"
      - 
        key: "Current"
        value: "0.5"
      - 
        key: "Remaining"
        value: "83.0"
  - 
    level: 0
    name: "mavros: Time Sync"
    message: "Normal"
    hardware_id: "/dev/ttyPixhawk:921600"
    values: 
      - 
        key: "Timesyncs since startup"
        value: "3361"
      - 
        key: "Frequency (Hz)"
        value: "10.000005"
      - 
        key: "Last RTT (ms)"
        value: "1.375040"
      - 
        key: "Mean RTT (ms)"
        value: "2.221804"
      - 
        key: "Last remote time (s)"
        value: "384.336849000"
      - 
        key: "Estimated time offset (s)"
        value: "1626917783.223483801"
---

```

#### rosrun mavros checkid

```
OK. I got messages from 1:1.

---
Received 6190 messages, from 1 addresses
sys:comp   list of messages
  1:1     0, 1, 2, 4, 140, 141, 147, 24, 30, 31, 32, 33, 36, 74, 331, 76, 83, 340, 85, 230, 105, 111, 241, 242, 245

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.hanzheteng.com/quadrotor/companion-computer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
