Chapter 16 Stepping Motor
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
for i in range(0,4,1):
GPIO.output(motorPins[i],GPIO.LOW)
def loop():
w hile Tr ue:
moveSteps(1,3,512) #rotating 360° clockwise, a total of 2048 steps in a
circle, namely, 512 cycles.
time.sleep(0.5)
moveSteps(0,3,512) #rotating 360° anticlockwise
time.sleep(0.5)
def destroy():
GPIO. cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
e xcept KeyboardInterrupt: # When 'Ctrl+C' is pressed, the subprogram destroy() will
be executed.
destroy()
In the code, define four pins of stepping motor and coil power supply order of four steps rotation mode.
motorPins = ( 12, 16, 18, 22) #define pins connected to four phase ABCD of stepper
motor
CCWStep = (0x01,0x02,0x04,0x08) #define power supply order for coil for rotating
anticlockwise
CWStep = ( 0x08,0x04,0x02,0x01) #define power supply order for coil for rotating
clockwise
Subfunction moveOnePeriod (direction, ms) will drive the stepping motor rotating four step clockwise or
anticlockwise, four step as a cycle. Where parameter "dir" indicates the rotation direction, if "dir" is 1, the servo
will rotate forward, otherwise it rotates to reverse direction. Parameter "ms" indicates the time between each
two steps. The "ms" of stepping motor used in this project is 3ms (the shortest time), less than 3ms will exceed
the speed limit of stepping motor resulting in that motor can not rotate.
def moveOnePeriod(direction,ms):
for j in range(0,4,1): #cycle for power supply order
for i in range(0,4,1): #assign to each pin, a total of 4 pins
if ( direction == 1):#power supply order clockwise
GPIO. output(motorPins[i],((CCWStep[j] == 1<<i) and GPIO.HIGH orGPIO.LOW))
else : #power supply order anticlockwise
GPIO. output(motorPins[i],((CWStep[j] == 1<<i) a nd GPIO. HIGH or GPIO.LOW))
if(ms<3): #the delay can not be less than 3ms, otherwise it will exceed
speed limit of the motor
ms = 3