CuriousScientist

AccelStepper - Arduino - TB6600 - Updates

Sep 13th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.34 KB | None | 0 0
  1. //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //The code belongs to this tutorial video: https://youtu.be/PN5NjNRfgP4
  3. //Please visit https://curiousscientist.tech/blog/accelstepper-updates-clientsoftware
  4.  
  5. #include <AccelStepper.h>
  6.  
  7. //User-defined values
  8. long receivedSteps = 0; //Number of steps
  9. long receivedSpeed = 0; //Steps / second
  10. long receivedMaxSpeed = 0; //Steps / second
  11. long receivedAcceleration = 0; //Steps / second^2
  12. long CurrentPosition = 0;
  13. char receivedCommand; //a letter sent from the terminal
  14. long StartTime = 0;
  15. long PreviousTime = 0;
  16. //-------------------------------------------------------------------------------
  17. bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
  18. bool lastStepPosition = false; //follows the steps to see if the last step was preformed
  19. bool pingpong_CW = true;
  20. bool pingpong_CCW = true;
  21. bool pingpongAllowed = false;
  22. //-------------------------------------------------------------------------------
  23. AccelStepper stepper(1, 8, 9);// direction Digital 9 (CCW), pulses Digital 8 (CLK)
  24.  
  25. void setup()
  26. {
  27.     Serial.begin(115200); //define a baud rate
  28.     Serial.println("Demonstration of AccelStepper Library"); //print a messages
  29.     Serial.println("Send 'C' for printing the commands.");
  30.  
  31.     //setting up some default values for maximum speed and maximum acceleration
  32.     Serial.println("Default speed: 400 steps/s, default acceleration: 800 steps/s^2.");
  33.     stepper.setMaxSpeed(400); //SPEED = Steps / second
  34.     stepper.setAcceleration(800); //ACCELERATION = Steps /(second)^2
  35.     stepper.disableOutputs(); //disable outputs
  36.     StartTime = millis(); //start the timer
  37. }
  38.  
  39. void loop()
  40. {
  41.     //Constantly looping through these 4 functions.
  42.     //We only use non-blocking commands, so something else (should also be non-blocking) can be done during the movement of the motor
  43.     checkSerial(); //check serial port for new commands
  44.     RunTheMotor(); //function to handle the motor  
  45.     SendPosition();
  46.     PingPong();
  47. }
  48.  
  49.  
  50. void RunTheMotor() //function for the motor
  51. {
  52.     if (stepper.distanceToGo() != 0)
  53.     {
  54.         stepper.enableOutputs(); //enable pins
  55.         stepper.run(); //step the motor (this will step the motor by 1 step at each loop)  
  56.         lastStepPosition = true;
  57.     }
  58.     else //program enters this part if the runallowed is FALSE, we do not do anything
  59.     {
  60.        stepper.disableOutputs(); //disable outputs
  61.        if(lastStepPosition == true)
  62.        {
  63.           Serial.print("L");
  64.           Serial.println(stepper.currentPosition());//Print the message
  65.           //Serial.print("V"); //You can do the same with speed, but it slows down the arduino
  66.           //Serial.println(stepper.speed());//Print the message
  67.           lastStepPosition = false;
  68.        }
  69.         return;
  70.     }
  71. }
  72.  
  73. void SendPosition()
  74. {
  75.   if (stepper.distanceToGo() != 0)
  76.   {    
  77.     //The larger this number (300) the better. Multiple serial.println interferes with the stepper motor
  78.     if((millis()-StartTime) >= 400)
  79.     {
  80.       StartTime = millis();            
  81.       Serial.print("L");
  82.       Serial.println(stepper.currentPosition());//Print the message
  83.       //Serial.print("V"); //Alternatively, we can print speed too, but it can interfere with the motor at high speeds
  84.       //Serial.println(stepper.speed());//Print the message
  85.     }    
  86.   }
  87.   else
  88.   {
  89.   // skip
  90.   }  
  91. }
  92.  
  93.  
  94. void checkSerial() //function for receiving the commands
  95. {  
  96.     if (Serial.available() > 0) //if something comes from the computer
  97.     {
  98.         receivedCommand = Serial.read(); // pass the value to the receivedCommad variable
  99.         newData = true; //indicate that there is a new data by setting this bool to true
  100.  
  101.         if (newData == true) //we only enter this long switch-case statement if there is a new command from the computer
  102.         {
  103.             switch (receivedCommand) //we check what is the command
  104.             {
  105.  
  106.             case 'P': //P uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.      
  107.                 receivedSteps = Serial.parseFloat(); //value for the steps
  108.                 receivedSpeed = Serial.parseFloat(); //value for the speed
  109.                 Serial.println("Positive direction."); //print the action
  110.                 RotateRelative(); //Run the function
  111.                 //example: P2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 400 steps/s speed
  112.                 //In theory, this movement should take 5 seconds
  113.                 break;
  114.        
  115.                 case 'R': //R uses the moveTo() function of the AccelStepper library, which means that it moves absolutely to the current position.        
  116.                 receivedSteps = Serial.parseFloat(); //value for the steps
  117.                 receivedSpeed = Serial.parseFloat(); //value for the speed     
  118.                 Serial.println("Absolute position (+)."); //print the action
  119.                 RotateAbsolute(); //Run the function
  120.                 //example: R800 400 - It moves to the position which is located at +800 steps away from 0.
  121.                 break;
  122.        
  123.                 case 'S': // Stops the motor
  124.                 stepper.stop(); //stop motor
  125.                 stepper.disableOutputs(); //disable power
  126.                 Serial.println("Stopped."); //print action
  127.                 runallowed = false; //disable running
  128.                 pingpongAllowed = false; //disable pingpong
  129.                 break;
  130.        
  131.                 case 'A': // Updates acceleration        
  132.                 //runallowed = false; //we still keep running disabled, since we just update a variable
  133.                 //stepper.disableOutputs(); //disable power
  134.                 receivedAcceleration = Serial.parseFloat(); //receive the acceleration from serial
  135.                 stepper.setAcceleration(receivedAcceleration); //update the value of the variable
  136.                 //Serial.print("New acceleration value: "); //confirm update by message
  137.                 //Serial.println(receivedAcceleration); //confirm update by message
  138.                 break;
  139.        
  140.                 case 'V': // Updates speed
  141.                 //runallowed = false; //we still keep running disabled, since we just update a variable
  142.                 //stepper.disableOutputs(); //disable power
  143.                 receivedSpeed = Serial.parseFloat(); //receive the acceleration from serial
  144.                 stepper.setSpeed(receivedSpeed); //update the value of the variable
  145.                 //Serial.print("New speed value: "); //confirm update by message
  146.                 //Serial.println(receivedSpeed); //confirm update by message
  147.                 break;
  148.                
  149.                 case 'v': // Updates Max speed
  150.                 //runallowed = false; //we still keep running disabled, since we just update a variable
  151.                 //stepper.disableOutputs(); //disable power
  152.                 receivedMaxSpeed = Serial.parseFloat(); //receive the acceleration from serial
  153.                 stepper.setMaxSpeed(receivedMaxSpeed); //update the value of the variable
  154.                 //Serial.print("New Max speed value: "); //confirm update by message
  155.                 //Serial.println(receivedMaxSpeed); //confirm update by message
  156.                 break;
  157.                
  158.                 case 'L': //L: Location
  159.                 runallowed = false; //we still keep running disabled
  160.                 stepper.disableOutputs(); //disable power
  161.                 Serial.print("L");//Print the message
  162.                 Serial.println(stepper.currentPosition()); //Printing the current position in steps.
  163.                 break;
  164.                
  165.                 case 'U':
  166.                 runallowed = false; //we still keep running disabled
  167.                 stepper.disableOutputs(); //disable power
  168.                 stepper.setCurrentPosition(0); //Reset current position. "new home"            
  169.                 stepper.setSpeed(receivedSpeed); //We have to reupdate this, because the above function resets it.
  170.                 Serial.print("L"); //Print message
  171.                 Serial.println(stepper.currentPosition()); //Check position after reset.
  172.                 break;
  173.                
  174.                 case 'C':
  175.                 PrintCommands(); //Print the commands for controlling the motor
  176.                 break;
  177.                
  178.                 case 'K':
  179.                 runallowed = true;
  180.                 stepper.enableOutputs(); //enable pins
  181.                 stepper.setMaxSpeed(1000);
  182.                 Serial.println("PingPong");
  183.                 pingpong_CW = false;
  184.                 pingpong_CCW = false;
  185.                 pingpongAllowed = true;
  186.                 break;
  187.                
  188.                 default:
  189.                 //skip
  190.                 break;
  191.             }
  192.         }
  193.         //after we went through the above tasks, newData is set to false again, so we are ready to receive new commands again.
  194.         newData = false;       
  195.     }
  196. }
  197.  
  198. void PingPong()
  199. {      
  200.     if(pingpongAllowed == true) //If the pingpong function is allowed we enter
  201.     {
  202.       if(pingpong_CW == false) //CW rotation is not yet done
  203.       {  
  204.         stepper.moveTo(5000); //set a target position, it should be an absolute. relative (move()) leads to "infinite loop"
  205.        
  206.         if(stepper.distanceToGo() == 0) //When the above number of steps are completed, we manipulate the variables
  207.         {
  208.             pingpong_CW = true; //CW rotation is now done
  209.             pingpong_CCW = false; //CCW rotation is not yet done - this allows the code to enter the next ifs
  210.         }      
  211.       }
  212.        
  213.       if(pingpong_CW == true && pingpong_CCW == false) //CW is completed and CCW is not yet done
  214.       {
  215.         stepper.moveTo(0); //Absolute position
  216.        
  217.         if(stepper.distanceToGo() == 0) //When the number of steps are completed
  218.           {
  219.             pingpong_CCW = true; //CCW is now done
  220.             pingpong_CW = false; //CW is not yet done. This allows the code to enter the first if again!
  221.           }    
  222.       }
  223.     }
  224. }
  225.  
  226. void RotateRelative()
  227. {
  228.     //We move X steps from the current position of the stepper motor in a given direction (+/-).    
  229.     runallowed = true; //allow running - this allows entering the RunTheMotor() function.
  230.     stepper.setMaxSpeed(receivedSpeed); //set speed
  231.     stepper.move(receivedSteps); //set relative distance and direction
  232. }
  233.  
  234. void RotateAbsolute()
  235. {
  236.     //We move to an absolute position.
  237.     //The AccelStepper library keeps track of the position.
  238.  
  239.     runallowed = true; //allow running - this allows entering the RunTheMotor() function.
  240.     stepper.setMaxSpeed(receivedSpeed); //set speed
  241.     stepper.moveTo(receivedSteps); //set relative distance 
  242. }
  243.  
  244. void PrintCommands()
  245. {  
  246.     //Printing the commands
  247.     Serial.println(" 'C' : Prints all the commands and their functions.");
  248.     Serial.println(" 'P' : Rotates the motor - relative using move().");
  249.     Serial.println(" 'R' : Rotates the motor - absolute using moveTo().");
  250.     Serial.println(" 'S' : Stops the motor immediately.");
  251.     Serial.println(" 'A' : Sets an acceleration value.");
  252.     Serial.println(" 'V' : Sets a speed value using setSpeed().");
  253.     Serial.println(" 'v' : Sets a speed value using setMaxSpeed().");
  254.     Serial.println(" 'L' : Prints the current position/location of the motor using currentPosition().");
  255.     Serial.println(" 'U' : Updates the current position and makes it as the new 0 position using setCurrentPosition().");
  256.     Serial.println(" 'K' : Demonstrates an oscillating motion.");
  257. }
Add Comment
Please, Sign In to add comment