Hi,

I've got a question about Python Scripts in the Mission Planner:

I want to send waypoints to my Arducopter with a Python Script. I already found this page: http://code.google.com/p/arducopter/wiki/Python.

Unfortunately there is no information about how I can send whole waypoints to the quadrocopter.

I would be glad if someone would have an answer!

Views: 12851

Reply to This

Replies to This Discussion

hi,

anyone here succeeded in sending waypoints using python in the mission planner?

thanx

I can't get this code to work... 

First I got the error of there being no "ArdupilotMega", so I swapped it with "MissionPlanner" and that error went away. Then I got the error that "Locationwp undefined", so I added in "from MissionPlanner.Utilities import *" and instead used "item = Utilities.Locationwp()" and that previous error transformed into "Error running script attribute 'Locationwp'  of 'namespace#' object is read-only"

So, do you have an updated script that will perform the functions you detailed above??? THANKS!!

Hi Joe,

I had the same problems but now it works. Try this:

import clr
import MissionPlanner

clr.AddReference("MissionPlanner.Utilities")
item = MissionPlanner.Utilities.Locationwp()

MissionPlanner.Utilities.Locationwp.lat.SetValue(item,lat)
MissionPlanner.Utilities.Locationwp.lng.SetValue(item,lng)
MissionPlanner.Utilities.Locationwp.alt.SetValue(item,alt)

Does that help you?

hi,

I also tried this, nothing seems to happen - no error, but no waypoint either...

any ideas? do you need to specify the new waypoint #?

That does help, thank you! I no longer get errors on trying to set the waypoints... however, I cannot verify that the APM 2.5 is actually receiving the commands & executing them because I have neither an airframe, nor HIL capability. =/

Also, I'm now getting the error when trying to set the mode to "RTL" at the end of the script. Here's my script:

import sys
import clr
import time
import MissionPlanner #import *
clr.AddReference("MissionPlanner.Utilities") # from MissionPlanner.Utilities import *
print 'Starting Mission'
item = MissionPlanner.Utilities.Locationwp() # item = Utilities.Locationwp()
lat = 39.343674
lng = -86.029741
alt = 45.720000
MissionPlanner.Utilities.Locationwp.lat.SetValue(item,lat)
MissionPlanner.Utilities.Locationwp.lng.SetValue(item,lng)
MissionPlanner.Utilities.Locationwp.alt.SetValue(item,alt)
print 'WP 1 set'
MAV.setGuidedModeWP(item)
print 'Going to WP 1'
time.sleep(15)
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(15)
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(15)
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(15)
print 'Mission Complete'
MAV.setMode(RETURN_TO_LAUNCH)
#Script.ChangeMode(RETURN_TO_LAUNCH)
print 'Returning to Launch'

And here's the error message I get when the script reaches the final command to change the mode to "RTL":

"Error running script name 'RETURN_TO_LAUNCH' is not defined"

Any idea on the proper syntax to change the mode at the end of the script??

I think I got the mode change at the end to work now, too... I was missing the double quotation marks inside the ChangeMode function so python was treating RTL as a variable instead of an literal argument being passed...

I think this will work: 

Script.ChangeMode("RTL")
print 'Returning to Launch'

I did... 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).

Hope this helps!

Wow ... the pythonscriptoption in mission planner is more helpful than I thought.

But were this new commands come from? MissionPlanner.Utilities.dll?

Is it possible to execute the "do_digicam_control" command via python?

I cant tel lyou the exact command, but something like

MAV.doCommand((MAVLink.MAV_CMD.DO_DIGICAM_CONTROL, 0, 0, 0, 0, 0, 0, 0);

should work

while im at it
MAV.doARM(true);

MAV.doReboot(false);

MAV.setWPCurrent(1);

MAV.GetParam("PARAMNAME");

MAV.setParam("PARM",1.0);

MAV.getWPCount();

MAV.getWP(1);

to list a few

thanks for your reply, but i cant get it work

the MAV.doCommand needs 8 arguments and "MAV_CMD" is expected, that is what i find out.

But no matter how I write it, I get an error "not defined"

I just found a website that might be helpful to me:

http://nullege.com/codes/show/src%40p%40y%40pymavlink-1.0.10%40pymavlink%40mavutil.py/415/mavlinkv10_common.MAV_CMD_DO_SET_SERVO/python

I do not get it to run :(

I would like to extend the lens from my camera automatically in "AUTOmode

and retract the lens when i return in STABILIZEmode

I use CAM_DURATION(MissionPlanner) and get_usb_power(CHDK) to communicate between APM and camera.

MissionPlannerscript:

while True:

if cs.mode == 'STABILIZE' and Script.GetParam('CAM_DURATION') == 2 :
Script.ChangeParam('CAM_DURATION',1)

if cs.mode == 'Auto' and Script.GetParam('CAM_DURATION') == 1 :
Script.ChangeParam('CAM_DURATION',2)

CHDKscript:


@title Cam
print "Script Started, Listening"
sleep 1000

goto "interval"
:interval
p = get_usb_power
m = get_mode
print m
if p > 5 and p < 15 and m = 0 then set_record 0
if p > 15 and p < 25 and m = 1 then set_record 1
if p > 15 and p < 25 and m = 0 then shoot
goto "interval"

with "Trigger Camera Now"  it works already.

But i need a possibility to trigger the cam in the pythonscript

after I've searched for days in the source code, I have found what I was looking for.

the command  MAV.setDigicamControl(1) can trigger the camera via pythonscript.

So I have full control over CHDK with a cheap USB cable like this: http://diydrones.com/profiles/blogs/apm-to-chdk-camera-link-tutorial

The pythonscript change the CAM_DURATION(the time the trigger is pressed),

CHDK count how long the trigger is pressed and depending on it do what you want.

In my case the lens moves out if i switch to AUTO and retracts if i in STABILIZE MODE.

Pythonscript:


while True:

if cs.mode == 'STABILIZE' and Script.GetParam('CAM_DURATION') == 2 :
Script.ChangeParam('CAM_DURATION',1)
MAV.setDigicamControl(1)

if cs.mode == 'Auto' and Script.GetParam('CAM_DURATION') == 1 :
Script.ChangeParam('CAM_DURATION',2)
MAV.setDigicamControl(1)

CHDKscript:


@title Cam
print "Script Started, Listening"
sleep 1000

goto "interval"
:interval
p = get_usb_power
m = get_mode
print m 
if p > 5 and p < 15 and m = 0 then set_record 0
if p > 15 and p < 25 and m = 1 then set_record 1
if p > 15 and p < 25 and m = 0 then shoot 
goto "interval"

the query "p >and p <" is because "p" varies a little bit  

another possibility would be the objective retract if you fall below a certain height,

too adjust the zoom factor via the height or remotecontrol

or whatever ...

Thanks for your help Michael Oborne

Reply to Discussion

RSS

© 2019   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service