Skip to end of metadata
Go to start of metadata

Creating a MATLAB USB connection

A MATLAB USB connection can be created with the EV3 as per the following example

b = Brick('ioType','usb')

Set and get brick name

The name of the connected EV3 brick can be changed and returned with

b.setBrickName('EV3')
name = b.getBrickName()

Commanding the motors

The outputStart and outputStop methods allow an EV3 motor to be started and stopped. You can also set the outputPower like so

% set motor power connected to port A on layer 0
b.outputPower(0,Device.MotorA,motorPower);
% start the motor
b.outputStart(0,Device.MotorA);
% run for 5 seconds
pause(5)
% turn the motor off with coast braking
b.outputStop(0,Device.MotorA,Device.Coast);

The motor can also be commanded to move to a particular position with the outputStepSpeed method as described here.

User selection

The code in Example.m shows how user input can be used to access various toolkit functions

% init the connection
disp('Connecting ... ')
% brick usb init
b = Brick('ioType','usb');
% beep to indicate connection
b.beep();
% print information
disp('===================================')
disp('EV3 example code')
disp('===================================')
disp('u - increase motor speed')
disp('d - decrease motor speed')
disp('t - start the motor')
disp('s - stop the motor')
disp('b - beep')
disp('o - output the tachometer')
disp('c - clear the tachometer')
disp('v - output the brick voltage')
disp('f - quit the program');
disp('===================================')
% user intput
userIn = '';
% motor power
motorPower = 10;
% set motor power and start
b.outputPower(0,Device.MotorA,motorPower)
b.outputStart(0,Device.MotorA)
while(~strcmp(userIn,'f'))
    % get input
    userIn = input('> Input(u,d,t,s,b,o,c,v,f): ','s');
    % 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
     % decrease speed
    if (userIn == 'd')
        motorPower = motorPower-10;
        if motorPower <= -100
            motorPower = -100;
        end
        b.outputPower(0,Device.MotorA,motorPower)
        disp(['> Motor Power: ' num2str(motorPower)]);
    end
    % start the motor
    if (userIn == 't')
        b.outputStart(0,Device.MotorA)
        disp('> Motor Started');
    end
    % stop the motor
    if (userIn == 's')
        b.outputStop(0,Device.MotorA,0)
        disp('> Motor Stopped');
    end
    % beep test
    if (userIn == 'b')
        b.beep();
        disp('> Beep');
    end
    % output the tachometer
    if (userIn == 'o')
        tacho = b.outputGetCount(0,Device.MotorA);
        disp(['> Tachometer: ' num2str(tacho)]);
    end
    % clear the tachometer
    if (userIn == 'c')
        b.outputClrCount(0,Device.MotorA)
        disp('> Cleared tacho');
    end
    % voltage output
    if (userIn == 'v')
        v = b.uiReadVbatt;
        disp(['> Brick voltage: ' num2str(v) 'V']);
    end
end
% delete the brick object
delete(b)

Reading the sensors

A sensor connected to the EV3 brick can be read using the inputReadSI function. The following example will read an ultrasonic sensor connected to port 1 and return the sensor reading in centimeters.

reading = b.inputReadSI(0,Device.Port1,Device.USDistCM);

The final parameter in the inputReadSI function describes what mode to read the device in e.g. for an ultrasonic sensor, 'Device.USDistCM' will return the reading in centimeters where as 'Device.USDistIN' will return the reading in inches.

Plotting sensor readings

The plotSensor functions shows how you can read sensor input and plot it in MATLAB

function plotSensor(brick,layer,no,mode)
	% start timing
	tic;
	% create figure
	hfig = figure('name','EV3 Sensor');
	% init the the data
	t = 0;
	x = 0;
	hplot = plot(t,x);
	% one read to set the mode
	reading = brick.inputReadSI(layer,no,mode);
	% set the title
	name = brick.inputDeviceGetName(layer,no);
	title(['Device name: ' name]);
	% set the y label
	name = brick.inputDeviceSymbol(layer,no);
	ylabel(['Sensor value (' name(1:end-1) ')']);
	% set the x label
	xlabel('Time (s)');
	% set the x axis
	xlim([0 10]);
	% wait until the figure is closed
	while(findobj('name','EV3 Sensor') == 1)
		% get the reading
		reading = brick.inputReadSI(layer,no,mode);
		t = [t toc];
		x = [x reading];
		set(hplot,'Xdata',t)
		set(hplot,'Ydata',x)
		drawnow
		% reset after 10 seconds
		if (toc > 10)
	   		% reset
	   		t = 0;
	   		x = x(end);
	   		tic
		end
	end
end

This function can be called with the code below. In this example, the gyro sensor has been connected to port 1 on the EV3 brick. Note that the available modes for each sensor is listed here.

b.plotSensor(0,Device.Port1,Device.GyroAng);
  • No labels