diff --git a/Software/Arduino/ControlStepperMotors216/ControlStepperMotors216.ino b/Software/Arduino/ControlStepperMotors216/ControlStepperMotors216.ino new file mode 100644 index 0000000..6cbdcde --- /dev/null +++ b/Software/Arduino/ControlStepperMotors216/ControlStepperMotors216.ino @@ -0,0 +1,213 @@ +/* +Author: F. Lionetto +Created on: November 25th, 2014 + +The sketch waits for commands sent through the USB port. + +Coordinate system: +- x axis across the strips, forward direction is toward the window, backward direction is toward the door (stepper motor 1); +- y axis along the strips; +- z axis toward the strips, forward direction is down, backward direction is up (stepper motor 2). + +The x micro switch is connected to pin 22. +The z micro switch is connected to pin 23. + +The stepper motors have two different speeds, one for non-precise and one for precise positioning, respectively. + +ASCII printable characters: ++ ---> 43; +0 ---> 48; +1 ---> 49; +2 ---> 50; +3 ---> 51. + +The only allowed commands (checked before and after being sent through the USB port) are: ++10 ---> move one step forward along x; ++20 ---> move one step backward along x ++30 ---> move at the opposite edge of the silicon sensor (along x); ++40 ---> move back; ++01 ---> move one step forward along z; ++02 ---> move one step backward along z; ++03 ---> do not do anything; ++04 ---> do not do anything. + +Distances in microns. + +For Hans410 sensor: +- the laser is initially on the strips corresponding to Beetle channels 216/220; +- +30 brings the laser on the strips corresponding to the Beetle channels 16/20; +- +40 brings the laser on the strips corresponding to the Beetle channels 216/220. +*/ + +#include +#include +#include "utility/Adafruit_PWMServoDriver.h" + +// Stepper motor properties. +const int stepsPerRevolution = 200; +const int singleStep = 5; +const int speedFast = 50; +const int speedSlow = 5; + +// Create the motor shield object with the default I2C address. +Adafruit_MotorShield AFMS = Adafruit_MotorShield(); + +// Connect a stepper motor with 200 steps per revolution (1.8 degree) to motor port #1 (M1 and M2). +Adafruit_StepperMotor *myMotorX = AFMS.getStepper(stepsPerRevolution, 1); +Adafruit_StepperMotor *myMotorZ = AFMS.getStepper(stepsPerRevolution, 2); + +// Micro switch properties. +const int switchXPin = 22; +const int switchZPin = 23; +int switchXState = 0; +int switchZState = 0; + +// Scan properties. +// Maximum distance from the x micro switch (!!!) +// const int maxStepsX = 16000; +// Maximum distance from the z micro switch (!!!) +// const int maxStepsZ = 5400; +// Move the laser 3.60 cm (7200 steps) away from the x micro switch +const int stepsFromXSwitch = 7200; +// Move the laser 2.00 cm (4000) away from the z micro switch +const int stepsFromZSwitch = 4000; +// Total number of steps we need along x. +// const int totStepsX = 200; +// Total number of steps we need along y. +// const int totStepsZ = 200; +// int stepsX = 0; +// int stepsZ = 0; +// Byte read from the USB port. +byte byteUSB; +// 1 if the byte read from the USB port is +. +boolean commandReceived = false; +// 1 if the byte read from the USB port is the first after +. +boolean xCommandReceived = false; +// 1 if the byte read from the USB port is the second after +. +boolean zCommandReceived = false; +int moveX = 0; +int moveZ = 0; + +void stepAction(int moveX, int moveZ) { + if (moveX == 1) { + myMotorX->step(1,FORWARD,DOUBLE); + Serial.println("Moved forward along x."); + } + else if (moveX == 2) { + myMotorX->step(1,BACKWARD,DOUBLE); + Serial.println("Moved backward along x."); + } + else if (moveX == 3) { + myMotorX->step(1900,BACKWARD,DOUBLE); + Serial.println("Moved at the opposite edge of the silicon sensor (along x)."); + } + else if (moveX == 4) { + myMotorX->step(1900,FORWARD,DOUBLE); + Serial.println("Moved back."); + } + if (moveZ == 1) { + myMotorZ->step(1,FORWARD,DOUBLE); + Serial.println("Moved forward along z."); + } + else if (moveZ == 2) { + myMotorZ->step(1,BACKWARD,DOUBLE); + Serial.println("Moved backward along z."); + } + else if (moveZ == 3) { + Serial.println("Nothing done."); + } + else if (moveZ == 4) { + Serial.println("Nothing done."); + } +} + +void setup() { + Serial.begin(9600); + Serial.println("This is a test!"); + + AFMS.begin(); + + myMotorX->setSpeed(speedFast); + myMotorZ->setSpeed(speedFast); + + pinMode(switchXPin,INPUT); + pinMode(switchZPin,INPUT); + + /* + Serial.println("Inizializing position along x..."); + + while (switchXState == LOW) { + switchXState = digitalRead(switchXPin); + if (switchXState == LOW) { + // The stepper motor is not at the origin of the coordinate system. + myMotorX->step(1,FORWARD,DOUBLE); + } + } + + Serial.println("Position along x successfully initialized."); + + Serial.println("Inizializing position along z..."); + + while (switchZState == LOW) { + switchZState = digitalRead(switchZPin); + if (switchZState == LOW) { + // The stepper motor is not at the origin of the coordinate system. + myMotorZ->step(1,BACKWARD,DOUBLE); + } + } + + Serial.println("Position along z successfully initialized."); + + Serial.println("Moving the laser 3.60 cm (7200 steps) away from the x micro switch..."); + + myMotorX->step(stepsFromXSwitch,BACKWARD,DOUBLE); + + Serial.println("Laser moved 3.60 cm (7200 steps) away from the x micro switch."); + + Serial.println("Moving the laser 2.00 cm (4000 steps) away from the z micro switch..."); + + myMotorZ->step(stepsFromZSwitch,FORWARD,DOUBLE); + + Serial.println("Laser moved 2.00 cm (4000 steps) away from the z micro switch."); + */ + + myMotorX->setSpeed(speedSlow); + myMotorZ->setSpeed(speedSlow); + + // myMotorX->release(); + // myMotorZ->release(); +} + +void loop() { + if (Serial.available()) { + byteUSB = Serial.read(); + Serial.println(byteUSB); + if ((byteUSB != 43) && (byteUSB !=48) && (byteUSB != 49) && (byteUSB != 50) && (byteUSB != 51) && (byteUSB != 52)) { + Serial.println("Invalid command received."); + } + else if ((commandReceived == false) && (byteUSB == 43)) { + commandReceived = true; + Serial.println("Valid command received."); + xCommandReceived = false; + zCommandReceived = false; + moveX = 0; + moveZ = 0; + } + else if ((commandReceived == true) && (xCommandReceived == false)) { + moveX = byteUSB - 48; + Serial.println(moveX); + xCommandReceived = true; + Serial.println("Command for x direction received."); + } + else if ((commandReceived == true) && (zCommandReceived == false)) { + moveZ = byteUSB - 48; + Serial.println(moveZ); + zCommandReceived = true; + Serial.println("Command for z direction received."); + } + } + else if ((commandReceived == true) && (xCommandReceived == true) && (zCommandReceived == true)) { + stepAction(moveX,moveZ); + commandReceived = false; + } +} diff --git a/Software/Arduino/ResetAndControlStepperMotors216/ResetAndControlStepperMotors216.ino b/Software/Arduino/ResetAndControlStepperMotors216/ResetAndControlStepperMotors216.ino new file mode 100644 index 0000000..5074094 --- /dev/null +++ b/Software/Arduino/ResetAndControlStepperMotors216/ResetAndControlStepperMotors216.ino @@ -0,0 +1,211 @@ +/* +Author: F. Lionetto +Created on: June 23rd, 2014 + +The sketch inizializes the position of the two stepper motors, move the laser away from the x and z micro switches, and then waits for commands sent through the USB port. + +Coordinate system: +- x axis across the strips, forward direction is toward the window, backward direction is toward the door (stepper motor 1); +- y axis along the strips; +- z axis toward the strips, forward direction is down, backward direction is up (stepper motor 2). + +The x micro switch is connected to pin 22. +The z micro switch is connected to pin 23. + +The stepper motors have two different speeds, one for non-precise and one for precise positioning, respectively. + +ASCII printable characters: ++ ---> 43; +0 ---> 48; +1 ---> 49; +2 ---> 50; +3 ---> 51. + +The only allowed commands (checked before and after being sent through the USB port) are: ++10 ---> move one step forward along x; ++20 ---> move one step backward along x ++30 ---> move at the opposite edge of the silicon sensor (along x); ++40 ---> move back; ++01 ---> move one step forward along z; ++02 ---> move one step backward along z; ++03 ---> do not do anything; ++04 ---> do not do anything. + +Distances in microns. + +For Hans410 sensor: +- the laser is initially on the strips corresponding to Beetle channels 216/220; +- +30 brings the laser on the strips corresponding to the Beetle channels 16/20; +- +40 brings the laser on the strips corresponding to the Beetle channels 216/220. +*/ + +#include +#include +#include "utility/Adafruit_PWMServoDriver.h" + +// Stepper motor properties. +const int stepsPerRevolution = 200; +const int singleStep = 5; +const int speedFast = 50; +const int speedSlow = 5; + +// Create the motor shield object with the default I2C address. +Adafruit_MotorShield AFMS = Adafruit_MotorShield(); + +// Connect a stepper motor with 200 steps per revolution (1.8 degree) to motor port #1 (M1 and M2). +Adafruit_StepperMotor *myMotorX = AFMS.getStepper(stepsPerRevolution, 1); +Adafruit_StepperMotor *myMotorZ = AFMS.getStepper(stepsPerRevolution, 2); + +// Micro switch properties. +const int switchXPin = 22; +const int switchZPin = 23; +int switchXState = 0; +int switchZState = 0; + +// Scan properties. +// Maximum distance from the x micro switch (!!!) +// const int maxStepsX = 16000; +// Maximum distance from the z micro switch (!!!) +// const int maxStepsZ = 5400; +// Move the laser 3.60 cm (7200 steps) away from the x micro switch +const int stepsFromXSwitch = 7200; +// Move the laser 2.00 cm (4000) away from the z micro switch +const int stepsFromZSwitch = 4000; +// Total number of steps we need along x. +// const int totStepsX = 200; +// Total number of steps we need along y. +// const int totStepsZ = 200; +// int stepsX = 0; +// int stepsZ = 0; +// Byte read from the USB port. +byte byteUSB; +// 1 if the byte read from the USB port is +. +boolean commandReceived = false; +// 1 if the byte read from the USB port is the first after +. +boolean xCommandReceived = false; +// 1 if the byte read from the USB port is the second after +. +boolean zCommandReceived = false; +int moveX = 0; +int moveZ = 0; + +void stepAction(int moveX, int moveZ) { + if (moveX == 1) { + myMotorX->step(1,FORWARD,DOUBLE); + Serial.println("Moved forward along x."); + } + else if (moveX == 2) { + myMotorX->step(1,BACKWARD,DOUBLE); + Serial.println("Moved backward along x."); + } + else if (moveX == 3) { + myMotorX->step(1900,BACKWARD,DOUBLE); + Serial.println("Moved at the opposite edge of the silicon sensor (along x)."); + } + else if (moveX == 4) { + myMotorX->step(1900,FORWARD,DOUBLE); + Serial.println("Moved back."); + } + if (moveZ == 1) { + myMotorZ->step(1,FORWARD,DOUBLE); + Serial.println("Moved forward along z."); + } + else if (moveZ == 2) { + myMotorZ->step(1,BACKWARD,DOUBLE); + Serial.println("Moved backward along z."); + } + else if (moveZ == 3) { + Serial.println("Nothing done."); + } + else if (moveZ == 4) { + Serial.println("Nothing done."); + } +} + +void setup() { + Serial.begin(9600); + Serial.println("This is a test!"); + + AFMS.begin(); + + myMotorX->setSpeed(speedFast); + myMotorZ->setSpeed(speedFast); + + pinMode(switchXPin,INPUT); + pinMode(switchZPin,INPUT); + + Serial.println("Inizializing position along x..."); + + while (switchXState == LOW) { + switchXState = digitalRead(switchXPin); + if (switchXState == LOW) { + // The stepper motor is not at the origin of the coordinate system. + myMotorX->step(1,FORWARD,DOUBLE); + } + } + + Serial.println("Position along x successfully initialized."); + + Serial.println("Inizializing position along z..."); + + while (switchZState == LOW) { + switchZState = digitalRead(switchZPin); + if (switchZState == LOW) { + // The stepper motor is not at the origin of the coordinate system. + myMotorZ->step(1,BACKWARD,DOUBLE); + } + } + + Serial.println("Position along z successfully initialized."); + + Serial.println("Moving the laser 3.60 cm (7200 steps) away from the x micro switch..."); + + myMotorX->step(stepsFromXSwitch,BACKWARD,DOUBLE); + + Serial.println("Laser moved 3.60 cm (7200 steps) away from the x micro switch."); + + Serial.println("Moving the laser 2.00 cm (4000 steps) away from the z micro switch..."); + + myMotorZ->step(stepsFromZSwitch,FORWARD,DOUBLE); + + Serial.println("Laser moved 2.00 cm (4000 steps) away from the z micro switch."); + + myMotorX->setSpeed(speedSlow); + myMotorZ->setSpeed(speedSlow); + + // myMotorX->release(); + // myMotorZ->release(); +} + +void loop() { + if (Serial.available()) { + byteUSB = Serial.read(); + Serial.println(byteUSB); + if ((byteUSB != 43) && (byteUSB !=48) && (byteUSB != 49) && (byteUSB != 50) && (byteUSB != 51) && (byteUSB != 52)) { + Serial.println("Invalid command received."); + } + else if ((commandReceived == false) && (byteUSB == 43)) { + commandReceived = true; + Serial.println("Valid command received."); + xCommandReceived = false; + zCommandReceived = false; + moveX = 0; + moveZ = 0; + } + else if ((commandReceived == true) && (xCommandReceived == false)) { + moveX = byteUSB - 48; + Serial.println(moveX); + xCommandReceived = true; + Serial.println("Command for x direction received."); + } + else if ((commandReceived == true) && (zCommandReceived == false)) { + moveZ = byteUSB - 48; + Serial.println(moveZ); + zCommandReceived = true; + Serial.println("Command for z direction received."); + } + } + else if ((commandReceived == true) && (xCommandReceived == true) && (zCommandReceived == true)) { + stepAction(moveX,moveZ); + commandReceived = false; + } +} diff --git a/Software/Arduino/ResetAndControlStepperMotors80/ResetAndControlStepperMotors80.ino b/Software/Arduino/ResetAndControlStepperMotors80/ResetAndControlStepperMotors80.ino new file mode 100644 index 0000000..d60cffa --- /dev/null +++ b/Software/Arduino/ResetAndControlStepperMotors80/ResetAndControlStepperMotors80.ino @@ -0,0 +1,187 @@ +/* +Author: F. Lionetto +Created on: June 23rd, 2014 + +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. + +Coordinate system: +- x axis across the strips, forward direction is toward the window, backward direction is toward the door (stepper motor 1); +- y axis along the strips; +- z axis toward the strips, forward direction is down, backward direction is up (stepper motor 2). + +The x micro switch is connected to pin 22. +The z micro switch is connected to pin 23. + +The stepper motors have two different speeds, one for non-precise and one for precise positioning, respectively. + +ASCII printable characters: ++ ---> 43; +0 ---> 48; +1 ---> 49; +2 ---> 50. + +The only allowed commands (checked before and after being sent through the USB port) are: ++10 ---> move one step forward along x; ++20 ---> move one step backward along x ++01 ---> move one step forward along z; ++02 ---> move one step backward along z. + +Distances in microns. +*/ + +#include +#include +#include "utility/Adafruit_PWMServoDriver.h" + +// Stepper motor properties. +const int stepsPerRevolution = 200; +const int singleStep = 5; +const int speedFast = 50; +const int speedSlow = 5; + +// Create the motor shield object with the default I2C address. +Adafruit_MotorShield AFMS = Adafruit_MotorShield(); + +// Connect a stepper motor with 200 steps per revolution (1.8 degree) to motor port #1 (M1 and M2). +Adafruit_StepperMotor *myMotorX = AFMS.getStepper(stepsPerRevolution, 1); +Adafruit_StepperMotor *myMotorZ = AFMS.getStepper(stepsPerRevolution, 2); + +// Micro switch properties. +const int switchXPin = 22; +const int switchZPin = 23; +int switchXState = 0; +int switchZState = 0; + +// Scan properties. +// Maximum distance from the x micro switch (!!!) +// const int maxStepsX = 16000; +// Maximum distance from the z micro switch (!!!) +// const int maxStepsZ = 5400; +// Move the laser 4.25 cm (8500 steps) away from the x micro switch +const int stepsFromXSwitch = 8500; +// Move the laser 2.00 cm (4000) away from the z micro switch +const int stepsFromZSwitch = 4000; +// Total number of steps we need along x. +// const int totStepsX = 200; +// Total number of steps we need along y. +// const int totStepsZ = 200; +// int stepsX = 0; +// int stepsZ = 0; +// Byte read from the USB port. +byte byteUSB; +// 1 if the byte read from the USB port is +. +boolean commandReceived = false; +// 1 if the byte read from the USB port is the first after +. +boolean xCommandReceived = false; +// 1 if the byte read from the USB port is the second after +. +boolean zCommandReceived = false; +int moveX = 0; +int moveZ = 0; + +void stepAction(int moveX, int moveZ) { + if (moveX == 1) { + myMotorX->step(1,FORWARD,DOUBLE); + Serial.println("Moving forward along x..."); + } + else if (moveX == 2) { + myMotorX->step(1,BACKWARD,DOUBLE); + Serial.println("Moving backward along x..."); + } + if (moveZ == 1) { + myMotorZ->step(1,FORWARD,DOUBLE); + Serial.println("Moving forward along z..."); + } + else if (moveZ == 2) { + myMotorZ->step(1,BACKWARD,DOUBLE); + Serial.println("Moving backward along z..."); + } +} + +void setup() { + Serial.begin(9600); + Serial.println("This is a test!"); + + AFMS.begin(); + + myMotorX->setSpeed(speedFast); + myMotorZ->setSpeed(speedFast); + + pinMode(switchXPin,INPUT); + pinMode(switchZPin,INPUT); + + Serial.println("Inizializing position along x..."); + + while (switchXState == LOW) { + switchXState = digitalRead(switchXPin); + if (switchXState == LOW) { + // The stepper motor is not at the origin of the coordinate system. + myMotorX->step(1,FORWARD,DOUBLE); + } + } + + Serial.println("Position along x successfully initialized."); + + Serial.println("Inizializing position along z..."); + + while (switchZState == LOW) { + switchZState = digitalRead(switchZPin); + if (switchZState == LOW) { + // The stepper motor is not at the origin of the coordinate system. + myMotorZ->step(1,BACKWARD,DOUBLE); + } + } + + Serial.println("Position along z successfully initialized."); + + Serial.println("Moving the laser 4.25 cm (8500 steps) away from the x micro switch..."); + + myMotorX->step(stepsFromXSwitch,BACKWARD,DOUBLE); + + Serial.println("Laser moved 4.25 cm (8500 steps) away from the x micro switch."); + + Serial.println("Moving the laser 2.00 cm (4000 steps) away from the z micro switch..."); + + myMotorZ->step(stepsFromZSwitch,FORWARD,DOUBLE); + + Serial.println("Laser moved 2.00 cm (4000 steps) away from the z micro switch."); + + myMotorX->setSpeed(speedSlow); + myMotorZ->setSpeed(speedSlow); + + // myMotorX->release(); + // myMotorZ->release(); +} + +void loop() { + if (Serial.available()) { + byteUSB = Serial.read(); + Serial.println(byteUSB); + if ((byteUSB != 43) && (byteUSB !=48) && (byteUSB != 49) && (byteUSB != 50)) { + Serial.println("Invalid command received."); + } + else if ((commandReceived == false) && (byteUSB == 43)) { + commandReceived = true; + Serial.println("Valid command received."); + xCommandReceived = false; + zCommandReceived = false; + moveX = 0; + moveZ = 0; + } + else if ((commandReceived == true) && (xCommandReceived == false)) { + moveX = byteUSB - 48; + Serial.println(moveX); + xCommandReceived = true; + Serial.println("Command for x direction received."); + } + else if ((commandReceived == true) && (zCommandReceived == false)) { + moveZ = byteUSB - 48; + Serial.println(moveZ); + zCommandReceived = true; + Serial.println("Command for z direction received."); + } + } + else if ((commandReceived == true) && (xCommandReceived == true) && (zCommandReceived == true)) { + stepAction(moveX,moveZ); + commandReceived = false; + } +} diff --git a/Software/Arduino/sketch_jun23a/sketch_jun23a.ino b/Software/Arduino/sketch_jun23a/sketch_jun23a.ino deleted file mode 100644 index c3db030..0000000 --- a/Software/Arduino/sketch_jun23a/sketch_jun23a.ino +++ /dev/null @@ -1,187 +0,0 @@ -/* -Author: F. Lionetto -Created on: June 23rd, 2014 - -The sketch inizializes the position of the two stepper motors, move the laser 4.25 cm (8500 steps) away from the x micro switch, move the laser 2.5 cm (5000 steps) away from the z micro switch, and then waits for commands sent through the USB port. - -Coordinate system: -- x axis across the strips, forward direction is toward the window, backward direction is toward the door (stepper motor 1); -- y axis along the strips; -- z axis toward the strips, forward direction is down, backward direction is up (stepper motor 2). - -The x micro switch is connected to pin 22. -The z micro switch is connected to pin 23. - -The stepper motors have two different speeds, one for non-precise and one for precise positioning, respectively. - -ASCII printable characters: -+ ---> 43; -0 ---> 48; -1 ---> 49; -2 ---> 50. - -The only allowed commands (checked before and after being sent through the USB port) are: -+10 ---> move one step forward along x; -+20 ---> move one step backward along x -+01 ---> move one step forward along z; -+02 ---> move one step backward along z. - -Distances in microns. -*/ - -#include -#include -#include "utility/Adafruit_PWMServoDriver.h" - -// Stepper motor properties. -const int stepsPerRevolution = 200; -const int singleStep = 5; -const int speedFast = 50; -const int speedSlow = 5; - -// Create the motor shield object with the default I2C address. -Adafruit_MotorShield AFMS = Adafruit_MotorShield(); - -// Connect a stepper motor with 200 steps per revolution (1.8 degree) to motor port #1 (M1 and M2). -Adafruit_StepperMotor *myMotorX = AFMS.getStepper(stepsPerRevolution, 1); -Adafruit_StepperMotor *myMotorZ = AFMS.getStepper(stepsPerRevolution, 2); - -// Micro switch properties. -const int switchXPin = 22; -const int switchZPin = 23; -int switchXState = 0; -int switchZState = 0; - -// Scan properties. -// Maximum distance from the x micro switch (!!!) -// const int maxStepsX = 16000; -// Maximum distance from the z micro switch (!!!) -// const int maxStepsZ = 5400; -// Move the laser 4.25 cm (8500 steps) away from the x micro switch -const int stepsFromXSwitch = 8500; -// Move the laser 2.5 cm (5000) away from the z micro switch -const int stepsFromZSwitch = 5000; -// Total number of steps we need along x. -// const int totStepsX = 200; -// Total number of steps we need along y. -// const int totStepsZ = 200; -// int stepsX = 0; -// int stepsZ = 0; -// Byte read from the USB port. -byte byteUSB; -// 1 if the byte read from the USB port is +. -boolean commandReceived = false; -// 1 if the byte read from the USB port is the first after +. -boolean xCommandReceived = false; -// 1 if the byte read from the USB port is the second after +. -boolean zCommandReceived = false; -int moveX = 0; -int moveZ = 0; - -void stepAction(int moveX, int moveZ) { - if (moveX == 1) { - myMotorX->step(1,FORWARD,DOUBLE); - Serial.println("Moving forward along x..."); - } - else if (moveX == 2) { - myMotorX->step(1,BACKWARD,DOUBLE); - Serial.println("Moving backward along x..."); - } - if (moveZ == 1) { - myMotorZ->step(1,FORWARD,DOUBLE); - Serial.println("Moving forward along z..."); - } - else if (moveZ == 2) { - myMotorZ->step(1,BACKWARD,DOUBLE); - Serial.println("Moving backward along z..."); - } -} - -void setup() { - Serial.begin(9600); - Serial.println("This is a test!"); - - AFMS.begin(); - - myMotorX->setSpeed(speedFast); - myMotorZ->setSpeed(speedFast); - - pinMode(switchXPin,INPUT); - pinMode(switchZPin,INPUT); - - Serial.println("Inizializing position along x..."); - - while (switchXState == LOW) { - switchXState = digitalRead(switchXPin); - if (switchXState == LOW) { - // The stepper motor is not at the origin of the coordinate system. - myMotorX->step(1,FORWARD,DOUBLE); - } - } - - Serial.println("Position along x successfully initialized."); - - Serial.println("Inizializing position along z..."); - - while (switchZState == LOW) { - switchZState = digitalRead(switchZPin); - if (switchZState == LOW) { - // The stepper motor is not at the origin of the coordinate system. - myMotorZ->step(1,BACKWARD,DOUBLE); - } - } - - Serial.println("Position along z successfully initialized."); - - Serial.println("Moving the laser 4.25 cm (8500 steps) away from the x micro switch..."); - - myMotorX->step(stepsFromXSwitch,BACKWARD,DOUBLE); - - Serial.println("Laser moved 4.25 cm (8500 steps) away from the x micro switch."); - - Serial.println("Moving the laser 2.5 cm (5000 steps) away from the z micro switch..."); - - myMotorZ->step(stepsFromZSwitch,FORWARD,DOUBLE); - - Serial.println("Laser moved 2.5 cm (5000 steps) away from the z micro switch."); - - myMotorX->setSpeed(speedSlow); - myMotorZ->setSpeed(speedSlow); - - // myMotorX->release(); - // myMotorZ->release(); -} - -void loop() { - if (Serial.available()) { - byteUSB = Serial.read(); - Serial.println(byteUSB); - if ((byteUSB != 43) && (byteUSB !=48) && (byteUSB != 49) && (byteUSB != 50)) { - Serial.println("Invalid command received."); - } - else if ((commandReceived == false) && (byteUSB == 43)) { - commandReceived = true; - Serial.println("Valid command received."); - xCommandReceived = false; - zCommandReceived = false; - moveX = 0; - moveZ = 0; - } - else if ((commandReceived == true) && (xCommandReceived == false)) { - moveX = byteUSB - 48; - Serial.println(moveX); - xCommandReceived = true; - Serial.println("Command for x direction received."); - } - else if ((commandReceived == true) && (zCommandReceived == false)) { - moveZ = byteUSB - 48; - Serial.println(moveZ); - zCommandReceived = true; - Serial.println("Command for z direction received."); - } - } - else if ((commandReceived == true) && (xCommandReceived == true) && (zCommandReceived == true)) { - stepAction(moveX,moveZ); - commandReceived = false; - } -} diff --git a/Software/CheckAlignment/CheckAlignment.C b/Software/CheckAlignment/CheckAlignment.C index 2b49a48..d56ba86 100644 --- a/Software/CheckAlignment/CheckAlignment.C +++ b/Software/CheckAlignment/CheckAlignment.C @@ -39,7 +39,7 @@ #include "../Tools/FindStrip.C" #include "../Tools/Par.C" -void CheckAlignment(char *sensor, char *filename, int firstx, int lastx, int stepx, char *externalPath=0); +void CheckAlignment(char *sensor, char *filename, const Float_t firstx, const Float_t lastx, const Float_t stepx, char *externalPath=0); int main(int argc, char *argv[]) { @@ -84,9 +84,9 @@ cout << "Last position along x: " << argv[4] << endl; cout << "Step between two subsequent data acquisitions: " << argv[5] << endl; if (argc == 6) - CheckAlignment(argv[1],argv[2],argv[3],argv[4],argv[5]); + CheckAlignment(argv[1],argv[2],atoi(argv[3]),atoi(argv[4]),atoi(argv[5])); else if (argc == 7) - CheckAlignment(argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]); + CheckAlignment(argv[1],argv[2],atoi(argv[3]),atoi(argv[4]),atoi(argv[5]),argv[6]); else { cout << "Error! Too many arguments given..." << endl; @@ -98,11 +98,115 @@ } } -void CheckAlignment(char *sensor, char *filename, int firstx, int lastx, int stepx, char *externalPath) +void CheckAlignment(char *sensor, char *filename, const Float_t firstx, const Float_t lastx, const Float_t stepx, char *externalPath) { cout << "**************************************************" << endl; cout << "Checking alignment..." << endl; cout << "**************************************************" << endl; + // Do not comment this line. + gROOT->ProcessLine("#include "); + + // Do some fanciness to get the directory right. + string inputDirectory = "~/TestStand/AnalysisResults/"+string(sensor)+"/Alignment"; + string outputDirectory = "~/TestStand/AnalysisResults/"+string(sensor)+"/Alignment"; + if (externalPath!=0) + outputDirectory = string(outputDirectory+"/"+externalPath); + cout << "The input directory is: " << inputDirectory << endl; + cout << "The output directory is: " << outputDirectory << endl; + + // Create the outputDirectory directory if it does not exist. + string path_to_make = "mkdir -p "+outputDirectory; + system(path_to_make.c_str()); + + cout << "Figures stored in: " << outputDirectory+"/Figures/"+filename << endl; + + // Create a directory named Figures inside the directory named outputDirectory if it does not exist. + string path_to_make_figures = "mkdir -p "+outputDirectory+"/Figures/"+filename; + system(path_to_make_figures.c_str()); + + TString path_to_figures = (string(path_to_make_figures)).substr((string(path_to_make_figures)).find_last_of(' ')+1); + + int strip; + string direction; + + char char_input_ROOT[200]; + string input_ROOT; + string output_ROOT = outputDirectory+"/Alignment-"+filename+".root"; + string inputFindStrip; + string outputFindStrip; + ostringstream convert; + string temp; + + const Int_t steps = (Int_t)((lastx-firstx)/stepx+1); + Float_t x[steps]; + Float_t mean4ADC[steps]; + + // Open output ROOT file. + TFile *output = TFile::Open(TString(output_ROOT),"RECREATE"); + + for (Int_t step=0;stepGet(Form("hADCPedSub%d",strip)); + TH1D *hist2 = (TH1D *)input->Get(Form("hADCPedSub%d",strip+NSkip)); + TH1D *hist3 = (TH1D *)input->Get(Form("hADCPedSub%d",strip-NSkip)); + TH1D *hist4; + if (direction == "left") + hist4 = (TH1D *)input->Get(Form("hADCPedSub%d",strip-2*NSkip)); + else if (direction == "right") + hist4 = (TH1D *)input->Get(Form("hADCPedSub%d",strip+2*NSkip)); + mean4ADC[step] = hist1->GetMean()+hist2->GetMean()+hist3->GetMean()+hist4->GetMean(); + + // Close input data ROOT files. + input->Close(); + } + + output->cd(); + + for (Int_t step=0;stepWrite(); + + // Close output ROOT file. + output->Close(); + return; } diff --git a/Software/Tools/FindStrip.C b/Software/Tools/FindStrip.C index 16a48aa..8f29b54 100644 --- a/Software/Tools/FindStrip.C +++ b/Software/Tools/FindStrip.C @@ -27,7 +27,7 @@ cout << "Finding the Beetle channel corresponding to the strip hit by the laser..." << endl; cout << "**************************************************" << endl; - cout << "Input file: " << inputFilename << ", " << inputFilename.c_str() << endl; + cout << "Input file: " << inputFilename << endl; cout << "Output file: " << outputFilename << endl; int found;