Newer
Older
TestStandRepository / Software / Arduino / sketch_jun17a / sketch_jun17a.ino
@Federica Lionetto Federica Lionetto on 8 Oct 2014 3 KB Added Arduino software
  1. /*
  2. Author: F. Lionetto
  3. Created on: June 4th, 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 perform a scan for finding the focal length.
  6.  
  7. Scan for finding the focal length:
  8. - at a given z position, move one step backward along x, wait, move one step backward along x, wait, and so on (enough times to go across two strips);
  9. - go back to the initial position along x;
  10. - move one step forward along z;
  11. - repeat from the beginning (enough times to be sure to find the focal length, but not that many to touch the silicon sensor);
  12. - again and again.
  13. In the current configuration, scan of 1 mm along x and 1 mm along z.
  14.  
  15. Coordinate system:
  16. - x axis across the strips, forward direction is toward the window, backward direction is toward the door (stepper motor 1);
  17. - y axis along the strips;
  18. - z axis toward the strips, forward direction is down, backward direction is up (stepper motor 2).
  19.  
  20. The x micro switch is connected to pin 22.
  21. The z micro switch is connected to pin 23.
  22.  
  23. Distances in microns.
  24. */
  25.  
  26. #include <Wire.h>
  27. #include <Adafruit_MotorShield.h>
  28. #include "utility/Adafruit_PWMServoDriver.h"
  29.  
  30. // Stepper motor properties.
  31. const int stepsPerRevolution = 200;
  32. const int singleStep = 5;
  33.  
  34. // Create the motor shield object with the default I2C address.
  35. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  36.  
  37. // Connect a stepper motor with 200 steps per revolution (1.8 degree) to motor port #1 (M1 and M2).
  38. Adafruit_StepperMotor *myMotorX = AFMS.getStepper(stepsPerRevolution, 1);
  39. Adafruit_StepperMotor *myMotorZ = AFMS.getStepper(stepsPerRevolution, 2);
  40.  
  41. // Micro switch properties.
  42. const int switchXPin = 22;
  43. const int switchZPin = 23;
  44. int switchXState = 0;
  45. int switchZState = 0;
  46.  
  47. // Scan properties.
  48. // Total number of steps we need along x.
  49. const int totStepsX = 200;
  50. // Total number of steps we need along y.
  51. const int totStepsZ = 200;
  52. int stepsX = 0;
  53. int stepsZ = 0;
  54.  
  55. void setup() {
  56. Serial.begin(9600);
  57. Serial.println("This is a test!");
  58.  
  59. AFMS.begin();
  60. myMotorX->setSpeed(50);
  61. myMotorZ->setSpeed(50);
  62. pinMode(switchXPin,INPUT);
  63. pinMode(switchZPin,INPUT);
  64. Serial.println("Inizializing position along x...");
  65.  
  66. while (switchXState == LOW) {
  67. switchXState = digitalRead(switchXPin);
  68. if (switchXState == LOW) {
  69. // The stepper motor is not at the origin of the coordinate system.
  70. myMotorX->step(1,FORWARD,DOUBLE);
  71. }
  72. }
  73. Serial.println("Position along x successfully initialized.");
  74. Serial.println("Inizializing position along z...");
  75.  
  76. while (switchZState == LOW) {
  77. switchZState = digitalRead(switchZPin);
  78. if (switchZState == LOW) {
  79. // The stepper motor is not at the origin of the coordinate system.
  80. myMotorZ->step(1,BACKWARD,DOUBLE);
  81. }
  82. }
  83. Serial.println("Position along z successfully initialized.");
  84. Serial.println("Moving the laser 5 cm (10000 steps) away from the x micro switch...");
  85. myMotorX->step(10000,BACKWARD,DOUBLE);
  86. Serial.println("Laser moved 5 cm (10000 steps) away from the x micro switch.");
  87.  
  88. Serial.println("Moving the laser 2.5 cm (5000 steps) away from the z micro switch...");
  89. myMotorZ->step(5000,FORWARD,DOUBLE);
  90. Serial.println("Laser moved 2.5 cm (5000 steps) away from the z micro switch.");
  91.  
  92. myMotorX->setSpeed(5);
  93. myMotorZ->setSpeed(5);
  94. myMotorX->release();
  95. myMotorZ->release();
  96. }
  97. void loop() {
  98. // Scan for finding the focal length.
  99. if(stepsZ < totStepsZ) {
  100. Serial.print("Data acquisition at z = ");
  101. Serial.println(stepsZ);
  102. if (stepsX < totStepsX) {
  103. myMotorX->step(1,BACKWARD,DOUBLE);
  104. myMotorX->release();
  105. stepsX = stepsX + 1;
  106. }
  107. else if (stepsX == totStepsX) {
  108. myMotorX->step(totStepsX,FORWARD,DOUBLE);
  109. myMotorX->release();
  110. stepsX = 0;
  111. myMotorZ->step(1,FORWARD,DOUBLE);
  112. myMotorZ->release();
  113. stepsZ = stepsZ + 1;
  114. }
  115. }
  116. else {
  117. Serial.println("Scan for finding the focal length completed.");
  118. }
  119. }