Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This program moves as directed by inputs given through
- //Matlab. They will include the axis, distance and number
- //of points of data. It will be plotted in a rectangular
- //format, which can also be a square.
- //Directions made for view from back of machine
- //Directions: X: + Left, - Right Y: + In, - Out Z: + Up, - Down
- //STARTING POSITIONS OF PROBE. PLACE MAGNET IN CENTER OF FIELD.
- /* Z axis: bottom left corner
- Y axis: top left corner
- X axis: top left corner
- */
- #include <Stepper.h>
- //number of steps on motor
- // the X and Y stepper motors each have 800 steps per revolution
- // each revolution in X and Y is 2mm wide
- // the Z stepper motor is untested
- #define STEPS 800
- //initializes stepper objects for each axis
- // first # goes to pulse, second # goes to direction
- Stepper stepperX(STEPS, 3, 6);
- Stepper stepperY(STEPS, 2, 5);
- Stepper stepperZ(STEPS, 4, 7);
- //Makes two general stepper objects.
- //These will change depending on which axis will be mapped.
- Stepper stepper1 = stepperX;
- Stepper stepper2 = stepperY;
- //magnetic field intensity
- double H = 0, hSum = 0;
- //declaring other variables
- int times = 0; // variable for matlab --> arduino com
- int axis, n1, n2;
- float numSteps1, numSteps2; // ensures that distance can be over 2 inches
- void setup()
- {
- Serial.begin(9600);
- // Prompts Matlab to begin the program
- // Ensures that the timing between runtimes for each program are the same
- Serial.println(1);
- // takes in an array of data, containing the size to map,
- // distance to map and which axis to map onto
- // if there's no data don't run the program
- while(times < 5) {
- if (times == 0 && Serial.available() > 0) {
- axis = Serial.parseInt();
- delay(1000);
- //Serial.print("axis: ");
- Serial.println(axis, DEC);
- if (axis != 0)
- times++;
- }
- if (times == 1 && Serial.available() > 0) {
- //number of steps ; 16400 makes one inch
- numSteps1 = Serial.parseFloat();
- delay(1000);
- //Serial.print("x numSteps: ");
- Serial.println(numSteps1);
- if (numSteps1 != 0)
- times++;
- }
- if (times == 2 && Serial.available() > 0) {
- //number of steps ; 16400 makes one inch
- numSteps2 = Serial.parseFloat();
- delay(1000);
- //Serial.print("y numSteps: ");
- Serial.println(numSteps2);
- if (numSteps2 != 0)
- times++;
- }
- if (times == 3 && Serial.available() > 0) {
- n1 = Serial.parseInt();
- delay(1000);
- //Serial.print("# of points x: ");
- Serial.println(n1, DEC);
- if (n1 != 0)
- times++;
- }
- if (times == 4 && Serial.available() > 0) {
- n2 = Serial.parseInt();
- delay(1000);
- Serial.println(n2, DEC);
- if (n2 != 0)
- times++;
- }
- if (times == 5) {
- //initialize correct stepper motors
- //axis == 1 not taken into account, since
- //it maps z axis on default.
- if (axis == 2) // y axis
- {
- stepper1 = stepperX;
- stepper2 = stepperZ;
- }
- else if (axis == 3) // z axis
- {
- stepper1 = stepperY;
- stepper2 = stepperZ;
- }
- /*
- Must set speed here so that Arduino knows exactly which
- motors to move. Do so after knowing which axis to plot
- on, so it initializes the correct stepper motors.
- The object stepper1, which may also represent stepperX
- or stepperY, does not take the same speed initialization
- as stepperX or stepperY, so it can't be initialized
- earlier in the program.
- */
- stepper1.setSpeed(200);
- stepper2.setSpeed(200);
- }
- }
- /*
- Creates loop for making a box; stopping number
- is based on how many points of data there are.
- delay after each movement to retrieve data
- */
- for(int i = 0; i < n2 / 2; i++)
- {
- //creates loop for first axis
- for(int a1 = 0; a1 < n1; a1++)
- {
- //divides the step sizes by a number
- //to get that many points
- takeData();
- //loop runs 10 times, but it should only move 9 times
- //so that it ends at the last point
- if(a1 < n1 - 1)
- stepper1.step(-numSteps1 / n1);
- delayMicroseconds(50);
- }
- //this moves it about the other axis one "step"
- stepper2.step(-numSteps2 / n2);
- delayMicroseconds(100);
- //if the probe ends on the opposite side of the scan
- //because of an odd number of points, just move it back.
- //don't take more data because it is already acquired
- if (n2 % 2 == 1 && i == n2 / 2 - 1) {
- stepper1.step(numSteps1 - numSteps1 / n1);
- } else {
- // repeat, but in the opposite direction.
- // Do Not go back in the opposite direction
- // if there is an odd number of steps
- for(int a1 = 0; a1 < n1; a1++)
- {
- takeData();
- if(a1 < n1 - 1)
- stepper1.step(numSteps1 / n1);
- delayMicroseconds(50);
- }
- //moves it another "step" again
- stepper2.step(-numSteps2 / n2);
- delayMicroseconds(100);
- }
- }
- //returns back to original position
- stepper2.step(numSteps2 - numSteps2 / n2);
- //Because it is not in a loop, the program will stop until
- //the next run. Be Careful: the motors will stay on if
- //plugged in, and may get very hot!
- }
- // Program only runs once, no need to loop
- void loop(){}
- //takes the data by taking 50 points of data
- //at the same spot and returning an average
- void takeData()
- {
- for(int i = 0; i < 50; i++)
- {
- H = analogRead(A0);
- hSum = hSum + H;
- }
- Serial.println(hSum/50);
- hSum = 0; //resets for next spot
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement