Skip to content

Commit

Permalink
Fix the power output truncation. Resolves #47 (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrandib authored Dec 15, 2024
1 parent d5e4e2e commit c963428
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion include/riden_modbus/riden_modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class RidenModbus
double value_to_voltage(const uint16_t value);
double value_to_voltage_in(const uint16_t value);
double value_to_current(const uint16_t value);
double value_to_power(const uint16_t value);
double values_to_power(const uint16_t *values);
uint16_t voltage_to_value(const double voltage);
uint16_t current_to_value(const double current);
double values_to_temperature(const uint16_t *values);
Expand Down
4 changes: 2 additions & 2 deletions include/riden_modbus/riden_modbus_registers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ enum class Register {
CurrentSet = 9,
VoltageOut = 10,
CurrentOut = 11,
AH = 12, // ???
PowerOut = 13,
PowerOut_H = 12,
PowerOut_L = 13,
VoltageIn = 14,
Keypad = 15,
Protection = 16, // OVP_OCP
Expand Down
13 changes: 7 additions & 6 deletions src/riden_modbus/riden_modbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool RidenModbus::get_all_values(AllValues &all_values)
all_values.current_set = value_to_current(values[+Register::CurrentSet]);
all_values.voltage_out = value_to_voltage(values[+Register::VoltageOut]);
all_values.current_out = value_to_current(values[+Register::CurrentOut]);
all_values.power_out = value_to_power(values[+Register::PowerOut]);
all_values.power_out = values_to_power(&(values[+Register::PowerOut_H]));
all_values.voltage_in = value_to_voltage_in(values[+Register::VoltageIn]);
all_values.keypad_locked = values[+Register::Keypad] != 0;
all_values.protection = value_to_protection(values[+Register::Protection]);
Expand Down Expand Up @@ -237,7 +237,7 @@ bool RidenModbus::get_current_out(double &current)

bool RidenModbus::get_power_out(double &power)
{
return read_power(Register::PowerOut, power);
return read_power(Register::PowerOut_H, power);
}

bool RidenModbus::is_keypad_locked(bool &keypad)
Expand Down Expand Up @@ -624,11 +624,11 @@ bool RidenModbus::write_current(const Register reg, const double current)

bool RidenModbus::read_power(const Register reg, double &power)
{
uint16_t value;
if (!read_holding_registers(reg, &value)) {
uint16_t values[2];
if (!read_holding_registers(reg, values, 2)) {
return false;
}
power = value_to_power(value);
power = values_to_power(values);
return true;
}

Expand Down Expand Up @@ -739,8 +739,9 @@ double RidenModbus::value_to_current(const uint16_t value)
return double(value) / i_multi;
}

double RidenModbus::value_to_power(const uint16_t value)
double RidenModbus::values_to_power(const uint16_t *values)
{
uint32_t value = (values[0] << 16) + values[1];
return double(value) / p_multi;
}

Expand Down

0 comments on commit c963428

Please sign in to comment.