Hello how can I change k values from MATLAB?? I need a script to control these values. Please help me!!
2 views (last 30 days)
Show older comments
#include <PID_v2.h>
int last_reading;
#include<Servo.h>
const int servoPin = 9;
const int TrigPin = 3;
const int EchoPin = 2;
float Kp = 3;
float Ki = 2;
float Kd = 2;
double Setpoint, Input, Output, ServoOutput;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
Servo myServo;
void setup() {
last_reading = 5;
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
myServo.attach(servoPin);
Input = readPosition();
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(-80, +80);
}
void loop()
{
Setpoint = 15;
Input = readPosition();
myPID.Compute();
last_reading = Input;
ServoOutput = 102 + Output;
myServo.write(ServoOutput);
}
float readPosition() {
delay(100);
long duration;
int cm, distance;
unsigned long now = millis();
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
duration = pulseIn(EchoPin, HIGH);
distance = (duration*0.034)/2;
cm = distance;
if (cm > 34)
{
cm = 34;
}
// if (cm<2)
//digitalWrite(check);
//cm=30-cm;
/* if (cm <7 ) {cm=6; Serial.println("");}*/
Serial.println(cm);
return cm;
}
3 Comments
Image Analyst
on 30 May 2022
float Kp = 3234234;
float Ki = 23423;
float Kd = 22389492834;
How about that? Those are changed k values.
Answers (2)
Sam Chak
on 30 May 2022
Okay, please use the MATLAB optimized nominal K values:
Kp = 1 + sqrt(2);
Ki = 1;
Kd = 1 + sqrt(2);
3 Comments
Sam Chak
on 31 May 2022
@mark spectre requested to get the k values from MATLAB, and I interpreted the message that the K values can be computed via MATLAB optimization algorithm. If my Answer is not what he seeks and he cares, then he will clarify, and luckily he did...
In practice, human operators generally cannot pluck the K values out of thin air. They are some standards (i.e., Ziegler-Nichols, Cohen-Coon) and I gave Mark the nominal values of the K parameters that come from MATLAB's LQR function. With the parameters such as mass (m), damping ratio (ζ), and natural frequency (ω) that can be easily provided by humans, then Mark can change the K values as desired.
Here is an example that produces the original K values:
% nominal K values
Kpn = 1 + sqrt(2);
Kin = 1;
Kdn = 1 + sqrt(2);
% parameters that can be easily provided by humans
m = 2; % mass (kg)
zeta = 0.525493; % damping ratio (can be set as 1 to simplify things) << 1 (response can become oscillatory)
omega = 0.788239; % natural frequency (affects the speed of the system response) >> 1 (fast), << 1 (slow)
% actual K values
Kp = Kpn*m*omega^2
Ki = Kin*m
Kd = Kdn*m*zeta*omega
Kp = 3.0000
Ki = 2
Kd = 2.0000
Thanks for showing the script fo the UI.
Image Analyst
on 30 May 2022
Edited: Image Analyst
on 30 May 2022
If you want a popup dialog box to ask them, then try this (otherwise use App Designer to make a full GUI):
% Ask user for three floating point numbers.
defaultValue = {'45.67', '78.91', '23.45'};
titleBar = 'Enter values';
userPrompt = {'Enter Kp : ', 'Enter Ki : ', 'Enter Kd'};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Convert to floating point from string.
Kp = str2double(caUserInput{1})
Ki = str2double(caUserInput{2})
Kd = str2double(caUserInput{3})
% Check usersValue1 for validity.
if isnan(Kp)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
Kp = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', Kp);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(Ki)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
Ki = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', Ki);
uiwait(warndlg(message));
end
% Do the same for usersValue3
% Check usersValue3 for validity.
if isnan(Kd)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
Kd = str2double(defaultValue{3});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', Ki);
uiwait(warndlg(message));
end
8 Comments
Image Analyst
on 31 May 2022
Then how is it going to happen? Magic? A miracle? Someone doing it for you?
Image Analyst
on 31 May 2022
OK, I'll give you a start. Attached is the GUI file. It looks like this but you can adapt it to add other controls.

You can use whatever type of control you like for the user to set the K values.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!