Based on what I've discovered so far in my search for an answer (most pertinently here & here), it appears the only commands that can be scripted in python for Mission Planner to execute are: 

  • Script.ChangeParam(name, value)
  • Script.GetParam(name)
  • Script.ChangeMode(name)                   # i.e., mode = RTL
  • Script.WaitFor(string, timeout)              # unsure what this command does
  • Script.RC(channel, PWM, sendnow)     # automates emulated RC transmissions?

However, MAVProxy is written & executed in python and can accomplish a whole lot more... Also, if I understand correctly, during execution of an established flight plan, Mission Planner automates sending the next command to the UAV based on the telemetry received... (i.e., Mission Planner sends the next command in the established flight plan after the telemetry received indicates the UAV has completed the current command, e.g., it doesn't tell the UAV to go to waypoint 2 until it has reached waypoint 1).

So, my question is:

Is there a way to script a flight plan in Python that executes commands based on TIME instead of telemetry???

 

[i.e., What are the python scripting commands/functions to command the UAV "go to waypoint 1, sleep(30), go to waypoint 2, sleep(30), LOITER, sleep(30), go to waypoint 4, toggle servo 8, RTL" without caring if the UAV actually ever made it to the waypoint before following the next command]

(based on the comment from Mission Planner's creator here, I would think this is possible, but I need a list of all possible commands to send with the corresponding Python syntax.)

Views: 9891

Reply to This

Replies to This Discussion

Nevermind... thanks to JakobSt, I developed a working time-based script.

Here's the script that worked for me in using ArduPlane (which should work for ArduCopter too):

import sys
import clr
import time
import MissionPlanner #import *
clr.AddReference("MissionPlanner.Utilities") # includes the Utilities class
time.sleep(10)                                             # wait 10 seconds before starting
print 'Starting Mission'
Script.ChangeMode("Guided")                     # changes mode to "Guided"
print 'Guided Mode'
item = MissionPlanner.Utilities.Locationwp() # creating waypoint
lat = 39.343674                                           # Latitude value
lng = -86.029741                                         # Longitude value
alt = 45.720000                                           # altitude value
MissionPlanner.Utilities.Locationwp.lat.SetValue(item,lat)     # sets latitude
MissionPlanner.Utilities.Locationwp.lng.SetValue(item,lng)   # sets longitude
MissionPlanner.Utilities.Locationwp.alt.SetValue(item,alt)     # sets altitude
print 'WP 1 set'
MAV.setGuidedModeWP(item)                                    # tells UAV "go to" the set lat/long @ alt
print 'Going to WP 1'
time.sleep(10)                                                            # wait 10 seconds
print 'Ready for next WP'
lat = 39.345358
lng = -86.029054
alt = 76.199999
MissionPlanner.Utilities.Locationwp.lat.SetValue(item,lat)
MissionPlanner.Utilities.Locationwp.lng.SetValue(item,lng)
MissionPlanner.Utilities.Locationwp.alt.SetValue(item,alt)
print 'WP 2 set'
MAV.setGuidedModeWP(item)
print 'Going to WP 2'
time.sleep(10)
print 'Ready for next WP'
lat = 39.342106
lng = -86.031371
alt = 53.340000
MissionPlanner.Utilities.Locationwp.lat.SetValue(item,lat)
MissionPlanner.Utilities.Locationwp.lng.SetValue(item,lng)
MissionPlanner.Utilities.Locationwp.alt.SetValue(item,alt)
print 'WP 3 set'
MAV.setGuidedModeWP(item)
print 'Going to WP 3'
time.sleep(10)
print 'Ready for next WP'
lat = 39.343540
lng = -86.028732
alt = 53.199999
MissionPlanner.Utilities.Locationwp.lat.SetValue(item,lat)
MissionPlanner.Utilities.Locationwp.lng.SetValue(item,lng)
MissionPlanner.Utilities.Locationwp.alt.SetValue(item,alt)
print 'WP 4 set'
MAV.setGuidedModeWP(item)
print 'Going to WP 4'
time.sleep(10)
print 'Mission Complete'
#MAV.setMode(RETURN_TO_LAUNCH)
Script.ChangeMode("RTL")                                      # Return to Launch point
print 'Returning to Launch'
time.sleep(10)
Script.ChangeMode("LOITER")                                # switch to "LOITER" mode
print 'LOITERING'

Note: this is a TIME-based script... it will guide the UAV to the next waypoint after waiting a specified amount of time. One would have to replace the "time.sleep(10)" lines with "Script.WaitFor('ARMING MOTORS',30000)" [and replace 'ARMING MOTORS' with whatever notification Mission Planner usually provides upon arriving to a waypoint] to replicate the mission plan creation (i.e., go to next waypoint after arriving at previous waypoint).

Hello, I'm very interested by your script, are you running it through the apm script interface ?

I would like to send waypoint to my UAV with a python script. And it seems to work when I launch this script through the mission planner.

Do you know how I can set a servo to a value with this kind of script ?

Thanks

Here the answer to my own question :  Yes it' easy to send waypoint or to control a servo with python script running through APM. Indeed for the servo you must use channel 5 or 6 as if it was to control a gimbal. Here an example of code to drop a load on a gps defined target based on Joe M example, I've not tested this code in flight yet, only on the ground:

import sys
from math import*
import clr
import time
import MissionPlanner import *
a = 3.14 / 180# rad/deg convertion
cible = (48.7639686*a,2.2907084*a) # gps pos of target
clr.AddReference("MissionPlanner.Utilities") # includes the Utilities class
time.sleep(10)                                             # wait 10 seconds before starting
print 'Starting Mission'
Script.ChangeMode("Guided")                     # changes mode to "Guided"
item = MissionPlanner.Utilities.Locationwp() # creating waypoint
lat = 48.7639686                                          # Latitude value
lng = 2.2907084                                         # Longitude value
alt = 45.720000                                           # altitude value
MissionPlanner.Utilities.Locationwp.lat.SetValue(item,lat)     # sets latitude
MissionPlanner.Utilities.Locationwp.lng.SetValue(item,lng)   # sets longitude
MissionPlanner.Utilities.Locationwp.alt.SetValue(item,alt)     # sets altitude
print 'Drop zone set'
MAV.setGuidedModeWP(item)                                    # tells UAV "go to" the set lat/long @ alt
print 'Going to DZ
dist = 10 # distance ini, just to go in the loop
Script.SendRC(5,1000,True) # servo pos 1 : close      
while abs(dist)>5: #5 is the radius in meter of the target, center on the gps pos
    pos = (cs.lat*a,cs.lng*a) # drone pos
    t1 = sin(pos[0]) * sin(cible[0])#distance to the target calcul
    t2 = cos(pos[0]) * cos(cible[0])
    t3 = cos(pos[1] - cible[1])
    t4 = t2 * t3
    t5 = t1 + t4
    rad_dist = atan(-t5/sqrt(-t5 * t5 +1)) + 2 * atan(1)
    dist = ((rad_dist * 3437.74677 * 1.1508) * 1.6093470878864446)*1000
    print dist
Script.SendRC(5,1900,True)#servo in pos 2 : open
print "Load dropped"
Script.ChangeMode("LOITER")   

nice work Thomas, good to see it getting used. and being used for something creative as well.

Great work on this! I've added this to the list of examples and tutorials in the manual here.

I, thanks.

After a few weeks working on an other project, I'm back to this project. But I've a problem, I can't run any python script anymore. As I try to run on of my script, which was working a few weeks ago, apm just freeze. Have you ever heard a problem like that ? I see only one modification between now and late january : I've updated apm.

Actually I've dowloaded an old version of Mission Planner and my scripts work with it, so I guess a changed in the last version has caused the freeze.

Today I tested it and got the positive result. I mounted APM2.5 on a quad-copter and I connected it with Mission Planner via telemetry link. Before flying I developed codes for Arming motors, throttle raise, stabilizing, roll, pitch, yaw, land and disarm. I successfully run all those developed scripts of python and I completed preliminary tests. My next target is to test it for RTL, loiter, Guided modes.

Thanks for your great support.

Hi all,

Does anyone know if it is possible to upload a whole mission through python (multiple waypoints). I was trying to use the code bellow to add multiple waypoints and do a AUTO mission. However it does not work.

Relative = MAV.MAV_FRAME.GLOBAL_RELATIVE_ALT

item = MissionPlanner.Utilities.Locationwp() # creating waypoint

MissionPlanner.Utilities.Locationwp.lat.SetValue(item,lat) # sets latitude

MissionPlanner.Utilities.Locationwp.lng.SetValue(item,lng) # sets longitude

MissionPlanner.Utilities.Locationwp.alt.SetValue(item,alt) # sets altitude

MAV.setWP(item, 2, Relative, 0) #add waypoint

Script.ChangeMode("Auto") #Change mode to AUTO

Thanks for the help,

Sebastian

Hey, I'm trying to do the same thing. Have you gotten it to work?

I have made progress on my project learning from your script.  Thanks.

My challenge now is to over fly the waypoint without going into loiter or to begin loiter on the far side of the loiter radius allowing at least an initial overflight of the waypoint.

Any ideas.

Sir, can you share the scripts  for throttle raise, stabilizing, roll, pitch, yaw, land and disarm?

you can contact me on my email id

rijunk1995@gmail.com

i'm doing my project on pixhawk and i need help  to land my arduplane automatically.

VENKAT RAVI KISHORE said:

Today I tested it and got the positive result. I mounted APM2.5 on a quad-copter and I connected it with Mission Planner via telemetry link. Before flying I developed codes for Arming motors, throttle raise, stabilizing, roll, pitch, yaw, land and disarm. I successfully run all those developed scripts of python and I completed preliminary tests. My next target is to test it for RTL, loiter, Guided modes.

Thanks for your great support.

Reply to Discussion

RSS

© 2017   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service