You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Blocking motor calls return when motor state becomes "stalled" #515
Currently, we use wait_until_not_moving to determine when to return from a blocking motor call. From its docstring:
Blocks until running is not in self.state or stalled is in self.state. The condition is checked when there is an I/O event related to the state attribute. Exits early when timeout (in milliseconds) is reached.
Returns True if the condition is met, and False if the timeout is reached.
In other words, if I request to move the motor for ten rotations and then part way through its motion the motor stalls, the call returns then while the motor is still trying to complete the movement. Additionally, there doesn't seem to be any way that the caller can tell whether the return was triggered by a stalled state or not.
Does this seem like the right behavior? It makes sense to me that a user might not want their code to freeze if the motor stalls, but at the same time I feel like this blurs the contract of what it means for one of these calls to return.
You could in principle check motor.state immediately upon return from the function. We can make it clear in the documentation that the function may spuriously return (when the motor stalls temporarily), similar to how it's done in C++ documentation for std::condition_variable::wait:
wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs
I think, if we want to support the "block and then check state" pattern, we should look into returning a flag from these blocking functions indicating whether the motor completed its movement or not (based on why we returned). Checking the state afterward seems like a recipe for race conditions. I believe this would require some additional data from wait_until_not_moving.
Some other options:
Add a parameter (or add a new valid value to the existing one) so the user can specify whether they want to return on stall or not. Perhaps the default could be no (i.e., "if the motor isn't at its goal I don't want to continue"), and then you could switch the behavior to signal that you are now ready to handle the early return.
Throw an exception when the motor stalls. I think this would make a lot of sense in the case where the user knows to add a try where needed, but makes it seem fatal to a user who doesn't.
I kind of like (2), perhaps in combination with either (1) or (3). I think the current behavior might make programs behave inconsistently when running at low speeds, so making "don't return on stall" the default option appeals to me.
I can't say I like the current functionality, but I don't see a pressing reason to change it nor is there a clear change to be made. So I figure this can be closed and we can re-open if it becomes an issue.
Activity
ddemidov commentedon Sep 11, 2018
You could in principle check
motor.stateimmediately upon return from the function. We can make it clear in the documentation that the function may spuriously return (when the motor stalls temporarily), similar to how it's done in C++ documentation forstd::condition_variable::wait:WasabiFan commentedon Sep 11, 2018
I think, if we want to support the "block and then check state" pattern, we should look into returning a flag from these blocking functions indicating whether the motor completed its movement or not (based on why we returned). Checking the
stateafterward seems like a recipe for race conditions. I believe this would require some additional data fromwait_until_not_moving.Some other options:
trywhere needed, but makes it seem fatal to a user who doesn't.I kind of like (2), perhaps in combination with either (1) or (3). I think the current behavior might make programs behave inconsistently when running at low speeds, so making "don't return on stall" the default option appeals to me.
dwalton76 commentedon Oct 26, 2018
The current behavior is pretty critical for Mindcuber, that bot often has to do motor turns that result in stalls.
Option 2 above is doable today via a wait_while(“running”) call.
dwalton76 commentedon Nov 10, 2018
Any objections to closing this one?
WasabiFan commentedon Nov 12, 2018
I can't say I like the current functionality, but I don't see a pressing reason to change it nor is there a clear change to be made. So I figure this can be closed and we can re-open if it becomes an issue.