> For the complete documentation index, see [llms.txt](https://docs.theseus.us/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.theseus.us/cyclops/developers/qgc-home-position-sender.md).

# QGC home position sender

This setup adds a QGroundControl Fly View action that tells an ArduPilot Lua script to send the flight controller's home position to the Cyclops computer.

## Components

* `../home_position_sender.lua` runs on the flight controller. It accepts either `COMMAND_LONG 31000` or a marker request (`COMMAND_LONG 512` with `param1=242`, `param2=31000`), reads the FC home position, then sends `MAV_CMD_DO_SET_HOME` to the Pi at sysid `218`, compid `190`.
* `FlyViewCustomActions.json` runs on the QGC computer. It adds the `Send Home to Pi` action in Fly View and sends command `31000` to the active vehicle.

## Flight controller setup

{% stepper %}
{% step %}

## Enable Lua scripting

Enable ArduPilot Lua scripting (`SCR_ENABLE=1`) and reboot the flight controller.
{% endstep %}

{% step %}

## Home position sender script

{% file src="/files/PUPmXn3Vu2BUpjtkp36i" %}

Copy `home_position_sender.lua` to the flight controller SD card:

```
/APM/scripts/home_position_sender.lua
```

{% endstep %}

{% step %}

## MAVLink Lua modules

Copy the MAVLink Lua modules to:

```
/APM/scripts/modules/MAVLink/
```

At minimum, this script needs:

```
mavlink_msgs.lua
mavlink_utils.lua
mavlink_msg_COMMAND_LONG.lua
mavlink_msg_COMMAND_ACK.lua
mavlink_msg_HEARTBEAT.lua
```

{% endstep %}

{% step %}

## Reboot

Reboot the flight controller or restart scripting.
{% endstep %}
{% endstepper %}

## Home and estimator-origin

The script has deterministic home selection logic:

1. Use `vehicle:home()` first.
2. Use `ahrs:get_home()` only if `vehicle:home()` did not return a home object.

What this means operationally:

* The script sends only the selected home position (`lat/lon`) to the Pi.
  * The device will use the terrain data from onboard maps to determine altitude.
* Estimator origin coordinates are never sent to the Pi by this script.
* Estimator origin must be initialized at least once before the home position is considered valid.
* Home position and estimator origin do not need exact coordinate equality, but they must be in the same operating area.
* If estimator origin is not initialized, or home resolves to an invalid value (`nil` or `0,0`), the script does not send `MAV_CMD_DO_SET_HOME` and QGC can show a timeout.

Expected startup message:

```
Home Sender ready (REQUEST_MESSAGE marker 31000, ... MAV channels)
```

## QGroundControl setup

{% file src="/files/DpHkpve7GGRTMLYIVYob" %}

Copy `FlyViewCustomActions.json` to QGC's MAVLink actions directory.

```
~/Documents/QGroundControl/MavlinkActions/FlyViewCustomActions.json
```

Restart QGC after copying the file.

## Testing the feature

{% stepper %}
{% step %}

### Set estimator origin

Click on the map and select "*Set Estimator Origin*"

<figure><img src="https://2060891120-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhzvlBbeiwnrjdGDAaBcQ%2Fuploads%2FaQ45LYW1PncieTR32f2p%2Fimage.png?alt=media&amp;token=001a2605-c909-4fa3-9012-1422ff058041" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Set home

Click on the map and select *"Set home here"*

<figure><img src="https://2060891120-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhzvlBbeiwnrjdGDAaBcQ%2Fuploads%2FiDv1WSx7ZW5lSeMiLV9x%2Fimage.png?alt=media&amp;token=938a733f-c0e5-4c0e-842d-e305a695c306" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Send home to Pi

In Fly View, open the Actions menu and click *"Send Home to Pi"*

<figure><img src="https://2060891120-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhzvlBbeiwnrjdGDAaBcQ%2Fuploads%2FmOWClpoE5K3cRlo5C38G%2Fimage.png?alt=media&amp;token=9813e896-471a-41e8-abce-9a9ccaccb0a4" alt=""><figcaption></figcaption></figure>

\
You should see the VPS startup status text messages in the MAV messages when the VPS restarts.
{% endstep %}
{% endstepper %}

## References

* ArduPilot Lua scripting: <https://ardupilot.org/copter/docs/common-lua-scripts.html>
* ArduPilot MAVLink command Lua example: <https://github.com/ArduPilot/ardupilot/blob/master/libraries/AP_Scripting/examples/MAVLink_Commands.lua>
* QGC custom MAVLink actions: <https://docs.qgroundcontrol.com/Stable_V5.0/en/qgc-user-guide/custom_actions/custom_actions.html>
