Menu

Drive a stepper motor with stepper.h library

2016-07-22
2016-08-23
  • Dimitris Katsaounis

    Hi
    With this library the user can drive up to 5 stepper motors (not simultaneously)
    The main method is
    MotorRotate( MotorNumber as Byte , RotationsPerMinute as Word, NumberOfSteps , Optional MotorDirection as Byte = GCBClockwise , Optional MotorStepMode as Byte = GCBWaveDrive , Optional MotorType as Byte = GCBBipolarMotor )
    Τhe possible directions of rotation are two
    GCBClockwise or GCBRotateRight and
    GCBAnticlockwise or GCBRotateLeft
    The possible step modes are three
    GCBWaveDrive Or FullStep One Phase
    GCBFullStep Or FullStep Two Phases and
    GCBHalfStep
    The possible types of motors are two
    GCBUnipolarMotor Drive with Transistor Array
    GCBBipolarMotor Drive with L298 Motor Driver
    The user should declare in main program the ports to use any motor (4 each). Even should indicate the steps per revolution having the motor.
    Ιn addition the user can define up to 5 limit switches to terminate the movement of each motor when the corresponding switch get the value <1>.

    '    Stepper Motor routines for the GCBASIC compiler
    '    Copyright (C) 2016 Katsaounis Dimitris
    
    '    This library is free software; you can redistribute it and/or
    '    modify it under the terms of the GNU Lesser General Public
    '    License as published by the Free Software Foundation; either
    '    version 2.1 of the License, or (at your option) any later version.
    
    '    This library is distributed in the hope that it will be useful,
    '    but WITHOUT ANY WARRANTY; without even the implied warranty of
    '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    '    Lesser General Public License for more details.
    
    '    You should have received a copy of the GNU Lesser General Public
    '    License along with this library; if not, write to the Free Software
    '    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    '
    '
    '    This library can handle up to 5 stepper motors. The user should declare in main program
    '    the ports you use any motor (4 each). Even should indicate the steps per revolution having the motor
    '    and the number of steps.
    '
    '
    
    'Startup routine
    #startup InitMotor
    
    'Define pins for 5 Motors
    #define GCBStepper1Pin1 Stepper1Pin1
    #define GCBStepper1Pin2 Stepper1Pin2
    #define GCBStepper1Pin3 Stepper1Pin3
    #define GCBStepper1Pin4 Stepper1Pin4
    #define GCBStepper2Pin1 Stepper2Pin1
    #define GCBStepper2Pin2 Stepper2Pin2
    #define GCBStepper2Pin3 Stepper2Pin3
    #define GCBStepper2Pin4 Stepper2Pin4
    #define GCBStepper3Pin1 Stepper3Pin1
    #define GCBStepper3Pin2 Stepper3Pin2
    #define GCBStepper3Pin3 Stepper3Pin3
    #define GCBStepper3Pin4 Stepper3Pin4
    #define GCBStepper4Pin1 Stepper4Pin1
    #define GCBStepper4Pin2 Stepper4Pin2
    #define GCBStepper4Pin3 Stepper4Pin3
    #define GCBStepper4Pin4 Stepper4Pin4
    #define GCBStepper5Pin1 Stepper5Pin1
    #define GCBStepper5Pin2 Stepper5Pin2
    #define GCBStepper5Pin3 Stepper5Pin3
    #define GCBStepper5Pin4 Stepper5Pin4
    
    'Define types of motors
    #define GCBUnipolarMotor  1  'Drive with Transistor Array
    #define GCBBipolarMotor   2  'Drive with L298 Motor Driver
    #define GCBDCMotor        3  'Drive with L298 Motor Driver
    
    'Define Directions
    #define GCBClockwise  1
    #define GCBAnticlockwise 2
    #define GCBRotateRight 1
    #define GCBRotateLeft 2
    
    'Define step modes
    #define GCBWaveDrive  1 'Or FullStep One Phase
    #define GCBFullStep   2 'Or FullStep Two Phases
    #define GCBHalfStep   3
    
    'Other Defines
    #define GCBMotorInfinity   1
    #define GCBSwitch1 Switch1Pin
    #define GCBSwitch2 Switch2Pin
    #define GCBSwitch3 Switch3Pin
    #define GCBSwitch4 Switch4Pin
    #define GCBSwitch5 Switch5Pin
    #define GCBStopSwitch StopSwitchPin
    
    'Dim Global Variables
    Dim RotationsPerMinute , RotationsPerSecond, StepsPerSecond as Word
    Dim StepsPerRevolution as Byte
    Dim StepsCount, NumberOfSteps as Long
    
    Sub InitMotor
    
             Dir GCBStepper1Pin1 Out
             Dir GCBStepper1Pin2 Out
             Dir GCBStepper1Pin3 Out
             Dir GCBStepper1Pin4 Out
             Set GCBStepper1Pin1 off
             Set GCBStepper1Pin2 off
             Set GCBStepper1Pin3 off
             Set GCBStepper1Pin4 off
      #ifdef Stepper2Pin1
             Dir GCBStepper2Pin1 Out
             Dir GCBStepper2Pin2 Out
             Dir GCBStepper2Pin3 Out
             Dir GCBStepper2Pin4 Out
             Set GCBStepper2Pin1 off
             Set GCBStepper2Pin2 off
             Set GCBStepper2Pin3 off
             Set GCBStepper2Pin4 off
      #endif
      #ifdef Stepper3Pin1
             Dir GCBStepper3Pin1 Out
             Dir GCBStepper3Pin2 Out
             Dir GCBStepper3Pin3 Out
             Dir GCBStepper3Pin4 Out
             Set GCBStepper3Pin1 off
             Set GCBStepper3Pin2 off
             Set GCBStepper3Pin3 off
             Set GCBStepper3Pin4 off
      #endif
      #ifdef Stepper4Pin1
             Dir GCBStepper4Pin1 Out
             Dir GCBStepper4Pin2 Out
             Dir GCBStepper4Pin3 Out
             Dir GCBStepper4Pin4 Out
             Set GCBStepper4Pin1 off
             Set GCBStepper4Pin2 off
             Set GCBStepper4Pin3 off
             Set GCBStepper4Pin4 off
      #endif
      #ifdef Stepper5Pin1
             Dir GCBStepper5Pin1 Out
             Dir GCBStepper5Pin2 Out
             Dir GCBStepper5Pin3 Out
             Dir GCBStepper5Pin4 Out
             Set GCBStepper5Pin1 off
             Set GCBStepper5Pin2 off
             Set GCBStepper5Pin3 off
             Set GCBStepper5Pin4 off
      #endif
      #ifdef Switch1Pin
             Dir GCBSwitch1 In
      #endif
      #ifdef Switch2Pin
             Dir GCBSwitch2 In
      #endif
      #ifdef Switch3Pin
             Dir GCBSwitch3 In
      #endif
      #ifdef Switch4Pin
             Dir GCBSwitch4 In
      #endif
      #ifdef Switch5Pin
             Dir GCBSwitch5 In
      #endif
      #ifdef StopSwitchPin
             Dir GCBStopSwitch In
      #endif
    
    End Sub
    
    Sub  MotorRotate( MotorNumber as Byte , RotationsPerMinute as Word, NumberOfSteps , Optional MotorDirection as Byte = GCBClockwise , Optional MotorStepMode as Byte = GCBWaveDrive , Optional MotorType as Byte = GCBBipolarMotor )
         'Dim StepsCount as Long
         Dim StepsDirection as Integer
         MotorTimeDelay=MotorSetSpeed (RotationsPerMinute)
         If MotorType=GCBBipolarMotor Then
            if MotorStepMode=GCBFullStep Then
               If MotorDirection=GCBClockwise Then
                  SetMotorPin MotorNumber , 1 , 4,  1 , 1
                  Goto PinRelease
               else
                  SetMotorPin MotorNumber , 4 , 1, -1 , 4
                  Goto PinRelease
               End if
            End if
            if MotorStepMode=GCBWaveDrive Then
               If MotorDirection=GCBClockwise Then
                  SetMotorPin MotorNumber , 5 , 8 , 1 , 5
                  Goto PinRelease
               else
                  SetMotorPin MotorNumber , 8 , 5 , -1 , 8
                  Goto PinRelease
               End if
            End if
            if MotorStepMode=GCBHalfStep Then
               MotorTimeDelay=MotorTimeDelay/2
               If MotorDirection=GCBClockwise Then
                  SetMotorPin MotorNumber , 9 , 16 , 1 , 9
                  Goto PinRelease
               else
                  SetMotorPin MotorNumber , 16 , 9 ,-1 , 16
                  Goto PinRelease
               End if
            End if
         End if
         If MotorType=GCBUnipolarMotor Then
            NumberOfSteps +=1
            if MotorStepMode=GCBFullStep Then
               If MotorDirection=GCBClockwise Then
                  SetMotorPin MotorNumber , 17 , 20 , 1 , 17
                  Goto PinRelease
               else
                  SetMotorPin MotorNumber , 20 , 17 , -1 , 20
                  Goto PinRelease
               End if
            End if
            if MotorStepMode=GCBWaveDrive Then
               If MotorDirection=GCBClockwise Then
                  SetMotorPin MotorNumber , 21 , 24 , 1 , 21
                  Goto PinRelease
               else
                  SetMotorPin MotorNumber , 24 , 21 , -1 , 24
                  Goto PinRelease
               End if
            End if
            if MotorStepMode=GCBHalfStep Then
               MotorTimeDelay=MotorTimeDelay/2
               If MotorDirection=GCBClockwise Then
                  SetMotorPin MotorNumber , 25 , 32 , 1 , 25
                  Goto PinRelease
               Else
                  SetMotorPin MotorNumber , 32 , 25 , -1 , 32
               End if
            End if
         End if
    PinRelease:
         Select Case MotorNumber
            Case 1
                 GCBStepper1Pin1 = 0
                 GCBStepper1Pin2 = 0
                 GCBStepper1Pin3 = 0
                 GCBStepper1Pin4 = 0
            Case 2
                 #ifdef Stepper2Pin1
                        GCBStepper2Pin1 = 0
                        GCBStepper2Pin2 = 0
                        GCBStepper2Pin3 = 0
                        GCBStepper2Pin4 = 0
                 #endif
            Case 3
                 #ifdef Stepper3Pin1
                        GCBStepper3Pin1 = 0
                        GCBStepper3Pin2 = 0
                        GCBStepper3Pin3 = 0
                        GCBStepper3Pin4 = 0
                 #endif
            Case 4
                 #ifdef Stepper4Pin1
                        GCBStepper4Pin1 = 0
                        GCBStepper4Pin2 = 0
                        GCBStepper4Pin3 = 0
                        GCBStepper4Pin4 = 0
                 #endif
            Case 5
                 #ifdef Stepper5Pin1
                        GCBStepper5Pin1 = 0
                        GCBStepper5Pin2 = 0
                        GCBStepper5Pin3 = 0
                        GCBStepper5Pin4 = 0
                 #endif
         End Select
    End Sub
    
    Sub SetMotorPin (MotorNumber as Byte , TableBegin as Byte , TableEnd as Byte , StepsDirection as Integer , TablePosition as Byte)
        StepsCount=0
        Do until StepsCount = NumberOfSteps
    
            ReadTable PinSet , TablePosition , MotorPinValue
            Select Case MotorNumber
               Case 1
                    GCBStepper1Pin1 = MotorPinValue.0
                    GCBStepper1Pin2 = MotorPinValue.1
                    GCBStepper1Pin3 = MotorPinValue.2
                    GCBStepper1Pin4 = MotorPinValue.3
               Case 2
                    #ifdef Stepper2Pin1
                           GCBStepper2Pin1 = MotorPinValue.0
                           GCBStepper2Pin2 = MotorPinValue.1
                           GCBStepper2Pin3 = MotorPinValue.2
                           GCBStepper2Pin4 = MotorPinValue.3
                    #endif
               Case 3
                    #ifdef Stepper3Pin1
                           GCBStepper3Pin1 = MotorPinValue.0
                           GCBStepper3Pin2 = MotorPinValue.1
                           GCBStepper3Pin3 = MotorPinValue.2
                           GCBStepper3Pin4 = MotorPinValue.3
                    #endif
               Case 4
                    #ifdef Stepper4Pin1
                           GCBStepper4Pin1 = MotorPinValue.0
                           GCBStepper4Pin2 = MotorPinValue.1
                           GCBStepper4Pin3 = MotorPinValue.2
                           GCBStepper4Pin4 = MotorPinValue.3
                    #endif
               Case 5
                    #ifdef Stepper5Pin1
                           GCBStepper5Pin1 = MotorPinValue.0
                           GCBStepper5Pin2 = MotorPinValue.1
                           GCBStepper5Pin3 = MotorPinValue.2
                           GCBStepper5Pin4 = MotorPinValue.3
                    #endif
            End Select
            Wait MotorTimeDelay ms
            TablePosition=TablePosition + StepsDirection
            if StepsDirection>0 then
               if TablePosition=TableEnd + 1 then
                  TablePosition=TableBegin
               end if
            Else
               if TablePosition=TableEnd - 1 then
                  TablePosition=TableBegin
               end if
            End if
           'Swiches to End Rotation
            #ifdef GCBSwitch1
                   If GCBSwitch1 = 1 then Exit Sub
            #endif
            #ifdef GCBSwitch2
                   If GCBSwitch2 = 1 then Exit Sub
            #endif
            #ifdef GCBSwitch3
                   If GCBSwitch3 = 1 then Exit Sub
            #endif
            #ifdef GCBSwitch4
                   If GCBSwitch4 = 1 then Exit Sub
            #endif
            #ifdef GCBSwitch5
                   If GCBSwitch5 = 1 then Exit Sub
            #endif
            #ifdef GCBStopSwitch
                   If GCBStopSwitch = 1 then Exit Sub
            #endif
            StepsCount +=1
        Loop
         'Next
    End Sub
    
    Function MotorSetSpeed (RotationsPerMinute) as Word 'In milliseconds
      RotationsPerSecond=[Word]RotationsPerMinute/60
      if SysCalcTempX>=5 then RotationsPerSecond++
      StepsPerSecond=[Word]RotationsPerSecond*StepsPerRevolution
      MotorSetSpeed=[Word]1000/StepsPerSecond
    End Function
    
    Table PinSet
      5  '0b00000101 ' 1 BipolarFullStep
      6  '0b00000110
     10  '0b00001010
      9  '0b00001001 ' 4
      1  '0b00000001 ' 5 BipolarWaveDrive
      4  '0b00000100
      2  '0b00000010
      8  '0b00001000 ' 8
      1  '0b00000001 ' 9 BipolarHalfStep
      5  '0b00000101
      4  '0b00000100
      6  '0b00000110
      2  '0b00000010
     10  '0b00001010
      8  '0b00001000
      9  '0b00001001 '16
      3  '0b00000011 '17 UnipolarFullStep
      6  '0b00000110
     12  '0b00001100
      9  '0b00001001 '20
      1  '0b00000001 '21 UnipolarWaveDrive
      2  '0b00000010
      4  '0b00000100
      8  '0b00001000 '24
      1  '0b00000001 '25 UnipolarHalfStep
      3  '0b00000011
      2  '0b00000010
      6  '0b00000110
      4  '0b00000100
     12  '0b00001100
      8  '0b00001000
      9  '0b00001001 '32
    
    End Table
    

    The test program i use

    ; ----- Configuration
      '#chip mega2560, 16
    #chip mega328p, 16
    
    #include <UNO_mega328p.h>
    #include "Stepper.h"
    
    #define Stepper1Pin1  DIGITAL_2
    #define Stepper1Pin2  DIGITAL_4
    #define Stepper1Pin3  DIGITAL_3
    #define Stepper1Pin4  DIGITAL_5
    #define Stepper2Pin1  DIGITAL_6
    #define Stepper2Pin2  DIGITAL_7
    #define Stepper2Pin3  DIGITAL_8
    #define Stepper2Pin4  DIGITAL_9
    
    StepsPerRevolution = 48
    Wait 1 s
    
    do forever
      MotorRotate 1 , 60 , 96 ,  GCBClockwise , GCBWaveDrive , GCBUnipolarMotor
      wait 2 s
      MotorRotate 2 , 60 , 96 ,  GCBClockwise , GCBWaveDrive
      wait 2 s
      MotorRotate 1 , 60 , 96 ,  GCBAntiClockwise , GCBWaveDrive , GCBUnipolarMotor
      wait 2 s
      MotorRotate 2 , 60 , 96 ,  GCBAntiClockwise , GCBWaveDrive
      wait 2 s
      MotorRotate 1 , 60 , 96 ,  GCBClockwise , GCBHalfStep , GCBUnipolarMotor
      wait 2 s
      MotorRotate 2 , 60 , 96 ,  GCBClockwise , GCBHalfStep
      wait 2 s
      MotorRotate 1 , 60 , 96 ,  GCBAnticlockwise , GCBHalfStep , GCBUnipolarMotor
      wait 2 s
      MotorRotate 2 , 60 , 96 ,  GCBAticlockwise , GCBHalfStep
      wait 2 s
      MotorRotate 1 , 60 , 96 ,  GCBClockwise , GCBFullStep , GCBUnipolarMotor
      wait 2 s
      MotorRotate 2 , 60 , 96 ,  GCBClockwise , GCBFullStep
      wait 2 s
      MotorRotate 1 , 60 , 96 ,  GCBAnticlockwise , GCBFullStep , GCBUnipolarMotor
      wait 2 s
      MotorRotate 2 , 60 , 96 ,  GCBAntiClockwise , GCBFullStep
      wait 2 s
    
    loop
    

    My 27 Euros CNC works with Stepper.h library
    https://www.youtube.com/watch?v=ww4p01LJBG0

     
  • Dimitris Katsaounis

    The Library

     
  • Dimitris Katsaounis

    The test program

     

    Last edit: Dimitris Katsaounis 2016-07-22
  • Dimitris Katsaounis

    The cnc program

     

    Last edit: Dimitris Katsaounis 2016-07-22
  • Chris Roper

    Chris Roper - 2016-07-22

    Excelent.

     
  • Anobium

    Anobium - 2016-07-23

    Very good. Really good work.

     
  • jackjames

    jackjames - 2017-02-12

    Wow !!!
    Can not download the two .gcb files.
    ERROR 404

     

    Last edit: jackjames 2017-02-12
  • Anobium

    Anobium - 2017-02-12

    Odd. try tomorrow.
    I will also ping Dimi to see if he can repost. This has to be the only time when I did not absorb into the release as I always feared this - loss of code and good insights. :-(

     
  • jackjames

    jackjames - 2017-02-12

    OK. Thanks.

     
  • Anobium

    Anobium - 2017-02-12

    Sent directly by Dimitris Katsaounis today - hot off the press. http://gcbasic.sourceforge.net/newfiles/Stepper.zip I will leave here for a day or two then I will move into the main build. Too risky leaving them in the forum.

    Say thanks to Dimitris.

    :-)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.