spinFor not working

Is there any way that the spinFor function can stop working despite 1) having all the information that it needs and 2) having worked before? I was testing a new code today and for some reason the spinFor lines that I was using were not working. I can post a more detailed description if wanted.

Please post the specific line(s) of code that show the spin for function not working. Without that I can ask more general questions: Does the function do anything (I.e., a motor spins very very little)? Is the motor fully connected (solid red light on motor and responds to other input)? Finally, to confirm you are using C++ code in the VEXcode V5 app (not VScode extension).

Once you post the specific line(s) of code and answer the questions above, I’ll be able to help more!

P.S. for the future, it is helpful to post a more detailed description without someone having to ask.

I am working with VEXcode V5, yes.

This is the code that I’m using:

void allMove(int rotationAmount){
  std::cout << "Activity allMove by " << rotationAmount << " degrees." << std::endl;
  Brain.Screen.print("Activity allMove by %.2f degrees", rotationAmount);
  Brain.Screen.newLine();
  frontLeftMotor.spinFor(reverse, rotationAmount, degrees, false);
  rearLeftMotor.spinFor(reverse, rotationAmount, degrees, false);
  frontRightMotor.spinFor(reverse, rotationAmount, degrees, false);
  rearRightMotor.spinFor(reverse, rotationAmount, degrees);
}

as of writing this the method of printing the value was an issue so there’s one (minor) thing

When tested, it prints the value defined on the terminal, and it can print on the bot, but it doesn’t move.

int main(){

  .  .  .

  allMove(50);
  allTurnLeft(50);
  allTurnRight(50);
  allStop();
}

Despite defining rotationAmount as 50 throughout, it just doesn’t move.

The weirdest part is that when I call usercontrol (which uses the following bit of code) it moves.

void botDrive(){
  int move = Controller1.Axis3.position();
  int rotate = Controller1.Axis1.position();
  // int strafe = Controller1.Axis4.position();

  frontLeftMotor.spin(forward, move + rotate, percent);
  frontRightMotor.spin(forward, move - rotate, percent);
  rearLeftMotor.spin(forward, move + rotate, percent);
  rearRightMotor.spin(forward, move - rotate, percent);
}

I don’t know what’s happening, to be honest. The only thing I can think of is that something’s happening in the brain of the bot.

i think it may just be a corrupted file issue

I believe your function code, while not optimized, is functional. The issue is with the parameter you are passing in. You are telling the motors to rotate 50 degrees. A motor’s rotation IS NOT equal to a robot’s movement. A motor’s rotation is significantly smaller. For example, our robot (with a 4-wheel drive setup) requires each motor to rotate 951 degrees to drive across one VRC field tile (24 inches). Based on our robot, telling the motor’s to rotate 50 degrees would move the robot approximately 1.3 inches (an almost imperceptible movement).

So I believe that your code is executing perfectly, however the way you’ve made it means that the movement is so small you probably are not noticing it. Please see this forum post to see how to convert motor rotation degrees to actual robot movement or robot rotation.

Our team had issues with calculations like that, so we decided to do trial-and-error and benchmark our data, extrapolating from there. This is a direct link to our autonomous functions from this year. Please note that I code with the VScode extension (which you should switch to if possible) so some syntax and formatting may be different.

Here is what I suggest:

  1. Comment out all your function calls in main() except allMove()
  2. Pass in 688 as your parameter to allMove(); i.e., allMove(688);
  3. Pick up your robot while allowing the wheels free movement. This is to ensure the robot doesn’t run into something as I don’t know exactly how far it will go.
  4. Run the code and see if the motors spin.
  5. If they do, update your drive function either using our trial-and-error method or math calculations from the forum post. Test and refine your drive function until you can pass in something sensible (like inches) to your function and it’ll drive that much. I.e., allMove(24) should drive 24 inches which is one VRC field tile. Do the same with your turn functions. Comment back in function calls when applicable.
  6. If they don’t, check your motor connections (make sure they can spin at all). If they still don’t, make sure your code is actually updating (i.e., the brain is actually downloading the new version of your code. You can do this by changing a print statement in the function and making sure it updates accordingly). If that still doesn’t work, check how your motors are defined. I don’t know why you are having all the motors spin in reverse. They should spin forward, and the ones that need to be reversed having been set previously. This is how we declared our motors (the true/false is an isReversed boolean; true means the motor spins reversed):
