It is a new MechLab for NCKU ME next semester, a PID controller!
Hardware:
- Arduino Mega2560
- Motor Shield: Dual H-brigde VNH2SP30
The crucial part of the PID code is listed below
void loop()
{
// Run a period to estimate more precise speed
Motor.Tick();
// Run for one Second
delay(SamplingTime);
Motor.Tock();
// Read rotation speed from Encoder
Input = Motor.getSpeedRPS();
// PID Compute
PID_Controller.Compute();
if ((Output-prevOutput) >= oscGap) {
//Output = prevOutput;
}
Motor.setSpeed(Output);
prevOutput = Output;
// Show Data
/*
Serial.print("Input: ");
Serial.println(Input);
Serial.print("Setpoint: ");
Serial.println(Setpoint);
Serial.print("Error: ");
Serial.println(error);
Serial.print("Output: ");
Serial.println(Output);*/
Serial.println(error);
// wait a little while
delay(5);
}
My first test result (with KP=98, KI=8, PD=5)
There is almost no steady state error, however overshoot happens.
Next step is to tune the parameters and control the motor smoothly!
Some useful references