# hey guys can you help me with this.

8 views (last 30 days)
husam alrajab on 14 May 2019
Commented: husam alrajab on 17 May 2019
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);

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.

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 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;
Input = giris;
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(aggkp);
Serial.println(aggki);
Serial.println(aggkd);
b = 0;
}
b = b+1;
delay(50);
}
}
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 on 17 May 2019
i will .thanks for your help
{"ft_lang_label":"__label__en","ft_lang_prob":0.6317531,"math_prob":0.8702233,"size":577,"snap":"2019-51-2020-05","text_gpt3_token_len":156,"char_repetition_ratio":0.13612565,"word_repetition_ratio":0.0,"special_character_ratio":0.28422877,"punctuation_ratio":0.22142857,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97843367,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-29T12:11:58Z\",\"WARC-Record-ID\":\"<urn:uuid:2ba4b8f2-99df-40a7-be92-67e886062112>\",\"Content-Length\":\"120203\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c544b8f0-2b5f-4ded-87f2-5b5faeffa9be>\",\"WARC-Concurrent-To\":\"<urn:uuid:29a2a646-d9df-41c2-b129-4299a5571782>\",\"WARC-IP-Address\":\"104.110.193.39\",\"WARC-Target-URI\":\"https://au.mathworks.com/matlabcentral/answers/462104-hey-guys-can-you-help-me-with-this\",\"WARC-Payload-Digest\":\"sha1:TMT4ICWMUS3KNBWRVMH6QUHXFODKDUX3\",\"WARC-Block-Digest\":\"sha1:XF2T5S5H3TATZXRZFHXBDXPMFMON46O3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251796127.92_warc_CC-MAIN-20200129102701-20200129132701-00440.warc.gz\"}"}