> 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/gcs-software/mavlink/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 VNS Pi.

## 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 %}

## Copy the 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 %}

## Copy the 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 or restart scripting

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

## Home and Estimator-Origin Requirements

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

1. Click on the map and select "Set Estimator Origin"\
   ![](/files/M0XPZvSQsUBUGNMZ1nii)
2. Click on the map and select "Set home here"\
   ![](/files/bWT30FwRPqtC7cWT5oc7)
3. In Fly View, open the Actions menu and click "Send Home to pi"\
   ![](/files/wWFIkE277ofQBz9k7T3M)

You should see the vps startup status text messages in the mav messages when the vps restarts.

## 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>
