Realtime data from motor controller
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a question how to plot and show real-time data in App designer, maybe someone can help me, I am beginner in Matlab.
I get my data (e.g. Velocity) easy with webread() in hex and just have to convert into a decimal number, it already works.
But now I don’t know how to write an easy and efficiency program to get actually data e.g. velocity, motor current, torque to show them into two ways in App Designer:
- In an EditField to see the Data like a Display
- In a plot to see data in a graphic and safe
Thanks a lot for any advice to find a good solution :)
采纳的回答
Mario Malic
2020-12-31
Hey Christian,
It would be the best if you'd do few introductory examples in App Designer and you'll get an idea how to do your task.
% 1. Set the property value of the Edit Field component this way
app.EditField.Value = 5
% 2. Once you create axes in your app, use plot function
plot(ax, x, y); % ax is the handle to your axes, example app.UIAxes
21 个评论
Chris
2021-1-3
Hallo, I try it but the plot is not correct working after push the Start-Bottom. Thanks for helping me.
app.stopp = false;
startTime = datetime('now');
while ~app.stopp
t = datetime('now') - startTime;
plot(app.actualVelocityUIAxes,t,hex2dec(webread('2410/10')))
drawnow
pause(1/10);
app.stopp = app.done;
end
Mario Malic
2021-1-3
Hey again,
1) You need to keep the values on your plot for each plot command, that's why you should use hold, or you can do it in App Designer UI manually
2) When plotting a single point, you will not be able to see it unless you set a point marker.
It would be interesting to see if there will be some lag with webread and refreshing the plot.
app.stopp = false;
hold(app.actualVelocityUIAxes, 'on'); % Note 1
startTime = datetime('now');
while ~app.stopp
t = datetime('now') - startTime;
plot(app.actualVelocityUIAxes,t,hex2dec(webread('2410/10')), 'kd') % Note 2
drawnow
pause(1/10);
app.stopp = app.done;
end
Mario Malic
2021-1-3
编辑:Mario Malic
2021-1-3
It is going to be a little bit complicated, but doable, don't know if performance will suffer because you'll be redrawing the whole plot for each step. Added code creates a vector that would increase in column size by one for each step. You have to do the same for vector t. Code for plotting remains the same.
velocityData = []; % initialise the variable
app.stopp = false;
% hold(app.actualVelocityUIAxes, 'on'); % no need for this anymore
startTime = datetime('now');
while ~app.stopp
if isempty(velocityData)
velocityData = hex2dec(webread('2410/10'))
else
velocityData(end+1) = hex2dec(webread('2410/10'))
end
% Adjust for the t
t = datetime('now') - startTime;
plot(app.actualVelocityUIAxes,t,hex2dec(webread('2410/10')), '-b');
drawnow
pause(1/10);
app.stopp = app.done;
end
Chris
2021-1-3
Wow thanks, it really nice.
So its working so far. The only thing is without
% hold(app.actualVelocityUIAxes, 'on'); % no need for this anymore
the x-Axes (time) is not moving but with "hold" is doing the job fine.
Also If I used 'b' instead of 'kd' the plot shows nothing?
Thanks so much :)
Mario Malic
2021-1-3
编辑:Mario Malic
2021-1-3
I have forgotten to update the line with plot function
plot(app.actualVelocityUIAxes, t, velocityData, '-b');
You haven't updated the code for vector t. Write it in the same way as velocityData. I have left it as a small task for you to do.
You can read more about line and marker properties in documentation for plot function.
'-b' % blue line, note: this doesn't work if you plot a single point
'kd' % d - diamond marker, can't remember what k stands for
Chris
2021-1-3
Super, now it works how I like it. The last thing would be to safe the date. I guess I have too copied the data and safe it in a file? But I am grateful for you help :))))
velocityData = []; % initialise the variable
t = [];
app.stopp = false;
startTime = datetime('now');
while ~app.stopp
if isempty(velocityData)
velocityData = hex2dec(webread('2410/10'));
else
velocityData(end+1) = hex2dec(webread('2410/10'));
end
if isempty(t)
t = datetime('now') - startTime;
else
t(end+1) = datetime('now') - startTime;
end
plot(app.actualVelocityUIAxes, t, velocityData, '-b');
drawnow
pause(1/10);
app.stopp = app.done;
end
Mario Malic
2021-1-3
编辑:Mario Malic
2021-1-3
What do you mean by saving the date? Do you want to know when did you do the measurement? You can put it in a title or add text somewhere on the figure. Use datetime for date, make sure you convert it to string and add to title.
You're welcome, if this solved your problem, please accept the answer. Thanks in advance.
Chris
2021-1-4
After I finish the plot I would be good to have an option to safe the data of the plot in a table e.g. in excel or matlab?
Mario Malic
2021-1-4
Chris
2021-1-4
编辑:Chris
2021-1-4
Hallo Mario again ;) , I know you help me so much, but I really want to solve one little other problem and it is something with read data from an table.
Its about an velocity profile, that means I just have an table with x=time and y=velocity that means after e.g. for 10s the velocity is going to 50 rpm and than increase to 100 for 20s etc.
So I need to call every value of the array step by step that I can send my controller after the timeframe is finisehed the new value.
time [s] velocity [rpm]
10 50
20 100
10 150
20 200
10 250
.........
Thanks :)
t = readtable("Daten.xlsx","Sheet",2);
x = table2array(t(:,1));
y = table2array(t(:,3));
stairs(app.UIAxes,x,y,'-o');
Chris
2021-1-5
编辑:Chris
2021-1-5
Sorry, I will try to explain it better. I would like to send with webwrite() to my controller the new velocity data after the given time is over (like a Profile). My question how I can provide this data from the array e.g. in a loop until the profil is finished?
Incluse: to know how the motor is driving I showed in a plot to know what’s happen Thanks :)
stairs(app.UIAxes,x,y,'-o'); %its just so see how the Motor is driving
Mario Malic
2021-1-5
编辑:Mario Malic
2021-1-5
It's specific to the motor controls, how do you control it? You should find that out first, once you do it, you'll be able to approach the coding problem. It is of course done through the while loop, but I think that using webwrite without some meaningful pauses would cause issues.
Chris
2021-1-5
Yes but my pause would be the times from my table. I am sending the new Velocity-Value to the controller only if the time is done.
Yes, I have to integradet in a while loop but how I implent every value in the while loop? e.g. that first I have for 10s a velocity of 50rpm second 50s a veleocity of 100 and so on...
time= [10, 50, 100, 30,......]
veleoctiy = [50, 100,150, 200....]
Mario Malic
2021-1-5
编辑:Mario Malic
2021-1-5
Just an idea, you can probably write it similar to this
t = tic
% or use timer
while true % or t<timeEnd
if t<t1
% websave
elseif t>t1 && t<t2
% websave
elseif %
%
eiseif %
%
else
break
end
% pause(0.01); % not sure about this
end
Chris
2021-1-6
I thought I count how many data are in the column and read every loop the new data into pause and webwrite() until the loop is finished (in this case 4 times. But I still struggle with the implementation?
t = readtable("Daten.xlsx","Sheet",2);
time = table2array(t(:,2)); %time in seconds
velo = table2array(t(:,3));
l = length(time); %to know how many loops are necessary
n=1;
while n <= l
pause(% I need every new value from every row in the the column "time")
webwrite(% and every new value from every row in the the column "velocity")
n=n+1;
end
Mario Malic
2021-1-20
Hi Chris,
Related to your mail, I am not sure what did you mean by "velocity measured is always zero and if I increase the velocity the Torque is stacking but the velocity is working in the plot".
Are you reading the values correctly? I created a demo app with random numbers, there are Edit Field components to show the latest value for torque and velocity, so you can check if that's alright in your app.
Another thought: when velocity is zero, signal you get might not be zero, but very low, let's say 1E-3. On plot it will look like it's jumping around, which it actually is but such small number can be neglected.
Chris
2021-1-21
Hello Mario,
super! Thanks for create a demo app. Everything is working well.
"Another thought: when velocity is zero, signal you get might not be zero, but very low, let's say 1E-3. On plot it will look like it's jumping around, which it actually is but such small number can be neglected."
Exactly, that was the key I had in the beginning really high measurement errors sometimes (1E+9): Now I limited the range in the beginning and the plot is working well.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)