-
Notifications
You must be signed in to change notification settings - Fork 0
/
resistor_divider.py
143 lines (102 loc) · 4.53 KB
/
resistor_divider.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
""" Copyright (C) <2023> <Simone Cominato>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>"""
from resistor import Resistor
class Resistor_Divider():
@property
def input_voltage(self):
return self._input_voltage
@input_voltage.setter
def input_voltage(self, value):
if value == 0:
raise ValueError(f"input_voltage must be != 0")
self._input_voltage = value
@property
def output_voltage(self):
return self._output_voltage
@output_voltage.setter
def output_voltage(self, value):
if value == 0:
raise ValueError(f"output_voltage must be != 0")
self._output_voltage = value
@property
def resistors_tolerance(self):
return self._resistors_tolerance
@resistors_tolerance.setter
def resistors_tolerance(self, value):
if value <= 0 or value > 100:
raise ValueError(f"resistors_tolerance must be > 0 and <= 100")
self._resistors_tolerance = value
@property
def divider_current(self):
return self._divider_current
@divider_current.setter
def divider_current(self, value):
if value <= 0:
raise ValueError(f"divider_current must be > 0")
self._divider_current = value
__r_up = 0
__r_down = 0
__div_current = 0
__out_voltage = 0
def calc_divider(self) -> None:
rs = self.input_voltage / self.divider_current
actual_vout = 0
iterations = 0
while abs(self.output_voltage - actual_vout) > 0.01 and iterations < 100:
r_down = (self.output_voltage / self.input_voltage) * rs
r_down_std = Resistor(value = r_down,
tolerance = self.resistors_tolerance).get_std_value()
r_up = rs - r_down_std
r_up_std = Resistor(value = r_up,
tolerance = self.resistors_tolerance).get_std_value()
rs = r_up_std + r_down_std
actual_vout = self.input_voltage * (r_down_std / rs)
iterations += 1
self.__r_up = r_up_std
self.__r_down = r_down_std
self.__div_current = self.input_voltage / rs
self.__out_voltage = actual_vout
def __get_divider(self) -> tuple[float, float]:
return (self.__r_up, self.__r_down)
def __get_div_current(self) -> float:
return self.__div_current
def __get_output_voltage(self) -> float:
return self.__out_voltage
def print_divider_info(self) -> None:
print(f"Upper resistor: {self.__r_up} Ohm")
print(f"Lower resistor: {self.__r_down} Ohm")
print(f"Divider current: {self.__div_current} A")
print(f"Output voltage: {self.__out_voltage} V")
def __init__(self,
*,
input_voltage: float,
output_voltage: float,
resistors_tolerance: int,
divider_current: float) -> None:
self.input_voltage = input_voltage
self.output_voltage = output_voltage
self.resistors_tolerance = resistors_tolerance
self.divider_current = divider_current
def __str__(self) -> str:
rd_properties = f"input_voltage: {self.input_voltage} V\n"\
f"output_voltage: {self.output_voltage} V\n"\
f"resistors_tolerance: {self.resistors_tolerance} %\n"\
f"divider_current: {self.divider_current} A\n"
return rd_properties
if __name__ == "__main__":
resistor_divider = Resistor_Divider(input_voltage = 12,
output_voltage = 3,
resistors_tolerance = 1,
divider_current = 0.00017)
print(resistor_divider)
resistor_divider.calc_divider()
resistor_divider.print_divider_info()