-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_mechanism.py
101 lines (75 loc) · 1.96 KB
/
lock_mechanism.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import RPi.GPIO as GPIO
import time
pin_IR = 17
pin_servo_open = 16
pin_servo_lock = 27
pin_switch = 20
servoMax = 12
servoMin = 3
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_IR,GPIO.IN)
GPIO.setwarnings(False)
GPIO.setup(pin_servo_open, GPIO.OUT)
GPIO.setup(pin_servo_lock, GPIO.OUT)
GPIO.setup(pin_switch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
servo_open = GPIO.PWM(pin_servo_open, 50)
servo_lock = GPIO.PWM(pin_servo_lock, 50)
servo_open.start(0)
servo_lock.start(0)
IR_cnt = 0
switch_cnt = 0
lock_switch = 0
def servo_open_pos(degree):
if degree > 180:
degree = 180
duty = servoMin + (degree*(servoMax-servoMin)/180.0)
print("Degree: {}, Duty: {}".format(degree, duty))
servo_open.ChangeDutyCycle(duty)
def servo_lock_pos(degree):
if degree > 180:
degree = 180
duty = servoMin + (degree*(servoMax-servoMin)/180.0)
print("Degree: {}, Duty: {}".format(degree, duty))
servo_lock.ChangeDutyCycle(duty)
#open_init
#servo
servo_lock_pos(0)
servo_open_pos(0)
time.sleep(1)
while True:
#lock
if lock_switch == 0:
#IR detected
if GPIO.input(pin_IR) == 1:
IR_cnt += 1
#servo
if IR_cnt >= 10:
print("lock", IR_cnt)
servo_lock_pos(120)
#time.sleep(1)
servo_open_pos(0)
time.sleep(1)
lock_switch = 1
IR_cnt = 0
#open
#print(switch_cnt)
elif lock_switch == 1:
if GPIO.input(pin_switch) == 1:
switch_cnt += 1
#servo
if switch_cnt >= 5:
print("open", switch_cnt)
servo_lock_pos(0)
time.sleep(0.5)
servo_open_pos(120)
time.sleep(0.5)
servo_open_pos(0)
time.sleep(0.5)
lock_switch = 0
switch_cnt = 0
#servo_lock.stop()
#servo_open.stop()
#pedometer
time.sleep(0.1)
print(IR_cnt, switch_cnt, lock_switch)
GPIO.cleanup