Skip to content

Commit

Permalink
Fix current alternating between positive/negative when the 9V AC tran…
Browse files Browse the repository at this point in the history
…sformer is not connected
  • Loading branch information
David00 committed Jun 19, 2023
1 parent e2ea16e commit f1ae9aa
Showing 1 changed file with 78 additions and 30 deletions.
108 changes: 78 additions & 30 deletions rpi_power_monitor/power_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,19 @@ def calculate_power(self, samples, board_voltage):
rms_current_ct1 = rms_current_ct1 * 2
if self.config['current_transformers']['channel_1'].get('reversed'):
# RMS current is always positive, so the reverse check is done off the calculated real power.
if real_power_1 < 0: # Make power positive (current is already positive)
real_power_1 = real_power_1 * -1
power_factor_1 = abs(power_factor_1)
else: # Make current negative (real power is already negative)
rms_current_ct1 = rms_current_ct1 * -1
# Only update the real power and RMS Current if the 9V AC voltage is live.
if rms_voltage_1 > 60:
if real_power_1 < 0: # Make power positive (current is already positive)
real_power_1 = real_power_1 * -1
power_factor_1 = abs(power_factor_1)
else: # Make current negative (real power is already negative)
rms_current_ct1 = rms_current_ct1 * -1

# If the 9V AC power supply is not connected or detected, the current and power sign should always be positive. 60 is picked simply as an arbitrary value.
if rms_voltage_1 < 60:
rms_current_ct2 = abs(rms_current_ct1)
real_power_1 = abs(real_power_1)


