Skip to content
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

[Bug] Incorrect motor loop time for ev3dev #955

Closed
laurensvalk opened this issue Feb 18, 2023 · 4 comments
Closed

[Bug] Incorrect motor loop time for ev3dev #955

laurensvalk opened this issue Feb 18, 2023 · 4 comments
Labels
bug Something isn't working platform: EV3 Issues related to LEGO MINDSTORMS EV3 software: ev3dev software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: control Issues involving control system algorithms topic: motors Issues involving motors

Comments

@laurensvalk
Copy link
Member

Describe the bug

Variation is expected, but it would be good to match the model, on average.

If we can't get the loop time down to 5 ms, we should instead discretize the model constants to match the average, e.g. 8 ms.

To reproduce

  • Install the latest build for pybricks-micropython to override the default.
  • Make sure latest pybricksdev is installed
  • Run:
~/git/pybricks-micropython$ tests/motors/run_test.py tests/motors/open_loop.py --target ev3dev --address 192.168.133.244 --show

Screenshots
image

@laurensvalk laurensvalk added triage Issues that have not been triaged yet platform: EV3 Issues related to LEGO MINDSTORMS EV3 topic: control Issues involving control system algorithms software: ev3dev software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: motors Issues involving motors and removed triage Issues that have not been triaged yet labels Feb 18, 2023
@laurensvalk
Copy link
Member Author

laurensvalk commented Feb 18, 2023

This makes it a lot better:

diff --git a/bricks/ev3dev/pbinit.c b/bricks/ev3dev/pbinit.c
index b5bc099d2..46674bd85 100644
--- a/bricks/ev3dev/pbinit.c
+++ b/bricks/ev3dev/pbinit.c
@@ -39,7 +39,7 @@ static pthread_t task_caller_thread;
 static void *task_caller(void *arg) {
     struct timespec ts;
     ts.tv_sec = 0;
-    ts.tv_nsec = PBIO_CONFIG_CONTROL_LOOP_TIME_MS * 1000000;
+    ts.tv_nsec = 1000000;
 
     while (!stopping_thread) {
         MP_THREAD_GIL_ENTER();
diff --git a/lib/pbio/src/motor_process.c b/lib/pbio/src/motor_process.c
index d258f4742..1d7719b76 100644
--- a/lib/pbio/src/motor_process.c
+++ b/lib/pbio/src/motor_process.c
@@ -28,6 +28,9 @@ PROCESS_THREAD(pbio_motor_process, ev, data) {
     for (;;) {
         PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));
 
         // Update battery voltage.
         pbio_battery_update();
 
@@ -36,9 +39,6 @@ PROCESS_THREAD(pbio_motor_process, ev, data) {
 
         // Update servos
         pbio_servo_update_all();
 
         // Reset timer to wait for next update
-        etimer_restart(&timer);
+        etimer_reset(&timer);
     }
 
     PROCESS_END();

And there is also etimer_reset, but it appears that the increased frequency in the background thread is needed both ways. EDIT: I knew this seemed familiar, see here. Using etimer_reset is perhaps better after all.

@laurensvalk
Copy link
Member Author

laurensvalk commented Feb 18, 2023

This has a huge impact on performance (in a good way - motors work way better) and may even fix #953.

It also fixes speed estimation.

laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Feb 18, 2023
For the observer model, it is more important for the loop time to be
constant on average.

This reverts commit 2174461.

See pybricks/support#955
@laurensvalk
Copy link
Member Author

laurensvalk commented Feb 21, 2023

Linked PR in pybricks/pybricks-micropython#146

@laurensvalk laurensvalk added the bug Something isn't working label Feb 21, 2023
laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Feb 23, 2023
For the observer model, it is more important for the loop time to be
constant on average.

This reverts commit 2174461.

See pybricks/support#955
laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Feb 23, 2023
For the observer model, it is more important for the loop time to be
constant on average.

This reverts commit 2174461.

See pybricks/support#955
@laurensvalk
Copy link
Member Author

pybricks/pybricks-micropython#146 is now merged, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working platform: EV3 Issues related to LEGO MINDSTORMS EV3 software: ev3dev software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: control Issues involving control system algorithms topic: motors Issues involving motors
Projects
None yet
Development

No branches or pull requests

1 participant