Ball and Beam experiment

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?
:frowning: :frowning: :frowning:

#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.
}

Your comments say the Output range is -80 to +80 but you set it to -20/+80.

Yes, I changed it. But I don't think that problem is in it. Even when it was (-80;80), it doesn't work correct.

Almukhammed:
Then I set these values in the following code in Arduino in the hope that it'll happen, but alas.

What does it actually do? We know it doesn't work, otherwise you wouldn't be here. In what EXACT way doesn't it work?

Please use [ code ] tags when posting code. The forum software eats some of your code if you don't.

MorganS:
What does it actually do? We know it doesn't work, otherwise you wouldn't be here. In what EXACT way doesn't it work?

Please use [ code ] tags when posting code. The forum software eats some of your code if you don't.

The system needs to keep the ball at a certain distance. But the ball goes farther from the sensor. I used the Ulstra Sonik sensor HC-SR04 and servo motor. But the problem, I think, in my code, so I used existing code in which used a different sensor and I changed it a bit. I can't understand where can be the error if you can can help.

#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.
}

Test the distance sensor to make sure it is detecting the ball and measuring the distance accurately.

I have already tested. It works correctly. I find where is the error, thank you all!!!

hey bro, Did you find the problem? I am doing a Ball and beam symtem and i have some problem the same as you. So can you show me where code wrong? Thanks very much

I have same problems, Can you show me where here is the error !!!

Dylan1310:
I have same problems, Can you show me where here is the error !!!

Since the person who asked in April, 2018 didn't get an answer is seems unlikely that you will get one by asking in July, 2019. You should probably go find some other example to work from.