Advertisement
Guest User

mfm code arduino

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. //This program moves as directed by inputs given through
  2. //Matlab. They will include the axis, distance and number
  3. //of points of data. It will be plotted in a rectangular
  4. //format, which can also be a square.
  5.  
  6. //Directions made for view from back of machine
  7. //Directions: X: + Left, - Right Y: + In, - Out Z: + Up, - Down
  8.  
  9. //STARTING POSITIONS OF PROBE. PLACE MAGNET IN CENTER OF FIELD.
  10. /* Z axis: bottom left corner
  11. Y axis: top left corner
  12. X axis: top left corner
  13. */
  14.  
  15. #include <Stepper.h>
  16.  
  17. //number of steps on motor
  18. // the X and Y stepper motors each have 800 steps per revolution
  19. // each revolution in X and Y is 2mm wide
  20. // the Z stepper motor is untested
  21. #define STEPS 800
  22.  
  23. //initializes stepper objects for each axis
  24. // first # goes to pulse, second # goes to direction
  25. Stepper stepperX(STEPS, 3, 6);
  26. Stepper stepperY(STEPS, 2, 5);
  27. Stepper stepperZ(STEPS, 4, 7);
  28.  
  29. //Makes two general stepper objects.
  30. //These will change depending on which axis will be mapped.
  31. Stepper stepper1 = stepperX;
  32. Stepper stepper2 = stepperY;
  33.  
  34. //magnetic field intensity
  35. double H = 0, hSum = 0;
  36.  
  37. //declaring other variables
  38. int times = 0; // variable for matlab --> arduino com
  39. int axis, n1, n2;
  40.  
  41. float numSteps1, numSteps2; // ensures that distance can be over 2 inches
  42.  
  43. void setup()
  44. {
  45. Serial.begin(9600);
  46.  
  47. // Prompts Matlab to begin the program
  48. // Ensures that the timing between runtimes for each program are the same
  49. Serial.println(1);
  50.  
  51. // takes in an array of data, containing the size to map,
  52. // distance to map and which axis to map onto
  53. // if there's no data don't run the program
  54. while(times < 5) {
  55.  
  56. if (times == 0 && Serial.available() > 0) {
  57. axis = Serial.parseInt();
  58. delay(1000);
  59.  
  60. //Serial.print("axis: ");
  61. Serial.println(axis, DEC);
  62. if (axis != 0)
  63. times++;
  64. }
  65.  
  66. if (times == 1 && Serial.available() > 0) {
  67. //number of steps ; 16400 makes one inch
  68. numSteps1 = Serial.parseFloat();
  69. delay(1000);
  70.  
  71. //Serial.print("x numSteps: ");
  72. Serial.println(numSteps1);
  73. if (numSteps1 != 0)
  74. times++;
  75. }
  76.  
  77. if (times == 2 && Serial.available() > 0) {
  78. //number of steps ; 16400 makes one inch
  79. numSteps2 = Serial.parseFloat();
  80. delay(1000);
  81.  
  82. //Serial.print("y numSteps: ");
  83. Serial.println(numSteps2);
  84. if (numSteps2 != 0)
  85. times++;
  86. }
  87.  
  88. if (times == 3 && Serial.available() > 0) {
  89. n1 = Serial.parseInt();
  90. delay(1000);
  91.  
  92. //Serial.print("# of points x: ");
  93. Serial.println(n1, DEC);
  94. if (n1 != 0)
  95. times++;
  96. }
  97.  
  98. if (times == 4 && Serial.available() > 0) {
  99. n2 = Serial.parseInt();
  100. delay(1000);
  101.  
  102. Serial.println(n2, DEC);
  103. if (n2 != 0)
  104. times++;
  105. }
  106.  
  107. if (times == 5) {
  108. //initialize correct stepper motors
  109. //axis == 1 not taken into account, since
  110. //it maps z axis on default.
  111.  
  112. if (axis == 2) // y axis
  113. {
  114. stepper1 = stepperX;
  115. stepper2 = stepperZ;
  116. }
  117. else if (axis == 3) // z axis
  118. {
  119. stepper1 = stepperY;
  120. stepper2 = stepperZ;
  121. }
  122.  
  123. /*
  124. Must set speed here so that Arduino knows exactly which
  125. motors to move. Do so after knowing which axis to plot
  126. on, so it initializes the correct stepper motors.
  127. The object stepper1, which may also represent stepperX
  128. or stepperY, does not take the same speed initialization
  129. as stepperX or stepperY, so it can't be initialized
  130. earlier in the program.
  131. */
  132. stepper1.setSpeed(200);
  133. stepper2.setSpeed(200);
  134.  
  135. }
  136.  
  137. }
  138.  
  139. /*
  140. Creates loop for making a box; stopping number
  141. is based on how many points of data there are.
  142. delay after each movement to retrieve data
  143. */
  144. for(int i = 0; i < n2 / 2; i++)
  145. {
  146. //creates loop for first axis
  147. for(int a1 = 0; a1 < n1; a1++)
  148. {
  149. //divides the step sizes by a number
  150. //to get that many points
  151. takeData();
  152.  
  153. //loop runs 10 times, but it should only move 9 times
  154. //so that it ends at the last point
  155. if(a1 < n1 - 1)
  156. stepper1.step(-numSteps1 / n1);
  157. delayMicroseconds(50);
  158. }
  159.  
  160. //this moves it about the other axis one "step"
  161. stepper2.step(-numSteps2 / n2);
  162. delayMicroseconds(100);
  163.  
  164. //if the probe ends on the opposite side of the scan
  165. //because of an odd number of points, just move it back.
  166. //don't take more data because it is already acquired
  167. if (n2 % 2 == 1 && i == n2 / 2 - 1) {
  168. stepper1.step(numSteps1 - numSteps1 / n1);
  169. } else {
  170. // repeat, but in the opposite direction.
  171. // Do Not go back in the opposite direction
  172. // if there is an odd number of steps
  173. for(int a1 = 0; a1 < n1; a1++)
  174. {
  175. takeData();
  176. if(a1 < n1 - 1)
  177. stepper1.step(numSteps1 / n1);
  178. delayMicroseconds(50);
  179. }
  180.  
  181. //moves it another "step" again
  182. stepper2.step(-numSteps2 / n2);
  183. delayMicroseconds(100);
  184. }
  185. }
  186.  
  187. //returns back to original position
  188. stepper2.step(numSteps2 - numSteps2 / n2);
  189.  
  190. //Because it is not in a loop, the program will stop until
  191. //the next run. Be Careful: the motors will stay on if
  192. //plugged in, and may get very hot!
  193. }
  194.  
  195. // Program only runs once, no need to loop
  196. void loop(){}
  197.  
  198. //takes the data by taking 50 points of data
  199. //at the same spot and returning an average
  200. void takeData()
  201. {
  202. for(int i = 0; i < 50; i++)
  203. {
  204. H = analogRead(A0);
  205. hSum = hSum + H;
  206. }
  207. Serial.println(hSum/50);
  208. hSum = 0; //resets for next spot
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement