-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.py
195 lines (172 loc) · 5.96 KB
/
Functions.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#
# QI thruster based on Raspberry Pi 4B
#
# Serial port interface (SPI) with the digital potentiometer:
# GPIO 7 - SPI CE1
# GPIO 11 - SPI CLK
# GPIO 10 - SPI MOSI
#
# The second SPI is reserved for ADC
#
# GPIO outputs:
# GPIO 21 - enable 15 V OpAmp
# GPIO 20 - red LED for ON/OFF of 15 V OpAmp
# GPIO 16 - red LED for low battery
# GPIO 19 - reserved or multiplexer CH1
# GPIO 26 - reserved or multiplexer CH2
#
# ADC inputs:
# In 9 - battery (1/3 potential divider and 5 V buffer)
# In 8 - 15 V OpAmp output (1/3 potential divider and 5 V buffer)
# In 7 - HV output (1/1000 potential divider and 5 V buffer)
# In 6 - reserved or sensor input (5 V buffer)
# In 5 - reserved or sensor input (5 V buffer)
# In 4 - reserved or sensor input (without buffer)
# In 3 - reserved or sensor input (without buffer)
import UI
import spidev
import time
import ADS1263
import RPi.GPIO as GPIO
import os
from UI import Ui_MainWindow
#GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Activating the digital potentiometer
spi = spidev.SpiDev()
spi.open(0, 1)
spi.max_speed_hz = 976000
# Activating ADC HAT
REF = 5.0 # Reference voltage for ADC HAT
ADC = ADS1263.ADS1263()
ADC.ADS1263_init()
# Position of the digital potentiometer slider
def write_pot(input):
msb = input >> 8
lsb = input & 0xFF
spi.xfer([msb, lsb])
# Reading the voltage at the ADC channels
def Read_ADC(self,Ch):
ADC_Value = ADC.ADS1263_GetAll()
try:
control = ADC_Value[1]
except:
Ui_MainWindow.printf(self, 'Error with reading ADC. The program will be terminated')
GPIO_OFF()
write_pot(0x00)
OpAmp_ES('OFF')
exit()
Voltage = 0.0
try:
if (ADC_Value[Ch] >> 31 == 1):
Voltage = REF * 2 - ADC_Value[Ch] * REF / 0x80000000
else:
Voltage = ADC_Value[Ch] * REF / 0x7fffffff
except:
Ui_MainWindow.printf(self, 'Error with reading ADC. The program will be terminated')
GPIO_OFF()
write_pot(0x00)
OpAmp_ES('OFF')
exit()
return Voltage
# 15 Vcc Op Amp ON/OFF with the red LED indication
def OpAmp_ES(output):
opamp = 21 # controlling op am amp output
LED = 20 # Red LED for op amp ON/OFF
GPIO.setup(opamp, GPIO.OUT)
GPIO.setup(LED, GPIO.OUT)
if output == 'ON':
GPIO.output(opamp, GPIO.HIGH)
GPIO.output(LED, GPIO.HIGH)
elif output == 'OFF':
GPIO.output(opamp, GPIO.LOW)
GPIO.output(LED, GPIO.LOW)
# All GPIO outputs OFF
def GPIO_OFF():
LED16 = 16 # Red LED for low battery
LED20 = 20 # Red LED for Op Amp
GPIO19 = 19 # reserved (or current sensor range 1)
GPIO26 = 26 # reserved (or current sensor range 2)
GPIO.setup(LED16, GPIO.OUT)
GPIO.setup(LED20, GPIO.OUT)
GPIO.setup(GPIO19, GPIO.OUT)
GPIO.setup(GPIO26, GPIO.OUT)
GPIO.output(LED16, GPIO.LOW)
GPIO.output(LED20, GPIO.LOW)
GPIO.output(GPIO19, GPIO.LOW)
GPIO.output(GPIO26, GPIO.LOW)
# Controlling the battery charge
def Battery(self):
LED = 16 # Red LED for the battery
GPIO.setup(LED, GPIO.OUT)
V = Read_ADC(self,9) * 3.0
Ui_MainWindow.printf(self,'Battery voltage = '+str(V)+' V')
if 6.0 <= V <= 6.5:
Ui_MainWindow.printf(self,'Warning: Battery is low!')
GPIO.output(LED, GPIO.HIGH)
elif V < 6.0:
Ui_MainWindow.printf(self,'Warning: Raspberry Pi will be shutdown in 5 s because the battery is dangerously low (< 6 V)')
time.sleep(5)
GPIO_OFF() # All GPIO outputs OFF
write_pot(0x00) # nulling the output of digital potentiometer
OpAmp_ES('OFF') # OpAmp if OFF
os.system("shutdown now -h")
# Voltage one step up
def HV_up(self,Previous_NS, tau):
hex_Previous_NS = int(hex(Previous_NS), 16)
i = hex_Previous_NS
HV_actual = Read_ADC(self,7) # actual HV across the capacitor
if i <= 0x101 and HV_actual <= 4.0:
i = i + 0x01
write_pot(i)
OpAmp = Read_ADC(self,8) * 6.0 # output of Op Amp (Vcc = 15 V)
time.sleep(tau) # waiting for the capacitor charge
HV_actual = Read_ADC(self,7)
else:
OpAmp = Read_ADC(self,8) * 6.0 # output of Op Amp (Vcc = 15 V)
Battery(self)
Ui_MainWindow.printf(self,'Op Amp output = '+str(OpAmp)+' V')
Ui_MainWindow.printf(self,'Actual HV output = '+str(HV_actual)+' kV')
NS_stop = i
return NS_stop, OpAmp, HV_actual
# Voltage one step down
def HV_down(self,Previous_NS, tau):
hex_Previous_NS = int(hex(Previous_NS), 16)
i = hex_Previous_NS
if 0x00 < i:
i = i - 0x01
write_pot(i)
OpAmp = Read_ADC(self,8) * 6.0
time.sleep(tau) # waiting for the capacitor discharge
HV_actual = Read_ADC(self,7)
else:
OpAmp = Read_ADC(self,8) * 6.0 # output of Op Amp (Vcc = 15 V)
HV_actual = Read_ADC(self,7) # actual HV across the QI capacitor
Battery(self)
Ui_MainWindow.printf(self,'Op Amp output = '+str(OpAmp)+' V')
Ui_MainWindow.printf(self,'Actual HV output = '+str(HV_actual)+ ' kV')
NS_stop = i
return NS_stop, OpAmp, HV_actual
# Current sensor range
def CurrentSensorRange(range):
range1 = 19
range2 = 26
GPIO.setup(range1, GPIO.OUT)
GPIO.setup(range2, GPIO.OUT)
GPIO.output(range1, GPIO.LOW)
GPIO.output(range2, GPIO.LOW)
if range == 1:
GPIO.output(range1, GPIO.HIGH)
elif range == 2:
GPIO.output(range2, GPIO.HIGH)
# Current sensor CS-1
# Sensor resistance 15.6 k (+/- 0.1%, low voltage)
# Series resistance 1.5 MOhms (high voltage)
# This sensor was calibrated with ADC In 3 without a buffer. The calibration file was saved on Raspberry Pi.
def CurrentSensor(self,bias, Ch):
x = Read_ADC(self, Ch) - bias
y = -0.11372504497803 * x**8 + 1.4343168169319 * x**7 -7.3278668065487 * x**6 + \
19.4325718360446 * x**5 - 28.448512253201 * x**4 + \
22.3174566367283 * x**3 - 7.6319260686742 * x**2 + 1.1951000922818 * x
#return x # used during the sensor calibration
return y