From bc94f273916489f33259388a95e26a4dd6ad58bc Mon Sep 17 00:00:00 2001 From: Edwin Shepherd Date: Sat, 31 Oct 2026 17:54:34 +0000 Subject: [PATCH 1/2] brain/greengiant: fix reading servo position The getter read the ADC registers rather than the PWM registers, and on PiLow didn't invert the scaling used by the setter. Co-Authored-By: Claude Opus 5.5 --- robocon/brain/greengiant.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/robocon/brain/greengiant.py b/robocon/brain/greengiant.py index 9671d7a..41e31ff 100644 --- a/robocon/brain/greengiant.py +++ b/robocon/brain/greengiant.py @@ -385,11 +385,14 @@ def pwm(self): else: if self._mode is not PWM_SERVO: raise IOError(f"Attempt to read PWM property from pin configured as {self._mode}") - raw = read_high_low_data(self._bus, self._analog_base) + if self._pwm_base is None or self._pwm_base >= _GG_GPIO_PWM_BASE: + # the firmware can't report the PWM value of GPIO pins + return self._set_to + raw = read_high_low_data(self._bus, self._pwm_base) if self._version < 10: return ((raw - _GG_GG_PWM_CENTER) / _GG_GG_PWM_PERCENT_HALF_RANGE) * 100 else: - return ((raw - _GG_PiLow_PWM_CENTER) / _GG_PiLow_PWM_PERCENT_HALF_RANGE) * 100 + return ((raw - _GG_PiLow_PWM_CENTER) / _GG_PiLow_PWM_HALF_RANGE) * _GG_PiLow_PWM_PERCENT_HALF_RANGE @pwm.setter def pwm(self, percent): if self._pwm_base is not None: From f48cc94b19693f98eab722c4f4498af38213b7f8 Mon Sep 17 00:00:00 2001 From: Edwin Shepherd Date: Sat, 31 Oct 2026 17:54:34 +0000 Subject: [PATCH 2/2] brain/greengiant: fix reading motor power The scaling factor was multiplied rather than divided and the sign was dropped, so `I.motors[0] = -50` read back as 12.5. Co-Authored-By: Claude Opus 5.5 --- robocon/brain/greengiant.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/robocon/brain/greengiant.py b/robocon/brain/greengiant.py index 41e31ff..b3020ce 100644 --- a/robocon/brain/greengiant.py +++ b/robocon/brain/greengiant.py @@ -564,6 +564,10 @@ def __init__(self, bus, max_motor_voltage): # because we care about heating effects in the motors, we have to scale self.power_scaling_factor = max_motor_voltage / _SYSTEM_VOLTAGE + # the firmware truncates the direction register to a byte when it is + # read, so it always returns 0, remember the direction here instead + self._reversed = [False, False] + # should we set up the state of 12v power and enable here? self._bus.write_byte_data(_GG_I2C_ADDR, _GG_ENABLE_MOTORS, 0) # disable the motor controller @@ -578,7 +582,11 @@ def __getitem__(self, index): f"motor index must be in (0,1) but instead got {index}") hex_mag = self._bus.read_byte_data(_GG_I2C_ADDR, _GG_MOTOR_MAG_START + index) - return hex_mag * (100.0 / 256.0) * self.power_scaling_factor + if self.power_scaling_factor == 0: + return 0 + + percent = hex_mag * (100.0 / 256.0) / self.power_scaling_factor + return -percent if self._reversed[index] else percent def __setitem__(self, index, percent): """Clamps input value, converts from percentage to wiring pi format and @@ -589,6 +597,7 @@ def __setitem__(self, index, percent): percent = clamp(percent, -100, 100) direction = (percent < 0) + self._reversed[index] = direction self._bus.write_byte_data(_GG_I2C_ADDR, _GG_MOTOR_DIR_START + index, direction) # Scale such that 50% with a motor limit of 6V is really 3V