Connecting the movement of a cursor (controlled by data from encoders) while making a target move on the screen?
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I have two encoders which signals are acquired by matlab through an arduino board. And I have a function that makes the data acquisition through an infinte while loop.
Then I have a GUI (built with guide) that has an axes figure in which a target and a cursor should be moving once I clicked a push button in the GUI.
The target moves thanks to a for cycle that changes its position on the plot once every second.
Now, what I would like to do is to connect the encoders' signal to the cursor movement. The problem is that since I have basically two loops (one for the data acquisition and one for the target movement) I am not able to make the code running in the GUI because I have to wait that one of the two loops finishes before the other starts.
So basically the code is:
function EncodersData = read_arduino ()
%Read encoder data with a while cycle
%Compute the Cursor position in the while cycle
end
%In the GUI push button callback function
Cursor_Position = read_arduino();
%Change cursor position continuously somehow
for i = 1:60
pause (1)
%Change target Position
end
So in the code I just shared the Target position won't be updated in the plot becuase the while loop of the read_arduino function is infinite and it won't stop till I close the code.
So I need to be able to read the Cursor_Position continuously (not at the same velocity of the for cycle of the target) and update the cursor position continuously.
These are the solutions (that I was not able to make work) that I tried:
- Use simulink to pass the encoders' data to matlab (Problem: I wasn't able to read the data in real time from simulink)
- Use the Parallel computing Toolbox to run the read_arduino() function continuously and have a labReceive function to retrieve its data in the GUI (Problems: First, I tried and I am not able to define which Lab should do what; Second, I am not sure that this will sove the problem because if I put the labReceive command before the for cycle then I will never be able to read it and if I put it inside the for cycle then I am gonna read the value just once every second while I want the position of the Cursor to be updated continuously).
- Use a callback function in the GUI that runs continously and that can retrieve continuously the data from read_arduino() and hence update the cursor position (Problem: I am not sure matlab can do multithreading in this way even though with the function MoveMouse I am able to move the cursor basing its position on the position of the mouse without any dependance from the movement of the target, so the cursor is able to updates its position at a different velocity than the one of the target. So maybe I can do this but there is someting I am missing).
Does anyone know how can I solve this problem?
Thank you in advance.
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!