results[1] = {
'type': self.config['current_transformers']['channel_1']['type'],
Expand Down Expand Up @@ -595,11 +603,19 @@ def calculate_power(self, samples, board_voltage):
rms_current_ct2 = rms_current_ct2 * 2
if self.config['current_transformers']['channel_2'].get('reversed'):
# RMS current is always positive, so the reverse check is done off the calculated real power.
if real_power_2 < 0: # Make power positive (current is already positive)
real_power_2 = real_power_2 * -1
power_factor_2 = abs(power_factor_2)
else: # Make current negative (real power is already negative)
rms_current_ct2 = rms_current_ct2 * -1
# Only update the real power and RMS Current if the 9V AC voltage is live.
if rms_voltage_2 > 60:
if real_power_2 < 0: # Make power positive (current is already positive)
real_power_2 = real_power_2 * -1
power_factor_2 = abs(power_factor_2)
else: # Make current negative (real power is already negative)
rms_current_ct2 = rms_current_ct2 * -1

# If the 9V AC power supply is not connected or detected, the current and power sign should always be positive. 60 is picked simply as an arbitrary value.
if rms_voltage_2 < 60:
rms_current_ct2 = abs(rms_current_ct2)
real_power_2 = abs(real_power_2)


results[2] = {
'type': self.config['current_transformers']['channel_2']['type'],
Expand Down Expand Up @@ -627,11 +643,19 @@ def calculate_power(self, samples, board_voltage):
rms_current_ct3 = rms_current_ct3 * 2
if self.config['current_transformers']['channel_3'].get('reversed'):
# RMS current is always positive, so the reverse check is done off the calculated real power.
if real_power_3 < 0: # Make power positive (current is already positive)
real_power_3 = real_power_3 * -1
power_factor_3 = abs(power_factor_3)
else: # Make current negative (real power is already negative)
rms_current_ct3 = rms_current_ct3 * -1
# Only update the real power and RMS Current if the 9V AC voltage is live.
if rms_voltage_3 > 60:
if real_power_3 < 0: # Make power positive (current is already positive)
real_power_3 = real_power_3 * -1
power_factor_3 = abs(power_factor_3)
else: # Make current negative (real power is already negative)
rms_current_ct3 = rms_current_ct3 * -1

# If the 9V AC power supply is not connected or detected, the current and power sign should always be positive. 60 is picked simply as an arbitrary value.
if rms_voltage_3 < 60:
rms_current_ct2 = abs(rms_current_ct3)
real_power_3 = abs(real_power_3)


results[3] = {
'type': self.config['current_transformers']['channel_3']['type'],
Expand Down Expand Up @@ -659,11 +683,19 @@ def calculate_power(self, samples, board_voltage):
rms_current_ct4 = rms_current_ct4 * 2
if self.config['current_transformers']['channel_4'].get('reversed'):
# RMS current is always positive, so the reverse check is done off the calculated real power.
if real_power_4 < 0: # Make power positive (current is already positive)
real_power_4 = real_power_4 * -1
power_factor_4 = abs(power_factor_4)
else: # Make current negative (real power is already negative)
rms_current_ct4 = rms_current_ct4 * -1
# Only update the real power and RMS Current if the 9V AC voltage is live.
if rms_voltage_4 > 60:
if real_power_4 < 0: # Make power positive (current is already positive)
real_power_4 = real_power_4 * -1
power_factor_4 = abs(power_factor_4)
else: # Make current negative (real power is already negative)
rms_current_ct4 = rms_current_ct4 * -1

# If the 9V AC power supply is not connected or detected, the current and power sign should always be positive. 60 is picked simply as an arbitrary value.
if rms_voltage_4 < 60:
rms_current_ct2 = abs(rms_current_ct4)
real_power_4 = abs(real_power_4)


results[4] = {
'type': self.config['current_transformers']['channel_4']['type'],
Expand Down Expand Up @@ -691,11 +723,19 @@ def calculate_power(self, samples, board_voltage):
rms_current_ct5 = rms_current_ct5 * 2
if self.config['current_transformers']['channel_5'].get('reversed'):
# RMS current is always positive, so the reverse check is done off the calculated real power.
if real_power_5 < 0: # Make power positive (current is already positive)
real_power_5 = real_power_5 * -1
power_factor_5 = abs(power_factor_5)
else: # Make current negative (real power is already negative)
rms_current_ct5 = rms_current_ct5 * -1
# Only update the real power and RMS Current if the 9V AC voltage is live.
if rms_voltage_5 > 60:
if real_power_5 < 0: # Make power positive (current is already positive)
real_power_5 = real_power_5 * -1
power_factor_5 = abs(power_factor_5)
else: # Make current negative (real power is already negative)
rms_current_ct5 = rms_current_ct5 * -1

# If the 9V AC power supply is not connected or detected, the current and power sign should always be positive. 60 is picked simply as an arbitrary value.
if rms_voltage_5 < 60:
rms_current_ct2 = abs(rms_current_ct5)
real_power_5 = abs(real_power_5)


results[5] = {
'type': self.config['current_transformers']['channel_5']['type'],
Expand Down Expand Up @@ -723,11 +763,19 @@ def calculate_power(self, samples, board_voltage):
rms_current_ct6 = rms_current_ct6 * 2
if self.config['current_transformers']['channel_6'].get('reversed'):
# RMS current is always positive, so the reverse check is done off the calculated real power.
if real_power_6 < 0: # Make power positive (current is already positive)
real_power_6 = real_power_6 * -1
power_factor_6 = abs(power_factor_6)
else: # Make current negative (real power is already negative)
rms_current_ct6 = rms_current_ct6 * -1
# Only update the real power and RMS Current if the 9V AC voltage is live.
if rms_voltage_6 > 60:
if real_power_6 < 0: # Make power positive (current is already positive)
real_power_6 = real_power_6 * -1
power_factor_6 = abs(power_factor_6)
else: # Make current negative (real power is already negative)
rms_current_ct6 = rms_current_ct6 * -1

# If the 9V AC power supply is not connected or detected, the current and power sign should always be positive. 60 is picked simply as an arbitrary value.
if rms_voltage_6 < 60:
rms_current_ct2 = abs(rms_current_ct6)
real_power_6 = abs(real_power_6)


results[6] = {
'type': self.config['current_transformers']['channel_6']['type'],
Expand Down

0 comments on commit f1ae9aa

Please sign in to comment.