public MainWindow() { this.InitializeComponent(); //Listen for registration success Focus(FocusState.Keyboard); DJISDKManager.Instance.SDKRegistrationStateChanged += async(state, result) => { if (state != SDKRegistrationState.Succeeded) { var md = new MessageDialog(result.ToString()); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => await md.ShowAsync()); return; } //wait till initialization finish //use a large enough time and hope for the best await Task.Delay(1000); videoParser = new DJIVideoParser.Parser(); videoParser.Initialize(); videoParser.SetVideoDataCallack(0, 0, ReceiveDecodedData); DJISDKManager.Instance.VideoFeeder.GetPrimaryVideoFeed(0).VideoDataUpdated += OnVideoPush; await DJISDKManager.Instance.ComponentManager.GetFlightAssistantHandler(0, 0).SetObstacleAvoidanceEnabledAsync(new BoolMsg() { value = false }); await Task.Delay(5000); GimbalResetCommandMsg resetMsg = new GimbalResetCommandMsg() { value = GimbalResetCommand.UNKNOWN }; await DJISDKManager.Instance.ComponentManager.GetGimbalHandler(0, 0).ResetGimbalAsync(resetMsg); }; DJISDKManager.Instance.RegisterApp("489330473bb6ed62837e4f05"); }
private async void State_change(Object source, ElapsedEventArgs e) { /* * startTime = DateTime.Now; * endTime = DateTime.Now; * Double elapsedMillisecs = ((TimeSpan)(endTime - startTime)).TotalMilliseconds; * System.Diagnostics.Debug.WriteLine("Checking state, it has started {0}\n", elapsedMillisecs); */ //script_state++; System.Diagnostics.Debug.WriteLine("Current State: {0}", mission_state); bool destination_reached = false; switch (mission_state) { case 0: //take off { System.Diagnostics.Debug.WriteLine("Taking off..."); takeoffstarttime = DateTime.Now; var res = await DJISDKManager.Instance.ComponentManager.GetFlightControllerHandler(0, 0).StartTakeoffAsync(); mission_state = 1; break; } case 1: //roll { if (currentAltitude >= 1.1) { System.Diagnostics.Debug.WriteLine("Take off completed"); Attitude_control(); mission_state++; throttle = -0.2f; GimbalResetCommandMsg resetMsg = new GimbalResetCommandMsg() { value = GimbalResetCommand.UNKNOWN }; await DJISDKManager.Instance.ComponentManager.GetGimbalHandler(0, 0).ResetGimbalAsync(resetMsg); } else { System.Diagnostics.Debug.WriteLine("Climbing..."); } break; } case 2: { if (currentAltitude <= 1.0) { StartRecording(); Attitude_control(); StartRecording(); mission_state++; } else { System.Diagnostics.Debug.WriteLine("Going down to 1.0..."); } break; } case 3: //go to WP1 { if (myWP[1].compare(current2Dpostion) <= myWP[1].tolarence) { System.Diagnostics.Debug.WriteLine("WP1 arrived"); Attitude_control(); StopRecording(); System.Diagnostics.Debug.WriteLine("current position {0}, {1}", current2Dpostion.x, current2Dpostion.y); throttle = -0.2f; mission_state++; } else { roll = max_speed; pitch = pitchPID.get(myWP[1].offsetFromPath(myWP[0], current2Dpostion)); System.Diagnostics.Debug.WriteLine("pitch will be: {0}", pitch); //pitch = pitchPID.get(myWP[1].offsetFromPath(myWP[0], current2Dpostion)); System.Diagnostics.Debug.WriteLine("current position {0}, {1}", current2Dpostion.x, current2Dpostion.y); System.Diagnostics.Debug.WriteLine("Flying to WP1, distance = {0}", myWP[1].compare(current2Dpostion)); System.Diagnostics.Debug.WriteLine("Perpendicular distance = {0}", myWP[1].offsetFromPath(myWP[0], current2Dpostion)); } break; } case 4: { if (currentAltitude <= 0.4) { Attitude_control(); //StartRecording(); mission_state++; } else { System.Diagnostics.Debug.WriteLine("Going down to 0.4..."); } break; } case 5: //go to WP2 { if (myWP[2].compare(current2Dpostion) <= myWP[2].tolarence) { System.Diagnostics.Debug.WriteLine("WP2 arrived"); Attitude_control(); System.Diagnostics.Debug.WriteLine("current position {0}, {1}", current2Dpostion.x, current2Dpostion.y); mission_state = 16; } else { roll = -max_speed; //pitch = pitchPID.get(myWP[1].offsetFromPath(myWP[0], current2Dpostion)); System.Diagnostics.Debug.WriteLine("current position {0}, {1}", current2Dpostion.x, current2Dpostion.y); System.Diagnostics.Debug.WriteLine("Flying to WP2, distance = {0}", myWP[2].compare(current2Dpostion)); System.Diagnostics.Debug.WriteLine("Perpendicular distance = {0}", myWP[2].offsetFromPath(myWP[1], current2Dpostion)); } break; } case 16: { System.Diagnostics.Debug.WriteLine("Mission Completed. Press H to land"); break; } default: { StopRecording(); System.Diagnostics.Debug.WriteLine("Mission Inpterrupted"); break; } } //periodically adjust yaw angle yaw = yawPID.get(hold_rotation - true_north_heading); if (DJISDKManager.Instance != null) { DJISDKManager.Instance.VirtualRemoteController.UpdateJoystickValue(throttle, yaw, pitch, roll); } }
public MainPage() { this.InitializeComponent(); //Listen for registration success Focus(FocusState.Keyboard); DJISDKManager.Instance.SDKRegistrationStateChanged += async(state, result) => { if (state != SDKRegistrationState.Succeeded) { var md = new MessageDialog(result.ToString()); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => await md.ShowAsync()); return; } //wait till initialization finish //use a large enough time and hope for the best await Task.Delay(1000); videoParser = new DJIVideoParser.Parser(); videoParser.Initialize(); videoParser.SetVideoDataCallack(0, 0, ReceiveDecodedData); DJISDKManager.Instance.VideoFeeder.GetPrimaryVideoFeed(0).VideoDataUpdated += OnVideoPush; await DJISDKManager.Instance.ComponentManager.GetFlightAssistantHandler(0, 0).SetObstacleAvoidanceEnabledAsync(new BoolMsg() { value = false }); await Task.Delay(5000); GimbalResetCommandMsg resetMsg = new GimbalResetCommandMsg() { value = GimbalResetCommand.UNKNOWN }; await DJISDKManager.Instance.ComponentManager.GetGimbalHandler(0, 0).ResetGimbalAsync(resetMsg); }; DJISDKManager.Instance.RegisterApp("489330473bb6ed62837e4f05"); //Call once to initialize fields SetControlState(false); this._timer = new Stopwatch(); this._timer.Start(); var flightAssistant = DJISDKManager.Instance?.ComponentManager.GetFlightAssistantHandler(0, 0); flightAssistant?.SetVisionAssistedPositioningEnabledAsync(new BoolMsg { value = true }); flightAssistant?.SetObstacleAvoidanceEnabledAsync(new BoolMsg { value = true }); var flightController = DJISDKManager.Instance?.ComponentManager.GetFlightControllerHandler(0, 0); var attitudeUpdateTimer = new Stopwatch(); attitudeUpdateTimer.Start(); flightController.AttitudeChanged += (sender, value) => { _attitudeInterval = attitudeUpdateTimer.Elapsed; attitudeUpdateTimer.Restart(); if (value.HasValue) { _attitude = new Attitude { pitch = value.Value.pitch, roll = value.Value.roll, yaw = value.Value.yaw }; } UpdateYawCommand(); UpdateAction(); UpdateDebugPrintBoxAsync(); }; var heightUpdateTimer = new Stopwatch(); heightUpdateTimer.Start(); flightController.AltitudeChanged += (sender, value) => { _heightInterval = heightUpdateTimer.Elapsed; heightUpdateTimer.Restart(); if (value.HasValue) { _height = value.Value.value; } UpdateHeightCommand(); UpdateAction(); UpdateDebugPrintBoxAsync(); }; }