You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#! /usr/bin/env python3
# encoding=utf-8
import RPi.GPIO as GPIO
import time
import signal
import atexit
import MySQL as m
def lock_insert(num):
t = time.strftime('%Y-%m-%d %H:%M:%S')
m.ms_insert(num, t, "open")
def lock_update(num):
m.ms_update("close_state", "Y", num)
t = time.strftime('%Y-%m-%d %H:%M:%S')
m.ms_update("time_close", t, num)
def lock_control():
atexit.register(GPIO.cleanup)
num = m.ms_select("max(number)")[0][0] + 1
servopin = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(servopin, GPIO.OUT, initial=False)
p = GPIO.PWM(servopin, 50) # 50HZ:频率就是周期脉冲的周期的倒数
p.start(0) # start(initdutycycle)占空比0-100间0表示暂不输出
time.sleep(2)
for i in range(0, 181, 10):
p.ChangeDutyCycle(2.5 + 10 * i / 180) # 设置转动角度
time.sleep(0.02) # 等该20ms周期结束
p.ChangeDutyCycle(0) # 归零信号
time.sleep(0.2)
lock_insert(str(num))
time.sleep(5)
for i in range(181, 0, -10):
p.ChangeDutyCycle(2.5 + 10 * i / 180)
time.sleep(0.02)
p.ChangeDutyCycle(0)
time.sleep(0.2)
lock_update(str(num))
GPIO.cleanup(servopin)