"motor" is spinning? parameter not working?

Trying to detect if motor is spinning. Running code as shown below, console shows LeftMotor and RightMotor position changing, indicating motors are spinning. Print statement “spinning?” shows negative for both LeftMotor and RightMotor which, to my understanding, indicates that the designated motors are not spinning.
Spinning2

Would be grateful if someone could set me straight as to what am I missing here?

isSpinning is only used with spinFor and spinToPosition commands. It is used to determine when they are complete (ie. if the motor is spinning to a position) if called with the “wait for completion” parameter set to false.

2 Likes

Thanks.

Did not find that caveat in the literature. Are there any recommendations for detecting a robot using Spin Forward/Reverse on the drive motors in a stalled condition (i. e. stuck) with wheels not spinning? Could probably get the guys to conjure something using “Motor position in degrees” but hopefully there is a simpler way? For example, the current Spin commands are inside of a P-loop that updates velocity constantly. I suppose one might do the same with a SpinFor command provided it is allowable to constantly update the command specified number of degrees, inside of a loop? For example, use a Spin to position where is initialized to, say 360 and then, when ready to stop, change to zero and also stop Motor? Kind of a cludge but would it work?

To check for a stalled motor, you could either monitor velocity (which would be low) or current (which would be high).

3 Likes

Ooops, sorry. I just found out that less that/greater than brackets are not allowable. Spin to position Var where Var is initialized to, say, 360 and the, when ready to stop, change Var to zero and also stop motor.

Of course. Current might be best. I’ll get them to play with that approach.

Thanks!!

James, I did some work trying to monitor current in order to detect a motor stall. Was not able to find much documentation other than the Help facility provided by VexCodeIQ. Noted that reported current readings would move between 0 and 1.2 amps. In looking for a “threshold” value to detect motor stall, I found that the reported amperage of a stalled motor would vary, depending on motor speed. Motors turning at faster velocities would report higher current levels when stalled than would motors turning at lower velocities. It would appear, therefore, that there is no general threshold value, suggesting that a simple greater than test will not be sufficient. I was wondering if you were aware of any other documentation and/or examples?

It’s a bit tough in blocks due to limited API (even though it’s all C++ underneath not everything is exposed to limit the number of blocks).

In C++ I might monitor the motor direction, which is just helper function to check velocity really. There’s also a protected function called command() that can be used to read the last motor command velocity. So perhaps something like this.

stall detect demo
//----------------------------------------------------------------------------
//                                                                            
//    Module:       main.cpp                                                  
//    Author:       {author}                                                  
//    Created:      {date}                                                    
//    Description:  IQ project                                                
//                                                                            
//----------------------------------------------------------------------------

// Include the IQ Library
#include "iq_cpp.h"

// Allows for easier use of the VEX Library
using namespace vex;

namespace vex {
  class motorx : public motor {
    public:
      using motor::motor;
      using motor::command;
  };
};

vex::motorx  m1(PORT5);

void
checkForStall() {
    int stall_time = 0;

    while( 1 ) {
      // check for command velocity > 10rpm
      // command function is protected so we need sub class of motor
      if( fabs(m1.command(velocityUnits::rpm)) > 10 ) {

        // if motor stopped then direction will be undefined
        if( m1.direction() == directionType::undefined ) {
          stall_time++;
        }
        else {
          stall_time = 0;
        }

        // after 200mS stop motor
        if( stall_time == 10 ) {
          Brain.Screen.printAt( 10, 40, "stalled");
          m1.stop();
        }
      }
      else {
        stall_time = 0;
      }

      this_thread::sleep_for(20);
    }
}


int main() {
    // thread to monitor the motor, it could monitor several
    thread t1(checkForStall);

    // test, stall detection should not trigger when motor is running slowly
    while(1) {
      m1.spin( forward, 50, rpm );
      this_thread::sleep_for(2000);
      m1.spin( forward, 5, rpm );
      this_thread::sleep_for(2000);
    }
}
4 Likes

Many thanks!!

Although my 12 yr old student is a very accomplished coder, he is not quite up to speed on C++ . We are still in the VEX IQ Elementary division. He is, however, learning Python. Would the Python implementation have similar functionality to the C++ API? We may take a look a that.

Python would be pretty much the same, except no need to subclass motor.

stall detect demo - python
# Library imports
from vex import *

# Brain should be defined by default
brain=Brain()

# Robot configuration code
brain_inertial = Inertial()

# Begin project code
m1 = Motor(Ports.PORT5)

def check_for_stall():
    stall_time = 0
    while True:
      if abs(m1.command()) > 10:
          if m1.direction() == DirectionType.UNDEFINED:
              stall_time = stall_time + 1
          else:
              stall_time = 0

          if stall_time == 10:
              brain.screen.print_at("stalled", x=10, y=40)
              m1.stop()
      else:
          stall_time = 0

      sleep(20)

t1 = Thread(check_for_stall)

m1.spin(FORWARD, 50, RPM)

and if you are using VEXcode (rather than VS Code extension) for Python, disable linter warning like this.

      if abs(m1.command()) > 10: #pylint: disable=no-member
3 Likes

Many thanks! I’ll explore both alternatives with my programmer.