/// <summary>Called by the <see cref="ModuleSPU.OnFixedUpdate"/> method during the "Physics" engine phase.</summary> public void OnFixedUpdate() { if (RTCore.Instance == null) { return; } if (Vessel == null) { Vessel = SignalProcessor.Vessel; RoverComputer.SetVessel(Vessel); PIDController.SetVessel(Vessel); } // only handle onFixedUpdate if the ship is unpacked if (Vessel.packed) { return; } // Do we have a loaded configuration? if (_fcLoadedConfigs != null) { // than load Load(_fcLoadedConfigs); _fcLoadedConfigs = null; } // Re-attach periodically Vessel.OnFlyByWire -= OnFlyByWirePre; Vessel.OnFlyByWire -= OnFlyByWirePost; if (Vessel != SignalProcessor.Vessel) { SanctionedPilots.Clear(); Vessel = SignalProcessor.Vessel; RoverComputer.SetVessel(Vessel); PIDController.SetVessel(Vessel); } // set flight control. Vessel.OnFlyByWire = OnFlyByWirePre + Vessel.OnFlyByWire + OnFlyByWirePost; //Update PID loop PIDController.OnFixedUpdate(); // Send updates for Target if (Vessel == FlightGlobals.ActiveVessel && FlightGlobals.fetch.VesselTarget != LastTarget.Target) { Enqueue(TargetCommand.WithTarget(FlightGlobals.fetch.VesselTarget)); UpdateLastTarget(); } }
/// <summary>Flight Computer constructor.</summary> /// <param name="s">A signal processor (most probably a <see cref="ModuleSPU"/> instance.)</param> public FlightComputer(ISignalProcessor s) { SignalProcessor = s; Vessel = s.Vessel; SanctionedPilots = new List <Action <FlightCtrlState> >(); LastTarget = TargetCommand.WithTarget(null); var attitude = AttitudeCommand.Off(); _activeCommands[attitude.Priority] = attitude; //Use http://www.ni.com/white-paper/3782/en/ to fine-tune PIDController = new PIDController(PIDKp, PIDKi, PIDKd, 1.0, -1.0, true); PIDController.SetVessel(Vessel); GameEvents.onVesselChange.Add(OnVesselChange); GameEvents.onVesselSwitching.Add(OnVesselSwitching); GameEvents.onGameSceneSwitchRequested.Add(OnSceneSwitchRequested); RoverComputer = new RoverComputer(this, RoverPIDKp, RoverPIDKi, RoverPIDKd); RoverComputer.SetVessel(Vessel); // Add RT listeners from KSP Autopilot StockAutopilotCommand.UIreference = GameObject.FindObjectOfType <VesselAutopilotUI>(); for (var index = 0; index < StockAutopilotCommand.UIreference.modeButtons.Length; index++) { var buttonIndex = index; // prevent compiler optimisation from assigning static final index value StockAutopilotCommand.UIreference.modeButtons[index].onClick.AddListener(delegate { StockAutopilotCommand.AutopilotButtonClick(buttonIndex, this); }); // bad idea to use RemoveAllListeners() since no easy way to re-add the original stock listener to onClick } }