PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ArduinoIO/ArduinoIO/arduino.m

https://code.google.com/p/arduinolab/
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

  1. classdef arduino < handle
  2. % This class defines an "arduino" object (Version 2.3)
  3. % Giampiero Campa, Apr 2010, Copyright 2009 The MathWorks, Inc.
  4. properties (SetAccess=private,GetAccess=private)
  5. aser % Serial Connection
  6. pins % Pin Status Vector
  7. srvs % Servo Status Vectord
  8. mspd % Motor Speed Status
  9. sspd % Servo Speed Status
  10. mots % Motor Server Running on the Arduino Board
  11. end
  12. methods
  13. % constructor, connects to the board and creates an arduino object
  14. function a=arduino(comPort)
  15. % Add target directories and save the updated path
  16. addpath(fullfile(pwd));
  17. savepath
  18. % check nargin
  19. if nargin<1,
  20. comPort='DEMO';
  21. disp('Note: a DEMO connection will be created');
  22. disp('Use a the com port, e.g. ''COM5'' as input argument to connect to the real board');
  23. end
  24. % check port
  25. if ~ischar(comPort),
  26. error('The input argument must be a string, e.g. ''COM8'' ');
  27. end
  28. % check if we are already connected
  29. if isa(a.aser,'serial') && isvalid(a.aser) && strcmpi(get(a.aser,'Status'),'open'),
  30. disp(['It looks like Arduino is already connected to port ' comPort ]);
  31. disp('Delete the object to force disconnection');
  32. disp('before attempting a connection to a different port.');
  33. return;
  34. end
  35. % check whether serial port is currently used by MATLAB
  36. if ~isempty(instrfind({'Port'},{comPort})),
  37. disp(['The port ' comPort ' is already used by MATLAB']);
  38. disp(['If you are sure that Arduino is connected to ' comPort]);
  39. disp('then delete the object to disconnect and execute:');
  40. disp([' delete(instrfind({''Port''},{''' comPort '''}))']);
  41. disp('to delete the port before attempting another connection');
  42. error(['Port ' comPort ' already used by MATLAB']);
  43. end
  44. % define serial object
  45. a.aser=serial(comPort);
  46. % connection
  47. if strcmpi(get(a.aser,'Port'),'DEMO'),
  48. % handle demo mode
  49. fprintf(1,'Demo mode connection ..');
  50. for i=1:4,
  51. fprintf(1,'.');
  52. pause(1);
  53. end
  54. fprintf(1,'\n');
  55. pause(1);
  56. % chk is 1 or 2 depending on the script running on the board
  57. chk=round(1+rand);
  58. else
  59. % actual connection
  60. % open port
  61. try
  62. fopen(a.aser);
  63. catch ME,
  64. disp(ME.message)
  65. delete(a);
  66. error(['Could not open port: ' comPort]);
  67. end
  68. % it takes several seconds before any operation could be attempted
  69. fprintf(1,'Attempting connection ..');
  70. for i=1:4,
  71. fprintf(1,'.');
  72. pause(1);
  73. end
  74. fprintf(1,'\n');
  75. % query script type
  76. fwrite(a.aser,[57 57],'uchar');
  77. chk=fscanf(a.aser,'%d');
  78. % exit if there was no answer
  79. if isempty(chk)
  80. delete(a);
  81. 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.');
  82. end
  83. end
  84. % check returned value
  85. if chk==1,
  86. disp('Basic I/O Script detected !');
  87. elseif chk==2,
  88. disp('Motor Shield Script detected !');
  89. else
  90. delete(a);
  91. error('Unknown Script. Please make sure that either adiosrv.pde or motorsrv.pde are running on the Arduino');
  92. end
  93. % sets a.mots flag
  94. a.mots=chk-1;
  95. % set a.aser tag
  96. a.aser.Tag='ok';
  97. % initialize pin vector (-1 is unassigned, 0 is input, 1 is output)
  98. a.pins=-1*ones(1,19);
  99. % initialize servo vector (-1 is unknown, 0 is detached, 1 is attached)
  100. a.srvs=0*ones(1,2);
  101. % initialize motor vector (0 to 255 is the speed)
  102. a.mspd=0*ones(1,4);
  103. % initialize stepper vector (0 to 255 is the speed)
  104. a.sspd=0*ones(1,2);
  105. % notify successful installation
  106. disp('Arduino successfully connected !');
  107. end % arduino
  108. % distructor, deletes the object
  109. function delete(a)
  110. % if it is a serial, valid and open then close it
  111. if isa(a.aser,'serial') && isvalid(a.aser) && strcmpi(get(a.aser,'Status'),'open'),
  112. if ~isempty(a.aser.Tag),
  113. try
  114. % trying to leave it in a known unharmful state
  115. for i=2:19,
  116. a.pinMode(i,'output');
  117. a.digitalWrite(i,0);
  118. a.pinMode(i,'input');
  119. end
  120. catch ME
  121. % disp but proceed anyway
  122. disp(ME.message);
  123. end
  124. end
  125. fclose(a.aser);
  126. end
  127. % if it's an object delete it
  128. if isobject(a.aser),
  129. delete(a.aser);
  130. end
  131. end % delete
  132. % disp, displays the object
  133. function disp(a) % display
  134. if isvalid(a),
  135. if isa(a.aser,'serial') && isvalid(a.aser),
  136. disp(['<a href="matlab:help arduino">arduino</a> object connected to ' a.aser.port ' port']);
  137. if a.mots==1,
  138. disp('Motor Shield Server running on the arduino board');
  139. disp(' ');
  140. a.servoStatus
  141. a.motorSpeed
  142. a.stepperSpeed
  143. disp(' ');
  144. 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>');
  145. 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>');
  146. else
  147. disp('IO Server running on the arduino board');
  148. disp(' ');
  149. a.pinMode
  150. disp(' ');
  151. 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>');
  152. end
  153. disp(' ');
  154. else
  155. disp(['<a href="matlab:help arduino">arduino</a> object connected to an invalid serial port']);
  156. disp('Please delete the arduino object');
  157. disp(' ');
  158. end
  159. else
  160. disp(['Invalid <a href="matlab:help arduino">arduino</a> object']);
  161. disp('Please clear the object and instantiate another one');
  162. disp(' ');
  163. end
  164. end
  165. % pin mode, changes pin mode
  166. function pinMode(a,pin,str)
  167. % a.pinMode(pin,str); specifies the pin mode of a digital pins.
  168. % The first argument before the function name, a, is the arduino object.
  169. % The first argument, pin, is the number of the digital pin (2 to 19).
  170. % The second argument, str, is a string that can be 'input' or 'output',
  171. % Called with one argument, as a.pin(pin) it returns the mode of
  172. % the digital pin, called without arguments, prints the mode of all the
  173. % digital pins. Note that the digital pins from 0 to 13 are located on
  174. % the upper right part of the board, while the digital pins from 14 to 19
  175. % are better known as "analog input" pins and are located in the lower
  176. % right corner of the board.
  177. %
  178. % Examples:
  179. % a.pinMode(11,'output') % sets digital pin #11 as output
  180. % a.pinMode(10,'input') % sets digital pin #10 as input
  181. % val=a.pinMode(10); % returns the status of digital pin #10
  182. % a.pinMode(5); % prints the status of digital pin #5
  183. % a.pinMode; % prints the status of all pins
  184. %
  185. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  186. % check nargin
  187. if nargin>3,
  188. error('This function cannot have more than 3 arguments, object, pin and str');
  189. end
  190. % first argument must be the arduino variable
  191. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  192. % if pin argument is there check it
  193. if nargin>1,
  194. errstr=a.checknum(pin,'pin number',2:19);
  195. if ~isempty(errstr), error(errstr); end
  196. end
  197. % if str argument is there check it
  198. if nargin>2,
  199. errstr=a.checkstr(str,'pin mode',{'input','output'});
  200. if ~isempty(errstr), error(errstr); end
  201. end
  202. % perform the requested action
  203. if nargin==3,
  204. % check a.aser for validity
  205. errstr=arduino.checkser(a.aser,'valid');
  206. if ~isempty(errstr), error(errstr); end
  207. %%%%%%%%%%%%%%%%%%%%%%%%% CHANGE PIN MODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  208. % assign value
  209. if lower(str(1))=='o', val=1; else val=0; end
  210. if strcmpi(get(a.aser,'Port'),'DEMO'),
  211. % handle demo mode here
  212. % average digital output delay
  213. pause(0.0087);
  214. else
  215. % do the actual action here
  216. % check a.aser for openness
  217. errstr=arduino.checkser(a.aser,'open');
  218. if ~isempty(errstr), error(errstr); end
  219. % send mode, pin and value
  220. fwrite(a.aser,[48 97+pin 48+val],'uchar');
  221. end
  222. % detach servo 1 or 2 if pins 10 or 9 are used
  223. if pin==10 || pin==9, a.servoDetach(11-pin); end
  224. % store 0 for input and 1 for output
  225. a.pins(pin)=val;
  226. elseif nargin==2,
  227. % print pin mode for the requested pin
  228. mode={'UNASSIGNED','set as INPUT','set as OUTPUT'};
  229. disp(['Digital Pin ' num2str(pin) ' is currently ' mode{2+a.pins(pin)}]);
  230. else
  231. % print pin mode for each pin
  232. mode={'UNASSIGNED','set as INPUT','set as OUTPUT'};
  233. for i=2:19;
  234. disp(['Digital Pin ' num2str(i,'%02d') ' is currently ' mode{2+a.pins(i)}]);
  235. end
  236. end
  237. end % pinmode
  238. % digital read
  239. function val=digitalRead(a,pin)
  240. % val=a.digitalRead(pin); performs digital input on a given arduino pin.
  241. % The first argument before the function name, a, is the arduino object.
  242. % The argument pin, is the number of the digital pin (2 to 19)
  243. % where the digital input needs to be performed. Note that the digital pins
  244. % from 0 to 13 are located on the upper right part of the board, while the
  245. % digital pins from 14 to 19 are better known as "analog input" pins and
  246. % are located in the lower right corner of the board.
  247. %
  248. % Example:
  249. % val=a.digitalRead(4); % reads pin #4
  250. %
  251. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  252. % check nargin
  253. if nargin~=2,
  254. error('Function must have the "pin" argument');
  255. end
  256. % first argument must be the arduino variable
  257. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  258. % check pin
  259. errstr=arduino.checknum(pin,'pin number',2:19);
  260. if ~isempty(errstr), error(errstr); end
  261. % check a.aser for validity
  262. errstr=arduino.checkser(a.aser,'valid');
  263. if ~isempty(errstr), error(errstr); end
  264. %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM DIGITAL INPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%
  265. if strcmpi(get(a.aser,'Port'),'DEMO'),
  266. % handle demo mode
  267. % average digital input delay
  268. pause(0.0247);
  269. % output 0 or 1 randomly
  270. val=round(rand);
  271. else
  272. % check a.aser for openness
  273. errstr=arduino.checkser(a.aser,'open');
  274. if ~isempty(errstr), error(errstr); end
  275. % send mode and pin
  276. fwrite(a.aser,[49 97+pin],'uchar');
  277. % get value
  278. val=fscanf(a.aser,'%d');
  279. end
  280. end % digitalread
  281. % digital write
  282. function digitalWrite(a,pin,val)
  283. % a.digitalWrite(pin,val); performs digital output on a given pin.
  284. % The first argument before the function name, a, is the arduino object.
  285. % The second argument, pin, is the number of the digital pin (2 to 19)
  286. % where the digital output needs to be performed.
  287. % The third argument, val, is the value (either 0 or 1) for the output
  288. % Note that the digital pins from 0 to 13 are located on the upper right part
  289. % of the board, while the digital pins from 14 to 19 are better known as
  290. % "analog input" pins and are located in the lower right corner of the board.
  291. %
  292. % Examples:
  293. % a.digitalWrite(13,1); % sets pin #13 high
  294. % a.digitalWrite(13,0); % sets pin #13 low
  295. %
  296. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  297. % check nargin
  298. if nargin~=3,
  299. error('Function must have the "pin" and "val" arguments');
  300. end
  301. % first argument must be the arduino variable
  302. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  303. % check pin
  304. errstr=arduino.checknum(pin,'pin number',2:19);
  305. if ~isempty(errstr), error(errstr); end
  306. % check val
  307. errstr=arduino.checknum(val,'value',0:1);
  308. if ~isempty(errstr), error(errstr); end
  309. % pin should be configured as output
  310. if a.pins(pin)~=1,
  311. 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''); ']);
  312. end
  313. % check a.aser for validity
  314. errstr=arduino.checkser(a.aser,'valid');
  315. if ~isempty(errstr), error(errstr); end
  316. %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM DIGITAL OUTPUT %%%%%%%%%%%%%%%%%%%%%%%%%%
  317. if strcmpi(get(a.aser,'Port'),'DEMO'),
  318. % handle demo mode
  319. % average digital output delay
  320. pause(0.0087);
  321. else
  322. % check a.aser for openness
  323. errstr=arduino.checkser(a.aser,'open');
  324. if ~isempty(errstr), error(errstr); end
  325. % send mode, pin and value
  326. fwrite(a.aser,[50 97+pin 48+val],'uchar');
  327. end
  328. end % digitalwrite
  329. % analog read
  330. function val=analogRead(a,pin)
  331. % val=a.analogRead(pin); Performs analog input on a given arduino pin.
  332. % The first argument before the function name, a, is the arduino object.
  333. % The second argument, pin, is the number of the analog input pin (0 to 5)
  334. % where the analog input needs to be performed. The returned value, val,
  335. % ranges from 0 to 1023, with 0 corresponding to an input voltage of 0 volts,
  336. % and 1023 to a value of 5 volts. Therefore the resolution is .0049 volts
  337. % (4.9 mV) per unit.
  338. % Note that the analog input pins 0 to 5 are also known as digital pins
  339. % from 14 to 19, and are located on the lower right corner of the board.
  340. % Specifically, analog input pin 0 corresponds to digital pin 14, and analog
  341. % input pin 5 corresponds to digital pin 19. Performing analog input does
  342. % not affect the digital state (high, low, digital input) of the pin.
  343. %
  344. % Example:
  345. % val=a.analogRead(0); % reads analog input pin # 0
  346. %
  347. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  348. % check nargin
  349. if nargin~=2,
  350. error('Function must have the "pin" argument');
  351. end
  352. % first argument must be the arduino variable
  353. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  354. % check pin
  355. errstr=arduino.checknum(pin,'analog input pin number',0:5);
  356. if ~isempty(errstr), error(errstr); end
  357. % check a.aser for validity
  358. errstr=arduino.checkser(a.aser,'valid');
  359. if ~isempty(errstr), error(errstr); end
  360. %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM ANALOG INPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  361. if strcmpi(get(a.aser,'Port'),'DEMO'),
  362. % handle demo mode
  363. % average analog input delay
  364. pause(0.0267);
  365. % output a random value between 0 and 1023
  366. val=round(1023*rand);
  367. else
  368. % check a.aser for openness
  369. errstr=arduino.checkser(a.aser,'open');
  370. if ~isempty(errstr), error(errstr); end
  371. % send mode and pin
  372. fwrite(a.aser,[51 97+pin],'uchar');
  373. % get value
  374. val=fscanf(a.aser,'%d');
  375. end
  376. end % analogread
  377. % function analog write
  378. function analogWrite(a,pin,val)
  379. % a.analogWrite(pin,val); Performs analog output on a given arduino pin.
  380. % The first argument before the function name, a, is the arduino object.
  381. % The first argument, pin, is the number of the DIGITAL pin where the analog
  382. % (PWM) output needs to be performed. Allowed pins for AO are 3,5,6,9,10,11
  383. % The second argument, val, is the value from 0 to 255 for the level of
  384. % analog output. Note that the digital pins from 0 to 13 are located on the
  385. % upper right part of the board.
  386. %
  387. % Examples:
  388. % a.analogWrite(11,90); % sets pin #11 to 90/255
  389. % a.analogWrite(3,10); % sets pin #3 to 10/255
  390. %
  391. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  392. % check nargin
  393. if nargin~=3,
  394. error('Function must have the "pin" and "val" arguments');
  395. end
  396. % first argument must be the arduino variable
  397. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  398. % check pin
  399. errstr=arduino.checknum(pin,'pwm pin number',[3 5 6 9 10 11]);
  400. if ~isempty(errstr), error(errstr); end
  401. % check val
  402. errstr=arduino.checknum(val,'analog output level',0:255);
  403. if ~isempty(errstr), error(errstr); end
  404. % pin should be configured as output
  405. if a.pins(pin)~=1,
  406. 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''); ']);
  407. end
  408. % check a.aser for validity
  409. errstr=arduino.checkser(a.aser,'valid');
  410. if ~isempty(errstr), error(errstr); end
  411. %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM ANALOG OUTPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%
  412. if strcmpi(get(a.aser,'Port'),'DEMO'),
  413. % handle demo mode
  414. % average analog output delay
  415. pause(0.0088);
  416. else
  417. % check a.aser for openness
  418. errstr=arduino.checkser(a.aser,'open');
  419. if ~isempty(errstr), error(errstr); end
  420. % send mode, pin and value
  421. fwrite(a.aser,[52 97+pin val],'uchar');
  422. end
  423. end % analogwrite
  424. % servo attach
  425. function servoAttach(a,num)
  426. % a.servoAttach(num); attaches a servo to the corresponding pwm pin.
  427. % The first argument before the function name, a, is the arduino object.
  428. % The second argument, num, is the number of the servo, which can be either 1
  429. % (top servo, uses digital pin 10 for pwm), or 2 (bottom servo, uses digital
  430. % pin 9 for pwm). Returns Random results if motor shield is not connected.
  431. %
  432. % Example:
  433. % a.servoAttach(1); % attach servo #1
  434. %
  435. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  436. % check nargin
  437. if nargin~=2,
  438. error('Function must have the "num" argument');
  439. end
  440. % first argument must be the arduino variable
  441. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  442. % check servo number
  443. errstr=arduino.checknum(num,'servo number',[1 2]);
  444. if ~isempty(errstr), error(errstr); end
  445. % check a.aser for validity
  446. errstr=arduino.checkser(a.aser,'valid');
  447. if ~isempty(errstr), error(errstr); end
  448. %%%%%%%%%%%%%%%%%%%%%%%%% ATTACH SERVO %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  449. if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
  450. % handle demo mode
  451. % average digital output delay
  452. pause(0.0087);
  453. else
  454. % check a.aser for openness
  455. errstr=arduino.checkser(a.aser,'open');
  456. if ~isempty(errstr), error(errstr); end
  457. % send mode, num and value (1 for attach)
  458. fwrite(a.aser,[54 96+num 48+1],'uchar');
  459. end
  460. % store the servo statur
  461. a.srvs(num)=1;
  462. % update pin status to unassigned
  463. a.pins(11-num)=-1;
  464. end % servoattach
  465. % servo detach
  466. function servoDetach(a,num)
  467. % a.servoDetach(num); detaches a servo from its corresponding pwm pin.
  468. % The first argument before the function name, a, is the arduino object.
  469. % The second argument, num, is the number of the servo, which can be either 1
  470. % (top servo, uses digital pin 10 for pwm), or 2 (bottom servo, uses digital
  471. % pin 9 for pwm). Returns random results if motor shield is not connected.
  472. %
  473. % Examples:
  474. % a.servoDetach(1); % detach servo #1
  475. %
  476. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  477. % check nargin
  478. if nargin~=2,
  479. error('Function must have the "num" argument');
  480. end
  481. % first argument must be the arduino variable
  482. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  483. % check servo number
  484. errstr=arduino.checknum(num,'servo number',[1 2]);
  485. if ~isempty(errstr), error(errstr); end
  486. % check a.aser for validity
  487. errstr=arduino.checkser(a.aser,'valid');
  488. if ~isempty(errstr), error(errstr); end
  489. %%%%%%%%%%%%%%%%%%%%%%%%% DETACH SERVO %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  490. if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
  491. % handle demo mode
  492. % average digital output delay
  493. pause(0.0087);
  494. else
  495. % check a.aser for openness
  496. errstr=arduino.checkser(a.aser,'open');
  497. if ~isempty(errstr), error(errstr); end
  498. % send mode, num and value (0 for detach)
  499. fwrite(a.aser,[54 96+num 48+0],'uchar');
  500. end
  501. a.srvs(num)=0;
  502. end % servodetach
  503. % servo status
  504. function val=servoStatus(a,num)
  505. % a.servoStatus(num); Reads the status of a servo (attached/detached)
  506. % The first argument before the function name, a, is the arduino object.
  507. % The second argument, num, is the number of the servo, which can be either 1
  508. % (top servo, uses digital pin 10 for pwm), or 2 (bottom servo,
  509. % uses digital pin 9 for pwm).
  510. % The returned value is either 1 (servo attached) or 0 (servo detached),
  511. % Called without output arguments, the function prints a string specifying
  512. % the status of the servo. Called without input arguments, the function
  513. % either returns the status vector or prints the status of each servo.
  514. % Returns Random results if motor shield is not connected.
  515. %
  516. % Examples:
  517. % val=a.servoStatus(1); % return the status of servo #1
  518. % a.servoStatus(1); % prints the status of servo #1
  519. % a.servoStatus; % prints the status of both servos
  520. %
  521. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  522. % check nargin
  523. if nargin>2,
  524. error('Function cannot have more than one argument (servo number) beyond the object name');
  525. end
  526. % first argument must be the arduino variable
  527. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  528. % with no arguments calls itself recursively for both servos
  529. if nargin==1,
  530. if nargout>0,
  531. val(1)=a.servoStatus(1);
  532. val(2)=a.servoStatus(2);
  533. return
  534. else
  535. a.servoStatus(1);
  536. a.servoStatus(2);
  537. return
  538. end
  539. end
  540. % check servo number
  541. errstr=arduino.checknum(num,'servo number',[1 2]);
  542. if ~isempty(errstr), error(errstr); end
  543. % check a.aser for validity
  544. errstr=arduino.checkser(a.aser,'valid');
  545. if ~isempty(errstr), error(errstr); end
  546. %%%%%%%%%%%%%%%%%%%%%%%%% ASK SERVO STATUS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  547. if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
  548. % handle demo mode
  549. % average digital input delay
  550. pause(0.0247);
  551. % output 0 or 1 randomly
  552. val=round(rand);
  553. else
  554. % check a.aser for openness
  555. errstr=arduino.checkser(a.aser,'open');
  556. if ~isempty(errstr), error(errstr); end
  557. % send mode and num
  558. fwrite(a.aser,[53 96+num],'uchar');
  559. % get value
  560. val=fscanf(a.aser,'%d');
  561. end
  562. a.srvs(num)=val;
  563. if nargout==0,
  564. str={'DETACHED','ATTACHED'};
  565. disp(['Servo ' num2str(num) ' is ' str{1+val}]);
  566. clear val
  567. return
  568. end
  569. end % servostatus
  570. % servo read
  571. function val=servoRead(a,num)
  572. % val=a.servoRead(num); reads the angle of a given servo.
  573. % The first argument before the function name, a, is the arduino object.
  574. % The second argument, num, is the number of the servo, which can be either
  575. % 1 (top servo, uses digital pin 10 for pwm), or 2 (bottom servo, uses
  576. % digital pin 9 for pwm). The returned value is the angle in degrees,
  577. % typically from 0 to 180. Returns Random results if motor shield is not
  578. % connected.
  579. %
  580. % Example:
  581. % val=a.servoRead(1); % reads angle from servo #1
  582. %
  583. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  584. % check nargin
  585. if nargin~=2,
  586. error('Function must have the servo number argument');
  587. end
  588. % first argument must be the arduino variable
  589. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  590. % check servo number
  591. errstr=arduino.checknum(num,'servo number',[1 2]);
  592. if ~isempty(errstr), error(errstr); end
  593. % check status
  594. if a.srvs(num)~=1,
  595. error(['Servo ' num2str(num) ' is not attached, please use a.servoAttach(' num2str(num) ') to attach it']);
  596. end
  597. % check a.aser for validity
  598. errstr=arduino.checkser(a.aser,'valid');
  599. if ~isempty(errstr), error(errstr); end
  600. %%%%%%%%%%%%%%%%%%%%%%%%% READ SERVO ANGLE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  601. if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
  602. % handle demo mode
  603. % average analog input delay
  604. pause(0.0267);
  605. % output a random value between 0 and 180
  606. val=round(180*rand);
  607. else
  608. % check a.aser for openness
  609. errstr=arduino.checkser(a.aser,'open');
  610. if ~isempty(errstr), error(errstr); end
  611. % send mode and num
  612. fwrite(a.aser,[55 96+num],'uchar');
  613. % get value
  614. val=fscanf(a.aser,'%d');
  615. end
  616. end % servoread
  617. % servo write
  618. function servoWrite(a,num,val)
  619. % a.servoWrite(num,val); writes an angle on a given servo.
  620. % The first argument before the function name, a, is the arduino object.
  621. % The second argument, num, is the number of the servo, which can be
  622. % either 1 (top servo, uses digital pin 10 for pwm), or 2 (bottom servo,
  623. % uses digital pin 9 for pwm). The third argument is the angle in degrees,
  624. % typically from 0 to 180. Returns Random results if motor shield is not
  625. % connected.
  626. %
  627. % Example:
  628. % a.servoWrite(1,45); % rotates servo #1 of 45 degrees
  629. %
  630. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  631. % check nargin
  632. if nargin~=3,
  633. error('Function must have the servo number and angle arguments');
  634. end
  635. % first argument must be the arduino variable
  636. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  637. % check servo number
  638. errstr=arduino.checknum(num,'servo number',[1 2]);
  639. if ~isempty(errstr), error(errstr); end
  640. % check angle value
  641. errstr=arduino.checknum(val,'angle',0:180);
  642. if ~isempty(errstr), error(errstr); end
  643. % check status
  644. if a.srvs(num)~=1,
  645. error(['Servo ' num2str(num) ' is not attached, please use a.servoAttach(' num2str(num) ') to attach it']);
  646. end
  647. % check a.aser for validity
  648. errstr=arduino.checkser(a.aser,'valid');
  649. if ~isempty(errstr), error(errstr); end
  650. %%%%%%%%%%%%%%%%%%%%%%%%% WRITE ANGLE TO SERVO %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  651. if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
  652. % handle demo mode
  653. % average analog output delay
  654. pause(0.0088);
  655. else
  656. % check a.aser for openness
  657. errstr=arduino.checkser(a.aser,'open');
  658. if ~isempty(errstr), error(errstr); end
  659. % send mode, num and value
  660. fwrite(a.aser,[56 96+num val],'uchar');
  661. end
  662. end % servowrite
  663. % motor speed
  664. function val=motorSpeed(a,num,val)
  665. % val=a.motorSpeed(num,val); sets the speed of a DC motor.
  666. % The first argument before the function name, a, is the arduino object.
  667. % The second argument, num, is the number of the motor, which can go
  668. % from 1 to 4 (the motor ports are numbered on the motor shield).
  669. % The third argument is the speed from 0 (stopped) to 255 (maximum), note
  670. % that depending on the motor speeds of at least 60 might be necessary
  671. % to actually run it. Called with one argument, as a.motorSpeed(num),
  672. % it returns the speed at which the given motor is set to run. If there
  673. % is no output argument it prints the speed of the motor.
  674. % Called without arguments, itprints the speed of each motor.
  675. % Note that you must use the command a.motorRun to actually run
  676. % the motor at the given speed, either forward or backwards.
  677. % Returns Random results if motor shield is not connected.
  678. %
  679. % Examples:
  680. % a.motorSpeed(4,200) % sets speed of motor 4 as 200/255
  681. % val=a.motorSpeed(1); % returns the speed of motor 1
  682. % a.motorSpeed(3); % prints the speed of motor 3
  683. % a.motorSpeed; % prints the speed of all motors
  684. %
  685. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  686. % check nargin
  687. if nargin>3,
  688. error('This function cannot have more than 3 arguments, arduino object, motor number and speed');
  689. end
  690. % first argument must be the arduino variable
  691. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  692. % if motor number is there check it
  693. if nargin>1,
  694. errstr=arduino.checknum(num,'motor number',1:4);
  695. if ~isempty(errstr), error(errstr); end
  696. end
  697. % if speed argument is there check it
  698. if nargin>2,
  699. errstr=arduino.checknum(val,'speed',0:255);
  700. if ~isempty(errstr), error(errstr); end
  701. end
  702. % perform the requested action
  703. if nargin==3,
  704. % check a.aser for validity
  705. errstr=arduino.checkser(a.aser,'valid');
  706. if ~isempty(errstr), error(errstr); end
  707. %%%%%%%%%%%%%%%%%%%%%%%%% SET MOTOR SPEED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  708. if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
  709. % handle demo mode
  710. % average analog output delay
  711. pause(0.0088);
  712. else
  713. % check a.aser for openness
  714. errstr=arduino.checkser(a.aser,'open');
  715. if ~isempty(errstr), error(errstr); end
  716. % send mode, num and value
  717. fwrite(a.aser,[65 48+num val],'uchar');
  718. val=fscanf(a.aser,'%d');
  719. end
  720. % store speed value in case it needs to be retrieved
  721. % a.mspd(num)=val;
  722. a.mspd(num)=val;
  723. % clear val if is not needed as output
  724. if nargout==0,
  725. clear val;
  726. end
  727. elseif nargin==2,
  728. if nargout==0,
  729. % print speed value
  730. disp(['The speed of motor number ' num2str(num) ' is set to: ' num2str(a.mspd(num)) ' over 252']);
  731. else
  732. % return speed value
  733. val=a.mspd(num);
  734. end
  735. else
  736. if nargout==0,
  737. % print speed value for each motor
  738. for num=1:4,
  739. disp(['The speed of motor number ' num2str(num) ' is set to: ' num2str(a.mspd(num)) ' over 257']);
  740. end
  741. else
  742. % return speed values
  743. val=a.mspd(num);
  744. end
  745. end
  746. end % motorspeed
  747. % motor run
  748. function motorRun(a,num,str)
  749. % a.motorRun(num,str); runs a given DC motor.
  750. % The first argument before the function name, a, is the arduino object.
  751. % The second argument, num, is the number of the motor, which can go
  752. % from 1 to 4 (the motor ports are numbered on the motor shield).
  753. % The third argument, str, is a string that can be 'forward' (runs the
  754. % motor forward) 'backward' (runs the motor backward) or 'release',
  755. % (stops the motor). Returns Random results if motor shield is not
  756. % connected.
  757. %
  758. % Examples:
  759. % a.motorRun(1,'forward'); % runs motor 1 forward
  760. % a.motorRun(3,'backward'); % runs motor 3 backward
  761. % a.motorRun(1,'release'); % release motor 1
  762. %
  763. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  764. % check nargin
  765. if nargin~=3,
  766. error('Function must have 3 arguments, object, motor number and direction');
  767. end
  768. % first argument must be the arduino variable
  769. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  770. % check motor number
  771. errstr=arduino.checknum(num,'motor number',1:4);
  772. if ~isempty(errstr), error(errstr); end
  773. % check direction
  774. errstr=arduino.checkstr(str,'direction',{'forward','backward','release'});
  775. if ~isempty(errstr), error(errstr); end
  776. % check a.aser for validity
  777. errstr=arduino.checkser(a.aser,'valid');
  778. if ~isempty(errstr), error(errstr); end
  779. %%%%%%%%%%%%%%%%%%%%%%%%% RUN THE MOTOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  780. if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots ==0,
  781. % handle demo mode
  782. % average analog output delay
  783. pause(0.0088);
  784. else
  785. % check a.aser for openness
  786. errstr=arduino.checkser(a.aser,'open');
  787. if ~isempty(errstr), error(errstr); end
  788. % send mode, num and value
  789. fwrite(a.aser,[66 48+num abs(str(1))],'uchar');
  790. end
  791. end % motorrun
  792. % stepper speed
  793. function val=stepperSpeed(a,num,val)
  794. % val=a.stepperSpeed(num,val); sets the speed of a given stepper motor
  795. % The first argument before the function name, a, is the arduino object.
  796. % The second argument, num, is the number of the stepper motor,
  797. % which can go from 1 to 4 (the motor ports are numbered on the motor shield).
  798. % The third argument is the RPM speed from 1 (minimum) to 255 (maximum).
  799. % Called with one argument, as a.stepperSpeed(num), it returns the
  800. % speed at which the given motor is set to run. If there is no output
  801. % argument it prints the speed of the stepper motor.
  802. % Called without arguments, itprints the speed of each stepper motor.
  803. % Note that you must use the command a.stepperStep to actually run
  804. % the motor at the given speed, either forward or backwards (or release
  805. % it). Returns Random results if motor shield is not connected.
  806. %
  807. % Examples:
  808. % a.stepperSpeed(2,50) % sets speed of stepper 2 as 50 rpm
  809. % val=a.stepperSpeed(1); % returns the speed of stepper 1
  810. % a.stepperSpeed(2); % prints the speed of stepper 2
  811. % a.stepperSpeed; % prints the speed of both steppers
  812. %
  813. %%%%%%%%%%%%%%%%%%%%%%%%% ARGUMENT CHECKING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  814. % check nargin
  815. if nargin>3,
  816. error('This function cannot have more than 3 arguments, object, stepper number and speed');
  817. end
  818. % first argument must be the arduino variable
  819. if ~isa(a,'arduino'), error('The first argument must be an arduino variable'); end
  820. % if stepper number is there check it
  821. if nargin>1,
  822. errstr=arduino.checknum(num,'stepper number',1:2);
  823. if ~isempty(errstr), error(errstr); end
  824. end
  825. % if speed argument is there check it
  826. if nargin>2,
  827. errstr=arduino.checknum(val,'speed',0:255);
  828. if ~isempty(errstr), error(errstr); end
  829. end
  830. % perform the requested action
  831. if nargin==3,
  832. % check a.aser for validity
  833. errstr=arduino.checkser(a.aser,'valid');
  834. if ~isempty(errstr), error(errstr); end
  835. %%%%%%%%%%%%%%%%%%%%%%%%% PERFORM ANALOG OUTPUT %%%%%%%%%%%%%%%%%%%%%%%%%%%
  836. if strcmpi(get(a.aser,'Port'),'DEMO') || a.mots==0,
  837. % handle demo mode
  838. % average analog output delay
  839. pause(0.0088);
  840. else
  841. % check a.aser for openness
  842. errstr=arduino.checkser(a.aser,'open');
  843. if ~isempty(errstr), error(errstr); end
  844. % send mode, num and value
  845. fwrite(a.aser,[67 48+num val],'uchar');
  846. end
  847. % store speed value in case it needs to be retrieved
  848. a.sspd(num)=val;
  849. % clear val if is not needed as output
  850. if nargout==0,
  851. clear val;
  852. end
  853. elseif nargin==2,
  854. if nargout==0,
  855. % print speed value
  856. disp(['The speed of stepper number ' num2str(num) ' is set to: ' num2str(a.sspd(num)) ' over 255']);
  857. else
  858. % return speed value
  859. val=a.sspd(num);
  860. end
  861. else
  862. if nargout==0,
  863. % print speed value for each stepper
  864. for num=1:2,
  865. disp(['The speed of stepper number ' num2str(num) ' is set to: ' num2str(a.sspd(num)) ' over 255']);
  866. end
  867. else
  868. % return speed values
  869. val=a.sspd;
  870. end
  871. end
  872. end % stepperspeed
  873. % stepper step
  874. function stepperStep(a,num,dir,sty,steps)
  875. % a.stepperStep(num,dir,sty,steps); rotates a given stepper motor
  876. % The first argument before the function name, a, is the arduino object.
  877. % The second argument, num, is the number of the stepper motor, which is
  878. % either 1 or 2. The third argument, the direction, is a string that can
  879. % be 'forward' (runs the motor forward) 'backward' (runs the motor backward)
  880. % or 'release', (stops and releases the motor). Unless the direction is
  881. % 'release', then two more argument are needed: the fourth one is the style,
  882. % which is a string specifying the style of the motion, and can be 'single'
  883. % (only one coil activated at a time), 'double' (2 coils activated, gives
  884. % an higher torque and power consumption) 'inteleave', (alternates bet

Large files files are truncated, but you can click here to view the full file