Hello how can I change k values from MATLAB?? I need a script to control these values. Please help me!!

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

Answers (2)

Okay, please use the MATLAB optimized nominal K values:
Kp = 1 + sqrt(2);
Ki = 1;
Kd = 1 + sqrt(2);

3 Comments

How did you know those are the exact k values he wanted to use?
no i want to write a script in matlab and gui and make button to input this values??
@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.

Sign in to comment.

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

thank you i will try it but i want to istall support package but ihave problem with it can i install it offline for matlab 2020a??
last question i need to make text and read button with gui to entre diffrent k values not a pop up window ??
For inputdlg you don't need to download any additional support packages. It's included in base MATLAB.
If you want to create a custom GUI with edit fields or sliders on it to adjust the K values, then learn how to use App Designer:
the script you gave me how i can use it in button or gui anyway??? i mean how can i put it in the gui??
Did you run App Designer and place 3 edit text controls on the figure and 3 static text labels next to them?
Then, whenever you want to do something with those values, you just get them like
k1 = app.edtK1.Value; % Retrieve value from the control called "edtK1" and put its value into variable k1.
If you don't know what I'm talking about then you haven't yet taken the training on App Designer.
no i don't have time to learn it right now , but i can use gui.
Then how is it going to happen? Magic? A miracle? Someone doing it for you?
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.

Sign in to comment.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Products

Release

R2020a

Tags

Asked:

on 30 May 2022

Commented:

on 31 May 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!