Skip to content

Blocking motor calls return when motor state becomes "stalled" #515

Closed

Description

@WasabiFan

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.

Activity

  1. ddemidov commented on Sep 11, 2018

    @ddemidov
    Member

    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

  2. WasabiFan commented on Sep 11, 2018

    @WasabiFan
    MemberAuthor

    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:

    1. 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.
    2. 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.

  3. dwalton76 commented on Oct 26, 2018

    @dwalton76
    Collaborator

    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.

  4. dwalton76 commented on Nov 10, 2018

    @dwalton76
    Collaborator

    Any objections to closing this one?

  5. WasabiFan commented on Nov 12, 2018

    @WasabiFan
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions