Completed
Pull Request — master (#380)
by
unknown
03:38
created

PiPWM.getControlUnit()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""
3
A module to control the QO Raspberry Pi based H-Bridge hardware.
4
5
Qudi is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Qudi is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with Qudi. If not, see <http://www.gnu.org/licenses/>.
17
18
Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the
19
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
20
"""
21
22
23
from core.module import Base, ConfigOption
24
from interface.process_control_interface import ProcessControlInterface
25
from core.util.mutex import Mutex
26
27
import RPi.GPIO as GPIO
28
29
30
class PiPWM(Base, ProcessControlInterface):
31
    """ Hardware module for Raspberry Pi-based PWM controller.
32
    """
33
34
    _modclass = 'ProcessControlInterface'
35
    _modtype = 'hardware'
36
37
    channel = ConfigOption('channel', 0, missing='warn')
38
    freq = ConfigOption('frequency', 100)
39
40
    def __init__(self, **kwargs):
41
        super().__init__(**kwargs)
42
43
        #locking for thread safety
44
        self.threadlock = Mutex()
45
46
    def on_activate(self):
47
        """ Activate module.
48
        """
49
        # pin mapping
50
        if self.channel == 0:
51
            self.inapin = 5
52
            self.inbpin = 22
53
            self.pwmpin = 24
54
            self.enapin = 25
55
            self.enbpin = 23
56
            self.diapin = 17
57
            self.dibpin = 18
58
            self.fanpin = 27
59
        else:
60
            self.inapin = 16
61
            self.inbpin = 19
62
            self.pwmpin = 21
63
            self.enapin = 20
64
            self.enbpin = 26
65
            self.diapin = 12
66
            self.dibpin = 6
67
            self.fanpin = 13
68
69
        self.setupPins()
70
        self.startPWM()
71
72
    def on_deactivate(self):
73
        """ Deactivate module.
74
        """
75
        self.stopPWM()
76
77
    def setupPins(self):
78
        """ Set Raspberry Pi GPIO pins to the right mode.
79
        """
80
        GPIO.setmode(GPIO.BCM)
81
        GPIO.setup(self.enapin, GPIO.OUT)
82
        GPIO.setup(self.enbpin, GPIO.OUT)
83
        GPIO.setup(self.inapin, GPIO.OUT)
84
        GPIO.setup(self.inbpin, GPIO.OUT)
85
        GPIO.setup(self.pwmpin, GPIO.OUT)
86
        GPIO.setup(self.fanpin, GPIO.OUT)
87
        GPIO.setup(self.diapin, GPIO.IN)
88
        GPIO.setup(self.dibpin, GPIO.IN)
89
        self.p = GPIO.PWM(self.pwmpin, self.freq)
90
91
    def startPWM(self):
92
        """ Start the PWM output.
93
        """
94
        GPIO.output(self.fanpin, True)
95
        GPIO.output(self.enapin, True)
96
        GPIO.output(self.enbpin, True)
97
        GPIO.output(self.inapin, True)
98
        GPIO.output(self.inbpin, False)
99
100
        # Setup PWM and DMA channel 0
101
        self.p.start(0)
102
        self.dutycycle = 0
103
104
    def stopPWM(self):
105
        """ Stop the PWM output.
106
        """
107
        # Stop PWM
108
        self.p.stop()
109
        GPIO.output(self.enapin, False)
110
        GPIO.output(self.enbpin, False)
111
        GPIO.output(self.inapin, False)
112
        GPIO.output(self.inbpin, False)
113
        GPIO.output(self.fanpin, False)
114
115
    def changeDutyCycle(self, duty):
116
        """ Set the PWM duty cycle in percent.
117
118
            @param float duty: PWM duty cycle 0 < duty < 100
119
        """
120
        self.dutycycle = 0
121
        if duty >= 0:
122
            GPIO.output(self.inapin, True)
123
            GPIO.output(self.inbpin, False)
124
        else:
125
            GPIO.output(self.inapin, False)
126
            GPIO.output(self.inbpin, True)
127
        self.p.ChangeDutyCycle(abs(duty))
128
129
    def set_control_value(self, value):
130
        """ Set control value for this controller.
131
132
            @param float value: control value, in this case duty cycle in percent
133
        """
134
        with self.threadlock:
135
            self.changeDutyCycle(value)
136
137
    def get_control_value(self):
138
        """ Get control value for this controller.
139
140
            @return float: control value, in this case duty cycle in percent
141
        """
142
        return self.dutycycle
143
144
    def get_control_unit(self):
145
        """ Get unit for control value.
146
147
            @return tuple(str, str): short and text form of unit
148
        """
149
        return ('%', 'percent')
150
151
    def get_control_limits(self):
152
        """ Get minimum and maxuimum value for control value.
153
154
            @return tuple(float, float): min and max control value
155
        """
156
        return (-100, 100)
157
158
159
class PiPWMHalf(PiPWM):
160
    """ PWM controller restricted to positive values.
161
    """
162
163
    def __init__(self, **kwargs):
164
        super().__init__(**kwargs)
165
        #locking for thread safety
166
        self.threadlock = Mutex()
167
168
    def get_control_limits(self):
169
        """ Get minimum and maxuimum value for control value.
170
171
            @return tuple(float, float): min and max control value
172
        """
173
        return (0, 100)
174