Peter-Jan
Posts: 10
Joined: Thu Jun 18, 2015 11:41 am

steppermotor user control

Thu Jun 18, 2015 11:47 am

hello guys,

im working on a project where i need to control steppermotors via a raspberry pi the steppermotors do work but my question is how do i get user control so if i do w it go's forward for example.

this is the code im using at the moment:

Code: Select all

# Import required libraries
import time
import RPi.GPIO as GPIO
import random


# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO signals to use
# Pins 11,12,13,15
# GPIO17,GPIO18,GPIO27,GPIO22 for motor X1
# GPIO23, GPIO24, GPIO26, GPIO4 for motor X2
# Motor X1 achteruit
StepPinsX1B = [22,27,18,17]
# Motot X1 vooruit
StepPinsX1 = [17,18,27,22]
# Motor X2 vooruit
StepPinsX2 = [23,24,25,4]
# Motor X2 achteruit
StepPinsX2B = [4,25,24,23]
# Set all pins as output
for pin in StepPinsX1:
  print "Setup pins X1"
  GPIO.setup(pin,GPIO.OUT)
  GPIO.output(pin, False)
for pinX in StepPinsX2:
  print "Setup pins X2"
  GPIO.setup(pinX,GPIO.OUT)
  GPIO.output(pinX, False)

# Define some settings
StepCounter = 0
WaitTime = 0.1
# speed up
Up = "u"
# direction back
Back = "b"
# direction left
Left = "l"
# direction right
Right = "r"

# Define simple sequence
StepCount1 = 4
Seq1 = []
Seq1 = range(0, StepCount1)
Seq1[0] = [1,0,0,0]
Seq1[1] = [0,1,0,0]
Seq1[2] = [0,0,1,0]
Seq1[3] = [0,0,0,1]

# Define advanced sequence
# as shown in manufacturers datasheet
StepCount2 = 8
Seq2 = []
Seq2 = range(0, StepCount2)
Seq2[0] = [1,0,0,0]
Seq2[1] = [1,1,0,0]
Seq2[2] = [0,1,0,0]
Seq2[3] = [0,1,1,0]
Seq2[4] = [0,0,1,0]
Seq2[5] = [0,0,1,1]
Seq2[6] = [0,0,0,1]
Seq2[7] = [1,0,0,1]

# Choose a sequence to use
Seq = Seq2
StepCount = StepCount2

# Start main loop

try:
  while 1==1:
	
    for pin in range(0, 4):
      xpin = StepPinsX1B[pin]
      if Seq[StepCounter][pin]!=0:
        print " Step %i Enable %i" %(StepCounter,xpin)

        GPIO.output(xpin, True)
      else:
        GPIO.output(xpin, False)
    for pin1 in range(0, 4):
      xpin1 = StepPinsX2[pin1]
      if Seq[StepCounter][pin1]!=0:
        print " Step %i Enable %i" %(StepCounter,xpin1)

        GPIO.output(xpin1, True)
      else:
        GPIO.output(xpin1, False)

    StepCounter += 1

# If we reach the end of the sequence
# start again
    if (StepCounter==StepCount):
      StepCounter = 0
    if (StepCounter<0):
      StepCounter = StepCount
#   toets = input()
#   print (toets)
#    if toets = "s"
#      print (toets)
#    WaitTime = (WaitTime+0.1)
#    else print (WaitTime)
 
  # Wait before moving on
    time.sleep(WaitTime)

except Up:
    
       print "u is pushed"
       WaitTime = (WaitTime-0.5)
       print (WaitTime)
  

except KeyboardInterrupt:
  GPIO.cleanup()

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: steppermotor user control

Fri Jun 19, 2015 7:02 pm

Google "PyGame events", you can catch keyboard input whilst in a loop handling other stuff.

Dave.
Apple say... Monkey do !!

Peter-Jan
Posts: 10
Joined: Thu Jun 18, 2015 11:41 am

Re: steppermotor user control

Mon Jun 22, 2015 7:45 am

oke thank you

Return to “Python”