Advertisement
CuriousScientist

Accelstepper homing with AttachInterrupt

Nov 8th, 2019
1,892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to this tutorial video: https://youtu.be/0Xi7unlq1L4
  3. //The tutorial video for the AccelStepper library: https://youtu.be/AR0un3kg-iM
  4.  
  5. //linear carriage:
  6. /* c goes away from the motor
  7.  * s or o goes towards the motor
  8.  * h does homing, based on the default direction
  9.  * max range is 4000 steps = 10 rotations = 200 mm = 20 cm
  10. */
  11.  
  12. /*
  13. If you want to buy the gadgets shown in the video and support me at the same time, you can use the following links:
  14. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  15. Variable Power Supply (9-24V 3A 72W): https://www.banggood.com/custlink/DGKvSpwFvQ
  16. TB6600 Stepper Driver: https://www.banggood.com/custlink/KvvvZr1Pmj
  17. NEMA17 Stepper Motor: https://www.banggood.com/custlink/vGK3ic1tm2
  18. Limit switch on PCB (1PCS): https://www.banggood.com/custlink/DGDv6shnVd
  19. Limit switch on PCB (5PCS): https://www.banggood.com/custlink/DGvv0bYbcT
  20. Limit switch on PCB (10 PCS): https://www.banggood.com/custlink/m3GmeQdntG
  21. Limit switch (10PCS) Requires soldering and cables: https://www.banggood.com/custlink/mKG3BQdQW2
  22. Aluminium profile 2020 T-slot: https://www.banggood.com/custlink/vm3GBbybPc
  23. Right angle joint for 2020 profile: https://www.banggood.com/custlink/mm3DgbdsPP
  24. Various M5 screws for 2020 profile: https://www.banggood.com/custlink/DG3DebEbtO
  25. Sliding nut for 2020 profile: https://www.banggood.com/custlink/GmmmgAhQFe
  26. T8 lead screw with anti-backlash nut: https://www.banggood.com/custlink/D3GG0KdsYY
  27. T8 nut housing block: https://www.banggood.com/custlink/m3vmB3EsDO
  28. KPxxx Pillow Bearing: https://www.banggood.com/custlink/DK3veYunZC
  29. */
  30.  
  31. #include <AccelStepper.h>
  32.  
  33. long receivedMMdistance = 0; //distance in mm from the computer
  34. long receivedDelay = 0; //delay between two steps, received from the computer
  35. long receivedAcceleration = 0; //acceleration value from computer
  36. char receivedCommand; //character for commands
  37. /* s = Start (CCW) // needs steps and speed values
  38.  * o = open (CCW) // needs steps and speed values
  39.  * c = close (CW) //needs steps and speed values
  40.  * a = set acceleration // needs acceleration value
  41.  * h = homing // goes back slowly to the origin. Origin should be determined manually as well as all the parameters! (speed, dir, acc...etc)
  42.  * n = stop right now! // just the 'n' is needed
  43.  */
  44.  
  45. bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
  46.  
  47. const byte ledPin = 4; //led status pin, just to get a visual feedback from the button
  48. const byte interruptPin = 2; //pin for the microswitch using attachInterrupt();
  49.  
  50. // direction Digital 9 (CCW), pulses Digital 8 (CLK)
  51. AccelStepper stepper(1, 8, 9);
  52.  
  53.  
  54.  
  55.  
  56. void setup()
  57. {
  58.   pinMode(interruptPin, INPUT_PULLUP); // internal pullup resistor (debouncing)
  59.   attachInterrupt(digitalPinToInterrupt(interruptPin), stopMotor, FALLING);
  60.   //If you choose FALLING, make sure that the switch connects the pin 2 to the GND when it is pressed.
  61.  //You can change FALLING but make sure that you connect the switch to GND or +5V accordingly!
  62.  
  63. //LED pins, OFF by default
  64.   pinMode(ledPin, OUTPUT);
  65.   digitalWrite(ledPin,LOW);  
  66.  
  67.   Serial.begin(9600); //define baud rate
  68.   Serial.println("Testing Accelstepper"); //print a message
  69.  
  70.   //setting up some default values for maximum speed and maximum acceleration
  71.   stepper.setMaxSpeed(2000); //SPEED = Steps / second
  72.   stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
  73.   stepper.disableOutputs(); //disable outputs, so the motor is not getting warm (no current)
  74.  
  75.  
  76. }
  77.  
  78. void loop()
  79. {
  80.  
  81.   checkSerial(); //check serial port for new commands
  82.  
  83.   continuousRun2(); //method to handle the motor
  84.  
  85. }
  86.  
  87.  
  88. void continuousRun2() //method for the motor
  89. {
  90.   if (runallowed == true)
  91.   {
  92.     if (abs(stepper.currentPosition()) < receivedMMdistance) //abs() is needed because of the '<'
  93.     {
  94.       stepper.enableOutputs(); //enable pins
  95.       stepper.run(); //step the motor (this will step the motor by 1 step at each loop)
  96.     }
  97.     else //program enters this part if the required distance is completed
  98.     {
  99.      
  100.       runallowed = false; //disable running -> the program will not try to enter this if-else anymore
  101.       stepper.disableOutputs(); // disable power
  102.       Serial.print("POS: ");
  103.       Serial.println(stepper.currentPosition()); // print pos -> this will show you the latest relative number of steps
  104.       stepper.setCurrentPosition(0); //reset the position to zero
  105.       Serial.print("POS: ");
  106.       Serial.println(stepper.currentPosition()); // print pos -> this will show you the latest relative number of steps; we check here if it is zero for real
  107.     }
  108.  
  109.  
  110.   }
  111.   else //program enters this part if the runallowed is FALSE, we do not do anything
  112.   {
  113.     return;
  114.  
  115.   }
  116. }
  117.  
  118. void checkSerial() //method for receiving the commands
  119. {  
  120.   //switch-case would also work, and maybe more elegant
  121.  
  122.   if (Serial.available() > 0) //if something comes
  123.   {
  124.     receivedCommand = Serial.read(); // this will read the command character
  125.     newData = true; //this creates a flag
  126.   }
  127.  
  128.   if (newData == true) //if we received something (see above)
  129.   {
  130.     //START - MEASURE
  131.     if (receivedCommand == 's') //this is the measure part
  132.     {
  133.       //example s 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
  134.       runallowed = true; //allow running
  135.      
  136.  
  137.       receivedMMdistance = Serial.parseFloat(); //value for the steps
  138.       receivedDelay = Serial.parseFloat(); //value for the speed
  139.  
  140.       Serial.print(receivedMMdistance); //print the values for checking
  141.       Serial.print(receivedDelay);
  142.       Serial.println("Measure "); //print the action
  143.       stepper.setMaxSpeed(receivedDelay); //set speed
  144.       stepper.move(receivedMMdistance); //set distance
  145.  
  146.     }
  147.     //START - OPEN
  148.     if (receivedCommand == 'o') //OPENING
  149.     {
  150.       //example o 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed
  151.       runallowed = true; //allow running
  152.      
  153.  
  154.  
  155.       receivedMMdistance = Serial.parseFloat(); //value for the steps
  156.       receivedDelay = Serial.parseFloat(); //value for the speed
  157.  
  158.       Serial.print(receivedMMdistance); //print the values for checking
  159.       Serial.print(receivedDelay);
  160.       Serial.println("OPEN "); //print the action
  161.       stepper.setMaxSpeed(receivedDelay); //set speed
  162.       stepper.move(receivedMMdistance); //set distance
  163.  
  164.     }
  165.  
  166.     //START - CLOSE
  167.     if (receivedCommand == 'c') //CLOSING - Rotates the motor in the opposite direction as opening
  168.     {
  169.       //example c 2000 500 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed; will rotate in the other direction
  170.       runallowed = true; //allow running
  171.      
  172.  
  173.  
  174.       receivedMMdistance = Serial.parseFloat(); //value for the steps
  175.       receivedDelay = Serial.parseFloat(); //value for the speed
  176.  
  177.       Serial.print(receivedMMdistance);  //print the values for checking
  178.       Serial.print(receivedDelay);
  179.       Serial.println("CLOSE "); //print action
  180.       stepper.setMaxSpeed(receivedDelay); //set speed
  181.       stepper.move(-1 * receivedMMdistance); ////set distance - negative value flips the direction
  182.  
  183.     }
  184.  
  185.     //STOP - STOP
  186.     if (receivedCommand == 'n') //immediately stops the motor
  187.     {
  188.       runallowed = false; //disable running
  189.        
  190.       stepper.setCurrentPosition(0); // reset position
  191.       Serial.println("STOP "); //print action
  192.       stepper.stop(); //stop motor
  193.       stepper.disableOutputs(); //disable power
  194.  
  195.     }
  196.  
  197.     //SET ACCELERATION
  198.     if (receivedCommand == 'a') //Setting up a new acceleration value
  199.     {
  200.       runallowed = false; //we still keep running disabled, since we just update a variable
  201.      
  202.       receivedAcceleration = Serial.parseFloat(); //receive the acceleration from serial
  203.  
  204.       stepper.setAcceleration(receivedAcceleration); //update the value of the variable
  205.  
  206.       Serial.println("ACC Updated "); //confirm update by message
  207.  
  208.     }
  209.  
  210.     //HOMING
  211.     if (receivedCommand == 'h') //homing, this movement will be interrupted via the attachInterrupt() triggered by the microswitch.
  212.     {
  213.       runallowed = true; //allow running          
  214.  
  215.       Serial.println("HOMING"); //print action
  216.       stepper.setAcceleration(100); //defining some low acceleration
  217.       stepper.setMaxSpeed(100); //set speed, 100 for test purposes
  218.       stepper.move(-1 * 20000); ////set distance - negative value flips the direction
  219.       //distance should be larger than the length of the whole path.
  220.       //I don't think that this is a safe way of homing. if the switch fails, the motor will keep running anyway
  221.     }
  222.  
  223.   }
  224.   //after we went through the above tasks, newData becomes false again, so we are ready to receive new commands again.
  225.   newData = false;
  226.  
  227.  
  228. }
  229.  
  230. void stopMotor()//function activated by the pressed microswitch
  231. {
  232.   //Stop motor, disable outputs; here we should also reset the numbers if there are any
  233.   runallowed = false; //disable running
  234.        
  235.       stepper.setCurrentPosition(0); // reset position
  236.       Serial.println("STOP "); //print action
  237.       stepper.stop(); //stop motor
  238.       stepper.disableOutputs(); //disable power
  239.  
  240.  
  241.   Serial.println("Pressed."); //feedback towards the serial port
  242.  
  243.   //This part might not work properly.
  244.   digitalWrite(4,HIGH); //turn on LED
  245.   delay(2000); //wait a bit
  246.   digitalWrite(4,LOW); //turn off the LED    
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement