Help with toggle code and motor temps

I need help! I have made 3 toggles that function very well one for snags and a claw however these motors get too hot after 2 minutes I have so far tried to just spin to position instead of just spin but nothing works they still get hot.

This is the regular code

void usercontrol(void) {
    
  // bool for the claw toggle
  bool toggleUp = false;
  bool buttonUpPressed = false;
  
  while (1) {
// bool for claw
   bool buttonUp = Controller2.ButtonUp.pressing();

// Claw Toggle Code
     if (buttonUp && !buttonUpPressed){
      buttonUpPressed = true;
      toggleUp = !toggleUp;
      Brain.Screen.printAt(14, 160 ,"Claw Triggred");
    }
    else if (!buttonUp)
    buttonUpPressed = false;
    if(toggleUp){
      motor(Claw).spin(vex::directionType::fwd,70,vex::percentUnits::pct);
    }
    else {
      motor(Claw).spin(vex::directionType::rev,70,vex::percentUnits::pct);
    }
}
}

with is with spin to position

if (buttonUp && !buttonUpPressed){
      buttonUpPressed = true;
      toggleUp = !toggleUp;
      Brain.Screen.printAt(12, 140 ,"Claw Triggred");
    }
    else if (!buttonUp)
    buttonUpPressed = false;
    if(toggleUp){
      motor(Claw).spinToPosition(-187,vex::rotationUnits::deg,100,vex::velocityUnits::pct, true);
    }
    else {
      motor(Claw).spinToPosition(-187,vex::rotationUnits::deg,100,vex::velocityUnits::pct, true);
    }

I’m using V5 Pro on a comp template

It seems to be the motors are running constantly for those several minutes. I would change the code so it doesn’t try to move the motor to the same spot every loop.

I can’t remember if you can get the current position of the motor, but that can be helpful to make sure the motor doesn’t try to run when it doesn’t need to.

SpinToPosition is helpful because you can tell the code not to run if it’s already at the position.

Something like (but I haven’t tested it, or programmed any robotics for a while):

if !(motor(Claw).position == -187) {
motor(Claw).spinToPosition(-187,vex::rotationUnits::deg,100,vex::velocityUnits::pct, true);
}

How would I go about clearing the position after auton?

Also what is the “!” for in

because that is giving me a error

Ah, well, it means opposite. So when the motor isn’t at -187; run the code. Such as !True == False.

I haven’t programmed any robotics for a while, and don’t really use C++. Well, I prefer Python, but am using another program which acts a lot like C++, so I’m not really sure what will work. I just though I’d give an example.

I assume you mean to their starting positions, that should be at 0 degrees - I think, because 0 is starting degree no matter the angle the motor starts at.

1 Like

To fix that issue, what you can do is start the movement and only send that command once. You can do that by setting the waitForCompletion flag to false. This will make it so that the motor command will run independently and will not hold up the rest of the thread. You also need to make sure that it won’t keep running it. You can do this with the buttonUpPressed variable that you have already created. This will make it so that it will only send the spinToPosition command once when you press the button. Your code should look something like this.

if (buttonUp && !buttonUpPressed){
  buttonUpPressed = true;
  toggleUp = !toggleUp;
  Brain.Screen.printAt(12, 140 ,"Claw Triggred");
}
else if (!buttonUp)
  if(buttonUpPressed){
    if(toggleUp){
      motor(Claw).spinToPosition(-187,vex::rotationUnits::deg,100,vex::velocityUnits::pct, false);
    }
    else {
      motor(Claw).spinToPosition(-187,vex::rotationUnits::deg,100,vex::velocityUnits::pct, false);
    }
  }
  buttonUpPressed = false;
}
1 Like

Why are the the spin to false? It what would make it true?

The false just means that it will run separately from this thread. You need to do this otherwise you won’t be able to drive anything else while the claw is going.

2 Likes

Alright I’ll test on the bot tomorrow and update this tread

Second question that came to mind could I use

motor(Claw).spin(vex::directionType::rev,70,vex::percentUnits::pct);

with the

buttonUpPressed = false;

To make

 if (buttonUp && !buttonUpPressed){
      buttonUpPressed = true;
      toggleUp = !toggleUp;
      Brain.Screen.printAt(12, 140 ,"Claw Triggred");
    }
    else if (!buttonUp)
    if(buttonUpPressed){
      if(toggleUp){
      motor(Claw).spin(vex::directionType::fwd,70,vex::percentUnits::pct);
      }
      else {
      motor(Claw).spin(vex::directionType::rev,70,vex::percentUnits::pct);
      }
    }
      buttonUpPressed = false;

Would this work?

Yes, that works, but you won’t be able to specify which position you want the claw to go to.

2 Likes