SITL/MAVPROXY/PYMAVLINK script code issue - soaring project

Hi all,

I’m working on a project with the goal to build soaring flights over the sea shore, to help NGO protecting sea environment.
After a deep look on MP documentation, it seems that writing Python scripts to enhance the Ardupilot soaring module will be the right way.
(If you want to help on this project, you’re welcome !! :grinning:)

So, I’ve installed Pymavlink + SITL + MAVPROXY under WSL

  • It works fine, SITL/Mavproxy are working well under Ubuntu => and MANY thanks to the process built by the Ardupilot dev team, it’s really usefull !! (I tried before with Cygwin, a nightmare …)
  • Now I’ve got problems to build scripts to make tests under SITL/Mavproxy, it’s more about my understanding on how to use the MAVLINK commands

Below an example about take off , quick flight and landing
When I’m starting this script, this is what I’ve got in my WSL console:

nico@LAPTOP:~/test-scripts/pymavlink$ ./Test1.py
Connected to system: 1 , component: 0
Waiting for the vehicle to arm
Armed!
End flight
nico@LAPTOP:~/

Nothing in the Mavproxy pop-up and nothing in the map, nothing in the SITL console as well
So it seems that I’ve got many to learn, but your help here will be very appreciated !

#!/usr/bin/python3
# test
import time
import math
from pymavlink import mavutil

# connect to SITL
master = mavutil.mavlink_connection('tcp:127.0.0.1:5762')

# wait for a heartbeat
master.wait_heartbeat()

# inform user
print("Connected to system:", master.target_system, ", component:", master.target_component)

# arm
master.mav.command_long_send(
    master.target_system,
    master.target_component,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
    0, # confirmation
    1, # param1 (1 to indicate arm)
    0, # param2 (all other params meaningless)
    0, # param3
    0, # param4
    0, # param5
    0, # param6
    0) # param7

# wait arming confirmed
print("Waiting for the vehicle to arm")
master.motors_armed_wait()
print('Armed!')

# Disarm
master.mav.command_long_send(
    master.target_system,
    master.target_component,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
    0, # confirmation
    1, # param1 (0 to indicate disarm)
    0, # param2 (all other params meaningless)
    0, # param3
    0, # param4
    0, # param5
    0, # param6
    0) # param7

# wait until disarming confirmed
master.motors_disarmed_wait()

# mode Guided 
master.mav.command_long_send(
    master.target_system,
    master.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_MODE,
    0, # confirmation
    1, # param1 (MAV_MODE_FLAG_CUSTOM_MODE_ENABLED)
    15, # param2 (flightmode number)
    0, # param3
    0, # param4
    0, # param5
    0, # param6
    0) # param7

#takeoff
master.mav.command_long_send(
    master.target_system,
    master.target_component,
    mavutil.mavlink.MAV_CMD_NAV_TAKEOFF,
    0, # confirmation
    30, # param1 (pitch angle)
    0, # param2
    0, # param3
    0, # param4
    0, # param5
    0, # param6
    100) # param7 (altitude)


#create NAV
master.mav.command_long_send(
    master.target_system,
    master.target_component,
    mavutil.mavlink.MAV_CMD_NAV_WAYPOINT,
    0, # confirmation
    0, # param1
    50, # param2 (Acc radius)
    0, # param3 (Pass by)
    0, # param4
    -35.36470932, # param5 (lat)
    149.16790353, # param6 (long)
    100) # param7 (altitude)

# message MISSION_ITEM_INT
mavutil.mavlink.MISSION_ITEM_INT()


# landing
master.mav.command_long_send(
    master.target_system,
    master.target_component,
    mavutil.mavlink.MAV_CMD_NAV_LAND,
    0, # confirmation
    0, # Abort Alt
    2, # Land Mode
    0,
    10, # Yaw Angle
    -35.363262, #lat
    149.165237, #long
    584) #alt

# end
print ("End flight")

Looking at your script, you are disarming the vehicle before starting the takeoff. So the vehicle won’t move.

Hi Stephan,
Thanks for your help, I’ll modify my script and reply.

Hi,
I modify the “disarm”

# Disarm
master.mav.command_long_send(
    master.target_system,
    master.target_component,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
    0, # confirmation
    0, # param1 (0 to indicate disarm)
    0, # param2 (all other params meaningless)
    0, # param3
    0, # param4
    0, # param5
    0, # param6
    0) # param7

Now, I’ve got same message on my console :

nico@LAPTOP:~/test-scripts/pymavlink$ ./Test1.py
Connected to system: 1 , component: 0
Waiting for the vehicle to arm
Armed!
End flight
nico@LAPTOP:~/

On the SITL console, the mode changed to “Guided” so I think I’ve gone to this part, but the next one is about “MAV_CMD_NAV_TAKEOFF” and I still don’t find what’s going wrong …
In the doc:
https://mavlink.io/en/messages/common.html#MAV_CMD_NAV_TAKEOFF
https://ardupilot.org/plane/docs/common-mavlink-mission-command-messages-mav_cmd.html#mav-cmd-nav-takeoff
I don’t know where I’m wrong …

There’s no wait after each of the mode change and takeoff commands. So the vehicle goes straight to to disarm at the end.

Also, your takeoff command doesn’t have a target altitude (param7).

Hi Stephen,
Based on your help and many tests on my side, I made some progress on this script.
I’ve got today issues and “blocking points”, maybe you can help - or someone else - , or maybe I’ve to create a new post, let me know

  • Because my simulation has to be aligned with my future flights to have the right level of simulation, I need to re-design the Arduplane parameters in SITL for a powered glider. I was thinking that I only have to modify ./tools/autotest/models/plane.parm but I think it’s not the right way

  • I need to have more throttle power at certain steps of my flight, I used this command but it seems that it’s not working on SITL. I can see an egress for throttle in Mavproxy but just a few second, maybe I have to specify in the script for how along the throttle needs to be at 75 % ?

#throttle to 75%
#https://ardupilot.org/plane/docs/common-mavlink-mission-command-messages-mav_cmd.html#mav-cmd-do-change-speed
master.mav.command_long_send(
    master.target_system,
    master.target_component,
mavutil.mavlink.MAV_CMD_DO_CHANGE_SPEED,
    0, # confirmation
    1, # param1
    5, # param2
    75, # param3
    0, # param4
    0, # param5
    0, # param6
    0) # param7
  • I need to have an altitude at 50m, well these commands don’t work in SITL

MAV_CMD_NAV_LOITER_TO_ALT : unsupported SITL
MAV_CMD_DO_CHANGE_ALTITUDE : NOK in SITL

#altitude 50m 
master.mav.command_long_send(
    master.target_system,
    master.target_component,
mavutil.mavlink.MAV_CMD_NAV_LOITER_TO_ALT,
    0, # confirmation
    100, # param1
    10, # param2
    0, # param3
    0, # param4
    0, # param5
    0, # param6
    50) # param7
  • I have to orientate the plane/the flight in a precise direction North, South … (in degrees), I didn’t find the way to do that in the Mavlink or Ardupilot commands.

If you can help, or anybody, or if you have ideas on how to achieve my scripts, many thanks !

Bye
Nicolas