Newer
Older
TestStandRepository / Software / Arduino / ResetZAndControlStepperMotors / ResetZAndControlStepperMotors.ino
@Federica Lionetto Federica Lionetto on 3 Dec 2014 5 KB Add ResetZAndControlStepperMotors Arduino sketch
  1. /*
  2. Author: F. Lionetto
  3. Created on: June 23rd, 2014
  4.  
  5. The sketch inizializes the position of the two stepper motors, move the laser 5 cm (10000 steps) away from the x micro switch, move the laser 2.5 cm (5000) away from the z micro switch, and then 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.  
  23. The only allowed commands (checked before and after being sent through the USB port) are:
  24. +10 ---> move one step forward along x;
  25. +20 ---> move one step backward along x
  26. +01 ---> move one step forward along z;
  27. +02 ---> move one step backward along z.
  28.  
  29. Distances in microns.
  30. */
  31.  
  32. #include <Wire.h>
  33. #include <Adafruit_MotorShield.h>
  34. #include "utility/Adafruit_PWMServoDriver.h"
  35.  
  36. // Stepper motor properties.
  37. const int stepsPerRevolution = 200;
  38. const int singleStep = 5;
  39. const int speedFast = 50;
  40. const int speedSlow = 5;
  41.  
  42. // Create the motor shield object with the default I2C address.
  43. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  44.  
  45. // Connect a stepper motor with 200 steps per revolution (1.8 degree) to motor port #1 (M1 and M2).
  46. Adafruit_StepperMotor *myMotorX = AFMS.getStepper(stepsPerRevolution, 1);
  47. Adafruit_StepperMotor *myMotorZ = AFMS.getStepper(stepsPerRevolution, 2);
  48.  
  49. // Micro switch properties.
  50. const int switchXPin = 22;
  51. const int switchZPin = 23;
  52. int switchXState = 0;
  53. int switchZState = 0;
  54.  
  55. // Scan properties.
  56. // Maximum distance from the x micro switch (!!!)
  57. // const int maxStepsX = 16000;
  58. // Maximum distance from the z micro switch (!!!)
  59. // const int maxStepsZ = 5400;
  60. // Move the laser 4.25 cm (8500 steps) away from the x micro switch
  61. const int stepsFromXSwitch = 8500;
  62. // Move the laser 3.00 cm (6000) away from the z micro switch
  63. const int stepsFromZSwitch = 6000;
  64. // Total number of steps we need along x.
  65. // const int totStepsX = 200;
  66. // Total number of steps we need along y.
  67. // const int totStepsZ = 200;
  68. // int stepsX = 0;
  69. // int stepsZ = 0;
  70. // Byte read from the USB port.
  71. byte byteUSB;
  72. // 1 if the byte read from the USB port is +.
  73. boolean commandReceived = false;
  74. // 1 if the byte read from the USB port is the first after +.
  75. boolean xCommandReceived = false;
  76. // 1 if the byte read from the USB port is the second after +.
  77. boolean zCommandReceived = false;
  78. int moveX = 0;
  79. int moveZ = 0;
  80.  
  81. void stepAction(int moveX, int moveZ) {
  82. if (moveX == 1) {
  83. myMotorX->step(1,FORWARD,DOUBLE);
  84. Serial.println("Moving forward along x...");
  85. }
  86. else if (moveX == 2) {
  87. myMotorX->step(1,BACKWARD,DOUBLE);
  88. Serial.println("Moving backward along x...");
  89. }
  90. if (moveZ == 1) {
  91. myMotorZ->step(1,FORWARD,DOUBLE);
  92. Serial.println("Moving forward along z...");
  93. }
  94. else if (moveZ == 2) {
  95. myMotorZ->step(1,BACKWARD,DOUBLE);
  96. Serial.println("Moving backward along z...");
  97. }
  98. }
  99.  
  100. void setup() {
  101. Serial.begin(9600);
  102. Serial.println("This is a test!");
  103.  
  104. AFMS.begin();
  105. myMotorX->setSpeed(speedFast);
  106. myMotorZ->setSpeed(speedFast);
  107. pinMode(switchXPin,INPUT);
  108. pinMode(switchZPin,INPUT);
  109. /*
  110. Serial.println("Inizializing position along x...");
  111.  
  112. while (switchXState == LOW) {
  113. switchXState = digitalRead(switchXPin);
  114. if (switchXState == LOW) {
  115. // The stepper motor is not at the origin of the coordinate system.
  116. myMotorX->step(1,FORWARD,DOUBLE);
  117. }
  118. }
  119. Serial.println("Position along x successfully initialized.");
  120. */
  121. Serial.println("Inizializing position along z...");
  122. while (switchZState == LOW) {
  123. switchZState = digitalRead(switchZPin);
  124. if (switchZState == LOW) {
  125. // The stepper motor is not at the origin of the coordinate system.
  126. myMotorZ->step(1,BACKWARD,DOUBLE);
  127. }
  128. }
  129. Serial.println("Position along z successfully initialized.");
  130. /*
  131. Serial.println("Moving the laser 4.25 cm (8500 steps) away from the x micro switch...");
  132. myMotorX->step(stepsFromXSwitch,BACKWARD,DOUBLE);
  133. Serial.println("Laser moved 4.25 cm (8500 steps) away from the x micro switch.");
  134. */
  135. Serial.println("Moving the laser 3.00 cm (6000 steps) away from the z micro switch...");
  136. myMotorZ->step(stepsFromZSwitch,FORWARD,DOUBLE);
  137. Serial.println("Laser moved 3.00 cm (6000 steps) away from the z micro switch.");
  138. myMotorX->setSpeed(speedSlow);
  139. myMotorZ->setSpeed(speedSlow);
  140. // myMotorX->release();
  141. // myMotorZ->release();
  142. }
  143. void loop() {
  144. if (Serial.available()) {
  145. byteUSB = Serial.read();
  146. Serial.println(byteUSB);
  147. if ((byteUSB != 43) && (byteUSB !=48) && (byteUSB != 49) && (byteUSB != 50)) {
  148. Serial.println("Invalid command received.");
  149. }
  150. else if ((commandReceived == false) && (byteUSB == 43)) {
  151. commandReceived = true;
  152. Serial.println("Valid command received.");
  153. xCommandReceived = false;
  154. zCommandReceived = false;
  155. moveX = 0;
  156. moveZ = 0;
  157. }
  158. else if ((commandReceived == true) && (xCommandReceived == false)) {
  159. moveX = byteUSB - 48;
  160. Serial.println(moveX);
  161. xCommandReceived = true;
  162. Serial.println("Command for x direction received.");
  163. }
  164. else if ((commandReceived == true) && (zCommandReceived == false)) {
  165. moveZ = byteUSB - 48;
  166. Serial.println(moveZ);
  167. zCommandReceived = true;
  168. Serial.println("Command for z direction received.");
  169. }
  170. }
  171. else if ((commandReceived == true) && (xCommandReceived == true) && (zCommandReceived == true)) {
  172. stepAction(moveX,moveZ);
  173. commandReceived = false;
  174. }
  175. }