Servers by jDrones

Adding mission parts at the beginning and end of new mission

@Eosbandi Hello there, do you know if there is a smart way of adding commands at the beginning and at te end of a new auto grid created mission?
Everytime i make a new mission i need to add some commands row at the beginning and some at the end, i was wondering if MP takes this need in consideration and there is a way to do it authomatically.

Edit: I was just rechecking and i see there is a load and append that should do what i need for the end part, now i have to find a load and put at top of mission :slight_smile:

Best would be MP would recognize the files by name and if it loads a file named top.waypoints it puts it on top of the actual mission, if it is named bottom.waypoints than it appends it at the bottom.

thanks,

Corrado

What about a python script that you can run from FlightData/Scrips
Something like this :slight_smile:

import sys
import math
import clr
import time
clr.AddReference("MissionPlanner")
import MissionPlanner
clr.AddReference("MAVLink") # includes the Mavlink class
import MAVLink


print 'Start Script'

MissionPlanner.MainV2.instance.FlightPlanner.InsertCommand(0, MAVLink.MAV_CMD.CONDITION_DELAY, 100, 0, 0, 0, 0, 0, 0, 0);
MissionPlanner.MainV2.instance.FlightPlanner.InsertCommand(1, MAVLink.MAV_CMD.DO_SET_SERVO, 9, 1000, 0, 0, 0, 0, 0);

A script that load top.waypoint and bottom.waypoint and adds them to the created mission at top and bottom?
Ok i run this and it inserts my commands at top as i need, now i need to append at bottom and move a line :slight_smile:
Ok, AddCommand, adds it at the end of the list, another issue solved.

Still to do:

  • Erase a line with id would be ok
  • Also get angle from generated grid and write it in a command :slight_smile:

MissionPlanner.MainV2.instance.FlightPlanner.Commands.Rows.RemoveAt(lineid);

I has no idea for the angle…

Great thanks, removing worked great. Really thanks for your help.

Corrado

For the angle i tried

Angolo=input("angolo: ")

But it doesn’t let me ineract with script in script terminal

OK, let’s try a different aproach. Make sure that you use the latest MP beta. Create a filename.cs file with the code below and put it in the MissionPlanner/Plugins directory. It will be compiled in runtime. It adds a line to the context menu (right click) on Flight Planner…
The important part is in the but_click function…

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MissionPlanner.Utilities;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using MissionPlanner;
using System.Drawing;

namespace Shortcuts
{
    public class Plugin : MissionPlanner.Plugin.Plugin
    {
		
		ToolStripMenuItem but;
		
        public override string Name
        {
            get { return "Small stuff"; }
        }

        public override string Version
        {
            get { return "0.10"; }
        }

        public override string Author
        {
            get { return "EOSBandi"; }
        }

        public override bool Init()
        {
            return true;
        }

        public override bool Loaded()
        {
            but = new ToolStripMenuItem("Fix mission top/bottom");
            but.Click += but_Click;
			ToolStripItemCollection col = Host.FPMenuMap.Items;
			col.Insert(0, but);
			
            return true;
        }

        public override bool Loop()
        {
            return true;
        }

        public override bool Exit()
        {
            return true;
        }

        //This for the Money

