Viper: can't do binary op between 'int' and 'object' #13697
-
I have the following code in a class: @micropython.viper
def _update(self, measured: int, dt: int):
"""Update the PID controller.
measured: The measured value of the system.
dt: The change in time, in seconds.
"""
kp_num, kp_shr, ki_num, ki_shr, kd_num, kd_shr = (
int(self._kp_num),
int(self._kp_shr),
int(self._ki_num),
int(self._ki_shr),
int(self._kd_num),
int(self._kd_shr),
)
err = int(self.setpoint) - int(measured)
p = int(0)
if kp_num != 0:
p = err * kp_num # Line 294 **error here**
p >>= kp_shr When I run it in Micropython 1.11, I get the following error:
I know supporting an old version of a library is nobody's idea of fun, so please just tell me to buzz off if this is too annoying to deal with. That said, am I doing something obviously wrong here? I feel like neither |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Viper is not as intelligent as MP. And if in doubt it always falls back to handle things as MP objects. @micropython.viper
def _update(self, measured: int, dt: int):
"""Update the PID controller.
measured: The measured value of the system.
dt: The change in time, in seconds.
"""
kp_num = int(self._kp_num)
kp_shr = int(self._kp_shr)
ki_num = int(self._ki_num)
ki_shr = int(self._ki_shr)
kd_num = int(self._kd_num)
kd_shr = int(self._kd_shr)
err = int(self.setpoint) - int(measured)
p = int(0)
if kp_num != 0:
p = err * kp_num # Line 294 **error here**
p >>= kp_shr does not have this error any longer. But a better way to got I think would be: from array import array
k_params = array('I', [kp_num, kp_shr, ki_num, ki_shr, kd_num, kd_shr])
@micropython.viper
def _update(self, measured: int, dt: int, k_pars:ptr32):
"""Update the PID controller.
measured: The measured value of the system.
dt: The change in time, in seconds.
"""
err = int(self.setpoint) - int(measured)
p: int = 0
if k_pars[0] != 0: # kp_num
p = err * k_pars[0]
p >>= k_pars[1] # kp_shr |
Beta Was this translation helpful? Give feedback.
Viper is not as intelligent as MP. And if in doubt it always falls back to handle things as MP objects.
So the tuple assignment was the culprit.
does not have …