- https://in.mathworks.com/help/matlab/ref/pause.html
- https://in.mathworks.com/help/matlab/ref/toc.html
about readButton and readRotation functions in programming LEGO EV3 with matlab
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I recently re-implemented the robot Arm project on LEGO EV3. (It has been programmed successfully in the Scratch programming language.)
In this project, there is one step which waits for user to press either up or down button. This command decides which direction the robot Arm will turn. In Scratch, it is has a block called "wait until". In matlab, I used while loop instead. But EV3 brick sometimes does not wait for me to press any button and seems randomly choose one button and leave the while loop. I do not konw what goes wrong. Could someone helps me? I copy my matlab code in the following.
direction = 0;
while direction == 0
if readButton(mylego, 'up')
direction = 1; %clockwise
end
if readButton(mylego, 'down')
direction = -1; %counterclockwise
end
end
I have another question on calling readRotation function for the motor. The return value sometimes is extremely large (> 10000). For example, I want to run the motor for 90 degrees clockwisely. I copied here two approaches to code this. I wonder which method is better, or there is other better way to implement this.
Approach 1:
motor.Speed = -50;
resetRotation(motor);
start(motor);
while 1
rotation = readRotation(motor);
if abs(rotation) >= 90
break;
end
end
stop(motor);
Approach 2:
motor.Speed = -50;
resetRotation(motor);
start(motor);
while abs(readRotation(motor)) <= 90
% nothing
end
stop(motor);
Which code might be more reliable to avoid the readRotation function returning abnormally large value? or maybe there are other ways to implement it better? Thank you. Look forward to hearing from you.
0 Commenti
Risposte (1)
arushi
il 3 Set 2024
Hi Nannan,
I understand that sometimes your LEGO EV3 does not wait for you to provide an input and starts to move on its own.
Regarding the issue with the while loop waiting for user input, it is possible that the loop is executing too quickly and not giving enough time for the user to press the button. To address this, you can introduce a small delay using the `pause` function. Here is an updated version of your code that incorporates a pause of 0.1 seconds within the loop:
direction = 0;
while direction == 0
if readButton(mylego, 'up')
direction = 1; % clockwise
end
if readButton(mylego, 'down')
direction = -1; % counterclockwise
end
pause(0.1); % Add a small delay to allow time for button press
end
By adding the `pause(0.1)` statement within the loop, it gives a brief pause of 0.1 seconds on each iteration, allowing the program to wait for user input.
Regarding the motor rotation, both approaches you provided can work, but there is a possibility of the `readRotation` function returning an abnormally large value. To handle this, you can set a timeout condition to avoid an infinite loop. Here is an updated version of your code that includes a timeout condition:
motor.Speed = -50;
resetRotation(motor);
start(motor);
timeout = 10; % Timeout in seconds
tic; % Start timer
while abs(readRotation(motor)) <= 90
if toc > timeout
break; % Break the loop if timeout is reached
end
end
stop(motor);
In this updated code, a timeout of 10 seconds is set using the `timeout` variable. The `tic` function starts a timer, and on each iteration of the loop, the `toc` function checks the elapsed time. If the timeout is reached (`toc > timeout`), the loop breaks, ensuring that the program does not get stuck indefinitely.
By incorporating the timeout condition, you can handle situations where the `readRotation` function returns abnormally large values or if the motor gets stuck for some reason.
Remember to adjust the timeout value according to your specific needs.
Please refer to the following MATLAB Documentation to know more about the MATLAB Support Package for LEGO MINDSTORMS EV3 Hardware:
Please refer to the following MATLAB documentation to know more about ‘pause’ and ‘tic-toc’ functions:
Hope the above solution helps you.
0 Commenti
Vedere anche
Categorie
Scopri di più su LEGO MINDSTORMS EV3 Hardware in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!