-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gyro-ballancing programs suddenly drop out in SPIKE and RI hub[Bug] #232
Comments
Thanks for sharing this, and great video too!
This is good to know. It means that the firmware is not frozen.
I think they might still be moving, but no longer being updated. It seems that the script itself gets stuck. I'm wondering if the I think I've seen this happen a few times too. I think I've only seen it on the Inventor Hub, not the Technic Hub, but that may not be conclusive just yet. It also seemed more likely to happen when Bluetooth was connected. |
It just happened again, but this time the animation froze also. |
Got the same problem... |
This also happens on the Technic Hub with the IMU (#245). |
Ok I might have hit this bug. Here's what I found" from pybricks.hubs import PrimeHub
from pybricks.tools import wait
hub = PrimeHub()
while True:
print(hub.imu.tilt())
wait(5) but if I remove the wait(5) then it runs for a bit then the IUM crashes |
I also found this to be a issue with the imu in testing beta code. the wait command is also how I corrected it as well |
I've had an initial look at this. We seem to be hitting A patch is given below to reproduce this, which gives the following output.
When starting the program again we see:
Code snippet and analysis patch from pybricks.hubs import PrimeHub
hub = PrimeHub()
while True:
print(hub.imu.tilt()) diff --git a/pybricks/util_pb/pb_imu.c b/pybricks/util_pb/pb_imu.c
index b010f3a7..8d6b6d68 100644
--- a/pybricks/util_pb/pb_imu.c
+++ b/pybricks/util_pb/pb_imu.c
@@ -9,6 +9,9 @@
#include <lsm6ds3tr_c_reg.h>
+#include <pbio/error.h>
+
+#include <pybricks/util_pb/pb_error.h>
#include <pybricks/util_pb/pb_imu.h>
#include STM32_HAL_H
@@ -42,6 +45,16 @@ void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c) {
_imu_dev.ctx.read_write_done = true;
}
+uint32_t i2c_error = HAL_I2C_ERROR_NONE;
+pbio_error_t i2c_status = PBIO_SUCCESS;
+
+void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
+{
+ i2c_error = HAL_I2C_GetError(hi2c);
+ i2c_status = PBIO_ERROR_IO;
+ _imu_dev.ctx.read_write_done = true;
+}
+
// REVISIT: if there is ever an error the PT threads will stall since we aren't
// handling the error callbacks.
@@ -175,6 +188,10 @@ void pb_imu_accel_read(pb_imu_dev_t *imu_dev, float_t *values) {
struct pt pt;
int16_t data[3];
+ if (__HAL_I2C_GET_FLAG(&hi2c, I2C_FLAG_BUSY)) {
+ pb_assert(PBIO_ERROR_BUSY);
+ }
+
PT_INIT(&pt);
while (PT_SCHEDULE(lsm6ds3tr_c_acceleration_raw_get(&pt, &imu_dev->ctx, (uint8_t *)data))) {
nlr_buf_t nlr;
@@ -186,6 +203,13 @@ void pb_imu_accel_read(pb_imu_dev_t *imu_dev, float_t *values) {
nlr_jump(nlr.ret_val);
}
}
+
+ if (i2c_status != PBIO_SUCCESS) {
+ mp_printf(&mp_plat_print, "i2c err = %d\n", i2c_error);
+ }
+
+ pb_assert(i2c_status);
+
values[0] = data[0] * imu_dev->accel_scale;
values[1] = data[1] * imu_dev->accel_scale;
values[2] = data[2] * imu_dev->accel_scale;
|
Related: https://electronics.stackexchange.com/questions/352730/stm32f4-i2c-address-timeout --- Maybe we should just drop some of the HAL stuff when we rewrite it anyway for the IMU process. https://community.st.com/s/question/0D50X0000AupXVgSQM/i2c-device-never-sets-sb-bit-after-start-is-transmitted There also seems to be an erratum for some STM32s with workarounds like these. |
This doesn't seem to be true. It has been running fine on the Technic Hub for a while now. But it fails on SPIKE Prime and SPIKE Essential. It could be lucky timing but maybe there really is a difference. Related to the links above, the following is only done in
But doing this again when the problem occurs didn't seem to make it work. Next up, trying some of the reset workarounds given above. EDIT: Doesn't work either. Stays locked up somehow. |
Have you tried looking at the code for I2C in mainline Micropython because STM32F4 mainline micropython has stable I2C |
Repeating my comments from #675 (comment):
If there is a faster way to reproduce the error, that would be helpful. |
It was quite easy to reproduce before we switched to the new process. If not much else has changed, maybe debugging can be done in the old code, and then apply the fix to the new code as well? Reading some of the links in the post above again, some things do sound suspiciously similar:
|
Have you noticed a difference with the new code on your hubs? |
It seems to run without hanging at least for a few minutes, but I'm not 100% sure this hub was affected before. Will check later. |
I think I found a way to speed up reproducing the error by just printing as fast as possible in a tight loop: while True:
print(
timer.time(),
*hub.imu.acceleration(),
*hub.imu.angular_velocity(),
hub.imu.temp(),
hub.imu.err(),
sep=','
) |
Is there a way to eliminate that the error could be at the IMU not the spike?? I2C very easily gets a NAK back. Does it only happen when using printing out the results fast or does it also happen when just reading the IMU fast but without printing out the result to the serial terminal? i.e can it be narrowed down to a solo problem with I2C or problem of print conflicting with I2C??? |
It happens the most quickly when printing in a tight loop. If a delay is added, then the problems seems to go away. It also seems to happen occasionally when motors are running even with the same delay. The thing printing (at least on the SPIKE hubs) and motors have in common is that they both use UART which has a higher priority interrupt. So maybe there is a timing problem of the I2C interrupt not being handle fast enough? This seems to be a well know issue, e.g. https://cnnblike.com/post/stm32-iicbug/ in addition to the links Laurens already gave. |
Occasionally there is an I2C bus error on STM32F13 MCUs that causes the polling processes to lock up because errors were not handled. This adds error handling with a I2C peripheral reset to recover from the error. Fixes: pybricks/support#232
Occasionally there is an I2C bus error on STM32F13 MCUs that causes the polling processes to lock up because errors were not handled. This adds error handling with a I2C peripheral reset to recover from the error. Fixes: pybricks/support#232
Describe the bug
Gyro-ballancing programs suddenly drop out in SPIKE and RI hub. the balancing program (based on Lausens Valk dem code) suddely stop working and the robot drops down, but the motors keep running and the animmation on the led matrix keeps working.
To reproduce
Steps to reproduce the behavior:
Expected behavior
should keep working, is in a infinite loop.
Screenshots
see movie file, Spike drops out after 3 full turns
spike.gyro.balancer.drops.out.mp4
The text was updated successfully, but these errors were encountered: