/pumpcontroller/Matlab/StepDos.m

http://dccn-lab.googlecode.com/ · MATLAB · 168 lines · 70 code · 19 blank · 79 comment · 4 complexity · f9c9c49587c143c8b1b283633bfb67a8 MD5 · raw file

  1. % Class "StepDos"
  2. %
  3. % *Methods*
  4. % - runPump
  5. % - startPump
  6. % - stopPump
  7. % - setRunDuration
  8. % - setFlowRate
  9. %
  10. %
  11. % *Example*
  12. % The controller is connected to the first serial port of a
  13. % windows computer (COM1). The run duration for pump1 is set to 1.5 seconds and the
  14. % flowrate to 50%. Finally the runPump method is used to... run the pump!
  15. % When you're really done, close the object. This will close and cleanup all the
  16. % references to the serial port.
  17. %
  18. % stepdos = StepDos('com1');
  19. % stepdos.setRunDuration(1, 1500);
  20. % stepdos.setFlowRate(1, 50);
  21. % stepdos.runPump(1);
  22. %
  23. % ... do more stuff here
  24. % ...
  25. %
  26. % stepdos.close();
  27. %
  28. % Another oneliner example (handy for testing):
  29. %
  30. % a = StepDos('com1'); a.setFlowRate(1, 80); a.runPump(1); a.close();
  31. %
  32. % If called with an empty com port string, no serial connection will be
  33. % established. The serial commands will be echo'd to stdout:
  34. %
  35. % a = StepDos('')
  36. % ...
  37. %
  38. %
  39. % *Protocol*
  40. % Custom protocol for the StepDos controller:
  41. %
  42. % 1 -> Runs pump 1 for the set duration
  43. % 2 -> Runs pump 2 for the set duration
  44. % 3 -> Runs pump 3 for the set duration
  45. %
  46. % 11 -> Starts pump 1
  47. % 12 -> Starts pump 2
  48. % 13 -> Starts pump 3
  49. %
  50. % 21 -> Stops pump 1
  51. % 22 -> Stops pump 2
  52. % 23 -> Stops pump 3
  53. %
  54. % 31 [duration] -> Sets run duration for pump 1 to [duration] milliseconds
  55. % 32 [duration] -> Sets run duration for pump 2 to [duration] milliseconds
  56. % 33 [duration] -> Sets run duration for pump 3 to [duration] milliseconds
  57. %
  58. % 41 [flow rate] -> Sets the relative flow rate* for pump 1, 0 - 10000 ^ 0 – 100%
  59. % 42 [flow rate] -> Sets the relative flow rate* for pump 2, 0 - 10000 ^ 0 – 100%
  60. % 43 [flow rate] -> Sets the relative flow rate* for pump 3, 0 - 10000 ^ 0 – 100%
  61. classdef StepDos
  62. properties (SetAccess = public)
  63. serobj;
  64. debugmode = false;
  65. end
  66. methods
  67. function SD = StepDos(comport)
  68. if (strcmp(comport, ''))
  69. fprintf('StepDos: No Com port given, running in debug mode...\n')
  70. SD.debugmode = true;
  71. end
  72. if (not(SD.debugmode))
  73. delete(instrfind);
  74. SD.serobj = serial(comport);
  75. % serial port configuration
  76. set(SD.serobj, 'Baudrate', 9600);
  77. set(SD.serobj, 'Parity', 'none');
  78. set(SD.serobj, 'Databits', 8); % number of data bits
  79. set(SD.serobj, 'StopBits', 1); % number of stop bits
  80. set(SD.serobj, 'Terminator', 'CR/LF'); % line ending character
  81. % see also:
  82. % http://www.mathworks.com/matlabcentral/newsreader/view_original/292759
  83. set(SD.serobj, 'InputBufferSize', 1); % set read buffBuffer for read
  84. set(SD.serobj, 'FlowControl', 'none'); %
  85. % open the serial port
  86. fopen(SD.serobj);
  87. % since matlab pulls the DTR line, the arduino will reset
  88. % so we have to wait for the initialization of the controller
  89. oldState = pause('query');
  90. pause on;
  91. pause(2.5);
  92. pause(oldState);
  93. end
  94. end
  95. % runPump
  96. % pumpNumber - number of the pump 1,2 or 3
  97. function runPump(SD, pumpNumber)
  98. offset = 1; % number to send for the first pump
  99. cmd = sprintf('%d', offset - 1 + pumpNumber);
  100. SD.sendcmd(cmd);
  101. end
  102. % startPump
  103. % pumpNumber - number of the pump 1,2 or 3
  104. function startPump(SD, pumpNumber)
  105. offset = 11; % number to send for the first pump
  106. cmd = sprintf('%d', offset - 1 + pumpNumber);
  107. SD.sendcmd(cmd);
  108. end
  109. % stopPump
  110. % pumpNumber - number of the pump 1,2 or 3
  111. function stopPump(SD, pumpNumber)
  112. offset = 21; % number to send for the first pump
  113. cmd = sprintf('%d', offset - 1 + pumpNumber);
  114. SD.sendcmd(cmd);
  115. end
  116. % setRunDuration
  117. % pumpNumber - number of the pump 1,2 or 3
  118. % time - flow time in miliseconds
  119. function setRunDuration(SD, pumpNumber, time)
  120. offset = 31; % number to send for the first pump
  121. cmd = sprintf('%d %d', offset - 1 + pumpNumber, time);
  122. SD.sendcmd(cmd);
  123. end
  124. % setFlowRate
  125. % pumpNumber - number of the pump 1,2 or 3
  126. % flowRate - percentage (0 <= flowRate <= 100)
  127. function setFlowRate(SD, pumpNumber, flowRate)
  128. offset = 41; % number to send for the first pump
  129. cmd = sprintf('%d %d', offset - 1 + pumpNumber, round(flowRate * 100));
  130. SD.sendcmd(cmd);
  131. end
  132. function sendcmd(SD, cmd)
  133. if (SD.debugmode)
  134. fprintf('StepDos: sending: ');
  135. fprintf(cmd);
  136. fprintf('\n');
  137. else
  138. fprintf(SD.serobj, cmd);
  139. end
  140. end
  141. % close
  142. function close(SD)
  143. if (not(SD.debugmode))
  144. fclose(SD.serobj);
  145. delete(SD.serobj);
  146. end
  147. end
  148. end
  149. end