[프로그램]

C# PDI온도 제어 소스

Blackberrys 2023. 6. 26. 20:55
반응형


PDI(P-Proportional, D-Derivative, I-Integral) 제어 방식을 사용하여 제어하는 C# 코드

using System;

class TemperatureController
{
    private double Kp;  // P 게인 상수 (Proportional gain)
    private double Ki;  // I 게인 상수 (Integral gain)
    private double Kd;  // D 게인 상수 (Derivative gain)

    private double setpoint;  // 목표 온도 (Desired temperature)
    private double integral;  // 적분 값 (Integral value)
    private double lastError; // 이전 오차 값 (Last error value)

    // PDI 온도 제어기를 생성하는 생성자 (Constructor)
    public TemperatureController(double kp, double ki, double kd)
    {
        Kp = kp;
        Ki = ki;
        Kd = kd;
    }

    // 목표 온도 설정 메서드 (Set the desired temperature)
    public void SetSetpoint(double temperature)
    {
        setpoint = temperature;
    }

    // 온도 제어 루프를 업데이트하는 메서드 (Update the temperature control loop)
    public double Update(double currentTemperature, double dt)
    {
        // 오차 계산 (Calculate the error)
        double error = setpoint - currentTemperature;

        // 제어 신호 계산 (Calculate the control signal)
        double proportionalTerm = Kp * error;  // 비례 제어 (Proportional control)
        integral += Ki * error * dt;  // 적분 제어 (Integral control)
        double derivativeTerm = Kd * (error - lastError) / dt;  // 미분 제어 (Derivative control)
        double controlSignal = proportionalTerm + integral + derivativeTerm;

        // 다음 반복을 위해 현재 오차 저장 (Store the current error for the next iteration)
        lastError = error;

        return controlSignal;
    }
}

class Program
{
    static void Main()
    {
        // PDI 온도 제어기 객체 생성 및 PID 게인 설정 (Create a PDI temperature controller object and set PID gains)
        TemperatureController controller = new TemperatureController(0.5, 0.1, 0.2);

        // 목표 온도 설정 (Set the desired temperature)
        controller.SetSetpoint(70.0);

        // 시뮬레이션을 위한 초기 온도, 시간 단계, 총 시뮬레이션 시간 설정 (Set initial temperature, time step, and total simulation time for simulation)
        double currentTemperature = 60.0;
        double dt = 0.1;  // 시간 간격 (초 단위) (Time step in seconds)
        double totalTime = 10.0;  // 총 시뮬레이션 시간 (초 단위) (Total simulation time in seconds)
        double currentTime = 0.0;

        while (currentTime < totalTime)
        {
            // 제어 신호 업데이트하고 시스템에 적용 (Update the control signal and apply it to the system)
            double controlSignal = controller.Update(currentTemperature, dt);
            // PDI 시스템에 제어 신호 업데이트하고 시스템에 적용 (Update the control signal and apply it to the system)
double controlSignal = controller.Update(currentTemperature, dt);
// PDI 시스템에 제어 신호를 적용합니다. (예: 히터, 쿨러 등)
// 여기서는 제어 신호가 온도에 직접 영향을 주는 것으로 가정합니다.
currentTemperature += controlSignal * dt;

// 현재 온도 출력 (Print the current temperature)
Console.WriteLine($"시간: {currentTime:F2}초, 온도: {currentTemperature:F2}℃");

// 현재 시간 증가 (Increase the current time)
currentTime += dt;

 
위의 코드에서는 PDI 온도 제어를 시뮬레이션하기 위해 사용됩니다. 제어기의 Update 메서드를 호출하여 현재 온도와 시간 간격(dt)을 전달하여 제어 신호를 계산하고, 이를 시스템에 적용합니다. 이 예시에서는 제어 신호가 온도에 직접 영향을 주는 것으로 간주하고 있습니다. 그런 다음 현재 온도를 업데이트하고 현재 시간을 증가시킵니다. 마지막으로 현재 시간 및 온도를 출력합니다.


이 코드는 주어진 설정 온도에 도달하는 과정을 시뮬레이션하기 위한 예시입니다. 실제로 온도를 제어하기 위해서는 특정 시스템에 대한 인터페이스를 구현하여 제어 신호를 적절하게 전달해야 합니다. 예를 들어, 히터 또는 쿨러를 제어하는 방식에 따라 코드를 수정해야 할 수 있습니다.

I am a father of one boy and two girls living in Cheongju. We love to go "plogging" together every day. Plogging is an activity where we pick up litter while taking a walk, with the goal of preserving the environment and creating a clean community. It's a valuable time for our family to join forces.

You can find our activities on YouTube at https://www.youtube.com/@0070TV. We kindly ask for your support and encouragement.