Skip to content
Snippets Groups Projects
Commit 976cd5de authored by Tim Stadtmann's avatar Tim Stadtmann
Browse files

Implement sync-functionality in Motor-class

Also, lots of code cleanup, comments, etc.
parent 46b0a36e
No related branches found
No related tags found
No related merge requests found
......@@ -220,6 +220,7 @@ classdef CommunicationInterface < handle
function setProperties(brick, varargin)
p = inputParser();
p.KeepUnmatched = true;
% Set default values
defaultIOType = 'usb';
......
classdef ConnectionType < uint8
%ConnectionType Type resp. status of connection at a certain port.
enumeration
Unknown (111)
DaisyChain (117)
......
......@@ -7,82 +7,110 @@ classdef EV3 < handle
%
% Properties:
% Standard
% motorA[,B,C,D] -
% sensor1[,2,3,4] -
% debug -
% motorA[,B,C,D]
% sensor1[,2,3,4]
% debug - Debug turned on or off
% batteryMode - Mode for reading battery charge
% Dependent
% batteryValue - Current battery charge level
% get-only
% isConnected - Is virtual brick connected to physical one?
% commInterface - Brick object from sublayer class Brick -> used as interface to the physical brick.
% commInterface - Interface to communication layer
%
% Methods:
% Standard
% EV3 -
% connect - Connects EV3-object and its Motors and Sensors to physical brick via bt or usb.
% disconnect - Disconnects EV3-object and its Motors and Sensors from physical brick.
% update - Updates all Motors and Sensors
% coupleMotors - Creates and connects a SyncMotor-object using two chosen Motor-objects
% beep - Plays a 'beep' tone on brick.
% playTone - Plays tone on brick.
% stopTone - Stops tone currently played.
% tonePlayed - Tests if a tone is currently played.
% setProperties - Set multiple EV3 properties at once using MATLAB's inputParser.
% General
% EV3
% connect - Connects EV3-object and its Motors and Sensors to physical brick via bt or usb
% disconnect - Disconnects EV3-object and its Motors and Sensors from physical brick
% setProperties - Set multiple EV3 properties at once using MATLAB's inputParser
% Brick functions
% beep - Plays a 'beep' tone on brick
% playTone - Plays tone on brick
% stopTone - Stops tone currently played
% tonePlayed - Tests if a tone is currently played
%
%
% Notes
% * Creating multiple EV3 objects and connecting them to different physical bricks has not
% been thoroughly tested yet, but seems to work on a first glance.
% * When referring to an instance of this class the term 'virtual brick' is used from now
% on. The LEGO brick itself is referred to as 'physical brick'.
% on. The LEGO EV3 brick itself is referred to as 'physical brick'.
%
% Example
% % This small example connects to the brick, starts motorA with power 50, reads a value
% % from sensor1, brakes the motor and disconnects again.
% % This should roughly demonstrate how this toolbox works.
% % This small example connects to the brick, starts motorA with power 50 and time-limit
% % of 5 seconds. As long as it's running, sensor 1 is read and its readings are plotted
% % the end.
%
% brickObject = EV3();
%%%%%%%%%%
% brickObject = EV3('debug', 'on', 'batteryMode', 'Voltage');
% brickObject.connect('ioType', 'bt', 'serPort', '/dev/rfcomm0');
% brickObject.connect('bt', 'serPort', '/dev/rfcomm0');
%
% m = brickObject.motorA;
% m.power = 50;
% m.start();
% s = brickObject.sensor1;
% s.value
% m.tachoCount
% m.brakeMode = 'Brake';
% m.stop();
% brickObject.disconnect();
% delete brickObject;
% delete m; delete s;
%
% m.setProperties('Power', 50, 'BrakeMode', 'Brake', 'TachoLimitMode', 'Time', ...
% 'TachoLimit', 5000);
%
% if s.type ~= DeviceType.Color
% fprintf('Please connect EV3-color-sensor to port 1!\n');
% else
% s.mode = DeviceMode.Color.Ambient;
%
% readings = [];
% time = [];
%
% tic;
% m.start();
% pause(0.5);
% while m.isRunning
% readings = [readings, s.value];
% time = [time toc];
% end
%
% plot(time, readings);
% end
%
% brickObject.disconnect();
% brickObject.delete();
% clear all
%%%%%%%%%%
%
% Signature
% Author: Tim Stadtmann
% Date: 2016/05/19
% Updated: 2016/08/15
properties % Standard properties
%debug - Debug turned on or off
% In debug mode 1 (debug=1/'on'/true), everytime a command is passed to the sublayer
% ('communication layer'), there is feedback in the console about what command has been
% called. Debug mode 2 (debug=2) enables debug mode in the lower layers. Each packet
% sent and received is printed to the console.
% -> Any valid boolean (0/1/'on'/'off'/true/false) or 2 (for enabling debugging in
% lower layers)
debug;
%% Modi
%batteryMode - Mode for reading battery charge
% -> 'Percentage' / 'Voltage'
% See also EV3.BATTERYVALUE
batteryMode = 'Percentage';
%% Debug
debug;
batteryMode;
end
properties (Dependent) % Parameters to be read directly from physical brick
batteryValue; % Current battery status (either in Percentage or Voltage)
%batteryValue - Current battery charge
% Depending on batteryMode, the reading is either in percentage or voltage.
% See also EV3.BATTERYMODE
batteryValue;
end
properties (SetAccess = 'private') % Read-only properties that are set internally
isConnected = false; % Is virtual brick currently connected to physical brick?
commInterface = 0; % Brick object from sub-layer class Brick -> used as interface to the
% physical brick.
%isConnected - Virtual brick connected to physical one?
isConnected = false;
%commInterface - Interface to communication layer
commInterface = 0;
%% Motors and Sensors
......@@ -98,13 +126,13 @@ classdef EV3 < handle
end
properties (Hidden, Access = 'private') % Hidden properties for internal use only
init = 1; % Indicates 'init-phase' (Set to 1 as long as constructor is running)
init = true; % Indicates 'init-phase' (Set to 1 as long as constructor is running)
end
methods % Standard methods
%% Constructor
function ev3 = EV3(varargin)
% Sets properties of EV3-object and creates Motor- and Sensor-objects with default
%EV3 Sets properties of EV3-object and creates Motor- and Sensor-objects with default
% parameters.
%
% Arguments
......@@ -477,14 +505,8 @@ classdef EV3 < handle
end
function set.batteryMode(ev3, batteryMode)
% Check if batteryMode is valid and set ev3.batteryMode if it is.
%
% Arguments
% * batteryMode ('Voltage'/'Percentage'): Mode in which batteryValue will be read.
%
validModes = {'Voltage', 'Percentage'};
if ~ischar(batteryMode) || ~any(validatestring(batteryMode, validModes))
if ~ischar(batteryMode) || ~ismember(batteryMode, validModes)
error('EV3::set.batteryMode: Given parameter is not a valid battery mode.');
else
ev3.batteryMode = batteryMode;
......@@ -492,12 +514,6 @@ classdef EV3 < handle
end
function set.debug(ev3, debug)
% Check if debug is valid and set ev3.debug if it is.
%
% Arguments
% * debug (0/1/'on'/'off'/'true'/'false'/2)
%
if ~isBool(debug) && debug ~= 2
error('EV3::set.debug: Given parameter is not a bool.');
end
......@@ -518,16 +534,18 @@ classdef EV3 < handle
%
% Example
% b = EV3();
% b.connect('ioType', 'bt', 'serPort', '/dev/rfcomm0');
% b.connect('bt', 'serPort', '/dev/rfcomm0');
% b.setProperties('debug', 'on', 'batteryMode', 'Voltage');
% % Instead of: b.debug = 'on'; b.batteryMode = 'Voltage';
%
% See also EV3.DEBUG, EV3.BATTERYMODE
%
p = inputParser();
% Set default values
if ev3.init
defaultDebug = 0;
defaultDebug = false;
defaultBatteryMode = 'Percentage';
else
defaultDebug = ev3.debug;
......
This diff is collapsed.
......@@ -6,4 +6,3 @@ classdef MotorPort < uint8
MotorD (3)
end
end
\ No newline at end of file
......@@ -4,21 +4,18 @@ classdef Sensor < handle
% Properties:
% Standard
% debug - Debug mode turned on or off
% mode - Sensor mode in which the value will be read
% mode - Sensor mode in which the values will be read
% Dependent
% value - Value read from sensor
% get-only
% isConnected - Is virtual brick connected to physical one?
% brick - Brick object from sublayer class Brick -> used as interface to the physical brick.
% type - Type of connected sensor if any
%
% Methods:
% Standard
% EV3 -
% connect - Connects Sensor-object to physical brick.
% disconnect - Disconnects Sensor-object from physical brick.
% update - Updates Sensor-object to current status at its port.
% EV3
% connect - Connects Sensor-object to physical brick
% disconnect - Disconnects Sensor-object from physical brick
% reset - Resets value on sensor
% setProperties - Sets multiple Sensor properties at once using MATLAB's inputParser.
% setProperties - Sets multiple Sensor properties at once using MATLAB's inputParser
%
% Example
% % This small example should only roughly demonstrate how to work with sensors.
......@@ -177,7 +174,7 @@ classdef Sensor < handle
% Set default values
if sensor.init
defaultDebug = 0;
defaultMode = DeviceMode.Error.Undefined;
defaultMode = DeviceMode.Default.Undefined;
else
defaultDebug = sensor.debug;
defaultMode = sensor.mode;
......@@ -203,15 +200,36 @@ classdef Sensor < handle
%% Getter
function value = get.value(sensor)
if ~sensor.isConnected || ~sensor.sensorAtPort
warning('Sensor::get.value: Could not detect sensor at port %s.', ...
sensor.port);
value = 0;
if strcmp(class(sensor.mode), 'DeviceMode.Default') || ...
~isModeValid(sensor.mode, sensor.type)
warning('Sensor::get.value: Cannot read sensor values in current mode.');
return;
end
if sensor.connectedToBrick
try
value = sensor.getValue();
catch
warning('Sensor::get.value: Could not detect sensor at port %d.', ...
sensor.port+1);
return;
end
end
end
function conn = get.physicalSensorConnected(sensor)
currentType = sensor.type;
conn = (currentType<DeviceType.Unknown && ...
(currentType~=DeviceType.MediumMotor && currentType~=DeviceType.LargeMotor));
end
function sensorType = get.type(sensor)
if sensor.connectedToBrick
[sensorType, ~] = sensor.getTypeMode();
else
sensorType = DeviceType.Unknown;
end
end
%% Display
......@@ -224,16 +242,20 @@ classdef Sensor < handle
methods (Access = 'private') % Private brick functions that are wrapped by dependent params
function setMode(sensor, mode)
if ~sensor.isConnected || ~sensor.sensorAtPort
return
if ~sensor.connectedToBrick
error(['Sensor::getTachoCount: Sensor-Object not connected to comm handle.',...
'You have to call sensor.connect(commInterface) first!']);
% elseif ~sensor.physicalSensorConnected
% error('Sensor::getTachoCount: No physical sensor connected to Port %d',...
% sensor.port+1);
end
sensor.brick.inputReadSI(0, sensor.port_, mode); % Reading a value implicitly
sensor.commInterface.inputReadSI(0, sensor.port, mode); % Reading a value implicitly
% sets the mode.
if sensor.debug
fprintf('(DEBUG) Sensor::setMode: Called inputReadSI on Port %s\n',...
sensor.port);
fprintf('(DEBUG) Sensor::setMode: Called inputReadSI on Port %d.\n',...
sensor.port+1);
end
end
......@@ -245,15 +267,15 @@ classdef Sensor < handle
% this case, the inputReadSI-opCode is sent again to get the correct value.
%
if ~sensor.isConnected
error(['Sensor::getValue: Sensor-Object not connected to brick handle.',...
'You have to call sensor.connect(brick) first!']);
elseif ~sensor.sensorAtPort
error('Sensor::getValue: No physical sensor connected to Port %d.',...
sensor.port);
if ~sensor.connectedToBrick
error(['Sensor::getValue: Sensor-Object not connected to comm handle.',...
'You have to call sensor.connect(commInterface) first!']);
% elseif ~sensor.physicalSensorConnected
% error('Sensor::getValue: No physical sensor connected to Port %d.',...
% sensor.port+1);
end
val = sensor.brick.inputReadSI(0, sensor.port_, sensor.mode);
val = sensor.commInterface.inputReadSI(0, sensor.port, sensor.mode);
if strcmp(class(sensor.mode), 'DeviceMode.Color')
if sensor.mode == DeviceMode.Color.Col
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment