Esempio n. 1
0
        private async Task PIDTask()
        {
            var pid = new PIDController(
                (float)BrewProfileSettings.Instance.PIDKp,
                (float)BrewProfileSettings.Instance.PIDKi,
                (float)BrewProfileSettings.Instance.PIDKd,
                100f,
                0f);

            long previousMillisec = StartupTimer.Instance.Millis();

            while (IsRunning)
            {
                if (CurrentStep == null)
                {
                    await Task.Delay(10);

                    continue;
                }

                bool isBoiling = CurrentStep.Type == Objects.StepType.Boil;

                pid.SetPoint        = (float)CurrentStep.Temperature;
                pid.ProcessVariable = (float)TemperatureController.Instance.Controller.Temperature;
                float output = pid.ControlVariable(StartupTimer.Instance.Millis() - previousMillisec);
                previousMillisec = StartupTimer.Instance.Millis();

                //Check the output and see if we need to trigger the heater on or off
                if (output >= 100 || isBoiling)
                {
                    _ssrHeaterHandler.Switch(SSRState.On);
                }
                else
                {
                    _ssrHeaterHandler.Switch(SSRState.Off);
                }

                if (Utilities.IsDesktopComputer())
                {
                    (TemperatureController.Instance.Controller as TemperatureSensorMock)?
                    .MockMode(_ssrHeaterHandler.State == SSRState.On ? SensorMockMode.Increase : SensorMockMode.Decrease);
                }

                //Debug.WriteLine($"PID output: {output}, temperature: {pid.SetPoint}");

                await Task.Delay(500);
            }
        }