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

Merge branch 'feature-display'

Implements overriden display-function and adapts comments to MATLAB's
doc and help function in order to deliver a cleaner interface.
Also, for some reason, all file-access modes in /source are marked as
changed.
parent dc5c080f
No related branches found
No related tags found
No related merge requests found
Showing with 159 additions and 165 deletions
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
source/Color.m 100644 → 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
source/EV3.m 100644 → 100755
classdef EV3 < handle
classdef EV3 < MaskedHandle
% High-level class to work with physical bricks.
%
% This is the 'central' class (from user's view) when working with this toolbox. It
% delivers a convenient interface for creating a connection to the brick and sending
% commands to it.
%
% Properties:
% Standard
% 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 - Interface to communication layer
%
% Methods:
% 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 EV3 brick itself is referred to as 'physical brick'.
%
% Example
% % 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('debug', 'on', 'batteryMode', 'Voltage');
% brickObject.connect('bt', 'serPort', '/dev/rfcomm0');
%
% m = brickObject.motorA;
% s = brickObject.sensor1;
%
% 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
%debug - [Writable] Debug turned on or off (0/1/off/on/'false'/'true'/2)
% 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'
%batteryMode - [Writable] Mode for reading battery charge ('Percentage'/'Voltage')
% See also EV3.BATTERYVALUE
batteryMode;
end
properties (Dependent) % Parameters to be read directly from physical brick
%batteryValue - Current battery charge
%batteryValue - [Read-only] 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 - Virtual brick connected to physical one?
%isConnected - [Read-only] Virtual brick connected to physical one?
% See also EV3.CONNECT, EV3.DISCONNECT
isConnected = false;
%% Motors and Sensors
%motorA - [Read-only]
% See also MOTOR
motorA;
%motorB - [Read-only]
% See also MOTOR
motorB;
%motorC - [Read-only]
% See also MOTOR
motorC;
%motorD - [Read-only]
% See also MOTOR
motorD;
%sensor1 - [Read-only]
% See also SENSOR
sensor1;
%sensor2 - [Read-only]
% See also SENSOR
sensor2;
%sensor3 - [Read-only]
% See also SENSOR
sensor3;
%sensor4 - [Read-only]
% See also SENSOR
sensor4;
end
......@@ -466,13 +412,51 @@ classdef EV3 < handle
bat = ev3.getBattery();
end
%% Display
function display(ev3)
% WIP
warning('off','all');
builtin('disp', ev3);
displayProperties(ev3);
fprintf('\n\tDevices\n');
props = properties(ev3);
warning('off', 'all'); % Turn off warnings while reading values
for i = 1:length(props)
p = props{i};
if strcmp(class(ev3.(p)),'Sensor') || strcmp(class(ev3.(p)), 'Motor')
fprintf('\t%15s [Type: %s]\n', p, char(ev3.(p).type));
end
end
warning('on', 'all');
end
% function display(ev3)
% warning('off','all');
% %builtin('disp', ev3);
%
%
% props = properties(ev3); % Save list with property names as strings
% fprintf(' <a href="matlab:helpPopup EV3">EV3</a> with properties:\n\n');
% for i = 1:length(props)
% p = props{i};
%
% if isa(ev3.(p), 'handle') && ev3.(p).isvalid % Valid handle?
% if isa(ev3.(p), 'Brick') % Communication interface?
% fprintf('\t%15s: Brick-object (Interface for comm-layer)\n');
% else % Motor or Sensor object?
% fprintf('\t%15s: %s-object for port %s\n', p, class(ev3.(p)), ...
% port2str(class(ev3.(p)), ev3.(p).port));
% end
% elseif isnumeric(ev3.(p))
% fprintf('\t%15s: %d\n', p, ev3.(p));
% elseif ischar(ev3.(p))
% fprintf('\t%15s: %s\n', p, ev3.(p));
% else
% warning('EV3::display: Unsuspected parameter type.'); % Not really useful
% end
% end
%
% warning('on','all');
%
% end
end
methods (Access = 'private') % Private brick functions that are wrapped by dependent params
......
source/ID.m 100644 → 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
classdef MaskedHandle < handle
methods (Hidden)
function addlistener(varargin)
addlistener@addlistener(varargin{:})
end
function findobj(varargin)
findobj@findobj(varargin{:});
end
% function findprop(varargin)
% findprop@findprop(varargin{:});
% end
function notify(varargin)
notify@notify(varargin{:});
end
function ge(h1, h2)
ge@ge(h1, h2);
end
function gt(h1, h2)
gt@gt(h1, h2);
end
function le(h1, h2)
le@le(h1, h2);
end
function lt(h1, h2)
lt@lt(h1, h2);
end
function ne(h1, h2)
ne@ne(h1, h2);
end
function eq(h1, h2)
eq@eq(h1, h2);
end
end
end
source/Motor.m 100644 → 100755
classdef Motor < handle & dynamicprops
classdef Motor < MaskedHandle & dynamicprops
% Motor High-level class to work with motors.
%
% This class is supposed to ease the use of the brick's motors. It is possible to set all
% kinds of parameters, request the current status of the motor ports and of course send
% commands to the brick to be executed on the respective port.
%
% Properties:
% Standard
% power - Power level of motor in percent
% speedRegulation - Speed regulation turned on or off
% smoothStart - Degrees/Time for how far/long the motor should smoothly start (depends on limitMode)
% smoothStop - Degrees/Time for how far/long the motor should smoothly stop (depends on limitMode)
% limitValue - Degrees/Time for how far/long the motor should run (depends on limitMode)
% limitMode - Mode for motor limit
% brakeMode - Mode for braking
% debug - Debug mode turned on or off
% Dependent
% isRunning - True if motor is running (currentSpeed > 0)
% tachoCount - Current tacho count
% currentSpeed - Current speed of motor (should equal power if speedRegulation = 1)
% type - Current motor type (large or medium motor)
% Other
% port - Motor port
%
% Methods:
% Standard
% Motor
% connect - Connects Motor-object to physical brick
% disconnect - Disconnects Motor-object from physical brick
% setProperties - Sets multiple Motor properties at once using MATLAB's inputParser
% Brick functions
% start - Starts the motor taking all parameters set by user into account
% stop - Stops the motor :)
% syncedStart - Starts the motor synchronized with another
% syncedStop - Stops the motors previously started together with syncedStart
% waitFor - Stops execution of program as long as motor is running with tacholimit
% reset - Resets internal tacho count
% resetTachoCount - Resets tacho count to 0 (if running without tacholimit)
%
%
% Notes:
% * You don't need to create instances of this class. The EV3-class automatically creates
% instances for each motor port, and you can work with them via the EV3-object.
......@@ -50,60 +16,65 @@ classdef Motor < handle & dynamicprops
% Updated: 2016/08/15
properties % Standard properties to be set by user
%power - Power level of motor in percent
% -> Any numeric in [-100, 100]
%power - [Writable] Power level of motor in percent (numeric in [-100, 100])
power;
%speedRegulation - Speed regulation turned on or off
% -> Any valid boolean (0/1/'on'/'off'/true/false)
%speedRegulation - [Writable] Speed regulation turned on or off (0/1/off/on/'false'/'true')
% When turned on, motor will try to 'hold' its speed at given power level, whatever the
% load. In this mode, the highest possible speed depends on the load and mostly goes up
% to around 70-80 (at this point, the Brick internally input 100% power).
% When turned off, motor will constantly input the same power into the motor. The
% resulting speed will be somewhat lower, depending on the load.
speedRegulation;
%smoothStart - Degrees/Time for how far/long the motor should smoothly start (depends on limitMode)
% -> Any numeric in [0, limitValue]
%smoothStart - [Writable] Degrees/Time indicating how far/long the motor should smoothly start (numeric such that smoothStart+smoothStop < limitValue)
% Depending on limitMode, the input is interpreted either in degrees or milliseconds.
% The first [smoothStart]-milliseconds/degrees of limitValue the motor will slowly
% accelerate until reaching it's defined speed.
% See also MOTOR.LIMITMODE
smoothStart;
%smoothStop - Degrees/Time for how far/long the motor should smoothly stop (depends on limitMode)
% -> Any numeric in [0, limitValue]
%smoothStop - [Writable] Degrees/Time indicating how far/long the motor should smoothly stop (numeric such that smoothStart+smoothStop < limitValue)
% Depending on limitMode, the input is interpreted either in degrees or milliseconds.
% The last [smoothStop]-milliseconds/degrees of limitValue the motor will slowly
% slow down until it has stopped.
% See also MOTOR.LIMITMODE
smoothStop;
%limitValue- Degrees/Time for how far/long the motor should run (depends on limitMode)
% -> Any numeric >= 0 (in ms, if limitMode = 'Time')
%limitValue - [Writable] Degrees/Time indicating how far/long the motor should run (numeric>=0)
% Depending on limitMode, the input is interpreted either in degrees or milliseconds.
% See also MOTOR.LIMITMODE
limitValue;
%limitMode - Mode for motor limit
% -> 'Tacho' / 'Time'
%limitMode - [Writable] Mode for motor limit ('Tacho'/'Time')
% See also MOTOR.SMOOTHSTART, MOTOR.SMOOTHSTOP, MOTOR.LIMITMODE
limitMode;
%brakeMode - Mode for braking
% -> 'Brake' / 'Coast'
%brakeMode - [Writable] Mode for braking ('Brake'/'Coast')
% If 'Coast', the motor will (at tacholimit, if ~=0) coast to a stop. If
% 'Brake', the motor will stop immediately (at tacholimit, if ~=0) and hold the brake.
brakeMode;
%debug - Debug turned on or off
%debug - [Writable] Debug turned on or off (0/1/off/on/'false'/'true')
% In debug mode, everytime a command is passed to the sublayer ('communication layer'),
% there is feedback in the console about what command has been called, etc.
% -> Any valid boolean (0/1/'on'/'off'/true/false)
% there is feedback in the console about what command has been called
debug;
end
properties (SetAccess = 'private')
%port - Motor port
% This is only the string representation of the motor port to work with.
% Internally, either MotorPort-, MotorBitfield- or MotorInput-member will be used.
% -> 'A' / 'B' / 'C' / 'D'
port;
end
properties (Dependent) % Read-only parameters to be read directly from physical brick
%isRunning - True if motor is running (speed > 0)
%isRunning - [Read-only] True if motor is running (i.e. speed > 0)
isRunning;
%tachoCount - Current tacho count
%tachoCount - [Read-only] Current tacho count
tachoCount;
%currentSpeed - Current speed of motor (equals power if speedRegulation = true)
%currentSpeed - [Read-only] Current speed of motor
% If speedRegulation=on this should equal power, otherwise it will probably be lower
% than that.
% See also MOTOR.SPEEDREGULATION
currentSpeed;
%type - Type of connected device if any
%type - [Read-only] Type of connected device if any
% See also DEVICETYPE
type;
end
......@@ -112,6 +83,10 @@ classdef Motor < handle & dynamicprops
commInterface; % Communication interface
%% Hidden brick parameters
%port - [Read-only] Motor port
% This is only the string representation of the motor port to work with.
% Internally, either MotorPort-, MotorBitfield- or MotorInput-member will be used.
port;
% brakeMode is an actual parameter on the brick. To avoid inconsistencies with other
% modi and to prettify the output, a string representing it is saved. In order to avoid
......@@ -154,7 +129,7 @@ classdef Motor < handle & dynamicprops
%% Brick functions
function start(motor)
%start Starts the motor taking all parameters set by user into account.
%start Starts the motor
%
% Notes
% * Right now, alternatingly calling this function with and without tacho limit
......@@ -268,7 +243,7 @@ classdef Motor < handle & dynamicprops
end
function stop(motor)
%stop Stops the motor. :)
%stop Stops the motor
if ~motor.connectedToBrick
error(['Motor::stop: Motor-Object not connected to comm handle.',...
......@@ -291,9 +266,9 @@ classdef Motor < handle & dynamicprops
function syncedStart(motor, syncMotor, varargin)
%syncedStart Starts this motor synchronized with another
% This motor acts as a 'master', meaning that the synchronized control is done via
% it. When syncedStart is called, the master sets the slave's (syncMotor)
% properties to keep it consistent with the last parameters sent to the physical
% brick. So, for example, changing the power on the master motor will take effect
% this one. When syncedStart is called, the master sets some of the slave's
% (syncMotor) properties to keep it consistent with the physical brick. So, for
% example, changing the power on the master motor will take effect
% on the slave as soon as this method is called.
% The following parameters will be affected on the slave: power, brakeMode,
% limitValue, speedRegulation
......@@ -315,7 +290,7 @@ classdef Motor < handle & dynamicprops
% -> Values greater than +100 makes the right motor run the opposite
% direction of the left motor (Spin)"
% * This is right now a pretty 'heavy' function, as it tests if both motors are
% connected AND aren't running, wasting four commands, so keep that in mind
% connected AND aren't running, wasting four packets, so keep that in mind
%
% Example
% b = EV3(); b.connect('usb');
......@@ -448,16 +423,16 @@ classdef Motor < handle & dynamicprops
end
function waitFor(motor)
%waitFor Stops execution of program as long as motor is running (WITH TACHOLIMIT).
%waitFor Stops execution of program as long as motor is running
%
% Notes:
% * This one's a bit tricky. The opCode OutputReady makes the brick stop sending
% * (OLD)This one's a bit tricky. The opCode OutputReady makes the brick stop sending
% responses until the motor has stopped. For security reasons, in this toolbox
% there is an internal timeout for receiving messages from the brick. It raises
% an error if a reply takes too long, which would happen in this case. To
% an error if a reply takes too long, which would happen in this case. As a
% workaround, there is an infinite loop that catches errors from outputReady and
% continues then, until outputReady will actually finish without an error.
% * OutputReady (like OutputTest in isRunning) sometimes doesn't work. If
% * (OLD)OutputReady (like OutputTest in isRunning) sometimes doesn't work. If
% outputReady returns in less than a second, another while-loop iterates until
% the motor has stopped, this time using motor.isRunning() (this only works as
% long as not both OutputTest and OutputReady are buggy).
......@@ -533,13 +508,6 @@ classdef Motor < handle & dynamicprops
end
function resetTachoCount(motor)
%resetTachoCount Resets tacho count to 0 if running without tacholimit
% Compared to motor.reset, this resets the 'sensor mode' tacho count, a second
% tacho counter. This counter is used for reading the tacho count with inputRead
% and outputGetCount (via motor.tachoCount).
%
% See also MOTOR.RESET
%
if ~motor.connectedToBrick
error(['Motor::resetTachoCount: Motor-Object not connected to comm handle.',...
......@@ -830,9 +798,7 @@ classdef Motor < handle & dynamicprops
%% Display
function display(motor)
warning('off','all');
builtin('disp', motor);
warning('on','all');
displayProperties(motor);
end
end
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment