private void ControlLoop() { //_logger.Write("TimeStamp,Throttle,pitchSP,pitchPV,pitchOP"); while (_running) { long currentTime = DateTime.Now.Ticks; long deltaTime = currentTime - _previousTime; float deltaTimeFactor = (float)deltaTime / _loopSettings.LoopUnit; _previousTime = currentTime; if (deltaTime > _maxLoopTime) { _maxLoopTime = deltaTime; } if (currentTime > (_lastRadioTime + _loopSettings.RadioLoopPeriod)) { _radio.Update(); _lastRadioTime = currentTime; } if (currentTime > (_lastSensorTime + _loopSettings.SensorLoopPeriod)) { _gyro.Update(); _lastSensorTime = currentTime; } if (currentTime > (_lastControlTime + _loopSettings.ControlAlgorithmPeriod)) { _pid.Update(_radio.Axes, _gyro.Axes, deltaTimeFactor); _lastControlTime = currentTime; } if (currentTime > (_lastMotorTime + _loopSettings.MotorLoopPeriod)) { //_motors.Update(_radio.Throttle , _pid.Axes); _lastMotorTime = currentTime; } if (currentTime > (_lastTelemetryTime + _loopSettings.TelemetryLoopPeriod)) { _lastTelemetryTime = currentTime; //_logger.Write( // currentTime + "," + // _radio.Throttle + "," + // _radio.Axes.Pitch + "," + // _gyro.Axes.Pitch + "," + // _pid.Axes.Pitch); } } Debug.Print("exiting control loop"); }
public Controller(MotorMixer mixer, AxesController pid, Gyro gyro, Radio radio, ControllerLoopSettings loopSettings, MotorSettings motorSettings, ILogger logger) { TelemetryData telemetryData = new TelemetryData(); long previousTime = DateTime.Now.Ticks; long lastRadioTime = 0; long lastSensorTime = 0; long lastControlTime = 0; long lastMotorTime = 0; long lastTelemetryTime = 0; bool systemArmed = false; while (true) { long currentTime = DateTime.Now.Ticks; if (currentTime >= (lastRadioTime + loopSettings.RadioLoopPeriod)) { //Debug.Print((loopSettings.LoopUnit/(float)(currentTime-lastRadioTime)).ToString()); lastRadioTime = currentTime; radio.Update(); systemArmed = radio.Throttle > motorSettings.MinimumOutput; if (!systemArmed) { logger.Flush(); } } currentTime = DateTime.Now.Ticks; if (systemArmed && (currentTime >= (lastSensorTime + loopSettings.SensorLoopPeriod))) { //Debug.Print((loopSettings.LoopUnit / (float)(currentTime - lastSensorTime)).ToString()); lastSensorTime = currentTime; gyro.Update(); } currentTime = DateTime.Now.Ticks; if (systemArmed && (currentTime >= (lastControlTime + loopSettings.ControlAlgorithmPeriod))) { //Debug.Print((loopSettings.LoopUnit / (float)(currentTime - lastControlTime)).ToString()); lastControlTime = currentTime; pid.Update(radio.Axes, gyro.Axes, (float)(currentTime - previousTime) / loopSettings.LoopUnit); previousTime = currentTime; } currentTime = DateTime.Now.Ticks; if (currentTime >= (lastMotorTime + loopSettings.MotorLoopPeriod)) { //Debug.Print((loopSettings.LoopUnit / (float)(currentTime - lastMotorTime)).ToString()); if (systemArmed) { mixer.Update(radio.Throttle, pid.Axes); } else { mixer.SetSafe(); } lastMotorTime = currentTime; } currentTime = DateTime.Now.Ticks; if (systemArmed && (currentTime >= (lastTelemetryTime + loopSettings.TelemetryLoopPeriod))) { //Debug.Print((loopSettings.LoopUnit / (float)(currentTime - lastTelemetryTime)).ToString()); telemetryData.Update(gyro.Axes, radio.Axes, pid.Axes, currentTime); lastTelemetryTime = currentTime; logger.Write(telemetryData); } } }