You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Realtime data from motor controller
3 views (last 30 days)
Show older comments
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 :)
Accepted Answer
Mario Malic
on 31 Dec 2020
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 Comments
Chris
on 3 Jan 2021
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
on 3 Jan 2021
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
Chris
on 3 Jan 2021
Edited: Chris
on 3 Jan 2021
Super thanks! now its working and I can see the progress of velocity. I have two more questions?
- Its possible to see the plots as a line?
- safe this data that I can use it for an evaluation?
Thanks a lot! :)
Mario Malic
on 3 Jan 2021
Edited: Mario Malic
on 3 Jan 2021
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
on 3 Jan 2021
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
on 3 Jan 2021
Edited: Mario Malic
on 3 Jan 2021
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
on 3 Jan 2021
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
on 3 Jan 2021
Edited: Mario Malic
on 3 Jan 2021
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.
Mario Malic
on 4 Jan 2021
Chris
on 4 Jan 2021
Edited: Chris
on 4 Jan 2021
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');
Mario Malic
on 4 Jan 2021
I don't really understand what do you want to do, elaborate.
Chris
on 5 Jan 2021
Edited: Chris
on 5 Jan 2021
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
on 5 Jan 2021
Edited: Mario Malic
on 5 Jan 2021
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
on 5 Jan 2021
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
on 5 Jan 2021
Edited: Mario Malic
on 5 Jan 2021
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
on 6 Jan 2021
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
on 20 Jan 2021
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
on 21 Jan 2021
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.
Mario Malic
on 21 Jan 2021
Great to hear, you're welcome.
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)