Menu

Complete Newbie .. wanting to control stepper using GC basic

ashimotok0
2015-03-04
2015-07-06
  • ashimotok0

    ashimotok0 - 2015-03-04

    Hi I just discovered GC basic so I downloaded (looks good) and bought a PICkit 2 debug Express package hopefully to use for programming the PIC.

    I have also bought a stepper motor controller PCB module, with the driver chip on it, so I can input step/direction logic signals. What I want to do with GC basic is move a linear actuating stepper motor to 5 user defined (memorized) positions. Selection of 5 input to the appropriate PIC will select that the motor drives to the appropriate position (e.g. 1 st button when pressed steps out X no. of pulses and then stop, 2nd button will step to Y etc ect. The datum will be the stall position of the stepper. a sixth button will return to datum.

    Does this sound doable with GC basic and the PICKit 2?

    Sorry if I am asking basic questions but I am completely new to this.

    Ash (UK)

     

    Last edit: ashimotok0 2015-03-04
  • David Stephenson

    It's pretty straightforward. It depends on what your controller module does. Is it just a h-bridge or is it more sophisticated? It will also depend on the type of stepper you have a google search will give switching patterns for different types of coil arrangements.

    here's a program for rotating a stepper I pulled out of a disk drive.
    The port pins are connected to a h-bridge. If you want fast rotation you will have to use feedback from the motor to keep it in phase.

    ~~~~~

    chip 16F688,8

    config OSC=INTRC_OSC_NOCLKOUT, MCLRE=OFF, WDT=OFF

    dir portc.3 out
    dir portc.2 out
    dir portc.1 out
    dir portc.0 out
    set portc.0 off
    set portc.1 off
    set portc.2 off
    start:
    set portc.3 off
    set portc.0 on
    wait 4 ms
    set portc.0 off
    set portc.1 on
    wait 4 ms
    set portc.1 off
    set portc.2 on
    wait 4 ms
    set portc.2 off
    set portc.3 on
    wait 4 ms
    goto start
    ~~~~~~~~~

     

    Last edit: David Stephenson 2015-03-04
  • mmotte

    mmotte - 2015-03-07

    hello ashimotok0,

    I put this together today. It only has 4 positions but another could be copied in. I am running a stepper on a Pacific Scientic 6410 step and dir controller running a 200 step motor. My proto board has a 4 line LCD with a 5 position nav switch that i am using for the 4 buttons and an esc/select/mode button.

    when it starts up it is in manual mode. You run the motor to the first position in manual, then push the teach button, then one of the 4 buttons to store that value under. then run the motor to the second position and teach another button. same for the other 2 or 3 buttons.

    Then push the pbut to put into auto.

    then push one of the 4 buttons and the motor goes there.

    Also there is a zero button in manual that you can put the motor in a position and then press zero for 1 sec and that is zero position.

    GL
    Mike W9YS


    ~~~~~~~~~
    ' FILE: stepper_5_switch_control_V0_90.gcb
    ' DATE: 03/7/2015
    'VERSION: 0.92
    ' AUTHOR: mike Otte
    'arr!
    'Demo step and direction control
    'Goal is to have a step motor go to 4 positions
    'dependent on which of 4 switches is momentarily
    'depressed. I am imagining that the stepper is on a lead
    'screw and can have more than the 200 or 45 steps of
    'one rotation. Also we are using a stepper control board
    'with "step and direction" control inputs.
    'Other concerns are:
    'how to set home and or go to home position -?homeswitch
    'how to set each programmed position - manual buttons
    'and teach switch

    ' This demonstration code 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 General Public License for more details.
    '

    'General hardware configuration
    #chip 16F886,4
    
    'LCD connection settings
    #define LCD_IO 4
    #define LCD_DB4 PORTB.4
    #define LCD_DB5 PORTB.5
    #define LCD_DB6 PORTB.6
    #define LCD_DB7 PORTB.7
    #define LCD_RS PORTB.1
    #define LCD_RW PORTB.2
    #define LCD_Enable PORTB.3
    
    ' Button direction Variables
    #define LBut PORTA.5 'Left
    #define PBut PORTA.4 'Push
    #define DBut PORTA.3 'Down
    #define RBut PORTA.2 'Right
    #define UBut PORTA.1 'Up
    
    #define Stp PORTC.0 'Stepper motor control STEP command
    #define Direction  PORTC.1 'Stepper motor control Direction command
    
    Dir PORTC.0 Out
    Dir PORTC.1 Out
    dir PORTA   in
    
    Dim Position As Word   'Stepper accumulated position
    Dim pos1 As Word
    Dim pos2 As Word
    Dim pos3 As Word
    Dim pos4 As Word
    Dim count As Word
    Dim goal As Word
    Dim Target As Word
    mode = 0        'mode will allow more uses for the switches
                    'modes will be manual=0, Auto/operate=1, ?
    direction = 0   ' direction of motor  0 = cw , 1 =ccw
    position = 0
    pos1 = 0
    pos2 = 0
    pos3 = 0
    pos4 = 0
    
    CLS
    PRINT "Stepper Control V0.90"
    locate 1,2
    PRINT " GCBASIC "
    wait 1 s
    
    CLS
    locate 0,0
    print "Manual"
    locate 0,8
    print "Pos= "
    Print position
    locate 2,0
    Print pos1
    locate 2,12
    Print pos2
    locate 1,6
    Print pos3
    locate 3,6
    Print pos4
    

    do

    IF PBut = 0 Then 'center select button is pushed

    If mode=0 Then   'toggle mode
           mode = 1
           locate 0,0
           print "Auto  "
          Else
           mode = 0
           locate 0,0
           print "Manual"
          end If
    

    End If

    SELECT CASE mode

    CASE 0                 'Manual, Home, Teach
      Do until PBut = 0    'center select button is pushed to
       locate 0,8
       print "Pos=       "
       locate 0,13
       Print position
       IF LBut = 0 Then     'manual move position down
          position = position -1
    If position <0 Then position = 0
    
          MoveOneM
       end if
       IF RBut = 0 Then       'Manual move position up
    position = position +1
    If position > 32000 Then position = 32000
    
          MoveOneP
       end if
    
       IF UBut = 0 Then      ' Manual zero position - hold for 1 sec
          wait 1 sec
          IF UBut = 0 Then position = 0
       end if
       IF DBut = 0 Then         ' Manual Teach position
          CLS
          print "Teach - select button to teach"
          wait 1 s
    Do
            IF PBut = 0 Then  Exit do
    
            IF LBut = 0 Then     'teach button 1 position
        pos1 = position
              locate 2,0
              Print pos1
              IF LBut = 1 Then Exit do
    
            end if
            IF RBut = 0 Then     'teach button 2 position
        pos2 = position
              locate 2,12
              Print pos2
              IF RBut = 1 Then Exit do
    
            end if
    
            IF UBut = 0 Then      'teach button 3 position
        pos3 = position
              locate 1,6
              Print pos3
              IF UBut = 1 Then Exit do
    
            end if
            IF DBut = 0 Then       'teach button 4 position
        pos4 = position
              locate 3,6
              Print pos4
              IF DBut = 1 Then Exit do
    
            end if  
          Loop
          Wait 50 10Ms
          CLS
          locate 0,8
          print "Pos= "
          Print position
          locate 2,0
          Print pos1
          locate 2,12
          Print pos2
          locate 1,6
          Print pos3
          locate 3,6
          Print pos4
       end if
    
     Loop
    
    CASE 1
        Do until PBut = 0    'center select button is pushed to
           locate 0,8
           print "Pos=        "
           locate 0,13
           Print position
          IF LBut = 0 Then
       target = pos1
             MoveToTarget(target)
          End If
          IF RBut = 0 Then
       target = pos2
             MoveToTarget(target)
          end if
    
          IF UBut = 0 Then
       target = pos3
             MoveToTarget(target)
          end if
          IF DBut = 0 Then
       target = pos4
             MoveToTarget(target)
          end if
    
        loop
    
    END SELECT
    

    wait 200 ms
    loop
    ' ----------------- Subroutines below --------------------------

    sub MoveOneP
    direction = 0
    PulseOut Stp,2 ms
    wait 20 Ms
    end Sub

    sub MoveOneM
    direction = 1
    PulseOut Stp,2 Ms
    wait 20 Ms
    end Sub

    sub MoveToTarget(In goal As Word)
    If goal > position Then
    goal = goal - position
    direction = 0
    count = 0
    Do until count = goal
    PulseOut Stp,2 ms
    count++
    position= position +1
    'locate 0,8
    'print "Pos= "
    'locate 0,13
    'print position
    wait 2 Ms
    Loop

    Else
       goal =  position - goal
       direction = 1
       count = 0
       Do until count = goal
        PulseOut Stp,2 ms
        count++
        position = position -1
        'locate 0,8
        'print "Pos=        "
        'locate 0,13
        'print position
        wait 2 Ms
       Loop
    End If
    locate 0,8
    print "Pos= "
    Print position
    locate 2,0
    Print pos1
    locate 2,12
    Print pos2
    locate 1,6
    Print pos3
    locate 3,6
    Print pos4
    

    end Sub
    ~~~~~~~~~~~~


     
  • ashimotok0

    ashimotok0 - 2015-03-18

    That's so kind of You Mike.
    If I try pasting your code into Graphical version, it errors because the 'Else' statement is not recognized? Any ideas please?

    Sorry for my ignorance but I am a complete newbie to this. I have several controllers that have Step/direction control so no problem there.

    Thanks again ...Ash

     
  • mmotte

    mmotte - 2015-03-18

    Ash,
    I have never given GraphicalGCB more than a casual glance. So it was a learning experience to look at the program in graphical. I have not the time right now to setup the stepper and protoboard but I got it to compile. It should be worth a try.

    Graphical was inserting a bunch of "end if" where they didn't belong. Also there is something with commenting that i didn't understand, so i took them out. Also it didn't insert the end sub block on the subs.

    attached is the program

    GL
    Mike

     
  • ashimotok0

    ashimotok0 - 2015-03-19

    Thanks so much for all of this Mike.. What I did after my posted reply yesterday was to download the GCBasic IDE and I loaded your programme OK and it allowed conversion to Graphical Basic with no loading errors, when I tried it. I then compiled it and it then allowed me to created a .hex fil.... so looking good. I just need to buy the correct LCD and few more bits to try out. I am using a PIC16F887 chip (came as demo module with PICKIT2) is that compatible, sounds like it may be ?

    Cheers !! Ash

     
  • mmotte

    mmotte - 2015-03-19

    Ash,
    my pickit2 came with that 16f887 board too. i have used it for many trial programs. It should run the program just fine, just change the processor at the beginning of the file.
    The program i attached yesterday is vers 0.95. the long list above is 0.92 in which i used pulseout command. my stepper driver board has opto isolated inputs and i needed pull downs instead of pulse up for stepping. so i took the pulseout out and put port and wait commands in to pull down the opto for 2ms. and then wait 2 ms. this provides rotation on my motor with out losing steps. this program does not do ramping at the beginning and end of a transition. future option. then the pulses could be closer.

    i only used the 4 line lcd because that was attached to the proto board.

    gl
    mike

     
  • ashimotok0

    ashimotok0 - 2015-07-06

    Please see my personal message Mike...back on this project after absence due to other work commitments. kind regards ... Ash

     

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.