Skip to content
Snippets Groups Projects
Commit 2b285db0 authored by Dominik Mehlem's avatar Dominik Mehlem Committed by Amelie Caitlin Rauland
Browse files

switch from MATLAB serial to MATLAB serialport

parent 7703e94f
Branches
No related tags found
1 merge request!15switch from MATLAB serial to MATLAB serialport
...@@ -268,9 +268,10 @@ classdef EV3 < MaskedHandle ...@@ -268,9 +268,10 @@ classdef EV3 < MaskedHandle
end end
% Delete handle to comm-interface % Delete handle to comm-interface
if isCommInterfaceValid(ev3.commInterface) && ev3.commInterface ~= 0 if isCommInterfaceValid(ev3.commInterface)
ev3.commInterface.delete(); ev3.commInterface.delete();
end end
pause(5);
ev3.commInterface = 0; ev3.commInterface = 0;
ev3.isConnected = false; ev3.isConnected = false;
... ...
......
...@@ -926,7 +926,7 @@ classdef Motor < MaskedHandle & dynamicprops ...@@ -926,7 +926,7 @@ classdef Motor < MaskedHandle & dynamicprops
if ~motor.ev3Handle.isConnected || ~motor.physicalMotorConnected if ~motor.ev3Handle.isConnected || ~motor.physicalMotorConnected
success = false; success = false;
return; return;
end; end
if motor.currentSpeedRegulation if motor.currentSpeedRegulation
motor.handleCommand(@outputSpeed, true, 0, motor.port, power); motor.handleCommand(@outputSpeed, true, 0, motor.port, power);
... ...
......
...@@ -94,7 +94,8 @@ classdef btBrickIO < BrickIO ...@@ -94,7 +94,8 @@ classdef btBrickIO < BrickIO
% Set the connection handle % Set the connection handle
try try
if strcmp(brickIO.backend, 'serial') if strcmp(brickIO.backend, 'serial')
brickIO.handle = serial(brickIO.serialPort); brickIO.handle = serialport(brickIO.serialPort, 9600);
pause(3);
else else
brickIO.handle = Bluetooth(brickIO.deviceName,brickIO.channel); brickIO.handle = Bluetooth(brickIO.deviceName,brickIO.channel);
end end
...@@ -115,9 +116,12 @@ classdef btBrickIO < BrickIO ...@@ -115,9 +116,12 @@ classdef btBrickIO < BrickIO
end end
% Open the connection % Open the connection
if ~strcmp(brickIO.backend, 'serial')
brickIO.open; brickIO.open;
end end
end
function delete(brickIO) function delete(brickIO)
% Delete the btBrickIO object and closes the connection % Delete the btBrickIO object and closes the connection
...@@ -142,7 +146,9 @@ classdef btBrickIO < BrickIO ...@@ -142,7 +146,9 @@ classdef btBrickIO < BrickIO
% Open the bt handle % Open the bt handle
try try
if ~strcmp(brickIO.backend, 'serial')
fopen(brickIO.handle); fopen(brickIO.handle);
end
catch ME catch ME
if strcmp(ME.identifier, 'MATLAB:serial:fopen:opfailed') if strcmp(ME.identifier, 'MATLAB:serial:fopen:opfailed')
% Throw only clean CommError to avoid confusion in upper layers % Throw only clean CommError to avoid confusion in upper layers
...@@ -170,7 +176,9 @@ classdef btBrickIO < BrickIO ...@@ -170,7 +176,9 @@ classdef btBrickIO < BrickIO
try try
% Close the close handle % Close the close handle
if ~strcmp(brickIO.backend, 'serial')
fclose(brickIO.handle); fclose(brickIO.handle);
end
catch ME catch ME
% Throw combined error because error did not happen due to communication % Throw combined error because error did not happen due to communication
% failure % failure
...@@ -183,7 +191,7 @@ classdef btBrickIO < BrickIO ...@@ -183,7 +191,7 @@ classdef btBrickIO < BrickIO
end end
function rmsg = read(brickIO) function rmsg = read(brickIO)
% Reads data from the brick through bluetooth via fread and returns the data in uint8 format. % Reads data from the brick through bluetooth via read and returns the data in uint8 format.
if brickIO.debug > 0 if brickIO.debug > 0
fprintf('\t(DEBUG) (BT read) \n'); fprintf('\t(DEBUG) (BT read) \n');
...@@ -191,12 +199,20 @@ classdef btBrickIO < BrickIO ...@@ -191,12 +199,20 @@ classdef btBrickIO < BrickIO
try try
% Get the number of bytes to be read from the bt handle % Get the number of bytes to be read from the bt handle
if strcmp(brickIO.backend, 'serial')
nLength = read(brickIO.handle,2,'uint8');
else
nLength = fread(brickIO.handle,2); nLength = fread(brickIO.handle,2);
end
% Read the remaining bytes % Read the remaining bytes
if strcmp(brickIO.backend, 'serial')
rmsg = read(brickIO.handle,double(typecast(uint8(nLength),'uint16')),'uint8');
else
rmsg = fread(brickIO.handle,double(typecast(uint8(nLength),'uint16'))); rmsg = fread(brickIO.handle,double(typecast(uint8(nLength),'uint16')));
end
catch ME catch ME
if strcmp(ME.identifier, 'MATLAB:serial:fread:opfailed') if strcmp(ME.identifier, 'MATLAB:serialport:read:opfailed') || strcmp(ME.identifier, 'MATLAB:serial:fread:opfailed')
% Throw only clean CommError to avoid confusion in upper layers % Throw only clean CommError to avoid confusion in upper layers
msg = 'Failed to read data from Brick via Bluetooth.'; msg = 'Failed to read data from Brick via Bluetooth.';
id = [ID(), ':', 'CommError']; id = [ID(), ':', 'CommError'];
...@@ -212,11 +228,15 @@ classdef btBrickIO < BrickIO ...@@ -212,11 +228,15 @@ classdef btBrickIO < BrickIO
end end
% Append the reply size to the return message % Append the reply size to the return message
if strcmp(brickIO.backend, 'serial')
rmsg = uint8([nLength rmsg]);
else
rmsg = uint8([nLength' rmsg']); rmsg = uint8([nLength' rmsg']);
end end
end
function write(brickIO,wmsg) function write(brickIO,wmsg)
% Writes data to the brick through bluetooth via fwrite. % Writes data to the brick through bluetooth via write.
% %
% Arguments: % Arguments:
% wmsg (uint8 array): Data to be written to the brick via bluetooth % wmsg (uint8 array): Data to be written to the brick via bluetooth
...@@ -227,9 +247,13 @@ classdef btBrickIO < BrickIO ...@@ -227,9 +247,13 @@ classdef btBrickIO < BrickIO
try try
% Write to the bluetooth handle % Write to the bluetooth handle
if strcmp(brickIO.backend, 'serial')
write(brickIO.handle,wmsg, class(wmsg));
else
fwrite(brickIO.handle,wmsg); fwrite(brickIO.handle,wmsg);
end
catch ME catch ME
if strcmp(ME.identifier, 'MATLAB:serial:fwrite:opfailed') if strcmp(ME.identifier, 'MATLAB:serialport:write:opfailed') || strcmp(ME.identifier, 'MATLAB:serial:fwrite:opfailed')
% Throw only clean CommError to avoid confusion in upper layers % Throw only clean CommError to avoid confusion in upper layers
msg = 'Failed to send data to Brick via Bluetooth.'; msg = 'Failed to send data to Brick via Bluetooth.';
id = [ID(), ':', 'CommError']; id = [ID(), ':', 'CommError'];
... ...
......
...@@ -204,11 +204,11 @@ def print_success(brick, device): ...@@ -204,11 +204,11 @@ def print_success(brick, device):
# we can now use our bluetooth-EV3 with matlab # we can now use our bluetooth-EV3 with matlab
print(f''' print(f'''
################ USE WITH MATLAB ################ ################ USE WITH MATLAB ############
## 1. b=EV3 ## 1. b=EV3();
## 2. b.connect('bt','serPort','{device}') ## 2. b.connect('bt','serPort','{device}');
## 3. use b ## 3. b.beep();
################################################# #############################################
''') ''')
...@@ -258,13 +258,17 @@ def pair(brick, PIN, device, channel=1): ...@@ -258,13 +258,17 @@ def pair(brick, PIN, device, channel=1):
# the purpose of doing this here is to do it in a controlled manner so it can be semi-automatic, # the purpose of doing this here is to do it in a controlled manner so it can be semi-automatic,
# since the daemon does not automatically pop up a PIN request form upon receiving from the device. # since the daemon does not automatically pop up a PIN request form upon receiving from the device.
write(btctl, f'pair {MAC}') write(btctl, f'pair {MAC}')
print('Connecting...')
sleep(3)
write(btctl, f'{PIN}') # "enter" pin
print(f'Pairing {name}: accept at the device (PIN {PIN})') print(f'Pairing {name}: accept at the device AND confirm PIN. (PIN {PIN})')
sleep(1) sleep(1)
print('then press Enter to continue...') print('THEN press Enter to continue...')
input('If the prompt does not appear in a few seconds, abort and restart this script.') input('If the prompt does not appear in a few seconds, press Enter to retry pairing.')
write(btctl, f'{PIN}') # "enter" pin write(btctl, f'{PIN}') # "enter" pin
for c in range(5): for c in range(5):
print('.', end='', flush=True) print('.', end='', flush=True)
sleep(1) sleep(1)
... ...
......
...@@ -78,7 +78,7 @@ def print_dc_menu(): ...@@ -78,7 +78,7 @@ def print_dc_menu():
try: try:
print(f'\nClose matlab or make sure you disconnect /dev/{b.device} inside matlab,') print(f'\nClose matlab or make sure you disconnect /dev/{b.device} inside matlab,')
print('for example using "b.disconnect()".\n') print('for example by using "b.disconnect()".\n')
input('Then press Enter to continue.') input('Then press Enter to continue.')
except: except:
print() print()
... ...
......
...@@ -6,6 +6,7 @@ for i in /opt/mindstorms/tools/*; do ...@@ -6,6 +6,7 @@ for i in /opt/mindstorms/tools/*; do
ln -s $i /usr/local/bin/ ln -s $i /usr/local/bin/
done done
apt-get -y update
# copy laboratory documentation # copy laboratory documentation
echo "Copying documentation ..." echo "Copying documentation ..."
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment