Newer
Older
TestStandRepository / Software / Arduino / ControlStepperMotors216 / ControlStepperMotors216.ino
@Federica Lionetto Federica Lionetto on 19 Dec 2014 6 KB Add Focusing
  1. /*
  2. Author: F. Lionetto
  3. Created on: November 25th, 2014
  4.  
  5. The sketch waits for commands sent through the USB port.
  6.  
  7. Coordinate system:
  8. - x axis across the strips, forward direction is toward the window, backward direction is toward the door (stepper motor 1);
  9. - y axis along the strips;
  10. - z axis toward the strips, forward direction is down, backward direction is up (stepper motor 2).
  11.  
  12. The x micro switch is connected to pin 22.
  13. The z micro switch is connected to pin 23.
  14.  
  15. The stepper motors have two different speeds, one for non-precise and one for precise positioning, respectively.
  16.  
  17. ASCII printable characters:
  18. + ---> 43;
  19. 0 ---> 48;
  20. 1 ---> 49;
  21. 2 ---> 50;
  22. 3 ---> 51.
  23.  
  24. The only allowed commands (checked before and after being sent through the USB port) are:
  25. +10 ---> move one step forward along x;
  26. +20 ---> move one step backward along x
  27. +30 ---> move at the opposite edge of the silicon sensor (along x);
  28. +40 ---> move back;
  29. +01 ---> move one step forward along z;
  30. +02 ---> move one step backward along z;
  31. +03 ---> move 0.1 mm forward along z;
  32. +04 ---> move 0.1 mm backward along z.
  33.  
  34. Distances in microns.
  35.  
  36. For Hans410 sensor:
  37. - the laser is initially on the strips corresponding to Beetle channels 216/220;
  38. - +30 brings the laser on the strips corresponding to the Beetle channels 16/20;
  39. - +40 brings the laser on the strips corresponding to the Beetle channels 216/220.
  40. */
  41.  
  42. #include <Wire.h>
  43. #include <Adafruit_MotorShield.h>
  44. #include "utility/Adafruit_PWMServoDriver.h"
  45.  
  46. // Stepper motor properties.
  47. const int stepsPerRevolution = 200;
  48. const int singleStep = 5;
  49. const int speedFast = 50;
  50. const int speedSlow = 5;
  51.  
  52. // Create the motor shield object with the default I2C address.
  53. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  54.  
  55. // Connect a stepper motor with 200 steps per revolution (1.8 degree) to motor port #1 (M1 and M2).
  56. Adafruit_StepperMotor *myMotorX = AFMS.getStepper(stepsPerRevolution, 1);
  57. Adafruit_StepperMotor *myMotorZ = AFMS.getStepper(stepsPerRevolution, 2);
  58.  
  59. // Micro switch properties.
  60. const int switchXPin = 22;
  61. const int switchZPin = 23;
  62. int switchXState = 0;
  63. int switchZState = 0;
  64.  
  65. // Scan properties.
  66. // Maximum distance from the x micro switch (!!!)
  67. // const int maxStepsX = 16000;
  68. // Maximum distance from the z micro switch (!!!)
  69. // const int maxStepsZ = 5400;
  70. // Move the laser 3.60 cm (7200 steps) away from the x micro switch
  71. const int stepsFromXSwitch = 7200;
  72. // Move the laser 2.00 cm (4000) away from the z micro switch
  73. const int stepsFromZSwitch = 4000;
  74. // Total number of steps we need along x.
  75. // const int totStepsX = 200;
  76. // Total number of steps we need along y.
  77. // const int totStepsZ = 200;
  78. // int stepsX = 0;
  79. // int stepsZ = 0;
  80. // Byte read from the USB port.
  81. byte byteUSB;
  82. // 1 if the byte read from the USB port is +.
  83. boolean commandReceived = false;
  84. // 1 if the byte read from the USB port is the first after +.
  85. boolean xCommandReceived = false;
  86. // 1 if the byte read from the USB port is the second after +.
  87. boolean zCommandReceived = false;
  88. int moveX = 0;
  89. int moveZ = 0;
  90.  
  91. void stepAction(int moveX, int moveZ) {
  92. if (moveX == 1) {
  93. myMotorX->step(1,FORWARD,DOUBLE);
  94. Serial.println("Moved forward along x.");
  95. }
  96. else if (moveX == 2) {
  97. myMotorX->step(1,BACKWARD,DOUBLE);
  98. Serial.println("Moved backward along x.");
  99. }
  100. else if (moveX == 3) {
  101. myMotorX->step(1900,BACKWARD,DOUBLE);
  102. Serial.println("Moved at the opposite edge of the silicon sensor (along x).");
  103. }
  104. else if (moveX == 4) {
  105. myMotorX->step(1900,FORWARD,DOUBLE);
  106. Serial.println("Moved back.");
  107. }
  108. if (moveZ == 1) {
  109. myMotorZ->step(1,FORWARD,DOUBLE);
  110. Serial.println("Moved forward along z.");
  111. }
  112. else if (moveZ == 2) {
  113. myMotorZ->step(1,BACKWARD,DOUBLE);
  114. Serial.println("Moved backward along z.");
  115. }
  116. else if (moveZ == 3) {
  117. myMotorZ->step(20,FORWARD,DOUBLE);
  118. Serial.println("Moved 0.1 mm forward along z.");
  119. }
  120. else if (moveZ == 4) {
  121. myMotorZ->step(20,BACKWARD,DOUBLE);
  122. Serial.println("Moved 0.1 mm backward along z.");
  123. }
  124. }
  125.  
  126. void setup() {
  127. Serial.begin(9600);
  128. Serial.println("This is a test!");
  129.  
  130. AFMS.begin();
  131. myMotorX->setSpeed(speedFast);
  132. myMotorZ->setSpeed(speedFast);
  133. pinMode(switchXPin,INPUT);
  134. pinMode(switchZPin,INPUT);
  135. /*
  136. Serial.println("Inizializing position along x...");
  137.  
  138. while (switchXState == LOW) {
  139. switchXState = digitalRead(switchXPin);
  140. if (switchXState == LOW) {
  141. // The stepper motor is not at the origin of the coordinate system.
  142. myMotorX->step(1,FORWARD,DOUBLE);
  143. }
  144. }
  145. Serial.println("Position along x successfully initialized.");
  146. Serial.println("Inizializing position along z...");
  147.  
  148. while (switchZState == LOW) {
  149. switchZState = digitalRead(switchZPin);
  150. if (switchZState == LOW) {
  151. // The stepper motor is not at the origin of the coordinate system.
  152. myMotorZ->step(1,BACKWARD,DOUBLE);
  153. }
  154. }
  155. Serial.println("Position along z successfully initialized.");
  156. Serial.println("Moving the laser 3.60 cm (7200 steps) away from the x micro switch...");
  157. myMotorX->step(stepsFromXSwitch,BACKWARD,DOUBLE);
  158. Serial.println("Laser moved 3.60 cm (7200 steps) away from the x micro switch.");
  159.  
  160. Serial.println("Moving the laser 2.00 cm (4000 steps) away from the z micro switch...");
  161. myMotorZ->step(stepsFromZSwitch,FORWARD,DOUBLE);
  162. Serial.println("Laser moved 2.00 cm (4000 steps) away from the z micro switch.");
  163. */
  164.  
  165. myMotorX->setSpeed(speedSlow);
  166. myMotorZ->setSpeed(speedSlow);
  167. // myMotorX->release();
  168. // myMotorZ->release();
  169. }
  170. void loop() {
  171. if (Serial.available()) {
  172. byteUSB = Serial.read();
  173. Serial.println(byteUSB);
  174. if ((byteUSB != 43) && (byteUSB !=48) && (byteUSB != 49) && (byteUSB != 50) && (byteUSB != 51) && (byteUSB != 52)) {
  175. Serial.println("Invalid command received.");
  176. }
  177. else if ((commandReceived == false) && (byteUSB == 43)) {
  178. commandReceived = true;
  179. Serial.println("Valid command received.");
  180. xCommandReceived = false;
  181. zCommandReceived = false;
  182. moveX = 0;
  183. moveZ = 0;
  184. }
  185. else if ((commandReceived == true) && (xCommandReceived == false)) {
  186. moveX = byteUSB - 48;
  187. Serial.println(moveX);
  188. xCommandReceived = true;
  189. Serial.println("Command for x direction received.");
  190. }
  191. else if ((commandReceived == true) && (zCommandReceived == false)) {
  192. moveZ = byteUSB - 48;
  193. Serial.println(moveZ);
  194. zCommandReceived = true;
  195. Serial.println("Command for z direction received.");
  196. }
  197. }
  198. else if ((commandReceived == true) && (xCommandReceived == true) && (zCommandReceived == true)) {
  199. stepAction(moveX,moveZ);
  200. commandReceived = false;
  201. }
  202. }