forked from eiffelwong1/star_tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibration.py
73 lines (63 loc) · 2.83 KB
/
calibration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import RPi.GPIO as GPIO
import time
baseMotorPins = (5,6,13,19) # define pins connected to four phase ABCD of stepper motor
quarterMotorPins = (12, 25, 24, 23) # define pins connected to four phase ABCD of stepper motor
quarterSwitch = 17
baseSwitch = 27
CCWStep = (0x01,0x02,0x04,0x08) # define power supply order for rotating anticlockwise
CWStep = (0x08,0x04,0x02,0x01) # define power supply order for rotating clockwise
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(quarterSwitch, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(baseSwitch, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
for pin in baseMotorPins:
GPIO.setup(pin,GPIO.OUT)
for pin in quarterMotorPins:
GPIO.setup(pin,GPIO.OUT)
# as for four phase stepping motor, four steps is a cycle. the function is used to drive the stepping motor clockwise or anticlockwise to take four steps
def moveOnePeriod(motor, 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
if (direction == 1):# power supply order clockwise
GPIO.output(motor[i],((CCWStep[j] == 1<<i) and GPIO.HIGH or GPIO.LOW))
else : # power supply order anticlockwise
GPIO.output(motor[i],((CWStep[j] == 1<<i) and 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
time.sleep(ms*0.001)
# continuous rotation function, the parameter steps specifies the rotation cycles, every four steps is a cycle
def moveSteps(motor, direction, ms, steps):
for i in range(steps):
moveOnePeriod(motor, direction, ms)
# function used to stop motor
def motorStop(motor):
for i in range(0,4,1):
GPIO.output(motor[i],GPIO.LOW)
def calibrate():
while (True):
moveSteps(baseMotorPins, 1,3,1) # rotating 360 deg clockwise, a total of 2048 steps in a circle, 512 cycles
if (GPIO.input(baseSwitch) == GPIO.HIGH):
print ('Base limit reached')
motorStop(baseMotorPins)
break
time.sleep(1)
while (True):
# moveSteps(quarterMotorPins, 1,3,1) # rotating 360 deg clockwise, a total of 2048 steps in a circle, 512 cycles
print ('simulating the quarter gear motor movement')
if (GPIO.input(quarterSwitch) == GPIO.HIGH):
print('Quarter limit reached')
motorStop(quarterMotorPins)
break
def loop():
while True:
moveSteps(baseMotorPins, 1,3,1) # rotating 360 deg clockwise, a total of 2048 steps in a circle, 512 cycles
time.sleep(0.01)
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program entrance
print ('Program is starting...')
setup()
try:
calibrate()
except KeyboardInterrupt: # Press ctrl-c to end the program.
destroy()