/ArduinoIO/ArduinoIO/arduino.m
MATLAB | 1457 lines | 641 code | 347 blank | 469 comment | 164 complexity | 1d13f36ce06c4e1bfcc2717940e0a539 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- classdef arduino < handle
-
- % This class defines an "arduino" object (Version 2.3)
- % Giampiero Campa, Apr 2010, Copyright 2009 The MathWorks, Inc.
-
- properties (SetAccess=private,GetAccess=private)
- aser % Serial Connection
- pins % Pin Status Vector
- srvs % Servo Status Vectord
- mspd % Motor Speed Status
- sspd % Servo Speed Status
- mots % Motor Server Running on the Arduino Board
- end
-
- methods
-
- % constructor, connects to the board and creates an arduino object
- function a=arduino(comPort)
-
- % Add target directories and save the updated path
- addpath(fullfile(pwd));
- savepath
-
- % check nargin
- if nargin<1,
- comPort='DEMO';
- disp('Note: a DEMO connection will be created');
- disp('Use a the com port, e.g. ''COM5'' as input argument to connect to the real board');
- end
-
- % check port
- if ~ischar(comPort),
- error('The input argument must be a string, e.g. ''COM8'' ');
- end
-
- % check if we are already connected
- if isa(a.aser,'serial') && isvalid(a.aser) && strcmpi(get(a.aser,'Status'),'open'),
- disp(['It looks like Arduino is already connected to port ' comPort ]);
- disp('Delete the object to force disconnection');
- disp('before attempting a connection to a different port.');
- return;
- end
-
- % check whether serial port is currently used by MATLAB
- if ~isempty(instrfind({'Port'},{comPort})),
- disp(['The port ' comPort ' is already used by MATLAB']);
- disp(['If you are sure that Arduino is connected to ' comPort]);
- disp('then delete the object to disconnect and execute:');
- disp([' delete(instrfind({''Port''},{''' comPort '''}))']);
- disp('to delete the port before attempting another connection');
- error(['Port ' comPort ' already used by MATLAB']);
- end
-
- % define serial object
- a.aser=serial(comPort);
-
- % connection
- if strcmpi(get(a.aser,'Port'),'DEMO'),
- % handle demo mode
-
- fprintf(1,'Demo mode connection ..');
- for i=1:4,
- fprintf(1,'.');
- pause(1);
- end
- fprintf(1,'\n');
- pause(1);
-
- % chk is 1 or 2 depending on the script running on the board
- chk=round(1+rand);
-
- else
- % actual connection
-
- % open port
- try
- fopen(a.aser);
- catch ME,
- disp(ME.message)
- delete(a);
- error(['Could not open port: ' comPort]);
- end
-
- % it takes several seconds before any operation could be attempted
-
- fprintf(1,'Attempting connection ..');
- for i=1:4,
- fprintf(1,'.');
- pause(1);
- end
- fprintf(1,'\n');
-
- % query script type
- fwrite(a.aser,[57 57],'uchar');
- chk=fscanf(a.aser,'%d');
-
- % exit if there was no answer
- if isempty(chk)
- delete(a);
- error('Connection unsuccessful, please make sure that the Arduino is powered on, running either adiosrv.pde or mororsrv.pde, and that the board is connected to the indicated serial port. You might also try to unplug and re-plug the USB cable before attempting a reconnection.');
- end
-
- end
-
- % check returned value
- if chk==1,
- disp('Basic I/O Script detected !');
- elseif chk==2,
- disp('Motor Shield Script detected !');
- else
- delete(a);
- error('Unknown Script. Please make sure that either adiosrv.pde or motorsrv.pde are running on the Arduino');
- end
-
- % sets a.mots flag
- a.mots=chk-1;
-
- % set a.aser tag
- a.aser.Tag='ok';
-
- % initialize pin vector (-1 is unassigned, 0 is input, 1 is output)
- a.pins=-1*ones(1,19);
-
- % initialize servo vector (-1 is unknown, 0 is detached, 1 is attached)
- a.srvs=0*ones(1,2);
-
- % initialize motor vector (0 to 255 is the speed)
- a.mspd=0*ones(1,4);
-
- % initialize stepper vector (0 to 255 is the speed)
- a.sspd=0*ones(1,2);
-
- % notify successful installation
- disp('Arduino successfully connected !');
-
- end % arduino
-
- % distructor, deletes the object
- function delete(a)
-
- % if it is a serial, valid and open then close it
- if isa(a.aser,'serial') && isvalid(a.aser) && strcmpi(get(a.aser,'Status'),'open'),
- if ~isempty(a.aser.Tag),
- try
- % trying to leave it in a known unharmful state
- for i=2:19,
- a.pinMode(i,'output');
- a.digitalWrite(i,0);
- a.pinMode(i,'input');
- end
- catch ME
- % disp but proceed anyway
- disp(ME.message);
- end
-
- end
- fclose(a.aser);
- end
-
- % if it's an object delete it
- if isobject(a.aser),
- delete(a.aser);
- end
-
-
- end % delete
-
- % disp, displays the object
- function disp(a) % display
- if isvalid(a),
- if isa(a.aser,'serial') && isvalid(a.aser),
- disp(['<a href="matlab:help arduino">arduino</a> object connected to ' a.aser.port ' port']);
- if a.mots==1,
- disp('Motor Shield Server running on the arduino board');
- disp(' ');
- a.servoStatus
- a.motorSpeed
- a.stepperSpeed
- disp(' ');
- disp('Servo Methods: <a href="matlab:help servoStatus">servoStatus</a> <a href="matlab:help servoAttach">servoAttach</a> <a href="matlab:help servoDetach">servoDetach</a> <a href="matlab:help servoRead">servoRead</a> <a href="matlab:help servoWrite">servoWrite</a>');
- disp('DC Motors and Stepper Methods: <a href="matlab:help motorSpeed">motorSpeed</a> <a href="matlab:help motorRun">motorRun</a> <a href="matlab:help stepperSpeed">stepperSpeed</a> <a href="matlab:help stepperStep">stepperStep</a>');
- else
- disp('IO Server running on the arduino board');
- disp(' ');
- a.pinMode
- disp(' ');
- disp('Pin IO Methods: <a href="matlab:help pinMode">pinMode</a> <a href="matlab:help digitalRead">digitalRead</a> <a href="matlab:help digitalWrite">digitalWrite</a> <a href="matlab:help analogRead">analogRead</a> <a href="matlab:help analogWrite">analogWrite</a>');
- end
- disp(' ');
- else
- disp(['<a href="matlab:help arduino">arduino</a> object connected to an invalid serial port']);
- disp('Please delete the arduino object');
- disp(' ');
- end
- else
- disp(['Invalid <a href="matlab:help arduino">arduino</a> object']);
- disp('Please clear the object and instantiate another one');
- disp(' ');
- end
- end
-
- % pin mode, changes pin mode
- function pinMode(a,pin,str)
-
- % a.pinMode(pin,str); specifies the pin mode of a digital pins.
- % The first argument before the function name, a, is the arduino object.
- % The first argument, pin, is the number of the digital pin (2 to 19).
- % The second argument, str, is a string that can be 'input' or 'output',
- % Called with one argument, as a.pin(pin) it returns the mode of
- % the digital pin, called without arguments, prints the mode of all the
- % digital pins. Note that the digital pins from 0 to 13 are located on
- % the upper right part of the board, while the digital pins from 14 to 19
- % are better known as "analog input" pins and are located in the lower
- % right corner of the board.
- %
- % Examples:
- % a.pinMode(11,'output') % sets digital pin #11 as output
- % a.pinMode(10,'input') % sets digital pin #10 as input
- % val=a.pinMode(10); % returns the status of digital pin #10
- % a.pinMode(5); % prints the status of digital pin #5
- % a.pinMode; % prints the status of all pins
- %
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin>3,
- error('This function cannot have more than 3 arguments, object, pin and str');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % if pin argument is there check it
- if nargin>1,
- errstr=a.checknum(pin,'pin number',2:19);
- if ~isempty(errstr), error(errstr); end
- end
-
- % if str argument is there check it
- if nargin>2,
- errstr=a.checkstr(str,'pin mode',{'input','output'});
- if ~isempty(errstr), error(errstr); end
- end
-
- % perform the requested action
- if nargin==3,
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
- %%%%%%%%%%%%%%%%%%%%%%%%% CHANGE PIN MODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % assign value
- if lower(str(1))=='o', val=1; else val=0; end
-
- if strcmpi(get(a.aser,'Port'),'DEMO'),
- % handle demo mode here
-
- % average digital output delay
- pause(0.0087);
-
- else
- % do the actual action here
-
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, pin and value
- fwrite(a.aser,[48 97+pin 48+val],'uchar');
-
- end
-
- % detach servo 1 or 2 if pins 10 or 9 are used
- if pin==10 || pin==9, a.servoDetach(11-pin); end
-
- % store 0 for input and 1 for output
- a.pins(pin)=val;
-
- elseif nargin==2,
- % print pin mode for the requested pin
-
- mode={'UNASSIGNED','set as INPUT','set as OUTPUT'};
- disp(['Digital Pin ' num2str(pin) ' is currently ' mode{2+a.pins(pin)}]);
-
- else
- % print pin mode for each pin
-
- mode={'UNASSIGNED','set as INPUT','set as OUTPUT'};
- for i=2:19;
- disp(['Digital Pin ' num2str(i,'%02d') ' is currently ' mode{2+a.pins(i)}]);
- end
-
- end
-
- end % pinmode
-
- % digital read
- function val=digitalRead(a,pin)
-
- % val=a.digitalRead(pin); performs digital input on a given arduino pin.
- % The first argument before the function name, a, is the arduino object.
- % The argument pin, is the number of the digital pin (2 to 19)
- % where the digital input needs to be performed. Note that the digital pins
- % from 0 to 13 are located on the upper right part of the board, while the
- % digital pins from 14 to 19 are better known as "analog input" pins and
- % are located in the lower right corner of the board.
- %
- % Example:
- % val=a.digitalRead(4); % reads pin #4
- %
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=2,
- error('Function must have the "pin" argument');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check pin
- errstr=arduino.checknum(pin,'pin number',2:19);
- if ~isempty(errstr), error(errstr); end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM DIGITAL INPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO'),
- % handle demo mode
-
- % average digital input delay
- pause(0.0247);
-
- % output 0 or 1 randomly
- val=round(rand);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode and pin
- fwrite(a.aser,[49 97+pin],'uchar');
-
- % get value
- val=fscanf(a.aser,'%d');
-
- end
-
- end % digitalread
-
- % digital write
- function digitalWrite(a,pin,val)
-
- % a.digitalWrite(pin,val); performs digital output on a given pin.
- % The first argument before the function name, a, is the arduino object.
- % The second argument, pin, is the number of the digital pin (2 to 19)
- % where the digital output needs to be performed.
- % The third argument, val, is the value (either 0 or 1) for the output
- % Note that the digital pins from 0 to 13 are located on the upper right part
- % of the board, while the digital pins from 14 to 19 are better known as
- % "analog input" pins and are located in the lower right corner of the board.
- %
- % Examples:
- % a.digitalWrite(13,1); % sets pin #13 high
- % a.digitalWrite(13,0); % sets pin #13 low
- %
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=3,
- error('Function must have the "pin" and "val" arguments');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check pin
- errstr=arduino.checknum(pin,'pin number',2:19);
- if ~isempty(errstr), error(errstr); end
-
- % check val
- errstr=arduino.checknum(val,'value',0:1);
- if ~isempty(errstr), error(errstr); end
-
- % pin should be configured as output
- if a.pins(pin)~=1,
- warning('MATLAB:Arduino:digitalWrite',['If digital pin ' num2str(pin) ' is set as input, digital output takes place only after using a.pinMode(' num2str(pin) ',''output''); ']);
- end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM DIGITAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO'),
- % handle demo mode
-
- % average digital output delay
- pause(0.0087);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, pin and value
- fwrite(a.aser,[50 97+pin 48+val],'uchar');
-
- end
-
- end % digitalwrite
-
- % analog read
- function val=analogRead(a,pin)
-
- % val=a.analogRead(pin); Performs analog input on a given arduino pin.
- % The first argument before the function name, a, is the arduino object.
- % The second argument, pin, is the number of the analog input pin (0 to 5)
- % where the analog input needs to be performed. The returned value, val,
- % ranges from 0 to 1023, with 0 corresponding to an input voltage of 0 volts,
- % and 1023 to a value of 5 volts. Therefore the resolution is .0049 volts
- % (4.9 mV) per unit.
- % Note that the analog input pins 0 to 5 are also known as digital pins
- % from 14 to 19, and are located on the lower right corner of the board.
- % Specifically, analog input pin 0 corresponds to digital pin 14, and analog
- % input pin 5 corresponds to digital pin 19. Performing analog input does
- % not affect the digital state (high, low, digital input) of the pin.
- %
- % Example:
- % val=a.analogRead(0); % reads analog input pin # 0
- %
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=2,
- error('Function must have the "pin" argument');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check pin
- errstr=arduino.checknum(pin,'analog input pin number',0:5);
- if ~isempty(errstr), error(errstr); end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
- %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM ANALOG INPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO'),
- % handle demo mode
-
- % average analog input delay
- pause(0.0267);
-
- % output a random value between 0 and 1023
- val=round(1023*rand);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode and pin
- fwrite(a.aser,[51 97+pin],'uchar');
-
- % get value
- val=fscanf(a.aser,'%d');
-
- end
-
- end % analogread
-
- % function analog write
- function analogWrite(a,pin,val)
-
- % a.analogWrite(pin,val); Performs analog output on a given arduino pin.
- % The first argument before the function name, a, is the arduino object.
- % The first argument, pin, is the number of the DIGITAL pin where the analog
- % (PWM) output needs to be performed. Allowed pins for AO are 3,5,6,9,10,11
- % The second argument, val, is the value from 0 to 255 for the level of
- % analog output. Note that the digital pins from 0 to 13 are located on the
- % upper right part of the board.
- %
- % Examples:
- % a.analogWrite(11,90); % sets pin #11 to 90/255
- % a.analogWrite(3,10); % sets pin #3 to 10/255
- %
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=3,
- error('Function must have the "pin" and "val" arguments');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check pin
- errstr=arduino.checknum(pin,'pwm pin number',[3 5 6 9 10 11]);
- if ~isempty(errstr), error(errstr); end
-
- % check val
- errstr=arduino.checknum(val,'analog output level',0:255);
- if ~isempty(errstr), error(errstr); end
-
- % pin should be configured as output
- if a.pins(pin)~=1,
- warning('MATLAB:Arduino:analogWrite',['If digital pin ' num2str(pin) ' is set as input, pwm output takes place only after using a.pinMode(' num2str(pin) ',''output''); ']);
- end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM ANALOG OUTPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO'),
- % handle demo mode
-
- % average analog output delay
- pause(0.0088);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, pin and value
- fwrite(a.aser,[52 97+pin val],'uchar');
-
- end
-
-
- end % analogwrite
-
- % servo attach
- function servoAttach(a,num)
-
- % a.servoAttach(num); attaches a servo to the corresponding pwm pin.
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the servo, which can be either 1
- % (top servo, uses digital pin 10 for pwm), or 2 (bottom servo, uses digital
- % pin 9 for pwm). Returns Random results if motor shield is not connected.
- %
- % Example:
- % a.servoAttach(1); % attach servo #1
- %
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=2,
- error('Function must have the "num" argument');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check servo number
- errstr=arduino.checknum(num,'servo number',[1 2]);
- if ~isempty(errstr), error(errstr); end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ATTACH SERVO %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
- % handle demo mode
-
- % average digital output delay
- pause(0.0087);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, num and value (1 for attach)
- fwrite(a.aser,[54 96+num 48+1],'uchar');
-
- end
-
- % store the servo statur
- a.srvs(num)=1;
-
- % update pin status to unassigned
- a.pins(11-num)=-1;
-
- end % servoattach
-
- % servo detach
- function servoDetach(a,num)
-
- % a.servoDetach(num); detaches a servo from its corresponding pwm pin.
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the servo, which can be either 1
- % (top servo, uses digital pin 10 for pwm), or 2 (bottom servo, uses digital
- % pin 9 for pwm). Returns random results if motor shield is not connected.
- %
- % Examples:
- % a.servoDetach(1); % detach servo #1
- %
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=2,
- error('Function must have the "num" argument');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check servo number
- errstr=arduino.checknum(num,'servo number',[1 2]);
- if ~isempty(errstr), error(errstr); end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% DETACH SERVO %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
- % handle demo mode
-
- % average digital output delay
- pause(0.0087);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, num and value (0 for detach)
- fwrite(a.aser,[54 96+num 48+0],'uchar');
-
- end
-
- a.srvs(num)=0;
-
- end % servodetach
-
- % servo status
- function val=servoStatus(a,num)
-
- % a.servoStatus(num); Reads the status of a servo (attached/detached)
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the servo, which can be either 1
- % (top servo, uses digital pin 10 for pwm), or 2 (bottom servo,
- % uses digital pin 9 for pwm).
- % The returned value is either 1 (servo attached) or 0 (servo detached),
- % Called without output arguments, the function prints a string specifying
- % the status of the servo. Called without input arguments, the function
- % either returns the status vector or prints the status of each servo.
- % Returns Random results if motor shield is not connected.
- %
- % Examples:
- % val=a.servoStatus(1); % return the status of servo #1
- % a.servoStatus(1); % prints the status of servo #1
- % a.servoStatus; % prints the status of both servos
- %
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin>2,
- error('Function cannot have more than one argument (servo number) beyond the object name');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % with no arguments calls itself recursively for both servos
- if nargin==1,
- if nargout>0,
- val(1)=a.servoStatus(1);
- val(2)=a.servoStatus(2);
- return
- else
- a.servoStatus(1);
- a.servoStatus(2);
- return
- end
- end
-
- % check servo number
- errstr=arduino.checknum(num,'servo number',[1 2]);
- if ~isempty(errstr), error(errstr); end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ASK SERVO STATUS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
- % handle demo mode
-
- % average digital input delay
- pause(0.0247);
-
- % output 0 or 1 randomly
- val=round(rand);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode and num
- fwrite(a.aser,[53 96+num],'uchar');
-
- % get value
- val=fscanf(a.aser,'%d');
-
- end
-
- a.srvs(num)=val;
-
- if nargout==0,
- str={'DETACHED','ATTACHED'};
- disp(['Servo ' num2str(num) ' is ' str{1+val}]);
- clear val
- return
- end
-
- end % servostatus
-
- % servo read
- function val=servoRead(a,num)
-
- % val=a.servoRead(num); reads the angle of a given servo.
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the servo, which can be either
- % 1 (top servo, uses digital pin 10 for pwm), or 2 (bottom servo, uses
- % digital pin 9 for pwm). The returned value is the angle in degrees,
- % typically from 0 to 180. Returns Random results if motor shield is not
- % connected.
- %
- % Example:
- % val=a.servoRead(1); % reads angle from servo #1
- %
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=2,
- error('Function must have the servo number argument');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check servo number
- errstr=arduino.checknum(num,'servo number',[1 2]);
- if ~isempty(errstr), error(errstr); end
-
- % check status
- if a.srvs(num)~=1,
- error(['Servo ' num2str(num) ' is not attached, please use a.servoAttach(' num2str(num) ') to attach it']);
- end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% READ SERVO ANGLE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
- % handle demo mode
-
- % average analog input delay
- pause(0.0267);
-
- % output a random value between 0 and 180
- val=round(180*rand);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode and num
- fwrite(a.aser,[55 96+num],'uchar');
-
- % get value
- val=fscanf(a.aser,'%d');
-
- end
-
- end % servoread
-
- % servo write
- function servoWrite(a,num,val)
-
- % a.servoWrite(num,val); writes an angle on a given servo.
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the servo, which can be
- % either 1 (top servo, uses digital pin 10 for pwm), or 2 (bottom servo,
- % uses digital pin 9 for pwm). The third argument is the angle in degrees,
- % typically from 0 to 180. Returns Random results if motor shield is not
- % connected.
- %
- % Example:
- % a.servoWrite(1,45); % rotates servo #1 of 45 degrees
- %
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=3,
- error('Function must have the servo number and angle arguments');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check servo number
- errstr=arduino.checknum(num,'servo number',[1 2]);
- if ~isempty(errstr), error(errstr); end
-
- % check angle value
- errstr=arduino.checknum(val,'angle',0:180);
- if ~isempty(errstr), error(errstr); end
-
- % check status
- if a.srvs(num)~=1,
- error(['Servo ' num2str(num) ' is not attached, please use a.servoAttach(' num2str(num) ') to attach it']);
- end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% WRITE ANGLE TO SERVO %%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
- % handle demo mode
-
- % average analog output delay
- pause(0.0088);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, num and value
- fwrite(a.aser,[56 96+num val],'uchar');
-
- end
-
- end % servowrite
-
- % motor speed
- function val=motorSpeed(a,num,val)
-
- % val=a.motorSpeed(num,val); sets the speed of a DC motor.
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the motor, which can go
- % from 1 to 4 (the motor ports are numbered on the motor shield).
- % The third argument is the speed from 0 (stopped) to 255 (maximum), note
- % that depending on the motor speeds of at least 60 might be necessary
- % to actually run it. Called with one argument, as a.motorSpeed(num),
- % it returns the speed at which the given motor is set to run. If there
- % is no output argument it prints the speed of the motor.
- % Called without arguments, itprints the speed of each motor.
- % Note that you must use the command a.motorRun to actually run
- % the motor at the given speed, either forward or backwards.
- % Returns Random results if motor shield is not connected.
- %
- % Examples:
- % a.motorSpeed(4,200) % sets speed of motor 4 as 200/255
- % val=a.motorSpeed(1); % returns the speed of motor 1
- % a.motorSpeed(3); % prints the speed of motor 3
- % a.motorSpeed; % prints the speed of all motors
- %
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin>3,
- error('This function cannot have more than 3 arguments, arduino object, motor number and speed');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % if motor number is there check it
- if nargin>1,
- errstr=arduino.checknum(num,'motor number',1:4);
- if ~isempty(errstr), error(errstr); end
- end
-
- % if speed argument is there check it
- if nargin>2,
- errstr=arduino.checknum(val,'speed',0:255);
- if ~isempty(errstr), error(errstr); end
- end
-
- % perform the requested action
- if nargin==3,
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
- %%%%%%%%%%%%%%%%%%%%%%%%% SET MOTOR SPEED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
- % handle demo mode
-
- % average analog output delay
- pause(0.0088);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, num and value
- fwrite(a.aser,[65 48+num val],'uchar');
- val=fscanf(a.aser,'%d');
-
- end
-
- % store speed value in case it needs to be retrieved
- % a.mspd(num)=val;
- a.mspd(num)=val;
-
-
- % clear val if is not needed as output
- if nargout==0,
- clear val;
- end
-
- elseif nargin==2,
-
- if nargout==0,
- % print speed value
- disp(['The speed of motor number ' num2str(num) ' is set to: ' num2str(a.mspd(num)) ' over 252']);
- else
- % return speed value
- val=a.mspd(num);
-
- end
-
- else
-
- if nargout==0,
- % print speed value for each motor
- for num=1:4,
- disp(['The speed of motor number ' num2str(num) ' is set to: ' num2str(a.mspd(num)) ' over 257']);
- end
- else
- % return speed values
- val=a.mspd(num);
-
- end
-
- end
-
- end % motorspeed
-
- % motor run
- function motorRun(a,num,str)
-
- % a.motorRun(num,str); runs a given DC motor.
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the motor, which can go
- % from 1 to 4 (the motor ports are numbered on the motor shield).
- % The third argument, str, is a string that can be 'forward' (runs the
- % motor forward) 'backward' (runs the motor backward) or 'release',
- % (stops the motor). Returns Random results if motor shield is not
- % connected.
- %
- % Examples:
- % a.motorRun(1,'forward'); % runs motor 1 forward
- % a.motorRun(3,'backward'); % runs motor 3 backward
- % a.motorRun(1,'release'); % release motor 1
- %
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin~=3,
- error('Function must have 3 arguments, object, motor number and direction');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % check motor number
- errstr=arduino.checknum(num,'motor number',1:4);
- if ~isempty(errstr), error(errstr); end
-
- % check direction
- errstr=arduino.checkstr(str,'direction',{'forward','backward','release'});
- if ~isempty(errstr), error(errstr); end
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
-
- %%%%%%%%%%%%%%%%%%%%%%%%% RUN THE MOTOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots ==0,
- % handle demo mode
-
- % average analog output delay
- pause(0.0088);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, num and value
- fwrite(a.aser,[66 48+num abs(str(1))],'uchar');
-
- end
-
- end % motorrun
-
- % stepper speed
- function val=stepperSpeed(a,num,val)
-
- % val=a.stepperSpeed(num,val); sets the speed of a given stepper motor
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the stepper motor,
- % which can go from 1 to 4 (the motor ports are numbered on the motor shield).
- % The third argument is the RPM speed from 1 (minimum) to 255 (maximum).
- % Called with one argument, as a.stepperSpeed(num), it returns the
- % speed at which the given motor is set to run. If there is no output
- % argument it prints the speed of the stepper motor.
- % Called without arguments, itprints the speed of each stepper motor.
- % Note that you must use the command a.stepperStep to actually run
- % the motor at the given speed, either forward or backwards (or release
- % it). Returns Random results if motor shield is not connected.
- %
- % Examples:
- % a.stepperSpeed(2,50) % sets speed of stepper 2 as 50 rpm
- % val=a.stepperSpeed(1); % returns the speed of stepper 1
- % a.stepperSpeed(2); % prints the speed of stepper 2
- % a.stepperSpeed; % prints the speed of both steppers
- %
-
- %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- % check nargin
- if nargin>3,
- error('This function cannot have more than 3 arguments, object, stepper number and speed');
- end
-
- % first argument must be the arduino variable
- if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
-
- % if stepper number is there check it
- if nargin>1,
- errstr=arduino.checknum(num,'stepper number',1:2);
- if ~isempty(errstr), error(errstr); end
- end
-
- % if speed argument is there check it
- if nargin>2,
- errstr=arduino.checknum(val,'speed',0:255);
- if ~isempty(errstr), error(errstr); end
- end
-
- % perform the requested action
- if nargin==3,
-
- % check a.aser for validity
- errstr=arduino.checkser(a.aser,'valid');
- if ~isempty(errstr), error(errstr); end
-
- %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM ANALOG OUTPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
- % handle demo mode
-
- % average analog output delay
- pause(0.0088);
-
- else
-
- % check a.aser for openness
- errstr=arduino.checkser(a.aser,'open');
- if ~isempty(errstr), error(errstr); end
-
- % send mode, num and value
- fwrite(a.aser,[67 48+num val],'uchar');
-
- end
-
- % store speed value in case it needs to be retrieved
- a.sspd(num)=val;
-
- % clear val if is not needed as output
- if nargout==0,
- clear val;
- end
-
- elseif nargin==2,
-
- if nargout==0,
- % print speed value
- disp(['The speed of stepper number ' num2str(num) ' is set to: ' num2str(a.sspd(num)) ' over 255']);
- else
- % return speed value
- val=a.sspd(num);
- end
-
- else
-
- if nargout==0,
- % print speed value for each stepper
- for num=1:2,
- disp(['The speed of stepper number ' num2str(num) ' is set to: ' num2str(a.sspd(num)) ' over 255']);
- end
- else
- % return speed values
- val=a.sspd;
- end
-
- end
-
- end % stepperspeed
-
- % stepper step
- function stepperStep(a,num,dir,sty,steps)
-
- % a.stepperStep(num,dir,sty,steps); rotates a given stepper motor
- % The first argument before the function name, a, is the arduino object.
- % The second argument, num, is the number of the stepper motor, which is
- % either 1 or 2. The third argument, the direction, is a string that can
- % be 'forward' (runs the motor forward) 'backward' (runs the motor backward)
- % or 'release', (stops and releases the motor). Unless the direction is
- % 'release', then two more argument are needed: the fourth one is the style,
- % which is a string specifying the style of the motion, and can be 'single'
- % (only one coil activated at a time), 'double' (2 coils activated, gives
- % an higher torque and power consumption) 'inteleave', (alternates bet…
Large files files are truncated, but you can click here to view the full file