Advertisement
sinned6915

testr

Nov 1st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.53 KB | None | 0 0
  1. //
  2. // TestrBot Program Ver 1.0
  3. // Licence: Attribution-ShareAlike CC BY-SA
  4. // Created by Michael Graham, www.EngineerDog.com
  5. //
  6. // This program operates the complete Testrbot assembly including loadcell feedback,
  7. // stepper motor control, LCD Readout, & button control
  8. //
  9. // The Circuit:
  10. // A0 = is button direction input
  11. // A1 = home limit switch input, (Switch is normally closed returning 3.3v or 676 bits)
  12. // A2 = load cell input
  13. // A3 = top Limit Switch input, (Switch is normally closed returning 3.3v or 676 bits)
  14. // D4-D10 = LCD screen input
  15. // D11,12 = step & direction output
  16. //
  17. // Button Commands:
  18. // up key = MOVE UP WHILE KEY IS HELD
  19. // down key = MOVE DOWN WHILE KEY IS HELD
  20. // left key = MOVE TO LOWER HOME POSITION
  21. // right key = STOP MOVING
  22. // select key = AUTO TEST (runs in last direction moved!)
  23. //
  24. //____LCD DECLATRATIONS___________________________________________________________________________________________
  25. #include <LiquidCrystal.h>
  26. #include <LCDKeypad.h>
  27. LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7); //Defined LCD, (digital pins used by LCD)
  28. int adc_key_val[5] ={47, 145, 400, 600, 800 }; //(0 = right, 1 = up, 2 = down, 3 = left, 4 = select)
  29. int NUM_KEYS = 5;
  30. int adc_key_in;
  31. int key=-1;
  32. int oldkey=-1;
  33. String Status;
  34.  
  35. //____STEPPER MOTOR DECLATRATIONS_________________________________________________________________________________
  36. #include <AccelStepper.h>
  37. AccelStepper stepper(1, 12, 11); // Defined Stepper: (# motors, 'step' digital output pin, 'direction' digital output pin)
  38. const int HomeLimitSwitch = 1; // The analog input pin # for the home switch
  39. const int TopLimitSwitch = 3; // The analog input pin # for the home switch
  40. const int MoveDisp = 10; // steps to move if any move button is pressed
  41. const int MaxSpeed = 4000; // Max Speed in steps per second. 4000 is MAX for Ardu Uno. (See http://www.daycounter.com/Calculators/Stepper-Motor-Calculator.phtml)
  42. const int MaxAcceleration = 4000; // Steps per second per second
  43. const int StepsPerRevolution = 200; // Steps per revolution, a physical property of your stepper motor
  44. float GearRatio = 1.933333; // Output/Input teeth of gears 29/15
  45. const int RodTPI = 18; // Threaded rod threads per inch, a 5/16-18 rod is standard.
  46. float HomeLimitSwitchState = 0; // The current state of the homing limit switch
  47. float TopLimitSwitchState = 0; // The current state of the homing limit switch
  48.  
  49. //____LOAD CELL DECLATRATIONS______________________________________________________________________________________
  50. const int LoadPin = 2; // select the input pin for the loadcell
  51. float LoadOffset = 0; // Load added to measured load to offset it to zero, lbs
  52. float Load = 0; // Sensed load force, lbs.
  53. int OverloadForce = 185; // Testrbot will shutdown if it detects anything over this force, lbs.<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  54. float Disp = 0; // Sensed displacement in inches
  55. const int DelayTime = 200; // delay X millisec to control acquisition rate: (1000 = 1Hz, 200 = 5 Hz, 20 = 50Hz, 10 = 100Hz)
  56. double LoadScale = 40.00314; // lb/V, calculated as a function of nominal load, gain, & sensitivity
  57. double StepPerInch = StepsPerRevolution*RodTPI*GearRatio; //Calculates linear inches of travel per stepper step
  58. int Direction; // This variable will be used to detect the last direction moved
  59.  
  60. //____LCD SETUP___________________________________________________________________________________________________
  61. void setup()
  62. {
  63. Serial.begin(9600); // initialize serial communication at 9600 baud. The serial monitor is used for data collection.
  64. lcd.begin(16, 2);
  65. lcd.clear();
  66. lcd.setCursor(0,0);
  67. lcd.print("*TESTRBOT V1.0*");
  68. lcd.setCursor(0,1);
  69. lcd.print(" System Reset!");
  70. delay(2000);
  71.  
  72. //____STEPPER SETUP________________________________________________________________________________________________
  73. stepper.setMaxSpeed(MaxSpeed);
  74. stepper.setAcceleration(MaxAcceleration);
  75. stepper.setCurrentPosition(0);
  76. digitalWrite(A1, HIGH); //This pulls up the analog pin for the home limit switch to clean up the signal.
  77. digitalWrite(A3, HIGH); //This pulls up the analog pin for the home limit switch to clean up the signal.
  78.  
  79. //____LOADCELL SETUP_______________________________________________________________________________________________
  80. LoadOffset = -analogRead(LoadPin)*.0048828125*LoadScale; // Raw loadcell reading * Voltage Input Range divided by bit resolution (5v/1024) * Load cell scale factor. 'LoadOffset' is for setting the 'Load' to zero.
  81. }
  82.  
  83. //____LOOP__________________________________________________________________________________________________________
  84. void loop()
  85. {
  86. CollectData();
  87. delay (DelayTime);
  88. PrintSerial();
  89. lcd.clear();
  90. PrintLCD();
  91. key = get_key(adc_key_in); // convert into key press
  92. if (key != oldkey) // if new keypress is detected jump into this large if statement, otherwise restart LOOP
  93. {
  94. while (key == 1) // _________________________UP key (Move UP if key is held)__________________________________________
  95. {
  96. CollectData();
  97. if(TopLimitSwitchState > 1000) //Make sure you are not at the top before moving up
  98. {
  99. Serial.println("REACHED TOP OF TRAVEL, STOPPING!");
  100. Stop(); //If you are at the top, stop.
  101. break;
  102. }
  103. if (Load >= OverloadForce) //If the actuator has hit the overload force STOP.
  104. {
  105. Serial.println("REACHED MAX LOAD, STOPPING!");
  106. Stop();
  107. break;
  108. }
  109. Status = "UP";
  110. PrintLCD();
  111. stepper.move(MoveDisp);
  112. stepper.run();
  113. CollectData();
  114. key = get_key(adc_key_in); // Convert raw reading into specific key press
  115. if (key != 1) //If you let go of the key, this function exits the loop and restarts LOOP
  116. {
  117. oldkey = key;
  118. Status = " ";
  119. break;
  120. }
  121. }
  122. while (key == 2) //_______________________DOWN key (Move down if key is held)__________________________________________
  123. {
  124. CollectData();
  125. if(HomeLimitSwitchState > 1000) //Make sure you are not at home before moving down
  126. {
  127. GoHome(); //If you are at home its safe to call 'GoHome' Again because it knows to check for the switch status
  128. break;
  129. }
  130. if (Load >= OverloadForce) //If the actuator has hit the overload force STOP.
  131. {
  132. Serial.println("REACHED MAX LOAD, STOPPING!");
  133. Stop();
  134. break;
  135. }
  136. Status = "DOWN";
  137. PrintLCD();
  138. stepper.move(-MoveDisp);
  139. stepper.run();
  140. CollectData();
  141. key = get_key(adc_key_in); // Convert raw reading into specific key press
  142. if (key != 2) // If you let go of the key, this function exits the loop and restarts LOOP
  143. {
  144. oldkey = key;
  145. Status = " ";
  146. break;
  147. }
  148. }
  149. if (key == 3) //_________________LEFT KEY (Move to home position)______________________________________________
  150. {
  151. Status = "HOME";
  152. PrintLCD();
  153. PrintSerial();
  154. GoHome();
  155. }
  156. if (key == 4) //_______________________SELECT key (AUTO TEST)___________________________________________________
  157. {
  158. Status = "TEST";
  159. PrintLCD();
  160. PrintSerial();
  161. TestRun();
  162. }
  163. }
  164. }
  165.  
  166.  
  167. //____Function Assignment_____________________________________________________________________________________________
  168.  
  169. int get_key(unsigned int input) // This function converts the raw analog signal from A0 to key number
  170. {
  171. int k;
  172. for (k = 0; k < NUM_KEYS; k++)
  173. {
  174. if (input < adc_key_val[k])
  175. {
  176. return k;
  177. }
  178. }
  179. if (k >= NUM_KEYS)k = -1; // If no valid key is pressed
  180. return k;
  181. }
  182.  
  183. int CollectData() //This function collects all available data
  184. {
  185. Load = abs(analogRead(LoadPin)*.0048828125*LoadScale+LoadOffset+.00001); // The abs & .00001 prevent the negative sign from showing at 0 lbs.
  186. Disp = stepper.currentPosition()/StepPerInch; // Converts steps into inches
  187. adc_key_in = analogRead(0); // Check if key is being pressed
  188. TopLimitSwitchState = analogRead(TopLimitSwitch); // Check if top limit switch is tripped
  189. HomeLimitSwitchState = analogRead(HomeLimitSwitch); // Check if home limit switch is tripped
  190. }
  191.  
  192. int PrintSerial() //This function prints all available data to the serial monitor
  193. {
  194. Serial.print(millis()); // print the time in milliseconds since the program started
  195. Serial.print(','); // print a comma
  196. Serial.print(Load,6); // print the load to serial
  197. Serial.print(','); // print a comma
  198. Serial.print(Disp,6); // print the displacement to serial
  199. Serial.print(','); // print a comma
  200. Serial.println(Status); // print the actuator status to serial (HOME, STOP, UP, DOWN, TEST)
  201. }
  202.  
  203. int PrintLCD() //This function prints all available data to the LCD
  204. {
  205. lcd.setCursor(0,0);
  206. lcd.print("LOAD DISP STATUS"); //Header Line which is always showing
  207. lcd.setCursor(0,1);
  208. lcd.print(Load,1); //Current force to 1 decimal place
  209. lcd.print(" ");
  210. lcd.print(Disp); //Current displacementr to 2 decimal places
  211. lcd.print(" ");
  212. lcd.print(Status); //Current Status (HOME, STOP, UP, DOWN, TEST)
  213. }
  214.  
  215. int Stop() //This function stops all movement
  216. {
  217. oldkey = key;
  218. stepper.stop();
  219. Serial.println("STOPPED ON COMMAND");
  220. Status = "STOP";
  221. PrintLCD();
  222. delay(1000);
  223. }
  224.  
  225. int GoHome() //This function sends the actuator to the bottom home position
  226. {
  227. Serial.println("GOING HOME");
  228. Status = "HOME";
  229. while (true)
  230. {
  231. CollectData();
  232. if (HomeLimitSwitchState > 1000)
  233. {
  234. oldkey = key;
  235. stepper.stop();
  236. Serial.println("AT HOME POSITION");
  237. Status = "HOME";
  238. PrintLCD();
  239. PrintSerial();
  240. delay(1000);
  241. break;
  242. }
  243. key = get_key(adc_key_in); // convert into key press
  244. PrintLCD();
  245. if (key == 0) //0 is the stop key (right button) _________RIGHT KEY______________________________________________________
  246. {
  247. Stop();
  248. break;
  249. }
  250. stepper.move(-MoveDisp);
  251. stepper.run();
  252. }
  253. }
  254.  
  255. int TestRun() //This function sends the actuator in the last direction moved and collects data until hitting a limit switch or reaching the overload force
  256. {
  257. if (oldkey == 2) //If the last direction moved was down, the oldkey will be 2.
  258. {
  259. Serial.println("START TENSILE TEST");
  260. Direction = -1;
  261. }
  262. else
  263. {
  264. Serial.println("START COMPRESSION TEST");
  265. Direction = 1;
  266. }
  267. while (true)
  268. {
  269. CollectData();
  270. if (HomeLimitSwitchState > 1000) //If the actuator is at the home position I run GoHome which will tell it to stop & note that it is at 'home'
  271. {
  272. Serial.println("FINISHED TENSILE TEST RUN");
  273. GoHome();
  274. break;
  275. }
  276. if (TopLimitSwitchState > 1000) //If the actuator is at the top position I take it to mean that it has finished a compression run, so I send it home.
  277. {
  278. Serial.println("FINISHED COMPRESSION TEST RUN");
  279. GoHome();
  280. break;
  281. }
  282. if (Load >= OverloadForce) //If the actuator has hit the overload force I tell it to stop.
  283. {
  284. Serial.println("REACHED MAX LOAD, STOPPING!");
  285. Stop();
  286. break;
  287. }
  288. key = get_key(adc_key_in); // convert into key press
  289. if (key == 0) //0 is the right key which means 'stop'
  290. {
  291. Stop();
  292. break;
  293. }
  294. CollectData();
  295. PrintLCD();
  296. PrintSerial();
  297. stepper.move(MoveDisp * Direction);
  298. stepper.run();
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement