1 Star 0 Fork 0

yanhuizhang / MissionPlanner

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
MainV2.cs 177.12 KB
一键复制 编辑 原始数据 按行查看 历史
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400
using GMap.NET.WindowsForms;
using log4net;
using MissionPlanner.ArduPilot;
using MissionPlanner.Comms;
using MissionPlanner.Controls;
using MissionPlanner.GCSViews.ConfigurationView;
using MissionPlanner.Log;
using MissionPlanner.Maps;
using MissionPlanner.Utilities;
using MissionPlanner.Utilities.AltitudeAngel;
using MissionPlanner.Warnings;
using SkiaSharp;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MissionPlanner.Utilities.HW;
using Transitions;
namespace MissionPlanner
{
public partial class MainV2 : Form
{
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static menuicons displayicons; //do not initialize to allow update of custom icons
public abstract class menuicons
{
public abstract Image fd { get; }
public abstract Image fp { get; }
public abstract Image initsetup { get; }
public abstract Image config_tuning { get; }
public abstract Image sim { get; }
public abstract Image terminal { get; }
public abstract Image help { get; }
public abstract Image donate { get; }
public abstract Image connect { get; }
public abstract Image disconnect { get; }
public abstract Image bg { get; }
public abstract Image wizard { get; }
}
public class burntkermitmenuicons : menuicons
{
public override Image fd
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_flightdata_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_flightdata_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_flightdata_icon;
}
}
public override Image fp
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_flightplan_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_flightplan_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_flightplan_icon;
}
}
public override Image initsetup
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_initialsetup_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_initialsetup_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_initialsetup_icon;
}
}
public override Image config_tuning
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_tuningconfig_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_tuningconfig_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_tuningconfig_icon;
}
}
public override Image sim
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_simulation_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_simulation_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_simulation_icon;
}
}
public override Image terminal
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_terminal_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_terminal_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_terminal_icon;
}
}
public override Image help
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_help_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_help_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_help_icon;
}
}
public override Image donate
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_donate_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_donate_icon.png");
else
return global::MissionPlanner.Properties.Resources.donate;
}
}
public override Image connect
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_connect_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_connect_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_connect_icon;
}
}
public override Image disconnect
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_disconnect_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_disconnect_icon.png");
else
return global::MissionPlanner.Properties.Resources.light_disconnect_icon;
}
}
public override Image bg
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_icon_background.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_icon_background.png");
else
return global::MissionPlanner.Properties.Resources.bgdark;
}
}
public override Image wizard
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "light_wizard_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "light_wizard_icon.png");
else
return global::MissionPlanner.Properties.Resources.wizardicon;
}
}
}
public class highcontrastmenuicons : menuicons
{
public override Image fd
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_flightdata_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_flightdata_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_flightdata_icon;
}
}
public override Image fp
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_flightplan_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_flightplan_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_flightplan_icon;
}
}
public override Image initsetup
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_initialsetup_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_initialsetup_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_initialsetup_icon;
}
}
public override Image config_tuning
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_tuningconfig_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_tuningconfig_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_tuningconfig_icon;
}
}
public override Image sim
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_simulation_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_simulation_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_simulation_icon;
}
}
public override Image terminal
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_terminal_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_terminal_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_terminal_icon;
}
}
public override Image help
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_help_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_help_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_help_icon;
}
}
public override Image donate
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_donate_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_donate_icon.png");
else
return global::MissionPlanner.Properties.Resources.donate;
}
}
public override Image connect
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_connect_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_connect_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_connect_icon;
}
}
public override Image disconnect
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_disconnect_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_disconnect_icon.png");
else
return global::MissionPlanner.Properties.Resources.dark_disconnect_icon;
}
}
public override Image bg
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_icon_background.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_icon_background.png");
else
return null;
}
}
public override Image wizard
{
get
{
if (File.Exists(Settings.GetRunningDirectory() + "dark_wizard_icon.png"))
return Image.FromFile(Settings.GetRunningDirectory() + "dark_wizard_icon.png");
else
return global::MissionPlanner.Properties.Resources.wizardicon;
}
}
}
Controls.MainSwitcher MyView;
private static DisplayView _displayConfiguration = new DisplayView().Advanced();
public static event EventHandler LayoutChanged;
public static DisplayView DisplayConfiguration
{
get { return _displayConfiguration; }
set
{
_displayConfiguration = value;
Settings.Instance["displayview"] = _displayConfiguration.ConvertToString();
LayoutChanged?.Invoke(null, EventArgs.Empty);
}
}
public static bool ShowAirports { get; set; }
public static bool ShowTFR { get; set; }
private Utilities.adsb _adsb;
public bool EnableADSB
{
get { return _adsb != null; }
set
{
if (value == true)
{
_adsb = new Utilities.adsb();
if (Settings.Instance["adsbserver"] != null)
Utilities.adsb.server = Settings.Instance["adsbserver"];
if (Settings.Instance["adsbport"] != null)
Utilities.adsb.serverport = int.Parse(Settings.Instance["adsbport"].ToString());
}
else
{
Utilities.adsb.Stop();
_adsb = null;
}
}
}
//public static event EventHandler LayoutChanged;
/// <summary>
/// Active Comport interface
/// </summary>
public static MAVLinkInterface comPort
{
get
{
return _comPort;
}
set
{
if (_comPort == value)
return;
_comPort = value;
_comPort.MavChanged -= instance.comPort_MavChanged;
_comPort.MavChanged += instance.comPort_MavChanged;
instance.comPort_MavChanged(null, null);
}
}
static MAVLinkInterface _comPort = new MAVLinkInterface();
/// <summary>
/// passive comports
/// </summary>
public static List<MAVLinkInterface> Comports = new List<MAVLinkInterface>();
public delegate void WMDeviceChangeEventHandler(WM_DEVICECHANGE_enum cause);
public event WMDeviceChangeEventHandler DeviceChanged;
/// <summary>
/// other planes in the area from adsb
/// </summary>
public object adsblock = new object();
public ConcurrentDictionary<string, adsb.PointLatLngAltHdg> adsbPlanes = new ConcurrentDictionary<string, adsb.PointLatLngAltHdg>();
string titlebar;
/// <summary>
/// Comport name
/// </summary>
public static string comPortName = "";
public static int comPortBaud = 115200;
/// <summary>
/// mono detection
/// </summary>
public static bool MONO = false;
/// <summary>
/// speech engine enable
/// </summary>
public static bool speechEnable
{
get { return speechEngine == null ? false : speechEngine.speechEnable; }
set
{
if (speechEngine != null) speechEngine.speechEnable = value;
}
}
/// <summary>
/// spech engine static class
/// </summary>
public static ISpeech speechEngine { get; set; }
/// <summary>
/// joystick static class
/// </summary>
public static Joystick.Joystick joystick { get; set; }
/// <summary>
/// track last joystick packet sent. used to control rate
/// </summary>
DateTime lastjoystick = DateTime.Now;
/// <summary>
/// determine if we are running sitl
/// </summary>
public static bool sitl
{
get
{
if (MissionPlanner.GCSViews.SITL.SITLSEND == null) return false;
if (MissionPlanner.GCSViews.SITL.SITLSEND.Client.Connected) return true;
return false;
}
}
/// <summary>
/// hud background image grabber from a video stream - not realy that efficent. ie no hardware overlays etc.
/// </summary>
public static WebCamService.Capture cam { get; set; }
/// <summary>
/// controls the main serial reader thread
/// </summary>
bool serialThread = false;
bool pluginthreadrun = false;
bool joystickthreadrun = false;
Thread httpthread;
Thread joystickthread;
Thread serialreaderthread;
Thread pluginthread;
/// <summary>
/// track the last heartbeat sent
/// </summary>
private DateTime heatbeatSend = DateTime.Now;
/// <summary>
/// used to call anything as needed.
/// </summary>
public static MainV2 instance = null;
public static MainSwitcher View;
/// <summary>
/// store the time we first connect
/// </summary>
DateTime connecttime = DateTime.Now;
DateTime nodatawarning = DateTime.Now;
DateTime OpenTime = DateTime.Now;
DateTime connectButtonUpdate = DateTime.Now;
/// <summary>
/// declared here if i want a "single" instance of the form
/// ie configuration gets reloaded on every click
/// </summary>
public GCSViews.FlightData FlightData;
public GCSViews.FlightPlanner FlightPlanner;
GCSViews.SITL Simulation;
private Form connectionStatsForm;
private ConnectionStats _connectionStats;
/// <summary>
/// This 'Control' is the toolstrip control that holds the comport combo, baudrate combo etc
/// Otiginally seperate controls, each hosted in a toolstip sqaure, combined into this custom
/// control for layout reasons.
/// </summary>
public static ConnectionControl _connectionControl;
public static bool TerminalTheming = true;
public void updateLayout(object sender, EventArgs e)
{
MenuSimulation.Visible = DisplayConfiguration.displaySimulation;
MenuHelp.Visible = DisplayConfiguration.displayHelp;
MissionPlanner.Controls.BackstageView.BackstageView.Advanced = DisplayConfiguration.isAdvancedMode;
if (Settings.Instance.GetBoolean("menu_autohide") != DisplayConfiguration.autoHideMenuForce)
{
AutoHideMenu(DisplayConfiguration.autoHideMenuForce);
Settings.Instance["menu_autohide"] = DisplayConfiguration.autoHideMenuForce.ToString();
}
autoHideToolStripMenuItem.Visible = !DisplayConfiguration.autoHideMenuForce;
//Flight data page
if (MainV2.instance.FlightData != null)
{
TabControl t = MainV2.instance.FlightData.tabControlactions;
if (DisplayConfiguration.displayQuickTab && !t.TabPages.Contains(FlightData.tabQuick))
{
t.TabPages.Add(FlightData.tabQuick);
}
else if (!DisplayConfiguration.displayQuickTab && t.TabPages.Contains(FlightData.tabQuick))
{
t.TabPages.Remove(FlightData.tabQuick);
}
if (DisplayConfiguration.displayPreFlightTab && !t.TabPages.Contains(FlightData.tabPagePreFlight))
{
t.TabPages.Add(FlightData.tabPagePreFlight);
}
else if (!DisplayConfiguration.displayPreFlightTab && t.TabPages.Contains(FlightData.tabPagePreFlight))
{
t.TabPages.Remove(FlightData.tabPagePreFlight);
}
if (DisplayConfiguration.displayAdvActionsTab && !t.TabPages.Contains(FlightData.tabActions))
{
t.TabPages.Add(FlightData.tabActions);
}
else if (!DisplayConfiguration.displayAdvActionsTab && t.TabPages.Contains(FlightData.tabActions))
{
t.TabPages.Remove(FlightData.tabActions);
}
if (DisplayConfiguration.displaySimpleActionsTab && !t.TabPages.Contains(FlightData.tabActionsSimple))
{
t.TabPages.Add(FlightData.tabActionsSimple);
}
else if (!DisplayConfiguration.displaySimpleActionsTab && t.TabPages.Contains(FlightData.tabActionsSimple))
{
t.TabPages.Remove(FlightData.tabActionsSimple);
}
if (DisplayConfiguration.displayGaugesTab && !t.TabPages.Contains(FlightData.tabGauges))
{
t.TabPages.Add(FlightData.tabGauges);
}
else if (!DisplayConfiguration.displayGaugesTab && t.TabPages.Contains(FlightData.tabGauges))
{
t.TabPages.Remove(FlightData.tabGauges);
}
if (DisplayConfiguration.displayStatusTab && !t.TabPages.Contains(FlightData.tabStatus))
{
t.TabPages.Add(FlightData.tabStatus);
}
else if (!DisplayConfiguration.displayStatusTab && t.TabPages.Contains(FlightData.tabStatus))
{
t.TabPages.Remove(FlightData.tabStatus);
}
if (DisplayConfiguration.displayServoTab && !t.TabPages.Contains(FlightData.tabServo))
{
t.TabPages.Add(FlightData.tabServo);
}
else if (!DisplayConfiguration.displayServoTab && t.TabPages.Contains(FlightData.tabServo))
{
t.TabPages.Remove(FlightData.tabServo);
}
if (DisplayConfiguration.displayScriptsTab && !t.TabPages.Contains(FlightData.tabScripts))
{
t.TabPages.Add(FlightData.tabScripts);
}
else if (!DisplayConfiguration.displayScriptsTab && t.TabPages.Contains(FlightData.tabScripts))
{
t.TabPages.Remove(FlightData.tabScripts);
}
if (DisplayConfiguration.displayTelemetryTab && !t.TabPages.Contains(FlightData.tabTLogs))
{
t.TabPages.Add(FlightData.tabTLogs);
}
else if (!DisplayConfiguration.displayTelemetryTab && t.TabPages.Contains(FlightData.tabTLogs))
{
t.TabPages.Remove(FlightData.tabTLogs);
}
if (DisplayConfiguration.displayDataflashTab && !t.TabPages.Contains(FlightData.tablogbrowse))
{
t.TabPages.Add(FlightData.tablogbrowse);
}
else if (!DisplayConfiguration.displayDataflashTab && t.TabPages.Contains(FlightData.tablogbrowse))
{
t.TabPages.Remove(FlightData.tablogbrowse);
}
if (DisplayConfiguration.displayMessagesTab && !t.TabPages.Contains(FlightData.tabPagemessages))
{
t.TabPages.Add(FlightData.tabPagemessages);
}
else if (!DisplayConfiguration.displayMessagesTab && t.TabPages.Contains(FlightData.tabPagemessages))
{
t.TabPages.Remove(FlightData.tabPagemessages);
}
t.SelectedIndex = 0;
MainV2.instance.FlightData.loadTabControlActions();
}
if (MainV2.instance.FlightPlanner != null)
{
//hide menu items
MainV2.instance.FlightPlanner.updateDisplayView();
}
}
public MainV2()
{
log.Info("Mainv2 ctor");
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
// create one here - but override on load
Settings.Instance["guid"] = Guid.NewGuid().ToString();
// load config
LoadConfig();
// force language to be loaded
L10N.GetConfigLang();
ShowAirports = true;
// setup adsb
Utilities.adsb.UpdatePlanePosition += adsb_UpdatePlanePosition;
MAVLinkInterface.UpdateADSBPlanePosition += adsb_UpdatePlanePosition;
MAVLinkInterface.UpdateADSBCollision += (sender, tuple) =>
{
lock (adsblock)
{
if (MainV2.instance.adsbPlanes.ContainsKey(tuple.id))
{
// update existing
((adsb.PointLatLngAltHdg)instance.adsbPlanes[tuple.id]).ThreatLevel = tuple.threat_level;
}
}
};
MAVLinkInterface.gcssysid = (byte)Settings.Instance.GetByte("gcsid", MAVLinkInterface.gcssysid);
Form splash = Program.Splash;
splash?.Refresh();
Application.DoEvents();
instance = this;
InitializeComponent();
//Init Theme table and load BurntKermit as a default
ThemeManager.thmColor = new ThemeColorTable(); //Init colortable
ThemeManager.thmColor.InitColors(); //This fills up the table with BurntKermit defaults.
ThemeManager.thmColor.SetTheme(); //Set the colors, this need to handle the case when not all colors are defined in the theme file
if (Settings.Instance["theme"] == null) Settings.Instance["theme"] = "BurntKermit.mpsystheme";
ThemeManager.LoadTheme(Settings.Instance["theme"]);
Utilities.ThemeManager.ApplyThemeTo(this);
MyView = new MainSwitcher(this);
View = MyView;
//startup console
TCPConsole.Write((byte)'S');
// define default basestream
comPort.BaseStream = new SerialPort();
comPort.BaseStream.BaudRate = 115200;
_connectionControl = toolStripConnectionControl.ConnectionControl;
_connectionControl.CMB_baudrate.TextChanged += this.CMB_baudrate_TextChanged;
_connectionControl.CMB_serialport.SelectedIndexChanged += this.CMB_serialport_SelectedIndexChanged;
_connectionControl.CMB_serialport.Click += this.CMB_serialport_Click;
_connectionControl.cmb_sysid.Click += cmb_sysid_Click;
_connectionControl.ShowLinkStats += (sender, e) => ShowConnectionStatsForm();
srtm.datadirectory = Settings.GetDataDirectory() +
"srtm";
var t = Type.GetType("Mono.Runtime");
MONO = (t != null);
try
{
speechEngine = new Speech();
MAVLinkInterface.Speech = speechEngine;
CurrentState.Speech = speechEngine;
}
catch { }
Warnings.CustomWarning.defaultsrc = comPort.MAV.cs;
Warnings.WarningEngine.Start(speechEnable ? speechEngine : null);
Warnings.WarningEngine.WarningMessage += (sender, s) =>
{
MainV2.comPort.MAV.cs.messageHigh = s;
MainV2.comPort.MAV.cs.messageHighTime = DateTime.Now;
};
// proxy loader - dll load now instead of on config form load
new Transition(new TransitionType_EaseInEaseOut(2000));
foreach (object obj in Enum.GetValues(typeof(Firmwares)))
{
_connectionControl.TOOL_APMFirmware.Items.Add(obj);
}
if (_connectionControl.TOOL_APMFirmware.Items.Count > 0)
_connectionControl.TOOL_APMFirmware.SelectedIndex = 0;
PopulateSerialportList();
if (_connectionControl.CMB_serialport.Items.Count > 0)
{
_connectionControl.CMB_baudrate.SelectedIndex = 8;
_connectionControl.CMB_serialport.SelectedIndex = 0;
}
// ** Done
splash?.Refresh();
Application.DoEvents();
// load last saved connection settings
string temp = Settings.Instance.ComPort;
if (!string.IsNullOrEmpty(temp))
{
_connectionControl.CMB_serialport.SelectedIndex = _connectionControl.CMB_serialport.FindString(temp);
if (_connectionControl.CMB_serialport.SelectedIndex == -1)
{
_connectionControl.CMB_serialport.Text = temp; // allows ports that dont exist - yet
}
comPort.BaseStream.PortName = temp;
comPortName = temp;
}
string temp2 = Settings.Instance.BaudRate;
if (!string.IsNullOrEmpty(temp2))
{
var idx = _connectionControl.CMB_baudrate.FindString(temp2);
if (idx == -1)
{
_connectionControl.CMB_baudrate.Text = temp2;
}
else
{
_connectionControl.CMB_baudrate.SelectedIndex = idx;
}
comPortBaud = int.Parse(temp2);
}
string temp3 = Settings.Instance.APMFirmware;
if (!string.IsNullOrEmpty(temp3))
{
_connectionControl.TOOL_APMFirmware.SelectedIndex =
_connectionControl.TOOL_APMFirmware.FindStringExact(temp3);
if (_connectionControl.TOOL_APMFirmware.SelectedIndex == -1)
_connectionControl.TOOL_APMFirmware.SelectedIndex = 0;
MainV2.comPort.MAV.cs.firmware =
(Firmwares)Enum.Parse(typeof(Firmwares), _connectionControl.TOOL_APMFirmware.Text);
}
MissionPlanner.Utilities.Tracking.cid = new Guid(Settings.Instance["guid"].ToString());
// setup guids for droneshare
if (!Settings.Instance.ContainsKey("plane_guid"))
Settings.Instance["plane_guid"] = Guid.NewGuid().ToString();
if (!Settings.Instance.ContainsKey("copter_guid"))
Settings.Instance["copter_guid"] = Guid.NewGuid().ToString();
if (!Settings.Instance.ContainsKey("rover_guid"))
Settings.Instance["rover_guid"] = Guid.NewGuid().ToString();
if (Settings.Instance.ContainsKey("language") && !string.IsNullOrEmpty(Settings.Instance["language"]))
{
changelanguage(CultureInfoEx.GetCultureInfo(Settings.Instance["language"]));
}
if (splash != null)
{
this.Text = splash?.Text;
titlebar = splash?.Text;
}
if (!MONO) // windows only
{
if (Settings.Instance["showconsole"] != null && Settings.Instance["showconsole"].ToString() == "True")
{
}
else
{
int win = NativeMethods.FindWindow("ConsoleWindowClass", null);
NativeMethods.ShowWindow(win, NativeMethods.SW_HIDE); // hide window
}
// prevent system from sleeping while mp open
var previousExecutionState =
NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
}
ChangeUnits();
if (Settings.Instance["showairports"] != null)
{
MainV2.ShowAirports = bool.Parse(Settings.Instance["showairports"]);
}
// set default
ShowTFR = true;
// load saved
if (Settings.Instance["showtfr"] != null)
{
MainV2.ShowTFR = Settings.Instance.GetBoolean("showtfr", ShowTFR);
}
if (Settings.Instance["enableadsb"] != null)
{
MainV2.instance.EnableADSB = Settings.Instance.GetBoolean("enableadsb");
}
try
{
log.Info(Process.GetCurrentProcess().Modules.ToJSON());
}
catch
{
}
try
{
log.Info("Create FD");
FlightData = new GCSViews.FlightData();
log.Info("Create FP");
FlightPlanner = new GCSViews.FlightPlanner();
//Configuration = new GCSViews.ConfigurationView.Setup();
log.Info("Create SIM");
Simulation = new GCSViews.SITL();
//Firmware = new GCSViews.Firmware();
//Terminal = new GCSViews.Terminal();
FlightData.Width = MyView.Width;
FlightPlanner.Width = MyView.Width;
Simulation.Width = MyView.Width;
}
catch (ArgumentException e)
{
//http://www.microsoft.com/en-us/download/details.aspx?id=16083
//System.ArgumentException: Font 'Arial' does not support style 'Regular'.
log.Fatal(e);
CustomMessageBox.Show(e.ToString() +
"\n\n Font Issues? Please install this http://www.microsoft.com/en-us/download/details.aspx?id=16083");
//splash.Close();
//this.Close();
Application.Exit();
}
catch (Exception e)
{
log.Fatal(e);
CustomMessageBox.Show("A Major error has occured : " + e.ToString());
Application.Exit();
}
//set first instance display configuration
if (DisplayConfiguration == null)
{
DisplayConfiguration = DisplayConfiguration.Advanced();
}
// load old config
if (Settings.Instance["advancedview"] != null)
{
if (Settings.Instance.GetBoolean("advancedview") == true)
{
DisplayConfiguration = new DisplayView().Advanced();
}
// remove old config
Settings.Instance.Remove("advancedview");
} //// load this before the other screens get loaded
if (Settings.Instance["displayview"] != null)
{
try
{
DisplayConfiguration = Settings.Instance.GetDisplayView("displayview");
}
catch
{
DisplayConfiguration = DisplayConfiguration.Advanced();
}
}
LayoutChanged += updateLayout;
LayoutChanged(null, EventArgs.Empty);
if (Settings.Instance["CHK_GDIPlus"] != null)
GCSViews.FlightData.myhud.opengl = !bool.Parse(Settings.Instance["CHK_GDIPlus"].ToString());
if (Settings.Instance["CHK_hudshow"] != null)
GCSViews.FlightData.myhud.hudon = bool.Parse(Settings.Instance["CHK_hudshow"].ToString());
try
{
if (Settings.Instance["MainLocX"] != null && Settings.Instance["MainLocY"] != null)
{
this.StartPosition = FormStartPosition.Manual;
Point startpos = new Point(Settings.Instance.GetInt32("MainLocX"),
Settings.Instance.GetInt32("MainLocY"));
// fix common bug which happens when user removes a monitor, the app shows up
// offscreen and it is very hard to move it onscreen. Also happens with
// remote desktop a lot. So this only restores position if the position
// is visible.
foreach (Screen s in Screen.AllScreens)
{
if (s.WorkingArea.Contains(startpos))
{
this.Location = startpos;
break;
}
}
}
if (Settings.Instance["MainMaximised"] != null)
{
this.WindowState =
(FormWindowState)Enum.Parse(typeof(FormWindowState), Settings.Instance["MainMaximised"]);
// dont allow minimised start state
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
this.Location = new Point(100, 100);
}
}
if (Settings.Instance["MainHeight"] != null)
this.Height = Settings.Instance.GetInt32("MainHeight");
if (Settings.Instance["MainWidth"] != null)
this.Width = Settings.Instance.GetInt32("MainWidth");
// set presaved default telem rates
if (Settings.Instance["CMB_rateattitude"] != null)
CurrentState.rateattitudebackup = Settings.Instance.GetInt32("CMB_rateattitude");
if (Settings.Instance["CMB_rateposition"] != null)
CurrentState.ratepositionbackup = Settings.Instance.GetInt32("CMB_rateposition");
if (Settings.Instance["CMB_ratestatus"] != null)
CurrentState.ratestatusbackup = Settings.Instance.GetInt32("CMB_ratestatus");
if (Settings.Instance["CMB_raterc"] != null)
CurrentState.ratercbackup = Settings.Instance.GetInt32("CMB_raterc");
if (Settings.Instance["CMB_ratesensors"] != null)
CurrentState.ratesensorsbackup = Settings.Instance.GetInt32("CMB_ratesensors");
// make sure rates propogate
MainV2.comPort.MAV.cs.ResetInternals();
if (Settings.Instance["speechenable"] != null)
MainV2.speechEnable = Settings.Instance.GetBoolean("speechenable");
if (Settings.Instance["analyticsoptout"] != null)
MissionPlanner.Utilities.Tracking.OptOut = Settings.Instance.GetBoolean("analyticsoptout");
try
{
if (Settings.Instance["TXT_homelat"] != null)
MainV2.comPort.MAV.cs.HomeLocation.Lat = Settings.Instance.GetDouble("TXT_homelat");
if (Settings.Instance["TXT_homelng"] != null)
MainV2.comPort.MAV.cs.HomeLocation.Lng = Settings.Instance.GetDouble("TXT_homelng");
if (Settings.Instance["TXT_homealt"] != null)
MainV2.comPort.MAV.cs.HomeLocation.Alt = Settings.Instance.GetDouble("TXT_homealt");
// remove invalid entrys
if (Math.Abs(MainV2.comPort.MAV.cs.HomeLocation.Lat) > 90 ||
Math.Abs(MainV2.comPort.MAV.cs.HomeLocation.Lng) > 180)
MainV2.comPort.MAV.cs.HomeLocation = new PointLatLngAlt();
}
catch
{
}
}
catch
{
}
if (CurrentState.rateattitudebackup == 0) // initilised to 10, configured above from save
{
CustomMessageBox.Show(
"NOTE: your attitude rate is 0, the hud will not work\nChange in Configuration > Planner > Telemetry Rates");
}
// create log dir if it doesnt exist
try
{
if (!Directory.Exists(Settings.Instance.LogDir))
Directory.CreateDirectory(Settings.Instance.LogDir);
}
catch (Exception ex) { log.Error(ex); }
#if !NETSTANDARD2_0
#if !NETCOREAPP2_0
Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
#endif
#endif
// make sure new enough .net framework is installed
if (!MONO)
{
Microsoft.Win32.RegistryKey installed_versions =
Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
string[] version_names = installed_versions.GetSubKeyNames();
//version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion
double Framework = Convert.ToDouble(version_names[version_names.Length - 1].Remove(0, 1),
CultureInfo.InvariantCulture);
int SP =
Convert.ToInt32(installed_versions.OpenSubKey(version_names[version_names.Length - 1])
.GetValue("SP", 0));
if (Framework < 4.0)
{
CustomMessageBox.Show("This program requires .NET Framework 4.0. You currently have " + Framework);
}
}
if (Program.IconFile != null)
{
this.Icon = Icon.FromHandle(((Bitmap)Program.IconFile).GetHicon());
}
MenuArduPilot.Image = new Bitmap(Properties.Resources._0d92fed790a3a70170e61a86db103f399a595c70, (int)(200), 31);
MenuArduPilot.Width = MenuArduPilot.Image.Width;
if (Program.Logo2 != null)
MenuArduPilot.Image = Program.Logo2;
Application.DoEvents();
Comports.Add(comPort);
MainV2.comPort.MavChanged += comPort_MavChanged;
// save config to test we have write access
SaveConfig();
}
void cmb_sysid_Click(object sender, EventArgs e)
{
MainV2._connectionControl.UpdateSysIDS();
}
void comPort_MavChanged(object sender, EventArgs e)
{
log.Info("Mav Changed " + MainV2.comPort.MAV.sysid);
HUD.Custom.src = MainV2.comPort.MAV.cs;
CustomWarning.defaultsrc = MainV2.comPort.MAV.cs;
MissionPlanner.Controls.PreFlight.CheckListItem.defaultsrc = MainV2.comPort.MAV.cs;
// when uploading a firmware we dont want to reload this screen.
if (instance.MyView.current.Control != null && instance.MyView.current.Control.GetType() == typeof(GCSViews.InitialSetup))
{
var page = ((GCSViews.InitialSetup)instance.MyView.current.Control).backstageView.SelectedPage;
if (page != null && page.Text.Contains("Install Firmware"))
{
return;
}
}
if (this.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate
{
//enable the payload control page if a mavlink gimbal is detected
if (instance.FlightData != null)
{
instance.FlightData.updatePayloadTabVisible();
}
instance.MyView.Reload();
});
}
else
{
//enable the payload control page if a mavlink gimbal is detected
if (instance.FlightData != null)
{
instance.FlightData.updatePayloadTabVisible();
}
instance.MyView.Reload();
}
}
#if !NETSTANDARD2_0
#if !NETCOREAPP2_0
void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
// try prevent crash on resume
if (e.Mode == Microsoft.Win32.PowerModes.Suspend)
{
doDisconnect(MainV2.comPort);
}
}
#endif
#endif
private void BGLoadAirports(object nothing)
{
// read airport list
try
{
Utilities.Airports.ReadOurairports(Settings.GetRunningDirectory() +
"airports.csv");
Utilities.Airports.checkdups = true;
//Utilities.Airports.ReadOpenflights(Application.StartupPath + Path.DirectorySeparatorChar + "airports.dat");
log.Info("Loaded " + Utilities.Airports.GetAirportCount + " airports");
}
catch
{
}
}
public void switchicons(menuicons icons)
{
//Check if we starting
if (displayicons != null)
{
// dont update if no change
if (displayicons.GetType() == icons.GetType())
return;
}
displayicons = icons;
MainMenu.BackColor = SystemColors.MenuBar;
MainMenu.BackgroundImage = displayicons.bg;
MenuFlightData.Image = displayicons.fd;
MenuFlightPlanner.Image = displayicons.fp;
MenuInitConfig.Image = displayicons.initsetup;
MenuSimulation.Image = displayicons.sim;
MenuConfigTune.Image = displayicons.config_tuning;
MenuConnect.Image = displayicons.connect;
MenuHelp.Image = displayicons.help;
MenuFlightData.ForeColor = ThemeManager.TextColor;
MenuFlightPlanner.ForeColor = ThemeManager.TextColor;
MenuInitConfig.ForeColor = ThemeManager.TextColor;
MenuSimulation.ForeColor = ThemeManager.TextColor;
MenuConfigTune.ForeColor = ThemeManager.TextColor;
MenuConnect.ForeColor = ThemeManager.TextColor;
MenuHelp.ForeColor = ThemeManager.TextColor;
}
void adsb_UpdatePlanePosition(object sender, MissionPlanner.Utilities.adsb.PointLatLngAltHdg adsb)
{
lock (adsblock)
{
var id = adsb.Tag;
if (MainV2.instance.adsbPlanes.ContainsKey(id))
{
// update existing
((adsb.PointLatLngAltHdg)instance.adsbPlanes[id]).Lat = adsb.Lat;
((adsb.PointLatLngAltHdg)instance.adsbPlanes[id]).Lng = adsb.Lng;
((adsb.PointLatLngAltHdg)instance.adsbPlanes[id]).Alt = adsb.Alt;
((adsb.PointLatLngAltHdg)instance.adsbPlanes[id]).Heading = adsb.Heading;
((adsb.PointLatLngAltHdg)instance.adsbPlanes[id]).Time = DateTime.Now;
((adsb.PointLatLngAltHdg)instance.adsbPlanes[id]).CallSign = adsb.CallSign;
((adsb.PointLatLngAltHdg)instance.adsbPlanes[id]).Raw = adsb.Raw;
}
else
{
// create new plane
MainV2.instance.adsbPlanes[id] =
new adsb.PointLatLngAltHdg(adsb.Lat, adsb.Lng,
adsb.Alt, adsb.Heading, adsb.Speed, id,
DateTime.Now)
{ CallSign = adsb.CallSign, Raw = adsb.Raw };
}
try
{
// dont rebroadcast something that came from the drone
if (sender != null && sender is MAVLinkInterface)
return;
MAVLink.mavlink_adsb_vehicle_t packet = new MAVLink.mavlink_adsb_vehicle_t();
packet.altitude = (int)(MainV2.instance.adsbPlanes[id].Alt * 1000);
packet.altitude_type = (byte)MAVLink.ADSB_ALTITUDE_TYPE.GEOMETRIC;
packet.callsign = adsb.CallSign.ToCharArray();
packet.emitter_type = (byte)MAVLink.ADSB_EMITTER_TYPE.NO_INFO;
packet.heading = (ushort)(MainV2.instance.adsbPlanes[id].Heading * 100);
packet.lat = (int)(MainV2.instance.adsbPlanes[id].Lat * 1e7);
packet.lon = (int)(MainV2.instance.adsbPlanes[id].Lng * 1e7);
packet.ICAO_address = uint.Parse(id, NumberStyles.HexNumber);
packet.flags = (ushort)(MAVLink.ADSB_FLAGS.VALID_ALTITUDE | MAVLink.ADSB_FLAGS.VALID_COORDS |
MAVLink.ADSB_FLAGS.VALID_HEADING | MAVLink.ADSB_FLAGS.VALID_CALLSIGN);
//send to current connected
MainV2.comPort.sendPacket(packet, MainV2.comPort.MAV.sysid, MainV2.comPort.MAV.compid);
}
catch
{
}
}
}
private void ResetConnectionStats()
{
log.Info("Reset connection stats");
// If the form has been closed, or never shown before, we need do nothing, as
// connection stats will be reset when shown
if (this.connectionStatsForm != null && connectionStatsForm.Visible)
{
// else the form is already showing. reset the stats
this.connectionStatsForm.Controls.Clear();
_connectionStats = new ConnectionStats(comPort);
this.connectionStatsForm.Controls.Add(_connectionStats);
ThemeManager.ApplyThemeTo(this.connectionStatsForm);
}
}
private void ShowConnectionStatsForm()
{
if (this.connectionStatsForm == null || this.connectionStatsForm.IsDisposed)
{
// If the form has been closed, or never shown before, we need all new stuff
this.connectionStatsForm = new Form
{
Width = 430,
Height = 180,
MaximizeBox = false,
MinimizeBox = false,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = Strings.LinkStats
};
// Change the connection stats control, so that when/if the connection stats form is showing,
// there will be something to see
this.connectionStatsForm.Controls.Clear();
_connectionStats = new ConnectionStats(comPort);
this.connectionStatsForm.Controls.Add(_connectionStats);
this.connectionStatsForm.Width = _connectionStats.Width;
}
this.connectionStatsForm.Show();
ThemeManager.ApplyThemeTo(this.connectionStatsForm);
}
private void CMB_serialport_Click(object sender, EventArgs e)
{
string oldport = _connectionControl.CMB_serialport.Text;
PopulateSerialportList();
if (_connectionControl.CMB_serialport.Items.Contains(oldport))
_connectionControl.CMB_serialport.Text = oldport;
}
private void PopulateSerialportList()
{
_connectionControl.CMB_serialport.Items.Clear();
_connectionControl.CMB_serialport.Items.Add("AUTO");
_connectionControl.CMB_serialport.Items.AddRange(SerialPort.GetPortNames());
_connectionControl.CMB_serialport.Items.Add("TCP");
_connectionControl.CMB_serialport.Items.Add("UDP");
_connectionControl.CMB_serialport.Items.Add("UDPCl");
_connectionControl.CMB_serialport.Items.Add("WS");
}
private void MenuFlightData_Click(object sender, EventArgs e)
{
MyView.ShowScreen("FlightData");
}
private void MenuFlightPlanner_Click(object sender, EventArgs e)
{
MyView.ShowScreen("FlightPlanner");
}
public void MenuSetup_Click(object sender, EventArgs e)
{
if (Settings.Instance.GetBoolean("password_protect") == false)
{
MyView.ShowScreen("HWConfig");
}
else
{
var pw = "";
if (InputBox.Show("Enter Password", "Please enter your password", ref pw, true) ==
System.Windows.Forms.DialogResult.OK)
{
bool ans = Password.ValidatePassword(pw);
if (ans == false)
{
CustomMessageBox.Show("Bad Password", "Bad Password");
}
}
if (Password.VerifyPassword(pw))
{
MyView.ShowScreen("HWConfig");
}
}
}
private void MenuSimulation_Click(object sender, EventArgs e)
{
MyView.ShowScreen("Simulation");
}
private void MenuTuning_Click(object sender, EventArgs e)
{
if (Settings.Instance.GetBoolean("password_protect") == false)
{
MyView.ShowScreen("SWConfig");
}
else
{
var pw = "";
if (InputBox.Show("Enter Password", "Please enter your password", ref pw, true) ==
System.Windows.Forms.DialogResult.OK)
{
bool ans = Password.ValidatePassword(pw);
if (ans == false)
{
CustomMessageBox.Show("Bad Password", "Bad Password");
}
}
if (Password.VerifyPassword(pw))
{
MyView.ShowScreen("SWConfig");
}
}
}
private void MenuTerminal_Click(object sender, EventArgs e)
{
MyView.ShowScreen("Terminal");
}
public void doDisconnect(MAVLinkInterface comPort)
{
log.Info("We are disconnecting");
try
{
if (speechEngine != null) // cancel all pending speech
speechEngine.SpeakAsyncCancelAll();
comPort.BaseStream.DtrEnable = false;
comPort.Close();
}
catch (Exception ex)
{
log.Error(ex);
}
// now that we have closed the connection, cancel the connection stats
// so that the 'time connected' etc does not grow, but the user can still
// look at the now frozen stats on the still open form
try
{
// if terminal is used, then closed using this button.... exception
if (this.connectionStatsForm != null)
((ConnectionStats)this.connectionStatsForm.Controls[0]).StopUpdates();
}
catch
{
}
// refresh config window if needed
if (MyView.current != null)
{
if (MyView.current.Name == "HWConfig")
MyView.ShowScreen("HWConfig");
if (MyView.current.Name == "SWConfig")
MyView.ShowScreen("SWConfig");
}
try
{
System.Threading.ThreadPool.QueueUserWorkItem((WaitCallback)delegate
{
try
{
MissionPlanner.Log.LogSort.SortLogs(Directory.GetFiles(Settings.Instance.LogDir, "*.tlog"));
}
catch
{
}
}
);
}
catch
{
}
this.MenuConnect.Image = global::MissionPlanner.Properties.Resources.light_connect_icon;
}
public void doConnect(MAVLinkInterface comPort, string portname, string baud, bool getparams = true)
{
bool skipconnectcheck = false;
log.Info("We are connecting to " + portname + " " + baud);
switch (portname)
{
case "preset":
skipconnectcheck = true;
if (comPort.BaseStream is TcpSerial)
_connectionControl.CMB_serialport.Text = "TCP";
if (comPort.BaseStream is UdpSerial)
_connectionControl.CMB_serialport.Text = "UDP";
if (comPort.BaseStream is UdpSerialConnect)
_connectionControl.CMB_serialport.Text = "UDPCl";
if (comPort.BaseStream is SerialPort)
{
_connectionControl.CMB_serialport.Text = comPort.BaseStream.PortName;
_connectionControl.CMB_baudrate.Text = comPort.BaseStream.BaudRate.ToString();
}
break;
case "TCP":
comPort.BaseStream = new TcpSerial();
_connectionControl.CMB_serialport.Text = "TCP";
break;
case "UDP":
comPort.BaseStream = new UdpSerial();
_connectionControl.CMB_serialport.Text = "UDP";
break;
case "WS":
comPort.BaseStream = new WebSocket();
_connectionControl.CMB_serialport.Text = "WS";
break;
case "UDPCl":
comPort.BaseStream = new UdpSerialConnect();
_connectionControl.CMB_serialport.Text = "UDPCl";
break;
case "AUTO":
// do autoscan
Comms.CommsSerialScan.Scan(true);
DateTime deadline = DateTime.Now.AddSeconds(50);
while (Comms.CommsSerialScan.foundport == false || Comms.CommsSerialScan.run == 1)
{
System.Threading.Thread.Sleep(500);
Console.WriteLine("wait for port " + CommsSerialScan.foundport + " or " + CommsSerialScan.run);
if (DateTime.Now > deadline)
{
CustomMessageBox.Show(Strings.Timeout);
_connectionControl.IsConnected(false);
return;
}
}
return;
default:
comPort.BaseStream = new SerialPort();
break;
}
// Tell the connection UI that we are now connected.
_connectionControl.IsConnected(true);
// Here we want to reset the connection stats counter etc.
this.ResetConnectionStats();
comPort.MAV.cs.ResetInternals();
//cleanup any log being played
comPort.logreadmode = false;
if (comPort.logplaybackfile != null)
comPort.logplaybackfile.Close();
comPort.logplaybackfile = null;
try
{
log.Info("Set Portname");
// set port, then options
if (portname.ToLower() != "preset")
comPort.BaseStream.PortName = portname;
log.Info("Set Baudrate");
try
{
if (baud != "" && baud != "0")
comPort.BaseStream.BaudRate = int.Parse(baud);
}
catch (Exception exp)
{
log.Error(exp);
}
// prevent serialreader from doing anything
comPort.giveComport = true;
log.Info("About to do dtr if needed");
// reset on connect logic.
if (Settings.Instance.GetBoolean("CHK_resetapmonconnect") == true)
{
log.Info("set dtr rts to false");
comPort.BaseStream.DtrEnable = false;
comPort.BaseStream.RtsEnable = false;
comPort.BaseStream.toggleDTR();
}
comPort.giveComport = false;
// setup to record new logs
try
{
Directory.CreateDirectory(Settings.Instance.LogDir);
lock (this)
{
// create log names
var dt = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
var tlog = Settings.Instance.LogDir + Path.DirectorySeparatorChar +
dt + ".tlog";
var rlog = Settings.Instance.LogDir + Path.DirectorySeparatorChar +
dt + ".rlog";
// check if this logname already exists
int a = 1;
while (File.Exists(tlog))
{
Thread.Sleep(1000);
// create new names with a as an index
dt = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "-" + a.ToString();
tlog = Settings.Instance.LogDir + Path.DirectorySeparatorChar +
dt + ".tlog";
rlog = Settings.Instance.LogDir + Path.DirectorySeparatorChar +
dt + ".rlog";
}
//open the logs for writing
comPort.logfile =
new BufferedStream(File.Open(tlog, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None));
comPort.rawlogfile =
new BufferedStream(File.Open(rlog, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None));
log.Info("creating logfile " + dt + ".tlog");
}
}
catch (Exception exp2)
{
log.Error(exp2);
CustomMessageBox.Show(Strings.Failclog);
} // soft fail
// reset connect time - for timeout functions
connecttime = DateTime.Now;
// do the connect
comPort.Open(false, skipconnectcheck);
if (!comPort.BaseStream.IsOpen)
{
log.Info("comport is closed. existing connect");
try
{
_connectionControl.IsConnected(false);
UpdateConnectIcon();
comPort.Close();
}
catch
{
}
return;
}
comPort.getParamList();//comPort.MAV.sysid, comPort.MAV.compid);
_connectionControl.UpdateSysIDS();
// detect firmware we are conected to.
if (comPort.MAV.cs.firmware == Firmwares.ArduCopter2)
{
_connectionControl.TOOL_APMFirmware.SelectedIndex =
_connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.ArduCopter2);
}
else if (comPort.MAV.cs.firmware == Firmwares.Ateryx)
{
_connectionControl.TOOL_APMFirmware.SelectedIndex =
_connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.Ateryx);
}
else if (comPort.MAV.cs.firmware == Firmwares.ArduRover)
{
_connectionControl.TOOL_APMFirmware.SelectedIndex =
_connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.ArduRover);
}
else if (comPort.MAV.cs.firmware == Firmwares.ArduSub)
{
_connectionControl.TOOL_APMFirmware.SelectedIndex =
_connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.ArduSub);
}
else if (comPort.MAV.cs.firmware == Firmwares.ArduPlane)
{
_connectionControl.TOOL_APMFirmware.SelectedIndex =
_connectionControl.TOOL_APMFirmware.Items.IndexOf(Firmwares.ArduPlane);
}
// check for newer firmware
var softwares = Firmware.LoadSoftwares();
if (softwares.Count > 0)
{
try
{
string[] fields1 = comPort.MAV.VersionString.Split(' ');
foreach (Firmware.software item in softwares)
{
string[] fields2 = item.name.Split(' ');
// check primare firmware type. ie arudplane, arducopter
if (fields1[0] == fields2[0])
{
Version ver1 = VersionDetection.GetVersion(comPort.MAV.VersionString);
Version ver2 = VersionDetection.GetVersion(item.name);
if (ver2 > ver1)
{
Common.MessageShowAgain(Strings.NewFirmware + "-" + item.name,
Strings.NewFirmwareA + item.name + Strings.Pleaseup +
"[link;https://discuss.ardupilot.org/tags/stable-release;Release Notes]");
break;
}
// check the first hit only
break;
}
}
}
catch (Exception ex)
{
log.Error(ex);
}
}
FlightData.CheckBatteryShow();
/*
MissionPlanner.Utilities.Tracking.AddEvent("Connect", "Connect", comPort.MAV.cs.firmware.ToString(),
comPort.MAV.param.Count.ToString());
MissionPlanner.Utilities.Tracking.AddTiming("Connect", "Connect Time",
(DateTime.Now - connecttime).TotalMilliseconds, "");
MissionPlanner.Utilities.Tracking.AddEvent("Connect", "Baud", comPort.BaseStream.BaudRate.ToString(), "");
if(comPort.MAV.param.ContainsKey("SPRAY_ENABLE"))
MissionPlanner.Utilities.Tracking.AddEvent("Param", "Value", "SPRAY_ENABLE",comPort.MAV.param["SPRAY_ENABLE"].ToString());
if (comPort.MAV.param.ContainsKey("CHUTE_ENABLE"))
MissionPlanner.Utilities.Tracking.AddEvent("Param", "Value", "CHUTE_ENABLE", comPort.MAV.param["CHUTE_ENABLE"].ToString());
if (comPort.MAV.param.ContainsKey("TERRAIN_ENABLE"))
MissionPlanner.Utilities.Tracking.AddEvent("Param", "Value", "TERRAIN_ENABLE", comPort.MAV.param["TERRAIN_ENABLE"].ToString());
if (comPort.MAV.param.ContainsKey("ADSB_ENABLE"))
MissionPlanner.Utilities.Tracking.AddEvent("Param", "Value", "ADSB_ENABLE", comPort.MAV.param["ADSB_ENABLE"].ToString());
if (comPort.MAV.param.ContainsKey("AVD_ENABLE"))
MissionPlanner.Utilities.Tracking.AddEvent("Param", "Value", "AVD_ENABLE", comPort.MAV.param["AVD_ENABLE"].ToString());
*/
// save the baudrate for this port
Settings.Instance[_connectionControl.CMB_serialport.Text + "_BAUD"] = _connectionControl.CMB_baudrate.Text;
this.Text = titlebar + " " + comPort.MAV.VersionString;
// refresh config window if needed
if (MyView.current != null)
{
if (MyView.current.Name == "HWConfig")
MyView.ShowScreen("HWConfig");
if (MyView.current.Name == "SWConfig")
MyView.ShowScreen("SWConfig");
}
// load wps on connect option.
if (Settings.Instance.GetBoolean("loadwpsonconnect") == true)
{
// only do it if we are connected.
if (comPort.BaseStream.IsOpen)
{
MenuFlightPlanner_Click(null, null);
FlightPlanner.BUT_read_Click(null, null);
}
}
// get any rallypoints
if (MainV2.comPort.MAV.param.ContainsKey("RALLY_TOTAL") &&
int.Parse(MainV2.comPort.MAV.param["RALLY_TOTAL"].ToString()) > 0)
{
try
{
FlightPlanner.getRallyPointsToolStripMenuItem_Click(null, null);
double maxdist = 0;
foreach (var rally in comPort.MAV.rallypoints)
{
foreach (var rally1 in comPort.MAV.rallypoints)
{
var pnt1 = new PointLatLngAlt(rally.Value.y / 10000000.0f, rally.Value.x / 10000000.0f);
var pnt2 = new PointLatLngAlt(rally1.Value.y / 10000000.0f,
rally1.Value.x / 10000000.0f);
var dist = pnt1.GetDistance(pnt2);
maxdist = Math.Max(maxdist, dist);
}
}
if (comPort.MAV.param.ContainsKey("RALLY_LIMIT_KM") &&
(maxdist / 1000.0) > (float)comPort.MAV.param["RALLY_LIMIT_KM"])
{
CustomMessageBox.Show(Strings.Warningrallypointdistance + " " +
(maxdist / 1000.0).ToString("0.00") + " > " +
(float)comPort.MAV.param["RALLY_LIMIT_KM"]);
}
}
catch (Exception ex) { log.Warn(ex); }
}
// get any fences
if (MainV2.comPort.MAV.param.ContainsKey("FENCE_TOTAL") &&
int.Parse(MainV2.comPort.MAV.param["FENCE_TOTAL"].ToString()) > 1 &&
MainV2.comPort.MAV.param.ContainsKey("FENCE_ACTION"))
{
try
{
FlightPlanner.GeoFencedownloadToolStripMenuItem_Click(null, null);
}
catch (Exception ex) { log.Warn(ex); }
}
//Add HUD custom items source
HUD.Custom.src = MainV2.comPort.MAV.cs;
// set connected icon
this.MenuConnect.Image = displayicons.disconnect;
}
catch (Exception ex)
{
log.Warn(ex);
try
{
_connectionControl.IsConnected(false);
UpdateConnectIcon();
comPort.Close();
}
catch (Exception ex2)
{
log.Warn(ex2);
}
CustomMessageBox.Show("Can not establish a connection\n\n" + ex.Message);
return;
}
}
private void MenuConnect_Click(object sender, EventArgs e)
{
Connect();
}
private void Connect()
{
comPort.giveComport = false;
log.Info("MenuConnect Start");
// sanity check
if (comPort.BaseStream.IsOpen && comPort.MAV.cs.groundspeed > 4)
{
if ((int)DialogResult.No ==
CustomMessageBox.Show(Strings.Stillmoving, Strings.Disconnect, MessageBoxButtons.YesNo))
{
return;
}
}
try
{
log.Info("Cleanup last logfiles");
// cleanup from any previous sessions
if (comPort.logfile != null)
comPort.logfile.Close();
if (comPort.rawlogfile != null)
comPort.rawlogfile.Close();
}
catch (Exception ex)
{
CustomMessageBox.Show(Strings.ErrorClosingLogFile + ex.Message, Strings.ERROR);
}
comPort.logfile = null;
comPort.rawlogfile = null;
// decide if this is a connect or disconnect
if (comPort.BaseStream.IsOpen)
{
doDisconnect(comPort);
}
else
{
doConnect(comPort, _connectionControl.CMB_serialport.Text, _connectionControl.CMB_baudrate.Text);
}
_connectionControl.UpdateSysIDS();
if (comPort.BaseStream.IsOpen)
loadph_serial();
}
void loadph_serial()
{
try
{
if (comPort.MAV.SerialString == "")
return;
if (comPort.MAV.SerialString.Contains("CubeBlack") && !comPort.MAV.SerialString.Contains("CubeBlack+") &&
comPort.MAV.param.ContainsKey("INS_ACC3_ID") && comPort.MAV.param["INS_ACC3_ID"].Value == 0 &&
comPort.MAV.param.ContainsKey("INS_GYR3_ID") && comPort.MAV.param["INS_GYR3_ID"].Value == 0 &&
comPort.MAV.param.ContainsKey("INS_ENABLE_MASK") && comPort.MAV.param["INS_ENABLE_MASK"].Value >= 7)
{
MissionPlanner.Controls.SB.Show("Param Scan");
}
}
catch { }
try
{
if (comPort.MAV.SerialString == "")
return;
// brd type should be 3
// devids show which sensor is not detected
// baro does not list a devid
//devop read spi lsm9ds0_ext_am 0 0 0x8f 1
if (comPort.MAV.SerialString.Contains("CubeBlack") && !comPort.MAV.SerialString.Contains("CubeBlack+"))
{
Task.Run(() =>
{
bool bad1 = false;
byte[] data = new byte[0];
comPort.device_op(comPort.MAV.sysid, comPort.MAV.compid, out data,
MAVLink.DEVICE_OP_BUSTYPE.SPI,
"lsm9ds0_ext_g", 0, 0, 0x8f, 1);
if (data.Length != 0 && (data[0] != 0xd4 && data[0] != 0xd7))
bad1 = true;
comPort.device_op(comPort.MAV.sysid, comPort.MAV.compid, out data,
MAVLink.DEVICE_OP_BUSTYPE.SPI,
"lsm9ds0_ext_am", 0, 0, 0x8f, 1);
if (data.Length != 0 && data[0] != 0x49)
bad1 = true;
if (bad1)
this.BeginInvoke(method: (Action)delegate
{
MissionPlanner.Controls.SB.Show("SPI Scan");
});
});
}
}
catch { }
}
private void CMB_serialport_SelectedIndexChanged(object sender, EventArgs e)
{
if (_connectionControl.CMB_serialport.SelectedItem == _connectionControl.CMB_serialport.Text)
return;
comPortName = _connectionControl.CMB_serialport.Text;
if (comPortName == "UDP" || comPortName == "UDPCl" || comPortName == "TCP" || comPortName == "AUTO")
{
_connectionControl.CMB_baudrate.Enabled = false;
}
else
{
_connectionControl.CMB_baudrate.Enabled = true;
}
try
{
// check for saved baud rate and restore
if (Settings.Instance[_connectionControl.CMB_serialport.Text + "_BAUD"] != null)
{
_connectionControl.CMB_baudrate.Text =
Settings.Instance[_connectionControl.CMB_serialport.Text + "_BAUD"];
}
}
catch
{
}
}
/// <summary>
/// overriding the OnCLosing is a bit cleaner than handling the event, since it
/// is this object.
///
/// This happens before FormClosed
/// </summary>
/// <param name="e"></param>
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
log.Info("MainV2_FormClosing");
log.Info("GMaps write cache");
// speed up tile saving on exit
GMap.NET.GMaps.Instance.CacheOnIdleRead = false;
GMap.NET.GMaps.Instance.BoostCacheEngine = true;
Settings.Instance["MainHeight"] = this.Height.ToString();
Settings.Instance["MainWidth"] = this.Width.ToString();
Settings.Instance["MainMaximised"] = this.WindowState.ToString();
Settings.Instance["MainLocX"] = this.Location.X.ToString();
Settings.Instance["MainLocY"] = this.Location.Y.ToString();
log.Info("close logs");
AltitudeAngel.Dispose();
// close bases connection
try
{
comPort.logreadmode = false;
if (comPort.logfile != null)
comPort.logfile.Close();
if (comPort.rawlogfile != null)
comPort.rawlogfile.Close();
comPort.logfile = null;
comPort.rawlogfile = null;
}
catch
{
}
log.Info("close ports");
// close all connections
foreach (var port in Comports)
{
try
{
port.logreadmode = false;
if (port.logfile != null)
port.logfile.Close();
if (port.rawlogfile != null)
port.rawlogfile.Close();
port.logfile = null;
port.rawlogfile = null;
}
catch
{
}
}
log.Info("stop adsb");
Utilities.adsb.Stop();
log.Info("stop WarningEngine");
Warnings.WarningEngine.Stop();
log.Info("stop GStreamer");
GStreamer.StopAll();
log.Info("closing vlcrender");
try
{
while (vlcrender.store.Count > 0)
vlcrender.store[0].Stop();
}
catch
{
}
log.Info("closing pluginthread");
pluginthreadrun = false;
if (pluginthread != null)
pluginthread.Join();
log.Info("closing serialthread");
serialThread = false;
if (serialreaderthread != null)
serialreaderthread.Join();
log.Info("closing joystickthread");
joystickthreadrun = false;
if (joystickthread != null)
joystickthread.Join();
log.Info("closing httpthread");
// if we are waiting on a socket we need to force an abort
httpserver.Stop();
log.Info("sorting tlogs");
try
{
System.Threading.ThreadPool.QueueUserWorkItem((WaitCallback)delegate
{
try
{
MissionPlanner.Log.LogSort.SortLogs(Directory.GetFiles(Settings.Instance.LogDir, "*.tlog"));
}
catch
{
}
}
);
}
catch
{
}
log.Info("closing MyView");
// close all tabs
MyView.Dispose();
log.Info("closing fd");
try
{
FlightData.Dispose();
}
catch
{
}
log.Info("closing fp");
try
{
FlightPlanner.Dispose();
}
catch
{
}
log.Info("closing sim");
try
{
Simulation.Dispose();
}
catch
{
}
try
{
if (comPort.BaseStream.IsOpen)
comPort.Close();
}
catch
{
} // i get alot of these errors, the port is still open, but not valid - user has unpluged usb
// save config
SaveConfig();
Console.WriteLine(httpthread?.IsAlive);
Console.WriteLine(joystickthread?.IsAlive);
Console.WriteLine(serialreaderthread?.IsAlive);
Console.WriteLine(pluginthread?.IsAlive);
log.Info("MainV2_FormClosing done");
if (MONO)
this.Dispose();
}
/// <summary>
/// this happens after FormClosing...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
Console.WriteLine("MainV2_FormClosed");
if (joystick != null)
{
while (!joysendThreadExited)
Thread.Sleep(10);
joystick.Dispose(); //proper clean up of joystick.
}
}
private void LoadConfig()
{
try
{
log.Info("Loading config");
Settings.Instance.Load();
comPortName = Settings.Instance.ComPort;
}
catch (Exception ex)
{
log.Error("Bad Config File", ex);
}
}
private void SaveConfig()
{
try
{
log.Info("Saving config");
Settings.Instance.ComPort = comPortName;
if (_connectionControl != null)
Settings.Instance.BaudRate = _connectionControl.CMB_baudrate.Text;
Settings.Instance.APMFirmware = MainV2.comPort.MAV.cs.firmware.ToString();
Settings.Instance.Save();
}
catch (Exception ex)
{
CustomMessageBox.Show(ex.ToString());
}
}
/// <summary>
/// needs to be true by default so that exits properly if no joystick used.
/// </summary>
volatile private bool joysendThreadExited = true;
/// <summary>
/// thread used to send joystick packets to the MAV
/// </summary>
private void joysticksend()
{
float rate = 50; // 1000 / 50 = 20 hz
int count = 0;
DateTime lastratechange = DateTime.Now;
joystickthreadrun = true;
while (joystickthreadrun)
{
joysendThreadExited = false;
//so we know this thread is stil alive.
try
{
if (MONO)
{
log.Error("Mono: closing joystick thread");
break;
}
if (!MONO)
{
//joystick stuff
if (joystick != null && joystick.enabled)
{
if (!joystick.manual_control)
{
MAVLink.mavlink_rc_channels_override_t rc = new MAVLink.mavlink_rc_channels_override_t();
rc.target_component = comPort.MAV.compid;
rc.target_system = comPort.MAV.sysid;
if (joystick.getJoystickAxis(1) == Joystick.Joystick.joystickaxis.None) rc.chan1_raw = ushort.MaxValue;
if (joystick.getJoystickAxis(2) == Joystick.Joystick.joystickaxis.None) rc.chan2_raw = ushort.MaxValue;
if (joystick.getJoystickAxis(3) == Joystick.Joystick.joystickaxis.None) rc.chan3_raw = ushort.MaxValue;
if (joystick.getJoystickAxis(4) == Joystick.Joystick.joystickaxis.None) rc.chan4_raw = ushort.MaxValue;
if (joystick.getJoystickAxis(5) == Joystick.Joystick.joystickaxis.None) rc.chan5_raw = ushort.MaxValue;
if (joystick.getJoystickAxis(6) == Joystick.Joystick.joystickaxis.None) rc.chan6_raw = ushort.MaxValue;
if (joystick.getJoystickAxis(7) == Joystick.Joystick.joystickaxis.None) rc.chan7_raw = ushort.MaxValue;
if (joystick.getJoystickAxis(8) == Joystick.Joystick.joystickaxis.None) rc.chan8_raw = ushort.MaxValue;
if (joystick.getJoystickAxis(9) == Joystick.Joystick.joystickaxis.None) rc.chan9_raw = (ushort)0;
if (joystick.getJoystickAxis(10) == Joystick.Joystick.joystickaxis.None) rc.chan10_raw = (ushort)0;
if (joystick.getJoystickAxis(11) == Joystick.Joystick.joystickaxis.None) rc.chan11_raw = (ushort)0;
if (joystick.getJoystickAxis(12) == Joystick.Joystick.joystickaxis.None) rc.chan12_raw = (ushort)0;
if (joystick.getJoystickAxis(13) == Joystick.Joystick.joystickaxis.None) rc.chan13_raw = (ushort)0;
if (joystick.getJoystickAxis(14) == Joystick.Joystick.joystickaxis.None) rc.chan14_raw = (ushort)0;
if (joystick.getJoystickAxis(15) == Joystick.Joystick.joystickaxis.None) rc.chan15_raw = (ushort)0;
if (joystick.getJoystickAxis(16) == Joystick.Joystick.joystickaxis.None) rc.chan16_raw = (ushort)0;
if (joystick.getJoystickAxis(17) == Joystick.Joystick.joystickaxis.None) rc.chan17_raw = (ushort)0;
if (joystick.getJoystickAxis(18) == Joystick.Joystick.joystickaxis.None) rc.chan18_raw = (ushort)0;
if (joystick.getJoystickAxis(1) != Joystick.Joystick.joystickaxis.None) rc.chan1_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech1;
if (joystick.getJoystickAxis(2) != Joystick.Joystick.joystickaxis.None) rc.chan2_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech2;
if (joystick.getJoystickAxis(3) != Joystick.Joystick.joystickaxis.None) rc.chan3_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech3;
if (joystick.getJoystickAxis(4) != Joystick.Joystick.joystickaxis.None) rc.chan4_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech4;
if (joystick.getJoystickAxis(5) != Joystick.Joystick.joystickaxis.None) rc.chan5_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech5;
if (joystick.getJoystickAxis(6) != Joystick.Joystick.joystickaxis.None) rc.chan6_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech6;
if (joystick.getJoystickAxis(7) != Joystick.Joystick.joystickaxis.None) rc.chan7_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech7;
if (joystick.getJoystickAxis(8) != Joystick.Joystick.joystickaxis.None) rc.chan8_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech8;
if (joystick.getJoystickAxis(9) != Joystick.Joystick.joystickaxis.None) rc.chan9_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech9;
if (joystick.getJoystickAxis(10) != Joystick.Joystick.joystickaxis.None) rc.chan10_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech10;
if (joystick.getJoystickAxis(11) != Joystick.Joystick.joystickaxis.None) rc.chan11_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech11;
if (joystick.getJoystickAxis(12) != Joystick.Joystick.joystickaxis.None) rc.chan12_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech12;
if (joystick.getJoystickAxis(13) != Joystick.Joystick.joystickaxis.None) rc.chan13_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech13;
if (joystick.getJoystickAxis(14) != Joystick.Joystick.joystickaxis.None) rc.chan14_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech14;
if (joystick.getJoystickAxis(15) != Joystick.Joystick.joystickaxis.None) rc.chan15_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech15;
if (joystick.getJoystickAxis(16) != Joystick.Joystick.joystickaxis.None) rc.chan16_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech16;
if (joystick.getJoystickAxis(17) != Joystick.Joystick.joystickaxis.None) rc.chan17_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech17;
if (joystick.getJoystickAxis(18) != Joystick.Joystick.joystickaxis.None) rc.chan18_raw = (ushort)MainV2.comPort.MAV.cs.rcoverridech18;
if (lastjoystick.AddMilliseconds(rate) < DateTime.Now)
{
/*
if (MainV2.comPort.MAV.cs.rssi > 0 && MainV2.comPort.MAV.cs.remrssi > 0)
{
if (lastratechange.Second != DateTime.Now.Second)
{
if (MainV2.comPort.MAV.cs.txbuffer > 90)
{
if (rate < 20)
rate = 21;
rate--;
if (MainV2.comPort.MAV.cs.linkqualitygcs < 70)
rate = 50;
}
else
{
if (rate > 100)
rate = 100;
rate++;
}
lastratechange = DateTime.Now;
}
}
*/
// Console.WriteLine(DateTime.Now.Millisecond + " {0} {1} {2} {3} {4}", rc.chan1_raw, rc.chan2_raw, rc.chan3_raw, rc.chan4_raw,rate);
//Console.WriteLine("Joystick btw " + comPort.BaseStream.BytesToWrite);
if (!comPort.BaseStream.IsOpen)
continue;
if (comPort.BaseStream.BytesToWrite < 50)
{
if (sitl)
{
MissionPlanner.GCSViews.SITL.rcinput();
}
else
{
comPort.sendPacket(rc, rc.target_system, rc.target_component);
}
count++;
lastjoystick = DateTime.Now;
}
}
}
else
{
MAVLink.mavlink_manual_control_t rc = new MAVLink.mavlink_manual_control_t();
rc.target = comPort.MAV.compid;
if (joystick.getJoystickAxis(1) != Joystick.Joystick.joystickaxis.None)
rc.x = MainV2.comPort.MAV.cs.rcoverridech1;
if (joystick.getJoystickAxis(2) != Joystick.Joystick.joystickaxis.None)
rc.y = MainV2.comPort.MAV.cs.rcoverridech2;
if (joystick.getJoystickAxis(3) != Joystick.Joystick.joystickaxis.None)
rc.z = MainV2.comPort.MAV.cs.rcoverridech3;
if (joystick.getJoystickAxis(4) != Joystick.Joystick.joystickaxis.None)
rc.r = MainV2.comPort.MAV.cs.rcoverridech4;
if (lastjoystick.AddMilliseconds(rate) < DateTime.Now)
{
if (!comPort.BaseStream.IsOpen)
continue;
if (comPort.BaseStream.BytesToWrite < 50)
{
if (sitl)
{
MissionPlanner.GCSViews.SITL.rcinput();
}
else
{
comPort.sendPacket(rc, comPort.MAV.sysid, comPort.MAV.compid);
}
count++;
lastjoystick = DateTime.Now;
}
}
}
}
}
Thread.Sleep(20);
}
catch
{
} // cant fall out
}
joysendThreadExited = true; //so we know this thread exited.
}
/// <summary>
/// Used to fix the icon status for unexpected unplugs etc...
/// </summary>
private void UpdateConnectIcon()
{
if ((DateTime.Now - connectButtonUpdate).Milliseconds > 500)
{
// Console.WriteLine(DateTime.Now.Millisecond);
if (comPort.BaseStream.IsOpen)
{
if (this.MenuConnect.Image == null || (string)this.MenuConnect.Image.Tag != "Disconnect")
{
this.BeginInvoke((MethodInvoker)delegate
{
this.MenuConnect.Image = displayicons.disconnect;
this.MenuConnect.Image.Tag = "Disconnect";
this.MenuConnect.Text = Strings.DISCONNECTc;
_connectionControl.IsConnected(true);
});
}
}
else
{
if (this.MenuConnect.Image != null && (string)this.MenuConnect.Image.Tag != "Connect")
{
this.BeginInvoke((MethodInvoker)delegate
{
this.MenuConnect.Image = displayicons.connect;
this.MenuConnect.Image.Tag = "Connect";
this.MenuConnect.Text = Strings.CONNECTc;
_connectionControl.IsConnected(false);
if (_connectionStats != null)
{
_connectionStats.StopUpdates();
}
});
}
if (comPort.logreadmode)
{
this.BeginInvoke((MethodInvoker)delegate { _connectionControl.IsConnected(true); });
}
}
connectButtonUpdate = DateTime.Now;
}
}
ManualResetEvent PluginThreadrunner = new ManualResetEvent(false);
private void PluginThread()
{
Hashtable nextrun = new Hashtable();
pluginthreadrun = true;
PluginThreadrunner.Reset();
while (pluginthreadrun)
{
try
{
lock (Plugin.PluginLoader.Plugins)
{
foreach (var plugin in Plugin.PluginLoader.Plugins)
{
if (!nextrun.ContainsKey(plugin))
nextrun[plugin] = DateTime.MinValue;
if (DateTime.Now > plugin.NextRun)
{
// get ms till next run
int msnext = (int)(1000 / plugin.loopratehz);
// allow the plug to modify this, if needed
plugin.NextRun = DateTime.Now.AddMilliseconds(msnext);
try
{
bool ans = plugin.Loop();
}
catch (Exception ex)
{
log.Error(ex);
}
}
}
}
}
catch
{
}
// max rate is 100 hz - prevent massive cpu usage
System.Threading.Thread.Sleep(10);
}
while (Plugin.PluginLoader.Plugins.Count > 0)
{
var plugin = Plugin.PluginLoader.Plugins[0];
try
{
plugin.Exit();
}
catch (Exception ex)
{
log.Error(ex);
}
Plugin.PluginLoader.Plugins.Remove(plugin);
}
PluginThreadrunner.Set();
return;
}
ManualResetEvent SerialThreadrunner = new ManualResetEvent(false);
/// <summary>
/// main serial reader thread
/// controls
/// serial reading
/// link quality stats
/// speech voltage - custom - alt warning - data lost
/// heartbeat packet sending
///
/// and can't fall out
/// </summary>
private void SerialReader()
{
if (serialThread == true)
return;
serialThread = true;
SerialThreadrunner.Reset();
int minbytes = 10;
int altwarningmax = 0;
bool armedstatus = false;
string lastmessagehigh = "";
DateTime speechcustomtime = DateTime.Now;
DateTime speechlowspeedtime = DateTime.Now;
DateTime linkqualitytime = DateTime.Now;
while (serialThread)
{
try
{
Thread.Sleep(1); // was 5
try
{
if (GCSViews.ConfigTerminal.comPort is MAVLinkSerialPort)
{
}
else
{
if (GCSViews.ConfigTerminal.comPort != null && GCSViews.ConfigTerminal.comPort.IsOpen)
continue;
}
}
catch (Exception ex)
{
log.Error(ex);
}
// update connect/disconnect button and info stats
try
{
UpdateConnectIcon();
}
catch (Exception ex)
{
log.Error(ex);
}
// 30 seconds interval speech options
if (speechEnable && speechEngine != null && (DateTime.Now - speechcustomtime).TotalSeconds > 30 &&
(MainV2.comPort.logreadmode || comPort.BaseStream.IsOpen))
{
if (MainV2.speechEngine.IsReady)
{
if (Settings.Instance.GetBoolean("speechcustomenabled"))
{
MainV2.speechEngine.SpeakAsync(ArduPilot.Common.speechConversion(comPort.MAV, "" + Settings.Instance["speechcustom"]));
}
speechcustomtime = DateTime.Now;
}
// speech for battery alerts
//speechbatteryvolt
float warnvolt = Settings.Instance.GetFloat("speechbatteryvolt");
float warnpercent = Settings.Instance.GetFloat("speechbatterypercent");
if (Settings.Instance.GetBoolean("speechbatteryenabled") == true &&
MainV2.comPort.MAV.cs.battery_voltage <= warnvolt &&
MainV2.comPort.MAV.cs.battery_voltage >= 5.0)
{
if (MainV2.speechEngine.IsReady)
{
MainV2.speechEngine.SpeakAsync(ArduPilot.Common.speechConversion(comPort.MAV, "" + Settings.Instance["speechbattery"]));
}
}
else if (Settings.Instance.GetBoolean("speechbatteryenabled") == true &&
(MainV2.comPort.MAV.cs.battery_remaining) < warnpercent &&
MainV2.comPort.MAV.cs.battery_voltage >= 5.0 &&
MainV2.comPort.MAV.cs.battery_remaining != 0.0)
{
if (MainV2.speechEngine.IsReady)
{
MainV2.speechEngine.SpeakAsync(
ArduPilot.Common.speechConversion(comPort.MAV, "" + Settings.Instance["speechbattery"]));
}
}
}
// speech for airspeed alerts
if (speechEnable && speechEngine != null && (DateTime.Now - speechlowspeedtime).TotalSeconds > 10 &&
(MainV2.comPort.logreadmode || comPort.BaseStream.IsOpen))
{
if (Settings.Instance.GetBoolean("speechlowspeedenabled") == true && MainV2.comPort.MAV.cs.armed)
{
float warngroundspeed = Settings.Instance.GetFloat("speechlowgroundspeedtrigger");
float warnairspeed = Settings.Instance.GetFloat("speechlowairspeedtrigger");
if (MainV2.comPort.MAV.cs.airspeed < warnairspeed)
{
if (MainV2.speechEngine.IsReady)
{
MainV2.speechEngine.SpeakAsync(
ArduPilot.Common.speechConversion(comPort.MAV, "" + Settings.Instance["speechlowairspeed"]));
speechlowspeedtime = DateTime.Now;
}
}
else if (MainV2.comPort.MAV.cs.groundspeed < warngroundspeed)
{
if (MainV2.speechEngine.IsReady)
{
MainV2.speechEngine.SpeakAsync(
ArduPilot.Common.speechConversion(comPort.MAV, "" + Settings.Instance["speechlowgroundspeed"]));
speechlowspeedtime = DateTime.Now;
}
}
else
{
speechlowspeedtime = DateTime.Now;
}
}
}
// speech altitude warning - message high warning
if (speechEnable && speechEngine != null &&
(MainV2.comPort.logreadmode || comPort.BaseStream.IsOpen))
{
float warnalt = float.MaxValue;
if (Settings.Instance.ContainsKey("speechaltheight"))
{
warnalt = Settings.Instance.GetFloat("speechaltheight");
}
try
{
altwarningmax = (int)Math.Max(MainV2.comPort.MAV.cs.alt, altwarningmax);
if (Settings.Instance.GetBoolean("speechaltenabled") == true && MainV2.comPort.MAV.cs.alt != 0.00 &&
(MainV2.comPort.MAV.cs.alt <= warnalt) && MainV2.comPort.MAV.cs.armed)
{
if (altwarningmax > warnalt)
{
if (MainV2.speechEngine.IsReady)
MainV2.speechEngine.SpeakAsync(
ArduPilot.Common.speechConversion(comPort.MAV, "" + Settings.Instance["speechalt"]));
}
}
}
catch
{
} // silent fail
try
{
// say the latest high priority message
if (MainV2.speechEngine.IsReady &&
lastmessagehigh != MainV2.comPort.MAV.cs.messageHigh && MainV2.comPort.MAV.cs.messageHigh != null)
{
if (!MainV2.comPort.MAV.cs.messageHigh.StartsWith("PX4v2 "))
{
MainV2.speechEngine.SpeakAsync(MainV2.comPort.MAV.cs.messageHigh);
lastmessagehigh = MainV2.comPort.MAV.cs.messageHigh;
}
}
}
catch
{
}
}
// not doing anything
if (!MainV2.comPort.logreadmode && !comPort.BaseStream.IsOpen)
{
altwarningmax = 0;
}
// attenuate the link qualty over time
if ((DateTime.Now - MainV2.comPort.MAV.lastvalidpacket).TotalSeconds >= 1)
{
if (linkqualitytime.Second != DateTime.Now.Second)
{
MainV2.comPort.MAV.cs.linkqualitygcs = (ushort)(MainV2.comPort.MAV.cs.linkqualitygcs * 0.8f);
linkqualitytime = DateTime.Now;
// force redraw if there are no other packets are being read
GCSViews.FlightData.myhud.Invalidate();
}
}
// data loss warning - wait min of 10 seconds, ignore first 30 seconds of connect, repeat at 5 seconds interval
if ((DateTime.Now - MainV2.comPort.MAV.lastvalidpacket).TotalSeconds > 10
&& (DateTime.Now - connecttime).TotalSeconds > 30
&& (DateTime.Now - nodatawarning).TotalSeconds > 5
&& (MainV2.comPort.logreadmode || comPort.BaseStream.IsOpen)
&& MainV2.comPort.MAV.cs.armed)
{
if (speechEnable && speechEngine != null)
{
if (MainV2.speechEngine.IsReady)
{
MainV2.speechEngine.SpeakAsync("WARNING No Data for " +
(int)
(DateTime.Now - MainV2.comPort.MAV.lastvalidpacket)
.TotalSeconds + " Seconds");
nodatawarning = DateTime.Now;
}
}
}
// get home point on armed status change.
if (armedstatus != MainV2.comPort.MAV.cs.armed && comPort.BaseStream.IsOpen)
{
armedstatus = MainV2.comPort.MAV.cs.armed;
// status just changed to armed
if (MainV2.comPort.MAV.cs.armed == true &&
MainV2.comPort.MAV.apname != MAVLink.MAV_AUTOPILOT.INVALID &&
MainV2.comPort.MAV.aptype != MAVLink.MAV_TYPE.GIMBAL)
{
System.Threading.ThreadPool.QueueUserWorkItem(state =>
{
Thread.CurrentThread.Name = "Arm State change";
try
{
while (comPort.giveComport == true)
Thread.Sleep(100);
MainV2.comPort.MAV.cs.HomeLocation = new PointLatLngAlt(MainV2.comPort.getWP(0));
if (MyView.current != null && MyView.current.Name == "FlightPlanner")
{
// update home if we are on flight data tab
this.BeginInvoke((Action)delegate { FlightPlanner.updateHome(); });
}
}
catch
{
// dont hang this loop
this.BeginInvoke(
(Action)
delegate
{
CustomMessageBox.Show("Failed to update home location (" +
MainV2.comPort.MAV.sysid + ")");
});
}
});
}
if (speechEnable && speechEngine != null)
{
if (Settings.Instance.GetBoolean("speecharmenabled"))
{
string speech = armedstatus ? Settings.Instance["speecharm"] : Settings.Instance["speechdisarm"];
if (!string.IsNullOrEmpty(speech))
{
MainV2.speechEngine.SpeakAsync(ArduPilot.Common.speechConversion(comPort.MAV, speech));
}
}
}
}
if (comPort.MAV.param.TotalReceived < comPort.MAV.param.TotalReported)
{
if (comPort.MAV.param.TotalReported > 0 && comPort.BaseStream.IsOpen)
instance.status1.Percent =
(comPort.MAV.param.TotalReceived / (double)comPort.MAV.param.TotalReported) * 100.0;
}
// send a hb every seconds from gcs to ap
if (heatbeatSend.Second != DateTime.Now.Second)
{
MAVLink.mavlink_heartbeat_t htb = new MAVLink.mavlink_heartbeat_t()
{
type = (byte)MAVLink.MAV_TYPE.GCS,
autopilot = (byte)MAVLink.MAV_AUTOPILOT.INVALID,
mavlink_version = 3 // MAVLink.MAVLINK_VERSION
};
// enumerate each link
foreach (var port in Comports.ToArray())
{
if (!port.BaseStream.IsOpen)
continue;
// poll for params at heartbeat interval - primary mav on this port only
if (!port.giveComport)
{
try
{
// poll only when not armed
if (!port.MAV.cs.armed)
{
port.getParamPoll();
port.getParamPoll();
}
}
catch
{
}
}
// there are 3 hb types we can send, mavlink1, mavlink2 signed and unsigned
bool sentsigned = false;
bool sentmavlink1 = false;
bool sentmavlink2 = false;
// enumerate each mav
foreach (var MAV in port.MAVlist)
{
try
{
// poll for version if we dont have it - every mav every port
if (!port.giveComport && !MAV.cs.armed && (DateTime.Now.Second % 20) == 0 && MAV.cs.version < new Version(0, 1))
port.getVersion(MAV.sysid, MAV.compid, false);
// are we talking to a mavlink2 device
if (MAV.mavlinkv2)
{
// is signing enabled
if (MAV.signing)
{
// check if we have already sent
if (sentsigned)
continue;
sentsigned = true;
}
else
{
// check if we have already sent
if (sentmavlink2)
continue;
sentmavlink2 = true;
}
}
else
{
// check if we have already sent
if (sentmavlink1)
continue;
sentmavlink1 = true;
}
port.sendPacket(htb, MAV.sysid, MAV.compid);
}
catch (Exception ex)
{
log.Error(ex);
// close the bad port
try
{
port.Close();
}
catch
{
}
// refresh the screen if needed
if (port == MainV2.comPort)
{
// refresh config window if needed
if (MyView.current != null)
{
this.BeginInvoke((MethodInvoker)delegate ()
{
if (MyView.current.Name == "HWConfig")
MyView.ShowScreen("HWConfig");
if (MyView.current.Name == "SWConfig")
MyView.ShowScreen("SWConfig");
});
}
}
}
}
}
heatbeatSend = DateTime.Now;
}
// if not connected or busy, sleep and loop
if (!comPort.BaseStream.IsOpen || comPort.giveComport == true)
{
if (!comPort.BaseStream.IsOpen)
{
// check if other ports are still open
foreach (var port in Comports)
{
if (port.BaseStream.IsOpen)
{
Console.WriteLine("Main comport shut, swapping to other mav");
comPort = port;
break;
}
}
}
System.Threading.Thread.Sleep(100);
}
// read the interfaces
foreach (var port in Comports.ToArray())
{
if (!port.BaseStream.IsOpen)
{
// skip primary interface
if (port == comPort)
continue;
// modify array and drop out
Comports.Remove(port);
port.Dispose();
break;
}
DateTime startread = DateTime.Now;
// must be open, we have bytes, we are not yielding the port,
// the thread is meant to be running and we only spend 1 seconds max in this read loop
while (port.BaseStream.IsOpen && port.BaseStream.BytesToRead > minbytes &&
port.giveComport == false && serialThread && startread.AddSeconds(1) > DateTime.Now)
{
try
{
port.readPacket();
}
catch (Exception ex)
{
log.Error(ex);
}
}
// update currentstate of sysids on the port
foreach (var MAV in port.MAVlist)
{
try
{
MAV.cs.UpdateCurrentSettings(null, false, port, MAV);
}
catch (Exception ex)
{
log.Error(ex);
}
}
}
}
catch (Exception e)
{
Tracking.AddException(e);
log.Error("Serial Reader fail :" + e.ToString());
try
{
comPort.Close();
}
catch (Exception ex)
{
log.Error(ex);
}
}
}
Console.WriteLine("SerialReader Done");
SerialThreadrunner.Set();
}
protected override void OnLoad(EventArgs e)
{
// check if its defined, and force to show it if not known about
if (Settings.Instance["menu_autohide"] == null)
{
Settings.Instance["menu_autohide"] = "false";
}
try
{
AutoHideMenu(Settings.Instance.GetBoolean("menu_autohide"));
}
catch
{
}
MyView.AddScreen(new MainSwitcher.Screen("FlightData", FlightData, true));
MyView.AddScreen(new MainSwitcher.Screen("FlightPlanner", FlightPlanner, true));
MyView.AddScreen(new MainSwitcher.Screen("HWConfig", typeof(GCSViews.InitialSetup), false));
MyView.AddScreen(new MainSwitcher.Screen("SWConfig", typeof(GCSViews.SoftwareConfig), false));
MyView.AddScreen(new MainSwitcher.Screen("Simulation", Simulation, true));
MyView.AddScreen(new MainSwitcher.Screen("Help", typeof(GCSViews.Help), false));
// hide simulation under mono
if (Program.MONO)
{
MenuSimulation.Visible = false;
}
try
{
if (Control.ModifierKeys == Keys.Shift)
{
}
else
{
log.Info("Load Pluggins");
Plugin.PluginLoader.DisabledPluginNames.Clear();
foreach (var s in Settings.Instance.GetList("DisabledPlugins")) Plugin.PluginLoader.DisabledPluginNames.Add(s);
Plugin.PluginLoader.LoadAll();
log.Info("Load Pluggins... Done");
}
}
catch (Exception ex)
{
log.Error(ex);
}
if (Program.Logo != null && Program.name == "VVVVZ")
{
this.PerformLayout();
MenuFlightPlanner_Click(this, e);
MainMenu_ItemClicked(this, new ToolStripItemClickedEventArgs(MenuFlightPlanner));
}
else
{
this.PerformLayout();
log.Info("show FlightData");
MenuFlightData_Click(this, e);
log.Info("show FlightData... Done");
MainMenu_ItemClicked(this, new ToolStripItemClickedEventArgs(MenuFlightData));
}
// for long running tasks using own threads.
// for short use threadpool
this.SuspendLayout();
// setup http server
try
{
log.Info("start http");
httpthread = new Thread(new httpserver().listernforclients)
{
Name = "motion jpg stream-network kml",
IsBackground = true
};
httpthread.Start();
}
catch (Exception ex)
{
log.Error("Error starting TCP listener thread: ", ex);
CustomMessageBox.Show(ex.ToString());
}
log.Info("start joystick");
// setup joystick packet sender
joystickthread = new Thread(new ThreadStart(joysticksend))
{
IsBackground = true,
Priority = ThreadPriority.AboveNormal,
Name = "Main joystick sender"
};
joystickthread.Start();
log.Info("start serialreader");
// setup main serial reader
serialreaderthread = new Thread(SerialReader)
{
IsBackground = true,
Name = "Main Serial reader",
Priority = ThreadPriority.AboveNormal
};
serialreaderthread.Start();
log.Info("start plugin thread");
// setup main plugin thread
pluginthread = new Thread(PluginThread)
{
IsBackground = true,
Name = "plugin runner thread",
Priority = ThreadPriority.BelowNormal
};
pluginthread.Start();
ThreadPool.QueueUserWorkItem(LoadGDALImages);
ThreadPool.QueueUserWorkItem(BGLoadAirports);
ThreadPool.QueueUserWorkItem(BGCreateMaps);
//ThreadPool.QueueUserWorkItem(BGGetAlmanac);
ThreadPool.QueueUserWorkItem(BGgetTFR);
ThreadPool.QueueUserWorkItem(BGNoFly);
ThreadPool.QueueUserWorkItem(BGGetKIndex);
// update firmware version list - only once per day
ThreadPool.QueueUserWorkItem(BGFirmwareCheck);
log.Info("start AutoConnect");
AutoConnect.NewMavlinkConnection += (sender, serial) =>
{
try
{
log.Info("AutoConnect.NewMavlinkConnection " + serial.PortName);
MainV2.instance.BeginInvoke((Action)delegate
{
if (MainV2.comPort.BaseStream.IsOpen)
{
var mav = new MAVLinkInterface();
mav.BaseStream = serial;
MainV2.instance.doConnect(mav, "preset", serial.PortName);
MainV2.Comports.Add(mav);
}
else
{
MainV2.comPort.BaseStream = serial;
MainV2.instance.doConnect(MainV2.comPort, "preset", serial.PortName);
}
});
}
catch (Exception ex) { log.Error(ex); }
};
AutoConnect.NewVideoStream += (sender, gststring) =>
{
try
{
log.Info("AutoConnect.NewVideoStream " + gststring);
GStreamer.gstlaunch = GStreamer.LookForGstreamer();
if (!File.Exists(GStreamer.gstlaunch))
{
if (CustomMessageBox.Show(
"A video stream has been detected, but gstreamer has not been configured/installed.\nDo you want to install/config it now?",
"GStreamer", System.Windows.Forms.MessageBoxButtons.YesNo) ==
(int)System.Windows.Forms.DialogResult.Yes)
{
{
ProgressReporterDialogue prd = new ProgressReporterDialogue();
ThemeManager.ApplyThemeTo(prd);
prd.DoWork += sender2 =>
{
GStreamer.DownloadGStreamer(((i, s) =>
{
prd.UpdateProgressAndStatus(i, s);
if (prd.doWorkArgs.CancelRequested) throw new Exception("User Request");
}));
};
prd.RunBackgroundOperationAsync();
GStreamer.gstlaunch = GStreamer.LookForGstreamer();
}
if (!File.Exists(GStreamer.gstlaunch))
{
return;
}
}
else
{
return;
}
}
GStreamer.StartA(gststring);
}
catch (Exception ex)
{
log.Error(ex);
}
};
AutoConnect.Start();
BinaryLog.onFlightMode += (firmware, modeno) =>
{
try
{
if (firmware == "")
return null;
var modes = ArduPilot.Common.getModesList((Firmwares)Enum.Parse(typeof(Firmwares), firmware));
string currentmode = null;
foreach (var mode in modes)
{
if (mode.Key == modeno)
{
currentmode = mode.Value;
break;
}
}
return currentmode;
}
catch
{
return null;
}
};
GStreamer.onNewImage += (sender, image) =>
{
if (image == null)
{
GCSViews.FlightData.myhud.bgimage = null;
return;
}
var bmp = (image as Drawing.Bitmap);
if (bmp == null)
return;
var old = GCSViews.FlightData.myhud.bgimage;
GCSViews.FlightData.myhud.bgimage = new Bitmap(image.Width, image.Height, 4 * image.Width,
PixelFormat.Format32bppPArgb,
bmp.LockBits(Rectangle.Empty, null, SKColorType.Bgra8888)
.Scan0);
if (old != null)
old.Dispose();
};
vlcrender.onNewImage += (sender, image) =>
{
if (image == null)
{
GCSViews.FlightData.myhud.bgimage = null;
return;
}
var bmp = (image as Drawing.Bitmap);
if (bmp == null)
return;
var old = GCSViews.FlightData.myhud.bgimage;
GCSViews.FlightData.myhud.bgimage = new Bitmap(image.Width, image.Height, 4 * image.Width, PixelFormat.Format32bppPArgb,
bmp.LockBits(Rectangle.Empty, null, SKColorType.Bgra8888).Scan0);
if (old != null)
old.Dispose();
};
CaptureMJPEG.onNewImage += (sender, image) =>
{
if (image == null)
{
GCSViews.FlightData.myhud.bgimage = null;
return;
}
var bmp = (image as Drawing.Bitmap);
if (bmp == null)
return;
var old = GCSViews.FlightData.myhud.bgimage;
GCSViews.FlightData.myhud.bgimage = new Bitmap(image.Width, image.Height, 4 * image.Width, PixelFormat.Format32bppPArgb,
bmp.LockBits(Rectangle.Empty, null, SKColorType.Bgra8888).Scan0);
if (old != null)
old.Dispose();
};
//ZeroConf.EnumerateAllServicesFromAllHosts();
//ZeroConf.ProbeForRTSP();
CommsSerialScan.doConnect += port =>
{
if (MainV2.instance.InvokeRequired)
{
log.Info("CommsSerialScan.doConnect invoke");
MainV2.instance.BeginInvoke(
(Action)delegate ()
{
MAVLinkInterface mav = new MAVLinkInterface();
mav.BaseStream = port;
MainV2.instance.doConnect(mav, "preset", "0");
MainV2.Comports.Add(mav);
});
}
else
{
log.Info("CommsSerialScan.doConnect NO invoke");
MAVLinkInterface mav = new MAVLinkInterface();
mav.BaseStream = port;
MainV2.instance.doConnect(mav, "preset", "0");
MainV2.Comports.Add(mav);
}
};
try
{
if (!MONO)
{
log.Info("Load AltitudeAngel");
AltitudeAngel.Configure();
AltitudeAngel.Initialize();
log.Info("Load AltitudeAngel... Done");
}
}
catch (TypeInitializationException) // windows xp lacking patch level
{
//CustomMessageBox.Show("Please update your .net version. kb2468871");
}
catch (Exception ex)
{
Tracking.AddException(ex);
}
this.ResumeLayout();
Program.Splash?.Close();
log.Info("appload time");
MissionPlanner.Utilities.Tracking.AddTiming("AppLoad", "Load Time",
(DateTime.Now - Program.starttime).TotalMilliseconds, "");
int p = (int) Environment.OSVersion.Platform;
bool isWin = (p != 4) && (p != 6) && (p != 128);
bool winXp = isWin && Environment.OSVersion.Version.Major == 5;
if (winXp)
{
Common.MessageShowAgain("Windows XP",
"This is the last version that will support Windows XP, please update your OS");
// invalidate update url
System.Configuration.ConfigurationManager.AppSettings["UpdateLocationVersion"] =
"https://firmware.ardupilot.org/MissionPlanner/xp/";
System.Configuration.ConfigurationManager.AppSettings["UpdateLocation"] =
"https://firmware.ardupilot.org/MissionPlanner/xp/";
System.Configuration.ConfigurationManager.AppSettings["UpdateLocationMD5"] =
"https://firmware.ardupilot.org/MissionPlanner/xp/checksums.txt";
System.Configuration.ConfigurationManager.AppSettings["BetaUpdateLocationVersion"] = "";
}
try
{
// single update check per day - in a seperate thread
if (Settings.Instance["update_check"] != DateTime.Now.ToShortDateString())
{
System.Threading.ThreadPool.QueueUserWorkItem(checkupdate);
Settings.Instance["update_check"] = DateTime.Now.ToShortDateString();
}
else if (Settings.Instance.GetBoolean("beta_updates") == true)
{
MissionPlanner.Utilities.Update.dobeta = true;
System.Threading.ThreadPool.QueueUserWorkItem(checkupdate);
}
}
catch (Exception ex)
{
log.Error("Update check failed", ex);
}
// play a tlog that was passed to the program/ load a bin log passed
if (Program.args.Length > 0)
{
var cmds = ProcessCommandLine(Program.args);
if (cmds.ContainsKey("file") && File.Exists(cmds["file"]) && cmds["file"].ToLower().EndsWith(".tlog"))
{
FlightData.LoadLogFile(Program.args[0]);
FlightData.BUT_playlog_Click(null, null);
}
else if (cmds.ContainsKey("file") && File.Exists(cmds["file"]) &&
(cmds["file"].ToLower().EndsWith(".log") || cmds["file"].ToLower().EndsWith(".bin")))
{
LogBrowse logbrowse = new LogBrowse();
ThemeManager.ApplyThemeTo(logbrowse);
logbrowse.logfilename = Program.args[0];
logbrowse.Show(this);
logbrowse.BringToFront();
}
if (cmds.ContainsKey("script") && File.Exists(cmds["script"]))
{
// invoke for after onload finished
this.BeginInvoke((Action)delegate ()
{
try
{
FlightData.selectedscript = cmds["script"];
FlightData.BUT_run_script_Click(null, null);
}
catch (Exception ex)
{
CustomMessageBox.Show("Start script failed: " + ex.ToString(), Strings.ERROR);
}
});
}
if (cmds.ContainsKey("joy") && cmds.ContainsKey("type"))
{
if (cmds["type"].ToLower() == "plane")
{
MainV2.comPort.MAV.cs.firmware = Firmwares.ArduPlane;
}
else if (cmds["type"].ToLower() == "copter")
{
MainV2.comPort.MAV.cs.firmware = Firmwares.ArduCopter2;
}
else if (cmds["type"].ToLower() == "rover")
{
MainV2.comPort.MAV.cs.firmware = Firmwares.ArduRover;
}
else if (cmds["type"].ToLower() == "sub")
{
MainV2.comPort.MAV.cs.firmware = Firmwares.ArduSub;
}
var joy = new Joystick.Joystick(() => MainV2.comPort);
if (joy.start(cmds["joy"]))
{
MainV2.joystick = joy;
MainV2.joystick.enabled = true;
}
else
{
CustomMessageBox.Show("Failed to start joystick");
}
}
if (cmds.ContainsKey("rtk"))
{
var inject = new ConfigSerialInjectGPS();
if (cmds["rtk"].ToLower().Contains("http"))
{
inject.CMB_serialport.Text = "NTRIP";
var nt = new CommsNTRIP();
ConfigSerialInjectGPS.comPort = nt;
Task.Run(() =>
{
try
{
nt.Open(cmds["rtk"]);
nt.lat = MainV2.comPort.MAV.cs.HomeLocation.Lat;
nt.lng = MainV2.comPort.MAV.cs.HomeLocation.Lng;
nt.alt = MainV2.comPort.MAV.cs.HomeLocation.Alt;
this.BeginInvokeIfRequired(() => { inject.DoConnect(); });
}
catch (Exception ex)
{
this.BeginInvokeIfRequired(() => { CustomMessageBox.Show(ex.ToString()); });
}
});
}
}
if (cmds.ContainsKey("cam"))
{
try
{
MainV2.cam = new WebCamService.Capture(int.Parse(cmds["cam"]), null);
MainV2.cam.Start();
}
catch (Exception ex)
{
CustomMessageBox.Show(ex.ToString());
}
}
if (cmds.ContainsKey("gstream"))
{
GStreamer.gstlaunch = GStreamer.LookForGstreamer();
if (!File.Exists(GStreamer.gstlaunch))
{
if (CustomMessageBox.Show(
"A video stream has been detected, but gstreamer has not been configured/installed.\nDo you want to install/config it now?",
"GStreamer", System.Windows.Forms.MessageBoxButtons.YesNo) ==
(int)System.Windows.Forms.DialogResult.Yes)
{
GStreamerUI.DownloadGStreamer();
}
}
try
{
new Thread(delegate ()
{
// 36 retrys
for (int i = 0; i < 36; i++)
{
try
{
var st = GStreamer.StartA(cmds["gstream"]);
if (st == null)
{
// prevent spam
Thread.Sleep(5000);
}
else
{
while (st.IsAlive)
{
Thread.Sleep(1000);
}
}
}
catch (BadImageFormatException ex)
{
// not running on x64
log.Error(ex);
return;
}
catch (DllNotFoundException ex)
{
// missing or failed download
log.Error(ex);
return;
}
}
})
{ IsBackground = true, Name = "Gstreamer cli" }.Start();
}
catch (Exception ex)
{
log.Error(ex);
}
}
if (cmds.ContainsKey("port") && cmds.ContainsKey("baud"))
{
_connectionControl.CMB_serialport.Text = cmds["port"];
_connectionControl.CMB_baudrate.Text = cmds["baud"];
doConnect(MainV2.comPort, cmds["port"], cmds["baud"]);
}
}
GMapMarkerBase.length = Settings.Instance.GetInt32("GMapMarkerBase_length", 500);
GMapMarkerBase.DisplayCOG = Settings.Instance.GetBoolean("GMapMarkerBase_DisplayCOG", true);
GMapMarkerBase.DisplayHeading = Settings.Instance.GetBoolean("GMapMarkerBase_DisplayHeading", true);
GMapMarkerBase.DisplayNavBearing = Settings.Instance.GetBoolean("GMapMarkerBase_DisplayNavBearing", true);
GMapMarkerBase.DisplayRadius = Settings.Instance.GetBoolean("GMapMarkerBase_DisplayRadius", true);
GMapMarkerBase.DisplayTarget = Settings.Instance.GetBoolean("GMapMarkerBase_DisplayTarget", true);
}
public void LoadGDALImages(object nothing)
{
if (Settings.Instance.ContainsKey("GDALImageDir"))
{
try
{
GDAL.GDAL.ScanDirectory(Settings.Instance["GDALImageDir"]);
}
catch (Exception ex)
{
log.Error(ex);
}
}
}
private Dictionary<string, string> ProcessCommandLine(string[] args)
{
Dictionary<string, string> cmdargs = new Dictionary<string, string>();
string cmd = "";
foreach (var s in args)
{
if (s.StartsWith("-") || s.StartsWith("/") || s.StartsWith("--"))
{
cmd = s.TrimStart(new char[] { '-', '/', '-' }).TrimStart(new char[] { '-', '/', '-' });
continue;
}
if (cmd != "")
{
cmdargs[cmd] = s;
log.Info("ProcessCommandLine: " + cmd + " = " + s);
cmd = "";
continue;
}
if (File.Exists(s))
{
// we are not a command, and the file exists.
cmdargs["file"] = s;
log.Info("ProcessCommandLine: " + "file" + " = " + s);
continue;
}
log.Info("ProcessCommandLine: UnKnown = " + s);
}
return cmdargs;
}
private void BGFirmwareCheck(object state)
{
try
{
if (Settings.Instance["fw_check"] != DateTime.Now.ToShortDateString())
{
var fw = new Firmware();
var list = fw.getFWList();
if (list.Count > 1)
Firmware.SaveSoftwares(new Firmware.optionsObject() { softwares = list });
Settings.Instance["fw_check"] = DateTime.Now.ToShortDateString();
}
}
catch (Exception ex)
{
log.Error(ex);
}
}
private void BGGetKIndex(object state)
{
try
{
// check the last kindex date
if (Settings.Instance["kindexdate"] == DateTime.Now.ToShortDateString())
{
KIndex_KIndex(Settings.Instance.GetInt32("kindex"), null);
}
else
{
// get a new kindex
KIndex.KIndexEvent += KIndex_KIndex;
KIndex.GetKIndex();
Settings.Instance["kindexdate"] = DateTime.Now.ToShortDateString();
}
}
catch (Exception ex)
{
log.Error(ex);
}
}
private void BGgetTFR(object state)
{
try
{
tfr.tfrcache = Settings.GetUserDataDirectory() + "tfr.xml";
tfr.GetTFRs();
}
catch (Exception ex)
{
log.Error(ex);
}
}
private void BGNoFly(object state)
{
try
{
NoFly.NoFly.Scan();
}
catch (Exception ex)
{
log.Error(ex);
}
}
void KIndex_KIndex(object sender, EventArgs e)
{
CurrentState.KIndexstatic = (int)sender;
Settings.Instance["kindex"] = CurrentState.KIndexstatic.ToString();
}
private void BGCreateMaps(object state)
{
// sort logs
try
{
MissionPlanner.Log.LogSort.SortLogs(Directory.GetFiles(Settings.Instance.LogDir, "*.tlog"));
MissionPlanner.Log.LogSort.SortLogs(Directory.GetFiles(Settings.Instance.LogDir, "*.rlog"));
}
catch (Exception ex)
{
log.Error(ex);
}
try
{
// create maps
Log.LogMap.MapLogs(Directory.GetFiles(Settings.Instance.LogDir, "*.tlog", SearchOption.AllDirectories));
Log.LogMap.MapLogs(Directory.GetFiles(Settings.Instance.LogDir, "*.bin", SearchOption.AllDirectories));
Log.LogMap.MapLogs(Directory.GetFiles(Settings.Instance.LogDir, "*.log", SearchOption.AllDirectories));
if (File.Exists(tlogThumbnailHandler.tlogThumbnailHandler.queuefile))
{
Log.LogMap.MapLogs(File.ReadAllLines(tlogThumbnailHandler.tlogThumbnailHandler.queuefile));
File.Delete(tlogThumbnailHandler.tlogThumbnailHandler.queuefile);
}
}
catch (Exception ex)
{
log.Error(ex);
}
try
{
if (File.Exists(tlogThumbnailHandler.tlogThumbnailHandler.queuefile))
{
Log.LogMap.MapLogs(File.ReadAllLines(tlogThumbnailHandler.tlogThumbnailHandler.queuefile));
File.Delete(tlogThumbnailHandler.tlogThumbnailHandler.queuefile);
}
}
catch (Exception ex)
{
log.Error(ex);
}
}
private void checkupdate(object stuff)
{
if (Program.WindowsStoreApp)
return;
try
{
MissionPlanner.Utilities.Update.CheckForUpdate();
}
catch (Exception ex)
{
log.Error("Update check failed", ex);
}
}
private void MainV2_Resize(object sender, EventArgs e)
{
// mono - resize is called before the control is created
if (MyView != null)
log.Info("myview width " + MyView.Width + " height " + MyView.Height);
log.Info("this width " + this.Width + " height " + this.Height);
}
private void MenuHelp_Click(object sender, EventArgs e)
{
MyView.ShowScreen("Help");
}
/// <summary>
/// keyboard shortcuts override
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (GCSViews.ConfigTerminal.SSHTerminal) { return false; }
if (keyData == Keys.F12)
{
MenuConnect_Click(null, null);
return true;
}
if (keyData == Keys.F2)
{
MenuFlightData_Click(null, null);
return true;
}
if (keyData == Keys.F3)
{
MenuFlightPlanner_Click(null, null);
return true;
}
if (keyData == Keys.F4)
{
MenuTuning_Click(null, null);
return true;
}
if (keyData == Keys.F5)
{
comPort.getParamList();
MyView.ShowScreen(MyView.current.Name);
return true;
}
if (keyData == (Keys.Control | Keys.F)) // temp
{
Form frm = new temp();
ThemeManager.ApplyThemeTo(frm);
frm.Show();
return true;
}
/*if (keyData == (Keys.Control | Keys.S)) // screenshot
{
ScreenShot();
return true;
}*/
if (keyData == (Keys.Control | Keys.P))
{
new PluginUI().Show();
return true;
}
if (keyData == (Keys.Control | Keys.G)) // nmea out
{
Form frm = new SerialOutputNMEA();
ThemeManager.ApplyThemeTo(frm);
frm.Show();
return true;
}
if (keyData == (Keys.Control | Keys.X))
{
new GMAPCache().ShowUserControl();
return true;
}
if (keyData == (Keys.Control | Keys.L)) // limits
{
new DigitalSkyUI().ShowUserControl();
return true;
}
if (keyData == (Keys.Control | Keys.W)) // test ac config
{
new PropagationSettings().Show();
return true;
}
if (keyData == (Keys.Control | Keys.Z))
{
//ScanHW.Scan(comPort);
new Camera().test(MainV2.comPort.MAV);
return true;
}
if (keyData == (Keys.Control | Keys.T)) // for override connect
{
try
{
MainV2.comPort.Open(false);
}
catch (Exception ex)
{
CustomMessageBox.Show(ex.ToString());
}
return true;
}
if (keyData == (Keys.Control | Keys.Y)) // for ryan beall and ollyw42
{
// write
try
{
MainV2.comPort.doCommand((byte)MainV2.comPort.sysidcurrent, (byte)MainV2.comPort.compidcurrent, MAVLink.MAV_CMD.PREFLIGHT_STORAGE, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
}
catch
{
CustomMessageBox.Show("Invalid command");
return true;
}
//read
///////MainV2.comPort.doCommand(MAVLink09.MAV_CMD.PREFLIGHT_STORAGE, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
CustomMessageBox.Show("Done MAV_ACTION_STORAGE_WRITE");
return true;
}
if (keyData == (Keys.Control | Keys.J))
{
new DevopsUI().ShowUserControl();
return true;
}
if (ProcessCmdKeyCallback != null)
{
return ProcessCmdKeyCallback(ref msg, keyData);
}
return base.ProcessCmdKey(ref msg, keyData);
}
public delegate bool ProcessCmdKeyHandler(ref Message msg, Keys keyData);
public event ProcessCmdKeyHandler ProcessCmdKeyCallback;
public void changelanguage(CultureInfo ci)
{
log.Info("change lang to " + ci.ToString() + " current " + Thread.CurrentThread.CurrentUICulture.ToString());
if (ci != null && !Thread.CurrentThread.CurrentUICulture.Equals(ci))
{
Thread.CurrentThread.CurrentUICulture = ci;
Settings.Instance["language"] = ci.Name;
//System.Threading.Thread.CurrentThread.CurrentCulture = ci;
HashSet<Control> views = new HashSet<Control> { this, FlightData, FlightPlanner, Simulation };
foreach (Control view in MyView.Controls)
views.Add(view);
foreach (Control view in views)
{
if (view != null)
{
ComponentResourceManager rm = new ComponentResourceManager(view.GetType());
foreach (Control ctrl in view.Controls)
{
rm.ApplyResource(ctrl);
}
rm.ApplyResources(view, "$this");
}
}
}
}
public void ChangeUnits()
{
try
{
// dist
if (Settings.Instance["distunits"] != null)
{
switch (
(distances)Enum.Parse(typeof(distances), Settings.Instance["distunits"].ToString()))
{
case distances.Meters:
CurrentState.multiplierdist = 1;
CurrentState.DistanceUnit = "m";
break;
case distances.Feet:
CurrentState.multiplierdist = 3.2808399f;
CurrentState.DistanceUnit = "ft";
break;
}
}
else
{
CurrentState.multiplierdist = 1;
CurrentState.DistanceUnit = "m";
}
// alt
if (Settings.Instance["altunits"] != null)
{
switch (
(distances)Enum.Parse(typeof(altitudes), Settings.Instance["altunits"].ToString()))
{
case distances.Meters:
CurrentState.multiplieralt = 1;
CurrentState.AltUnit = "m";
break;
case distances.Feet:
CurrentState.multiplieralt = 3.2808399f;
CurrentState.AltUnit = "ft";
break;
}
}
else
{
CurrentState.multiplieralt = 1;
CurrentState.AltUnit = "m";
}
// speed
if (Settings.Instance["speedunits"] != null)
{
switch ((speeds)Enum.Parse(typeof(speeds), Settings.Instance["speedunits"].ToString()))
{
case speeds.meters_per_second:
CurrentState.multiplierspeed = 1;
CurrentState.SpeedUnit = "m/s";
break;
case speeds.fps:
CurrentState.multiplierspeed = 3.2808399f;
CurrentState.SpeedUnit = "fps";
break;
case speeds.kph:
CurrentState.multiplierspeed = 3.6f;
CurrentState.SpeedUnit = "kph";
break;
case speeds.mph:
CurrentState.multiplierspeed = 2.23693629f;
CurrentState.SpeedUnit = "mph";
break;
case speeds.knots:
CurrentState.multiplierspeed = 1.94384449f;
CurrentState.SpeedUnit = "kts";
break;
}
}
else
{
CurrentState.multiplierspeed = 1;
CurrentState.SpeedUnit = "m/s";
}
}
catch
{
}
}
private void CMB_baudrate_TextChanged(object sender, EventArgs e)
{
if (!int.TryParse(_connectionControl.CMB_baudrate.Text, out comPortBaud))
{
CustomMessageBox.Show(Strings.InvalidBaudRate, Strings.ERROR);
return;
}
var sb = new StringBuilder();
int baud = 0;
for (int i = 0; i < _connectionControl.CMB_baudrate.Text.Length; i++)
if (char.IsDigit(_connectionControl.CMB_baudrate.Text[i]))
{
sb.Append(_connectionControl.CMB_baudrate.Text[i]);
baud = baud * 10 + _connectionControl.CMB_baudrate.Text[i] - '0';
}
if (_connectionControl.CMB_baudrate.Text != sb.ToString())
{
_connectionControl.CMB_baudrate.Text = sb.ToString();
}
try
{
if (baud > 0 && comPort.BaseStream.BaudRate != baud)
comPort.BaseStream.BaudRate = baud;
}
catch (Exception)
{
}
}
private void MainMenu_MouseLeave(object sender, EventArgs e)
{
if (_connectionControl.PointToClient(Control.MousePosition).Y < MainMenu.Height)
return;
this.SuspendLayout();
panel1.Visible = false;
this.ResumeLayout();
}
void menu_MouseEnter(object sender, EventArgs e)
{
this.SuspendLayout();
panel1.Location = new Point(0, 0);
panel1.Width = menu.Width;
panel1.BringToFront();
panel1.Visible = true;
this.ResumeLayout();
}
private void autoHideToolStripMenuItem_Click(object sender, EventArgs e)
{
AutoHideMenu(autoHideToolStripMenuItem.Checked);
Settings.Instance["menu_autohide"] = autoHideToolStripMenuItem.Checked.ToString();
}
void AutoHideMenu(bool hide)
{
autoHideToolStripMenuItem.Checked = hide;
if (!hide)
{
this.SuspendLayout();
panel1.Dock = DockStyle.Top;
panel1.SendToBack();
panel1.Visible = true;
menu.Visible = false;
MainMenu.MouseLeave -= MainMenu_MouseLeave;
panel1.MouseLeave -= MainMenu_MouseLeave;
toolStripConnectionControl.MouseLeave -= MainMenu_MouseLeave;
this.ResumeLayout();
}
else
{
this.SuspendLayout();
panel1.Dock = DockStyle.None;
panel1.Visible = false;
MainMenu.MouseLeave += MainMenu_MouseLeave;
panel1.MouseLeave += MainMenu_MouseLeave;
toolStripConnectionControl.MouseLeave += MainMenu_MouseLeave;
menu.Visible = true;
menu.SendToBack();
this.ResumeLayout();
}
}
private void MainV2_KeyDown(object sender, KeyEventArgs e)
{
Message temp = new Message();
ProcessCmdKey(ref temp, e.KeyData);
Console.WriteLine("MainV2_KeyDown " + e.ToString());
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start(
"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=mich146%40hotmail%2ecom&lc=AU&item_name=Michael%20Oborne&no_note=0&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHostedGuest");
}
catch
{
CustomMessageBox.Show("Link open failed. check your default webpage association");
}
}
[StructLayout(LayoutKind.Sequential)]
internal class DEV_BROADCAST_HDR
{
internal Int32 dbch_size;
internal Int32 dbch_devicetype;
internal Int32 dbch_reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class DEV_BROADCAST_PORT
{
public int dbcp_size;
public int dbcp_devicetype;
public int dbcp_reserved; // MSDN say "do not use"
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)] public byte[] dbcp_name;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class DEV_BROADCAST_DEVICEINTERFACE
{
public Int32 dbcc_size;
public Int32 dbcc_devicetype;
public Int32 dbcc_reserved;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 16)]
internal Byte[]
dbcc_classguid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)] internal Byte[] dbcc_name;
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_CREATE:
try
{
DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
IntPtr devBroadcastDeviceInterfaceBuffer;
IntPtr deviceNotificationHandle = IntPtr.Zero;
Int32 size = 0;
// frmMy is the form that will receive device-change messages.
size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = size;
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devBroadcastDeviceInterface.dbcc_reserved = 0;
devBroadcastDeviceInterface.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE.ToByteArray();
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
deviceNotificationHandle = NativeMethods.RegisterDeviceNotification(this.Handle,
devBroadcastDeviceInterfaceBuffer, DEVICE_NOTIFY_WINDOW_HANDLE);
}
catch
{
}
break;
case WM_DEVICECHANGE:
// The WParam value identifies what is occurring.
WM_DEVICECHANGE_enum n = (WM_DEVICECHANGE_enum)m.WParam;
var l = m.LParam;
if (n == WM_DEVICECHANGE_enum.DBT_DEVICEREMOVEPENDING)
{
Console.WriteLine("DBT_DEVICEREMOVEPENDING");
}
if (n == WM_DEVICECHANGE_enum.DBT_DEVNODES_CHANGED)
{
Console.WriteLine("DBT_DEVNODES_CHANGED");
}
if (n == WM_DEVICECHANGE_enum.DBT_DEVICEARRIVAL ||
n == WM_DEVICECHANGE_enum.DBT_DEVICEREMOVECOMPLETE)
{
Console.WriteLine(((WM_DEVICECHANGE_enum)n).ToString());
DEV_BROADCAST_HDR hdr = new DEV_BROADCAST_HDR();
Marshal.PtrToStructure(m.LParam, hdr);
try
{
switch (hdr.dbch_devicetype)
{
case DBT_DEVTYP_DEVICEINTERFACE:
DEV_BROADCAST_DEVICEINTERFACE inter = new DEV_BROADCAST_DEVICEINTERFACE();
Marshal.PtrToStructure(m.LParam, inter);
log.InfoFormat("Interface {0}",
ASCIIEncoding.Unicode.GetString(inter.dbcc_name, 0, inter.dbcc_size - (4 * 3)));
break;
case DBT_DEVTYP_PORT:
DEV_BROADCAST_PORT prt = new DEV_BROADCAST_PORT();
Marshal.PtrToStructure(m.LParam, prt);
log.InfoFormat("port {0}",
ASCIIEncoding.Unicode.GetString(prt.dbcp_name, 0, prt.dbcp_size - (4 * 3)));
break;
}
}
catch
{
}
//string port = Marshal.PtrToStringAuto((IntPtr)((long)m.LParam + 12));
//Console.WriteLine("Added port {0}",port);
}
log.InfoFormat("Device Change {0} {1} {2}", m.Msg, (WM_DEVICECHANGE_enum)m.WParam, m.LParam);
if (DeviceChanged != null)
{
try
{
DeviceChanged((WM_DEVICECHANGE_enum)m.WParam);
}
catch
{
}
}
foreach (var item in MissionPlanner.Plugin.PluginLoader.Plugins)
{
item.Host.ProcessDeviceChanged((WM_DEVICECHANGE_enum)m.WParam);
}
break;
case 0x86: // WM_NCACTIVATE
//var thing = Control.FromHandle(m.HWnd);
var child = Control.FromHandle(m.LParam);
if (child is Form)
{
log.Debug("ApplyThemeTo " + child.Name);
ThemeManager.ApplyThemeTo(child);
}
break;
default:
//Console.WriteLine(m.ToString());
break;
}
base.WndProc(ref m);
}
const int DBT_DEVTYP_PORT = 0x00000003;
const int WM_CREATE = 0x0001;
const Int32 DBT_DEVTYP_HANDLE = 6;
const Int32 DBT_DEVTYP_DEVICEINTERFACE = 5;
const Int32 DEVICE_NOTIFY_WINDOW_HANDLE = 0;
const Int32 DIGCF_PRESENT = 2;
const Int32 DIGCF_DEVICEINTERFACE = 0X10;
const Int32 WM_DEVICECHANGE = 0X219;
public static Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
public enum WM_DEVICECHANGE_enum
{
DBT_CONFIGCHANGECANCELED = 0x19,
DBT_CONFIGCHANGED = 0x18,
DBT_CUSTOMEVENT = 0x8006,
DBT_DEVICEARRIVAL = 0x8000,
DBT_DEVICEQUERYREMOVE = 0x8001,
DBT_DEVICEQUERYREMOVEFAILED = 0x8002,
DBT_DEVICEREMOVECOMPLETE = 0x8004,
DBT_DEVICEREMOVEPENDING = 0x8003,
DBT_DEVICETYPESPECIFIC = 0x8005,
DBT_DEVNODES_CHANGED = 0x7,
DBT_QUERYCHANGECONFIG = 0x17,
DBT_USERDEFINED = 0xFFFF,
}
private void MainMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
foreach (ToolStripItem item in MainMenu.Items)
{
if (e.ClickedItem == item)
{
item.BackColor = ThemeManager.ControlBGColor;
}
else
{
item.BackColor = Color.Transparent;
item.BackgroundImage = displayicons.bg; //.BackColor = Color.Black;
}
}
//MainMenu.BackColor = Color.Black;
//MainMenu.BackgroundImage = MissionPlanner.Properties.Resources.bgdark;
}
private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
{
// full screen
if (fullScreenToolStripMenuItem.Checked)
{
this.TopMost = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Normal;
this.WindowState = FormWindowState.Maximized;
}
else
{
this.TopMost = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.WindowState = FormWindowState.Maximized;
}
}
private void readonlyToolStripMenuItem_Click(object sender, EventArgs e)
{
MainV2.comPort.ReadOnly = readonlyToolStripMenuItem.Checked;
}
private void connectionOptionsToolStripMenuItem_Click(object sender, EventArgs e)
{
new ConnectionOptions().Show(this);
}
private void MenuArduPilot_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("https://ardupilot.org/?utm_source=Menu&utm_campaign=MP");
}
catch
{
CustomMessageBox.Show("Failed to open url https://ardupilot.org");
}
}
private void connectionListToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
if (File.Exists(openFileDialog.FileName))
{
var lines = File.ReadAllLines(openFileDialog.FileName);
Regex tcp = new Regex("tcp://(.*):([0-9]+)");
Regex udp = new Regex("udp://(.*):([0-9]+)");
Regex udpcl = new Regex("udpcl://(.*):([0-9]+)");
Regex serial = new Regex("serial:(.*):([0-9]+)");
ConcurrentBag<MAVLinkInterface> mavs = new ConcurrentBag<MAVLinkInterface>();
Parallel.ForEach(lines, line =>
//foreach (var line in lines)
{
try
{
MAVLinkInterface mav = new MAVLinkInterface();
if (tcp.IsMatch(line))
{
var matches = tcp.Match(line);
var tc = new TcpSerial();
tc.client = new TcpClient(matches.Groups[1].Value, int.Parse(matches.Groups[2].Value));
mav.BaseStream = tc;
}
else if (udp.IsMatch(line))
{
var matches = udp.Match(line);
var uc = new UdpSerial(new UdpClient(int.Parse(matches.Groups[2].Value)));
uc.Port = matches.Groups[2].Value;
mav.BaseStream = uc;
}
else if (udpcl.IsMatch(line))
{
var matches = udpcl.Match(line);
var udc = new UdpSerialConnect();
udc.Port = matches.Groups[2].Value;
udc.client = new UdpClient(matches.Groups[1].Value, int.Parse(matches.Groups[2].Value));
mav.BaseStream = udc;
}
else if (serial.IsMatch(line))
{
var matches = serial.Match(line);
var port = new Comms.SerialPort();
port.PortName = matches.Groups[1].Value;
port.BaudRate = int.Parse(matches.Groups[2].Value);
mav.BaseStream = port;
mav.BaseStream.Open();
}
else
{
return;
}
mavs.Add(mav);
}
catch
{
}
}
);
foreach (var mav in mavs)
{
MainV2.instance.BeginInvoke((Action)delegate
{
doConnect(mav, "preset", "0", false);
Comports.Add(mav);
});
}
}
}
}
}
1
https://gitee.com/yanhuizhang/MissionPlanner.git
git@gitee.com:yanhuizhang/MissionPlanner.git
yanhuizhang
MissionPlanner
MissionPlanner
master

搜索帮助