		void but_Click(object sender, EventArgs e)
		{
			
			string angle = "0";
            InputQuery("Enter Angle", "This will be the heading", ref angle);
		    int angle_in_number = Int32.Parse(angle);
			
			Host.InsertWP(0, MAVLink.MAV_CMD.DO_SET_SERVO, 9, angle_in_number, 0, 0, 0, 0, 0);
            Host.InsertWP(1, MAVLink.MAV_CMD.DO_SET_SERVO, 10, 1000, 0, 0, 0, 0, 0);
 
			Host.AddWPtoList(MAVLink.MAV_CMD.DO_SET_SERVO, 9, 1000, 0, 0, 0, 0, 0);
            Host.AddWPtoList(MAVLink.MAV_CMD.DO_SET_SERVO, 10, 1000, 0, 0, 0, 0, 0);
				
		}
		
		
	 //// Utilities....	
     public static Boolean InputQuery(String caption, String prompt, ref String value)
        {
            Form form;
            form = new Form();
            form.AutoScaleMode = AutoScaleMode.Font;
            //form.Font = MissionPlanner.Drawing.SystemFonts.IconTitleFont;

            SizeF dialogUnits;
            dialogUnits = form.AutoScaleDimensions;

            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.Text = caption;

            form.ClientSize = new Size(
                        MulDiv(180, (int)dialogUnits.Width, 4),
                        MulDiv(63, (int)dialogUnits.Height, 8));

            form.StartPosition = FormStartPosition.CenterScreen;

            System.Windows.Forms.Label lblPrompt;
            lblPrompt = new System.Windows.Forms.Label();
            lblPrompt.Parent = form;
            lblPrompt.AutoSize = true;
            lblPrompt.Left = MulDiv(8, (int)dialogUnits.Width, 4);
            lblPrompt.Top = MulDiv(8, (int)dialogUnits.Height, 8);
            lblPrompt.Text = prompt;

            System.Windows.Forms.TextBox edInput;
            edInput = new System.Windows.Forms.TextBox();
            edInput.Parent = form;
            edInput.Left = lblPrompt.Left;
            edInput.Top = MulDiv(19, (int)dialogUnits.Height, 8);
            edInput.Width = MulDiv(164,(int) dialogUnits.Width, 4);
            edInput.Text = value;
            edInput.SelectAll();


            int buttonTop = MulDiv(41,(int) dialogUnits.Height, 8);
            //Command buttons should be 50x14 dlus
            Size buttonSize = new Size(50*(int)dialogUnits.Width/6, 14* (int)dialogUnits.Height/6);

            System.Windows.Forms.Button bbOk = new System.Windows.Forms.Button();
            bbOk.Parent = form;
            bbOk.Text = "OK";
            bbOk.DialogResult = DialogResult.OK;
            form.AcceptButton = bbOk;
            bbOk.Location = new Point(MulDiv(38, (int)dialogUnits.Width, 4), buttonTop);
            bbOk.Size = buttonSize;

            System.Windows.Forms.Button bbCancel = new System.Windows.Forms.Button();
            bbCancel.Parent = form;
            bbCancel.Text = "Cancel";
            bbCancel.DialogResult = DialogResult.Cancel;
            form.CancelButton = bbCancel;
            bbCancel.Location = new Point(MulDiv(92, (int)dialogUnits.Width, 4), buttonTop);
            bbCancel.Size = buttonSize;

            if (form.ShowDialog() == DialogResult.OK)
            {
                value = edInput.Text;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Multiplies two 32-bit values and then divides the 64-bit result by a 
        /// third 32-bit value. The final result is rounded to the nearest integer.
        /// </summary>
        public static int MulDiv(int nNumber, int nNumerator, int nDenominator)
        {
            return (int)Math.Round((float)nNumber * nNumerator / nDenominator);
        }

    }
}

It works great thank you very very much, i would only need the line to delete too :slight_smile:

use this

MissionPlanner.Controls.MyDataGridView commands; // At the top where toolstripmenuitem but is defined

commands = Host.MainForm.FlightPlanner.Controls.Find(“Commands”, true).FirstOrDefault() as MissionPlanner.Controls.MyDataGridView; //In Loaded, right before return true;

then : commands.Rows.RemoveAt(lineitem);

I think i lost you :frowning:

when i add

commands = Host.MainForm.FlightPlanner.Controls.Find(“Commands”, true).FirstOrDefault() as MissionPlanner.Controls.MyDataGridView;

the plugin doesn’t work anymore, right click doesn’t show fix mission anymore

I added it like this:

public override bool Loaded()
{
but = new ToolStripMenuItem(“Fix mission top/bottom”);
but.Click += but_Click;
ToolStripItemCollection col = Host.FPMenuMap.Items;
col.Insert(0, but);
commands = Host.MainForm.FlightPlanner.Controls.Find(“Commands”, true).FirstOrDefault() as MissionPlanner.Controls.MyDataGridView;
return true;
}

OK, this is the working full file :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MissionPlanner.Utilities;
using MissionPlanner.Controls;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using MissionPlanner;
using System.Drawing;

namespace Shortcuts
{
    public class Plugin : MissionPlanner.Plugin.Plugin
    {
		
		ToolStripMenuItem but;
		MissionPlanner.Controls.MyDataGridView commands;
		
		
        public override string Name
        {
            get { return "Small stuff"; }
        }

        public override string Version
        {
            get { return "0.10"; }
        }

        public override string Author
        {
            get { return "EOSBandi"; }
        }

        public override bool Init()
        {
            return true;
        }

        public override bool Loaded()
        {
            but = new ToolStripMenuItem("Fix mission top/bottom");
            but.Click += but_Click;
		ToolStripItemCollection col = Host.FPMenuMap.Items;
		col.Insert(0, but);
		commands = Host.MainForm.FlightPlanner.Controls.Find("Commands", true).FirstOrDefault() as MissionPlanner.Controls.MyDataGridView; 
            return true;
        }

