diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..d6431a7 --- /dev/null +++ b/Readme.md @@ -0,0 +1,19 @@ +https://github.com/LouisKimDev/AutoAC + +- master.py + - 라즈베리파이의 GPIO 라이브러리 이용 + - 서보모터 stop시 떨림현상 + - GPIO를 IN으로 설정해서 해결 + + - 3시간마다 4분 대기하고 메인 전원을 켠다 + - Time함수로 현재 시간 받는다 + - 30분을 기다린뒤 test모드를 켠다 + - 중복을 막기위해 매 30분 마다 20초씩 누적해서 대기한다 + + +- test.py + - pigpio 라이브러리 이용 + - 서보모터 Idle시 안정적 + - 함수 각도 테스트 필요 + + diff --git a/main.py b/main.py deleted file mode 100644 index f82cc34..0000000 --- a/main.py +++ /dev/null @@ -1,86 +0,0 @@ -import RPi.GPIO as GPIO -import time - -servo_1 = 18 -servo_2 = 23 -move_time = 0.5 - -GPIO.setmode(GPIO.BCM) -GPIO.setup(servo_1, GPIO.OUT) -GPIO.setup(servo_2, GPIO.OUT) - -pwm1 = GPIO.PWM(servo_1, 50) -pwm2 = GPIO.PWM(servo_2, 50) - -pwm1.start(3.0) -pwm2.start(3.0) - -GPIO.setwarnings(False) - -def Servo1Routine(iterations): - for i in range(iterations): - pwm1.ChangeDutyCycle(3) - time.sleep(move_time) - pwm1.ChangeDutyCycle(12.5) - time.sleep(move_time) - pwm1.ChangeDutyCycle(3) - time.sleep(move_time) - print(f'servo1 activated for {iterations} times') - - -def Servo2Routine(iterations): - for i in range(iterations): - pwm2.ChangeDutyCycle(3) - time.sleep(move_time) - pwm2.ChangeDutyCycle(12.5) - time.sleep(move_time) - pwm2.ChangeDutyCycle(3) - time.sleep(move_time) - print(f'servo2 activated for {iterations} times') - - -def PrintTime(): - print(time.strftime('%H:%M:%S')) - - - -try: - while True: - if(time.localtime().tm_hour % 3 == 0) and \ - time.localtime().tm_min == 0 and \ - time.localtime().tm_sec == 0: - - PrintTime() - time.sleep(5) - Servo1Routine(1) - Servo2Routine(4) - - print('3hr routine ended') - - - elif time.localtime().tm_min % 30 == 0 and \ - time.localtime().tm_sec == 0: - - PrintTime() - time.sleep(5) - Servo2Routine(4) - - print('30min routine ended') - - - elif time.localtime().tm_min % 13 == 0 and \ - time.localtime().tm_sec == 0: - print(time.strftime('%H:%M:%S')) - time.sleep(5) - - - else: - pwm1.stop() - pwm2.stop() - - -except KeyboardInterrupt: - pwm1.stop() - pwm2.stop() - print('End') - GPIO.cleanup() diff --git a/master.py b/master.py new file mode 100644 index 0000000..de7ddc5 --- /dev/null +++ b/master.py @@ -0,0 +1,138 @@ +import RPi.GPIO as GPIO +import time + +''' +duty clycle (degree) +10 : 160 +7.5 : 120 +5 : 80 +2.5 : 40 +''' + +class motorz: + def __init__(self): + self.SERVO_1 = 18 + self.SERVO_2 = 23 + self.MOVE_TIME = 0.3 + self.COUNT = 1 + + GPIO.setmode(GPIO.BCM) + GPIO.setup(self.SERVO_1, GPIO.OUT) + GPIO.setup(self.SERVO_2, GPIO.OUT) + self.pwm1 = GPIO.PWM(self.SERVO_1, 50) + self.pwm2 = GPIO.PWM(self.SERVO_2, 50) + self.pwm1.start(0) + self.pwm2.start(0) + GPIO.setwarnings(False) + + + def Servo1Routine(self, iterations): + GPIO.setup(self.SERVO_1, GPIO.OUT) + for i in range(iterations): + self.pwm1.ChangeDutyCycle(7.5) + time.sleep(self.MOVE_TIME) + self.pwm1.ChangeDutyCycle(10) + time.sleep(self.MOVE_TIME) + + self.pwm1.ChangeDutyCycle(7.5) + time.sleep(self.MOVE_TIME) + GPIO.setup(self.SERVO_1, GPIO.IN) + print(f'servo1 activated for {iterations} times') + + + def Servo2Routine(self, iterations): + for i in range(iterations): + GPIO.setup(self.SERVO_2, GPIO.OUT) + self.pwm2.ChangeDutyCycle(7.5) + time.sleep(self.MOVE_TIME) + self.pwm2.ChangeDutyCycle(10) + time.sleep(self.MOVE_TIME) + + self.pwm2.ChangeDutyCycle(7.5) + time.sleep(self.MOVE_TIME) + GPIO.setup(self.SERVO_2, GPIO.IN) + print(f'servo2 activated for {iterations} times') + + + def PrintTime(self): + print(time.strftime('%H:%M:%S')) + + + def test(self): + try: + while True: + if time.localtime().tm_sec % 1 == 0: + print('test routine start') + + self.Servo1Routine(1) + self.Servo2Routine(4) + + print('test routine ended') + + except KeyboardInterrupt: + self.pwm1.stop() + self.pwm2.stop() + print('End') + GPIO.cleanup() + + + def main(self): + try: + while True: + if (time.localtime().tm_hour == \ + 23 or 2 or 3 or 8 or 11 or 14 or 17 or 20) \ + and time.localtime().tm_min == 0 : + + time.sleep(240) + + self.PrintTime() + self.Servo1Routine(1) # 00:04:00 + self.Servo2Routine(4) + time.sleep(1820) + + self.PrintTime() + self.Servo2Routine(4) # 00:34:20 + time.sleep(1840) + + self.PrintTime() + self.Servo2Routine(4) # 01:05:00 + time.sleep(1860) + + self.PrintTime() + self.Servo2Routine(4) # 01:36:00 + time.sleep(1880) + + self.PrintTime() + self.Servo2Routine(4) # 02:07:20 + time.sleep(1900) + + self.PrintTime() + self.Servo2Routine(4) # 02:39:00 + + # print('3hr routine ended') + + # elif time.localtime().tm_min % 30 == 0 and \ + # time.localtime().tm_sec == 0: + + # self.PrintTime() + # time.sleep(240 + self.COUNT * 20) + + # self.Servo2Routine(4) + + # self.COUNT += 1 + + # print('30min routine ended') + + except KeyboardInterrupt: + self.pwm1.stop() + self.pwm2.stop() + print('End') + GPIO.cleanup() + + + +if __name__ == "__main__": + a = motorz() + a.main() +else: + print('Excution Failed') \ No newline at end of file diff --git a/piggpio_test.py b/piggpio_test.py new file mode 100644 index 0000000..f182500 --- /dev/null +++ b/piggpio_test.py @@ -0,0 +1,34 @@ +import pigpio +import time + +# pigpio 라이브러리를 이용 +''' +set_servo_pulsewidth(user_gpio, pulsewidth) + +Parameter: +1. user_gpio = 0~31 + +2. pulsewidth = 0(off), 500(0-degree) ~ 2500(180-degree) +pulsewidth를 500, 2500로 쓰면 서보모터에 데미지 줄 수 있다. +그래서 600 ~ 2400사이 권장 +각도 공식 +f(x) = 600 + 10x + +''' + +pi = pigpio.pi() +pi.set_servo_pulsewidth(18, 0) # 18번 채널에연결된 서보모터를 꺼줍니다. + +sleep(1) + +pi.set_servo_pulsewidth(18, 500) # 18번채널에 연결된 서보모터를 0도로 이동 + +sleep(1) + +pi.set_servo_pulsewidth(18, 1500) # 가운데로 이동 90도 + +sleep(1) + +pi.set_servo_pulsewidth(18, 2500) # 180도 끝으로 이동. + +sleep(1) \ No newline at end of file