LoginSignup
1

More than 5 years have passed since last update.

arduinoでPID_v1

Posted at

概要

arduinoでpid制御やってみた。

写真

image

サンプルコード

#include <PID_v1.h>

double Setpoint,
    Input,
    Output;
double Kp = 1,
    Ki = 1.8,
    Kd = 0.3;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
volatile float Duty;
volatile unsigned long UpNew,
    UpOld,
    DownNew,
    DownOld;
void isr()
{
    if (digitalRead(2) == LOW)
    {
        DownOld = DownNew;
        DownNew = micros();
        Duty = (DownNew - UpOld) * 30 / (DownNew - DownOld);
    }
    else
    {
        UpOld = UpNew;
        UpNew = micros();
    }
}
void setup()
{
    pinMode(3, OUTPUT);
    pinMode(2, INPUT_PULLUP);
    attachInterrupt(0, isr, CHANGE);
    Serial.begin(115200);
    Setpoint = 50;
    myPID.SetMode(AUTOMATIC);
}
void loop()
{
    Input = Duty;
    myPID.Compute();
    analogWrite(3, Output);
    Serial.print(Setpoint);
    Serial.print(",");
    Serial.print(Input);
    Serial.print(",");
    Serial.println(Output);
    delay(100);
}

以上。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1