Mission Planner - Python Script

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!

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Hi all

    Is it possible to send AT commands to one of the UART's to get a connected GSM/4G modem to connect to internet ? 



  • Michael Thomas said:

    I'm also very interested in this added scripting functionality.

    Additionally I'm wondering is it possible to add custom commands to the Mission Planner? It says these can be modified in the MAVLink Parameters Window, but I can't seem to find this. (http://dev.ardupilot.com/wiki/mavlink-parameters/)

    • For the uplink I want to add a 'Drop Bottle' and 'Activate Flight Termination' command for the Outback Challenge.
    • For the downlink I want to monitor an additional Outback Challenge Status Parameter (incl Loss of Datalink Mode, Loss of GPS Mode, Flight Termination Mode)

    Also I'm wondering does the Mission Planner send a heartbeat to the ArduPilot? If so, is there a way to change the frequency at which this is sent?

  • Dear Stefan,  do you have the codes for landing?

    if you have it , please share it to my mail id rijunk1995@gmail.com

    Stefan Lane Hardy said:

    I made my vehicle go into "Guided" mode from my Python script. However, I'm trying to create a safety control in my Python script so if I flip the Radio Control from Auto to Manual, the vehicle will stop running the Python script.  As of now, without the safety control, my vehicle just continues to run around continuously until I abort the script.

    I tried to input an "if" statement for cs.mode (ex. if cs.mode == "Manual", script.ChangeMode("Manual")).  However, it never seems to work.  Do you know if the cs.mode only applies to the mode I have written in Python, and has nothing to do with the mode the Radio is switched to?  Any ideas on a "switch to Manual" safety control?

    Here's my code:

    import socket
    import sys
    import math
    from math import sqrt
    import clr
    import time
    import re, string
    clr.AddReference("MissionPlanner.Utilities")
    import MissionPlanner #import *
    clr.AddReference("MissionPlanner.Utilities") #includes the Utilities class
    from MissionPlanner.Utilities import Locationwp


    HOST = 'localhost'   # Symbolic name meaning all available interfaces
    #SPORT = 5000 # Arbitrary non-privileged port 
    RPORT = 4000 # Arbitrary non-privileged port

    REMOTE = ''
    # Datagram (udp) socket
     

    rsock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    print 'Sockets created'

    # Bind socket to local host and port
    try:    
       rsock.bind((HOST,RPORT))
    except socket.error, msg:
       #print 'Bind failed. Error Code:'
       sys.stderr.write("[ERROR] %s\n" % msg[1])
       rsock.close()
       sys.exit()       

    print 'Receive Socket bind complete on ' + str(RPORT)

    print 'Starting Follow'
    Script.ChangeMode("Guided")                     # changes mode to "Guided"
    print 'Guided Mode'

    #keep talking with the Mission Planner server
    while 1:     

        msg = rsock.recv(1024)
        pattern = re.compile("[ ]")
        parameters = pattern.split(msg)

        latData = parameters[0]
        lngData = parameters[1]
        headingData = parameters[2]
        altData = parameters[3]
       
        float_lat = float(latData)
        float_lng = float(lngData)
        float_heading = float(headingData)
        float_alt = float(altData)


        """Safety Manual Mode Switch"""
        #while True:
          
        if cs.mode == 'MANUAL':
           Script.ChangeMode("Manual")
           rsock.close()
        else:
          
           #print cs.mode
           """Follower Offset"""
           XOffset= float(0) #User Input for x axis offset
           YOffset= float(-2) #User Input for y axis offset
           brng = math.radians(float_heading)
          # brng = float_heading*math.pi/180 #User input heading angle of follower in relation to leader.  0 degrees is forward.

           d = math.sqrt((XOffset**2)+(YOffset**2)) #Distance in m

           MperLat = 69.172*1609.34 #meters per degree of latitude. Length of degree (miles) at equator * meters in a mile
           MperLong = math.cos(float_lat)*69.172*1609.34 #meters per degree of longitude

           Lat_Offset_meters = YOffset/MperLat #lat distance offset in meters
           Long_Offset_meters = XOffset/MperLong #long distance offset in meters

           Follower_lat = float_lat + (Long_Offset_meters*math.sin(brng)) + (Lat_Offset_meters*math.cos(brng)) #rotates lat follower offset in relation to heading of leader
           Follower_long = float_lng - (Long_Offset_meters*math.cos(brng)) + (Lat_Offset_meters*math.sin(brng)) #rotates long follower offset in relation to heading of leader
           Follower_alt = float_alt + 10
           #Follower_alt = 10
      
           float_lat = float(Follower_lat)
           float_lng = float(Follower_long)
           float_alt = float(Follower_alt) #4-5 second lag induced on altitude waypoint line, unless alt is set to 0
       
           print(float_lat)
           print(float_lng)
           print(float_heading)
           print(float_alt)

           """Writing Waypoints"""
           item = MissionPlanner.Utilities.Locationwp() # creating waypoint
           MissionPlanner.Utilities.Locationwp.lat.SetValue(item,float_lat)
           MissionPlanner.Utilities.Locationwp.lng.SetValue(item,float_lng)
           #MissionPlanner.Utilities.Locationwp.groundcourse.SetValue(item,float_heading)
           MissionPlanner.Utilities.Locationwp.alt.SetValue(item,float_alt) #Can only use lat,lng, or alt
           MAV.setGuidedModeWP(item) #set waypoint
           print 'Waypoint Sent'
           print time.strftime('%X %x %Z')
    # exit
    rsock.close()
    print 'Script End'



  • 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.

    Dear VENKAT 

    We are working on a similar project and we are facing some problems on arming, take-off, disarming and QGC waypoint file reading in Mission Planner Simulator. 

    Could you please share with us the developed codes for the above actions or contact me on eleflygou@gmail.com?

    Thank you in advance

    Elef

    eleflygou@gmail.com

  • 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.

    • Hey Venkat, can you share the codes that you developed?

  • I made my vehicle go into "Guided" mode from my Python script. However, I'm trying to create a safety control in my Python script so if I flip the Radio Control from Auto to Manual, the vehicle will stop running the Python script.  As of now, without the safety control, my vehicle just continues to run around continuously until I abort the script.

    I tried to input an "if" statement for cs.mode (ex. if cs.mode == "Manual", script.ChangeMode("Manual")).  However, it never seems to work.  Do you know if the cs.mode only applies to the mode I have written in Python, and has nothing to do with the mode the Radio is switched to?  Any ideas on a "switch to Manual" safety control?

    Here's my code:

    import socket
    import sys
    import math
    from math import sqrt
    import clr
    import time
    import re, string
    clr.AddReference("MissionPlanner.Utilities")
    import MissionPlanner #import *
    clr.AddReference("MissionPlanner.Utilities") #includes the Utilities class
    from MissionPlanner.Utilities import Locationwp


    HOST = 'localhost'   # Symbolic name meaning all available interfaces
    #SPORT = 5000 # Arbitrary non-privileged port 
    RPORT = 4000 # Arbitrary non-privileged port

    REMOTE = ''
    # Datagram (udp) socket
     

    rsock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    print 'Sockets created'

    # Bind socket to local host and port
    try:    
       rsock.bind((HOST,RPORT))
    except socket.error, msg:
       #print 'Bind failed. Error Code:'
       sys.stderr.write("[ERROR] %s\n" % msg[1])
       rsock.close()
       sys.exit()       

    print 'Receive Socket bind complete on ' + str(RPORT)

    print 'Starting Follow'
    Script.ChangeMode("Guided")                     # changes mode to "Guided"
    print 'Guided Mode'

    #keep talking with the Mission Planner server
    while 1:     

        msg = rsock.recv(1024)
        pattern = re.compile("[ ]")
        parameters = pattern.split(msg)

        latData = parameters[0]
        lngData = parameters[1]
        headingData = parameters[2]
        altData = parameters[3]
       
        float_lat = float(latData)
        float_lng = float(lngData)
        float_heading = float(headingData)
        float_alt = float(altData)


        """Safety Manual Mode Switch"""
        #while True:
          
        if cs.mode == 'MANUAL':
           Script.ChangeMode("Manual")
           rsock.close()
        else:
          
           #print cs.mode
           """Follower Offset"""
           XOffset= float(0) #User Input for x axis offset
           YOffset= float(-2) #User Input for y axis offset
           brng = math.radians(float_heading)
          # brng = float_heading*math.pi/180 #User input heading angle of follower in relation to leader.  0 degrees is forward.

           d = math.sqrt((XOffset**2)+(YOffset**2)) #Distance in m

           MperLat = 69.172*1609.34 #meters per degree of latitude. Length of degree (miles) at equator * meters in a mile
           MperLong = math.cos(float_lat)*69.172*1609.34 #meters per degree of longitude

           Lat_Offset_meters = YOffset/MperLat #lat distance offset in meters
           Long_Offset_meters = XOffset/MperLong #long distance offset in meters

           Follower_lat = float_lat + (Long_Offset_meters*math.sin(brng)) + (Lat_Offset_meters*math.cos(brng)) #rotates lat follower offset in relation to heading of leader
           Follower_long = float_lng - (Long_Offset_meters*math.cos(brng)) + (Lat_Offset_meters*math.sin(brng)) #rotates long follower offset in relation to heading of leader
           Follower_alt = float_alt + 10
           #Follower_alt = 10
      
           float_lat = float(Follower_lat)
           float_lng = float(Follower_long)
           float_alt = float(Follower_alt) #4-5 second lag induced on altitude waypoint line, unless alt is set to 0
       
           print(float_lat)
           print(float_lng)
           print(float_heading)
           print(float_alt)

           """Writing Waypoints"""
           item = MissionPlanner.Utilities.Locationwp() # creating waypoint
           MissionPlanner.Utilities.Locationwp.lat.SetValue(item,float_lat)
           MissionPlanner.Utilities.Locationwp.lng.SetValue(item,float_lng)
           #MissionPlanner.Utilities.Locationwp.groundcourse.SetValue(item,float_heading)
           MissionPlanner.Utilities.Locationwp.alt.SetValue(item,float_alt) #Can only use lat,lng, or alt
           MAV.setGuidedModeWP(item) #set waypoint
           print 'Waypoint Sent'
           print time.strftime('%X %x %Z')
    # exit
    rsock.close()
    print 'Script End'

  • Do you have to set an altitude for a waypoint to be set and the vehicle follow it?

    I ask because I wrote a script for the rovers and used the same lat, long, and alt waypoint setting lines you used here.  In addition, I'm operating with mission planner on two different computers since I'm operating with 2 rovers.  One of the rovers is a leader, and the other a follower.  The follower grabs coordinates and parameters from the leader, and creates its own continuous waypoints to follow it.

    However, the problem lies in when I create the waypoint for the follower.  It works fine, but there is like a 4-5 second delay from when the leader Mission Planner script sends the parameters, and the follower Mission Planner script receives them.  I'm trying to get rid of this delay and I've found that the problem lies with the altitude line of code for setting the waypoint on the follower.  The lat/long are fine, but when I comment out the altitude, there is no delay at all.  The only problem is, the follower vehicle doesn't follow the leader without the altitude.  In fact, it doesn't move at all.  Therefore it looks like the altitude setting is necessary for writing waypoints in all vehicles, even though it doesn't make sense for rovers.  But, do you know of a way to reduce the latency or delay of this?

  • hi,

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

    thanx

  • is there a manual for python scripting for mission planner? Would the scripts in Mission planner run in Andropilot? I am looking to learn how to script an auto mode routine, but have only found brief references to python scripting, if there is a "User guide" for python scripting in Mission Planner, would you link me please? I would like to try my programming skills out on some practical flight routines for Arducopter. any help or hints to where to begin would be greatly appreciated. I also want to say THANK YOU MICHAEL for giving me my new obsession :P i love what you've written/created thanks in advance

This reply was deleted.

Activity