-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaserCtrl.py
67 lines (51 loc) · 1.5 KB
/
LaserCtrl.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# 提供LaserCtrl类及相关方法,用于控制激光头
import RPi.GPIO as gpio
import time
__author__ = 'vbcpascal'
__version__ = '1.0'
OPENED = True
CLOSED = False
class LaserCtrl(object):
def __init__(self, pin_pwm=None, frequency=50,
gpio_mode="BOARD"):
self.pin_pwm = pin_pwm
self.frequency = frequency
if (gpio_mode == "BCM"):
gpio.setmode(gpio.BCM)
else:
gpio.setmode(gpio.BOARD)
if self.pin_pwm != None:
gpio.setup(pin_pwm, gpio.OUT)
self.p = gpio.PWM(pin_pwm, frequency) # set frequency
self.p.start(0)
self.status = CLOSED
def open(self):
self.change_duty_cycle(85)
self.status = OPENED
def close(self):
self.change_duty_cycle(0)
self.status = CLOSED
def get_status(self):
return self.status
def change_duty_cycle(self, i):
self.p.ChangeDutyCycle(i)
def finish(self):
self.p.ChangeDutyCycle(0)
self.p.stop()
def test(self):
try:
while True:
try:
i = int(input('power: '))
self.change_duty_cycle(i)
except:
print('error input. Please input a number between 1 and 100.')
except KeyboardInterrupt:
print('close')
self.finish()
if __name__ == '__main__':
p = LaserCtrl(pin_pwm=12)
p.test()