motor leftBack = motor(PORT5, ratio18_1, true);
motor leftFront = motor(PORT11, ratio18_1, true);
motor rightBack = motor(PORT10, ratio18_1, false);
motor rightFront = motor(PORT20, ratio18_1, false);

If that still doesn’t work, please provide more details on what exactly is happening (the motors don’t move at all, the wheels don’t turn (the motors can move but the wheels may not turn), the bot moves but not in the right direction, the bot is inconsistent with its direction/distance, other issue). Please be specific!

2 Likes

Thank you for all the information, it does help, especially the forum post you linked. I’ll definitely be using the formulas in the post with my code from this point on.

On the issue with the motors not moving, I recently copied the code and moved it to a new file. This, for some reason, worked. My guess is that the file was somehow corrupted. Now I had been running it off a USB up to that point, but I had not had that problem before.

Also, the reason why the motors are spinning in ““reverse”” is due to how the bot reacts to the parameter. For some reason, when the first parameter is declared forward for our bot, it reverses. My guess is the way we connected our motors.

I do want to ask; how would I go about setting up Visual Studio? I tried using it recently, but I have no clue how to get code to run.

Once again, I appreciate the time you’ve spent helping!

do you mean pros or v5 in VScode?

No problem! Glad I could help.

Sometimes I do have issues with VEX not recognizing changes to a file and thus running a really old version that doesn’t work. You could have had something similar, glad you found a solution!

Regarding your motors spinning in reverse, I would highly recommend declaring motors that based on physical positioning mean that spinning “reverse” will actually spin forward as reversed. This allows you to declare the isReversed boolean once and never have to worry about which way specific motors will turn (forward will always go forward, reverse will always go reverse). For drivetrain, either your right side or left side motors will need to be set as reversed when they are declared (generally it is the right side, ours from this year was the left b/c we changed it midway through). See my prior post and code to see how I declared my left motors as reversed meaning in my auton functions I could always use spin forward. This just generally improves readability and helps make coding quickly easier.

It sounds like you solved the problems that were happening (mark whichever response, could even be your own, that detailed the way you solved your problem the best as the solution), so I’ll move on to VScode.

Finally, with VScode, as @5062F_Adam mentioned, you have two options. “Pros” is a custom-created API by Purdue University that lets you code VEX with additional functionality. I personally don’t use it so I can’t speak to it too much: this is the Pros Website if you’re interested. “V5” is the vex-created official extension which will be very similar to your current VEXcode but with added ability to have multiple files, use GitHub, live collaboration, and more (all thanks to VScode and stuff that pros also has). I use v5 and would be able to provide more detailed help, but I’m sure you can find amazing people in the community to help you with pros.

To use VScode in either way: you must have a Windows/Mac device. A school-chromebook will almost definitely not be able to run it, I’m not sure about a personal one. If you have a Windows/Mac, you can download VScode and go through the setup process. Then you have to make the choice between “pros” and “v5” and download the extension you want.

Feel free to do research into both and if you chose V5 and have issues setting it up, let me know!

I actually had the exact same problem for my autonomous, and I have no idea how to fix it. I just converted all my spinfor scripts to spin scripts, and then had the motors stop after a certain amount of time. However, I used Python not C++

Welcome to the forum @Mr_Blockman! If you are having issues with spinFor methods in Python and would like help, please post the relevant code and provide details on what the code is currently doing vs. should be doing. I would be glad to provide assistance!

heh it’s kinda too late for that, I changed my entire robot’s code to blocks :no_mouth:

On the bright side it worked…

Glad you found a solution! I would definitely recommend coming back to python or moving to c++ when you have more experience, they are much more powerful and capable than blocks. But for now, it’s great that you were able to recognize that blocks may be the best solution. Besides, it’s even in your name!