The QUT EV3 Matlab toolkit provides a collection of functions to read sensors and control motors. You can see a full reference on the QUT EV3 Matlab toolkit page. Here, we will go through a brief introduction to each sensor and motor type.
This part of the tutorial assumes that you connected Matlab to the EV3 via USB. Complete the :doc:`toolkit-setup` section before proceeding.
Also attach one of the motors to Port A on the brick.
Don't forget that when you want to start a new connection, you have to end the previous one first with delete(b)
.
Open Matlab, and open the Example.m
file from your <EV3>
directory. (You can also download it from here: :download:`Example.m <resources/Example.m>`.)
This example program demonstrates some of the functionality of the EV3 Matlab toolkit. The program will give you some basic interactive control over a motor connected to Port A.
Start the program, and play around with it using the instructions printed after start. Start the motors, and check the readings from the tachometer.
Task: What does the tachometer measure?
Let's inspect this program now.
% init the connection disp('Connecting ... ') % brick usb init b = Brick('ioType','usb'); % beep to indicate connection b.beep();
As you know already, disp
outputs a string to the Matlab console. We then initiate a connection via USB to the brick. Note that if you want to run this example through wireless, you could follow the instructions in :doc:`toolkit-setup`, and change this block of code to create a wireless connection. Finally, the successful connection is signalled by a beep.
% user input userIn = ''; ... while(~strcmp(userIn,'f')) % get input userIn = input('> Input(u,d,t,s,b,o,c,v,f): ','s'); ... end
Your input is monitored in a while loop. The condition for entering the while loop is that the user input (stored in the userIn
variable) be not equal to the letter f. So next time you input f, the while loop won't be entered, and the program will finish by closing the connection to the brick.
% increase speed if (userIn == 'u') motorPower = motorPower+10; if motorPower >= 100 motorPower = 100; end b.outputPower(0,Device.MotorA,motorPower) disp(['> Motor Power: ' num2str(motorPower)]); end
When you input the letter u, the value of the variable motorPower
is increased by 10. This is communicated to the brick by calling the outputPower
function, specifying the motor port with Device.MotorA
, and supplying the power value in motorPower
. Note that the motor power is a percentage value in the range -100..100
, which is why it is capped at 100 in the code.
Note how the motor power is outputted to the console. First, the number is converted to a string by the num2str
function. This is then combined into a vector of strings using the [string1 string2]
notation. Finally, this vector of strings is passed to the disp
function.
For those interested: the first argument of the outputPower
function is the usb chain level. This only matter when you have several bricks chained together via USB cables. In that case you could send commands to other bricks by specifying the appropriate level. Since we will always work with a single brick, the usb chain level should always be 0.
Task: Implement an input method where you can specify the amount by which the motor power should change.
Hint: You might want to search the internet for a Matlab function that turns strings into integers.
% output the tachometer if (userIn == 'o') tacho = b.outputGetCount(0,Device.MotorA); disp(['> Tachometer: ' num2str(tacho)]); end
When you input the letter o, the tachometer value of the motor is read an displayed. This value is the angle relative to the last time the tacho was cleared.
Task: Compute the average of all tacho measurements triggered by the input o. Output the result when the program is finishing.
Hint: You might want to store each read value in a vector. Serach the internet for how to add values to a vector in Matlab.
This example program doesn't read from any sensors - yet. Let's add a touch sensor, and let's count the number of bumps. Start by connecting the touch sensor to one of the numbered ports (Port 1-4).
The Sensor Reading page shows how to read from sensors. Choose the appropriate port setting and reading mode to obtain the right sensor value.
Task: Accept a new letter as an input, and as an answer output the number of bumps on the touch sensor.