MATLAB Answers

hey guys can you help me with this.

8 views (last 30 days)
i'm going to print four variable on serial in fact when i run it ;it going to write this four variable in one value . my question is who to print more than one variable on serial and send it to arduino to use it in the same time.this is my matlab gui codes.
global a;
a=serial('COM4');
setpoint=str2num(get(handles.s,'string'));
aggkp=str2num(get(handles.k1,'string'));
aggki=str2num(get(handles.k2,'string'));
aggkd=str2num(get(handles.k3,'string'));
fprintf(a,'%d',setpoint);
fprintf(a,'%d',aggkp);
fprintf(a,'%d',aggki);
fprintf(a,'%d',aggkd);

  0 Comments

Sign in to comment.

Accepted Answer

Mark Sherstan
Mark Sherstan on 17 May 2019
If you need to run all of that at the same time you need to run miltiple threads. Check out the Parallel computing toolbox for more information: https://www.mathworks.com/products/parallel-computing.html
I see you are running a PID controller so I would first check how long each of these tasks are actually taking using the profiler. If printing, writing, and updating the GUI is faster than you can acquire data and/or actuate your controller, then there is no need to run everything at the same time.

  3 Comments

husam alrajab
husam alrajab on 17 May 2019
yes im working on PID controller ball balancing project. in first i run it with matlab gui that i set a setpoint value and send it to arduino to make the ball in balance Compare with setpoint value. when i run it the setpoint value is sending to arduino and he is working well but when i want to print on serial kp ki kd values it not working.Thanks for your help it was so helpful.
this is my matlab gui codes.that it s take setpoint,kp,ki,kd values and send it to arduino.
cla(handles.axes1);
global a;
global b;
fprintf(b,'\nSetpoint cikis\n');
setpoint=str2num(get(handles.s,'string'));
aggkp=str2num(get(handles.k1,'string'));
aggki=str2num(get(handles.k2,'string'));
aggkd=str2num(get(handles.k3,'string'));
fprintf(a,'%d',setpoint);
pause(0.3);
fprintf(a,'%d',aggkp);
pause(0.3);
fprintf(a,'%d',aggki);
pause(0.3);
fprintf(a,'%d',aggkd);
x=0;
while(x<500)
x=x+1;
y1(x)=fscanf(a,'%d');
cikisi=fscanf(a,'%d');
akp=fscanf(a,'%d');
aki=fscanf(a,'%d');
akd=fscanf(a,'%d');
y2(x)=setpoint;
set(handles.cikis,'string',num2str(cikisi));
set(handles.kk1,'string',num2str(akp));
set(handles.kk2,'string',num2str(aki));
set(handles.kk3,'string',num2str(akd));
fprintf(b,'\n%10d %10d\n',setpoint,cikisi);
plot(y1,'r-','linewidth',3)
grid on;
axis([0 500 0 50])
plot(y2,'b-','linewidth',3)
grid on;
hold on;
drawnow
pause(0.01);
end
and this is my arduino code.
#include <PID_v1.h>
#include<Servo.h>
int ServoOutput;
int ADC1;
int giris ;
int a;
int t;
int b ;
int balans;
Servo myServo;
double Setpoint, Input, Output;
double aggKp, aggKi, aggKd;
double consKp=0.1, consKi=0, consKd=1.2;
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
void setup()
{
Serial.begin(9600);
a = 0;
myServo.attach(9);
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(-100,100);
}
void loop()
{
if(Serial.available() > 0){
a = Serial.parseInt();
}
else if (a==0)
{
myServo.write(140);
}
else
{
t=a;
delay(0.3);
aggkp=a;
delay(0.3);
aggki=a;
delay(0.3);
aggkd=a;
giris = map(analogRead(A0),0,1023,0,50);
Input = giris;
ADC1 = Input;
balans = (t/25)*2;
Setpoint =t - 27 - balans;
double gap = abs(Setpoint-Input);
if (gap < 10)
{
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
myPID.SetTunings(aggKp, aggKi, aggKd);
}
myPID.Compute();
ServoOutput= 117 + Output;
ServoOutput = 180 - ServoOutput;
myServo.write(ServoOutput);
if ( b==4){
Serial.println(ADC1);
Serial.println(ADC1);
Serial.println(aggkp);
Serial.println(aggki);
Serial.println(aggkd);
b = 0;
}
b = b+1;
delay(50);
}
}
Mark Sherstan
Mark Sherstan on 17 May 2019
Look at the Simulink support package for Arduino. You can achieve fantastic control with all the built in Simulink controllers. This link should get you started, you can even run in real time.
husam alrajab
husam alrajab on 17 May 2019
i will .thanks for your help

Sign in to comment.

More Answers (0)

Sign in to answer this question.