Run python script for dynamic home point

Hi Everyone,
I am preparing for ship-born multicopter operations tried to write a script to dynamically update the home point based on the ships location. For this purpose I have two GNSS GNSS ground stations on a boom roughly 2m apart from each other so I can periodically calculate a home point in a certain distance and direction to my reference system. All of this is working. My only problem is sending the new home point to the FC via MavLink, but I always get an error like: “NameError: name ‘MAVLink’ is not defined”
Here is a simplified example I use for testing:

import time
import MissionPlanner # Import Mission Planner functionality
#from MissionPlanner.MAVLink import MAVLink
import clr
clr.AddReference(“MissionPlanner.Utilities”) # For MAVLink commands
clr.AddReference(‘System’) # Add reference to .NET System library
clr.AddReference(“MissionPlanner.MAVLink”)
from System.IO.Ports import SerialPort
import math

time.sleep(3)

home_lat=60.123
home_lon=120.123
alt1=10.3

while True:
# Set the home point using the MAVLink command MAV_CMD_DO_SET_HOME
MAV.doCommand(MAVLink.MAV_CMD_DO_SET_HOME, 1, 0, 0, 0, home_lat, home_lon, alt1)
print(f"Updated Home Point to: Latitude {home_lat}, Longitude {home_lon}, Altitude {alt1}")

# Update home point every 10 seconds
time.sleep(10)

My guess is that I don’t have all required packages installed or that MissionPlanner does not find them.
I am fairly new to using python with MissionPlanner so any help is highly appreciated.

For reference: I use Mission Planner 1.3.82 (win10) and a cubeOrange with ArduCopter v4.5.7

Does this post help you?

By the way, can you educate us your connection method? Am I correct your python code run side by side with the GCS (MP) and not connect to Ardupilot FC with a companion computer?

A photo or picture will be ideal.

Thanks Jai,

the post and the example scripts on GitHub - ArduPilot/MissionPlanner: Mission Planner Ground Control Station for ArduPilot (c# .net) are certainly helpful in guiding me in the right direction, but, so far, I have not succeeded.

You are right. I want to run the python script within MP on my GCS and use the same telemetry link (RFD868) as for MP. I could make this work when opening the serial port for my telemetry link within the script but then I cannot use it for MIssion Planner…

Uart (serial) is one to one hardware protocol, unlike I2C, USB, etc. your idea and approach can only be done via modifying the MP source code or their example of the python script. alternatively, you have to use companion computer onboard with the Ardupilot FC using one of the Uart (serial) port.

I am curious MP python script approach, how do you launch the user python script. Additionally, the examples seem to execute once only, is it useful for your case?

I think this example is the closest. And you need to read up the documentation.

home = Locationwp().Set(-34.9805,117.8518,0, id)

MAV.setWP(home,0,MAVLink.MAV_FRAME.GLOBAL_RELATIVE_ALT);