        public override bool Loop()
        {
            return true;
        }

        public override bool Exit()
        {
            return true;
        }

        //This for the Money

		void but_Click(object sender, EventArgs e)
		{
			
			string angle = "0";
            InputQuery("Enter Angle", "This will be the heading", ref angle);
		    int angle_in_number = Int32.Parse(angle);
			
			Host.InsertWP(0, MAVLink.MAV_CMD.DO_SET_SERVO, 9, angle_in_number, 0, 0, 0, 0, 0);
            Host.InsertWP(1, MAVLink.MAV_CMD.DO_SET_SERVO, 10, 1000, 0, 0, 0, 0, 0);
 
			Host.AddWPtoList(MAVLink.MAV_CMD.DO_SET_SERVO, 9, 1000, 0, 0, 0, 0, 0);
            Host.AddWPtoList(MAVLink.MAV_CMD.DO_SET_SERVO, 10, 1000, 0, 0, 0, 0, 0);
			commands.Rows.RemoveAt(1);
				
		}
		
		
	 //// Utilities....	
     public static Boolean InputQuery(String caption, String prompt, ref String value)
        {
            Form form;
            form = new Form();
            form.AutoScaleMode = AutoScaleMode.Font;
            //form.Font = MissionPlanner.Drawing.SystemFonts.IconTitleFont;

            SizeF dialogUnits;
            dialogUnits = form.AutoScaleDimensions;

            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.Text = caption;

            form.ClientSize = new Size(
                        MulDiv(180, (int)dialogUnits.Width, 4),
                        MulDiv(63, (int)dialogUnits.Height, 8));

            form.StartPosition = FormStartPosition.CenterScreen;

            System.Windows.Forms.Label lblPrompt;
            lblPrompt = new System.Windows.Forms.Label();
            lblPrompt.Parent = form;
            lblPrompt.AutoSize = true;
            lblPrompt.Left = MulDiv(8, (int)dialogUnits.Width, 4);
            lblPrompt.Top = MulDiv(8, (int)dialogUnits.Height, 8);
            lblPrompt.Text = prompt;

            System.Windows.Forms.TextBox edInput;
            edInput = new System.Windows.Forms.TextBox();
            edInput.Parent = form;
            edInput.Left = lblPrompt.Left;
            edInput.Top = MulDiv(19, (int)dialogUnits.Height, 8);
            edInput.Width = MulDiv(164,(int) dialogUnits.Width, 4);
            edInput.Text = value;
            edInput.SelectAll();


            int buttonTop = MulDiv(41,(int) dialogUnits.Height, 8);
            //Command buttons should be 50x14 dlus
            Size buttonSize = new Size(50*(int)dialogUnits.Width/6, 14* (int)dialogUnits.Height/6);

            System.Windows.Forms.Button bbOk = new System.Windows.Forms.Button();
            bbOk.Parent = form;
            bbOk.Text = "OK";
            bbOk.DialogResult = DialogResult.OK;
            form.AcceptButton = bbOk;
            bbOk.Location = new Point(MulDiv(38, (int)dialogUnits.Width, 4), buttonTop);
            bbOk.Size = buttonSize;

            System.Windows.Forms.Button bbCancel = new System.Windows.Forms.Button();
            bbCancel.Parent = form;
            bbCancel.Text = "Cancel";
            bbCancel.DialogResult = DialogResult.Cancel;
            form.CancelButton = bbCancel;
            bbCancel.Location = new Point(MulDiv(92, (int)dialogUnits.Width, 4), buttonTop);
            bbCancel.Size = buttonSize;

            if (form.ShowDialog() == DialogResult.OK)
            {
                value = edInput.Text;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Multiplies two 32-bit values and then divides the 64-bit result by a 
        /// third 32-bit value. The final result is rounded to the nearest integer.
        /// </summary>
        public static int MulDiv(int nNumber, int nNumerator, int nDenominator)
        {
            return (int)Math.Round((float)nNumber * nNumerator / nDenominator);
        }

    }
}

Thank you very much, it works now, it was only the “”.

Thank you very very very much, it is now much nicer than i hoped for!!!

1 Like

You are welcome.
I think this is a nice example of the fact, that Mission Planner is an extremely versatile tool, and with a little coding you can do everything with it.

1 Like

Yes, it is a great tool, it is my C# skill lagging behind big time.

Thanks again.

perfect use of the c# plugin system. nice work guys

Servers by jDrones