Python Scripting in Mission Planner

3689558475?profile=original

I like the idea of having a script to your ground station. It is a way to implement some logic that your original GUI does not enable directly. It can also enable integration easily with other dlls and modules far beyond the original scope of the Mission Planner. The first thing I asked myself about when I found this "treasure" is what were the available variables and classes that I can already access, well I could not find a clear list, maybe I am not good at googling -which is true-. So I opened the source and tried to figure out myself.  

The main start here is Script.cs it is where the engine class and scope class exist. what we want here is the scope class, it is the INTERFACE point where you can LINK outter world with PYTHON script.

      scope.SetVariable (varname, realobject);

Using above function you can add more objects to link with and define names for it in Python. See the below figure where all objects are defined - and where you can add more if u want.

I traced back the added classes MainV2.comPort, MainV2.comPort.MAV.cs, this, this that are mapped to   MavLink.cs , CurrentState.cs,Script.cs that means any public variable of function in these classes can be accessed from python script.

 

Lets open Mission Planner and open the script window and check the above conclusion:

3689558655?profile=original

In the above image you can see Script.SendRC …. You can find this function in Script.cs 

3689558626?profile=original

Same for cs.alt which should exist in CurrentState.cs

3689558492?profile=original

Now you can get all the functions you want from these classes, you can even add your own classes and functions.

 

I hope you find my small blog is useful.

Thanks,

M. Hefny

E-mail me when people leave their comments –

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

Join diydrones

Comments

  • Hi

    Seems that pygame is not supported on Iron Python.

    pls check this https://metapep.wordpress.com/tag/pygame/

    also thegoogle code link inside seems to be old for Python 2.6

    PyGame – Stuff & Gadgets
    Posts about PyGame written by pepijndevos
  • Hi,
    Thanks for your immediate response...
    Yesterday I did some workouts on this issue, I am able to import different modules into Mission Planner but the typical problem which I'm facing is, though I imported sys module but still it is showing an error like "object has no attribute _getframe". I'm not getting any idea to solve this, if you are able then please help me.
    Thanks.
  • Hi,

    I made some investigation for you .... make sure that pygame uses the same 32-64 python version check lib downloads and isse link.

    Now I faced another issue related to path pygame definition and I used this apppraoch to define paths.

    sys.path.append('C:\\Python27\\Lib\\site-packages')
    sys.path.append('C:\\Python27\\include') 

    Now I got another error related to weak reference. most probably because  iron python compiled with IRON PYTHON 2.6 and I use 2.7 library .... I will make more investigation and get back to you.

  • MHenry can you please tell me how to import pygame into mission planner, when ever I try i'm getting an error like "Error running script cannot import base from pygame", if you have any idea then please let me know.
    -Thanks
  • Did you ever work with Way Point Navigation?? I tried several times with my both hex and quad-copters. I got good results with quad-copter but with hex-copter I dunno whats the problem but its all the time crashing. If you have any Idea please let me know. Thanks.

  • Hi MHefny, Did you ever work with optical flow sensor?? I gave a trail with that, my quad was able to hold that particular altitude but its not holding Pitch and Roll. It is all the time moving here and there so if you ever work with OFS please let me know about its tuning and all. Thanks. 

  • U welcome ..... I am happy when my work is useful... Thanks for the feedback :)

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

  • amazing work! Thank you very much!

  • @Bim - if I understand you correctly, you want to be able to write a short program outside of the Mission Planner (which will have access to some data from the quad) that will will then pass on new waypoints to Mission Planner.

    If so, this was a challenge that we had a few months back.  Here is a short description of how we were able to do that.

    First, there are two bits of not-so-well-documented MP functionality that we used:

    1) MP's ability to stream GPS information from the quad to any serial port.  (Check out the reference here: http://planner.ardupilot.com/wiki/common-other-mission-planner-feat...)

    2) The ability to send a new waypoint to MP through an http command (see Michael's post in this topic http://diydrones.com/forum/topics/apm-adaptive-flying-video-analysi...)

    From point 1) above you can get position information back from the quad (all the standard GPS stuff like lat, lon, height, number of satellites, quality of fix, etc). It sends the information as a NMEA sentences - about 2 a second.  

    From point 2) you can send a the quad a new waypoint

    To connect these we wrote a small Python program that:

    1. listened on the serial port for the NMEA information
    2. parsed this information and extracted the position information
    3. applied logic/calculations to create a new waypoint
    4. sent the new waypoint to the MP
    5. go back to 1.

    You may be asking "but how can I get other information back from the quad (let's say air temperature)?"

    That was a little more difficult and required a bit of hacking.  We changed the APM code to send the information in existing and "unused" fields in the NMEA strings.  Specifically, we used the HDOP (floating point) and the number of satellites (integer) fields.  These fields are not used by MP, only recorded.  This is not very difficult once you  understand how the APM code works (it required only one line of code change).

    I believe however, that the Python scripting inside MP has recently been upgraded, so much of what I have written here may already be out of date.  

This reply was deleted.