Hi all. I have a few questions on the system of ball and beam. As other Internet sources I calculated the transfer function of my system, which is calculated by the following formula T(s)=-mgd/L((J/r^2)+m). In simulink I put together a feedback system with pid controller. In the pid controller did the tuning and it is automatically calculated me values KP Ki KD. Then I set these values in the following code in Arduino in the hope that it'll happen, but alas. Tell me what's the problem?
#include<Servo.h>
#include<PID_v1.h>
const int servoPin = 9; //Servo Pin
float Kp = 0.11785963205528; //Initial Proportional Gain
float Ki = 0.00786245517853044; //Initial Integral Gain
float Kd = 11.5469558498532; //Intitial Derivative Gain
double Setpoint, Input, Output, ServoOutput;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); //Initialize PID object, which is in the class PID.
Servo myServo; //Initialize Servo.
void setup() {
Serial.begin(9600); //Begin Serial
myServo.attach(servoPin); //Attach Servo
Input = readPosition(); //Calls function readPosition() and sets the balls
// position as the input to the PID algorithm
myPID.SetMode(AUTOMATIC); //Set PID object myPID to AUTOMATIC
myPID.SetOutputLimits(-20,80); //Set Output limits to -80 and 80 degrees.
}
void loop()
{
Setpoint = 10;
Input = readPosition();
myPID.Compute(); //computes Output in range of -80 to 80 degrees
ServoOutput=102+Output; // 102 degrees is my horizontal
myServo.write(ServoOutput); //Writes value of Output to servo
}
float readPosition() {
delay(40); //Don't set too low or echos will run into eachother.
const int pingPin = 10;
const int pingPin2 = 11;
long duration, cm;
unsigned long now = millis();
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin2, HIGH);
cm = duration/(29*2);
if(cm > 30) // 30 cm is the maximum position for the ball
{cm=30;
}
Serial.println(cm);
return cm; //Returns distance value.
}