Not being able to move during autonomous period?

I am participating in the vex tower takeover challenge, and for a reason I cannot figure out, in our autonomous period code, our motors don’t seem to move. I have tried every move command, and asked lots of people at the event. I was recommended to see if people on the forum could help, so is there any reason the below code should not work inside a competition template?

Brain.Screen.clearLine();
Brain.Screen.print("autonomous is running");
while(Brain.timer(sec) <= 5  &&  LeftMotor.rotation(degrees) >= -90 ){
LeftMotor.rotateFor(reverse , 1 , degrees);
LeftMotor2.rotateFor(reverse , 1 , degrees);
RightMotor.rotateFor(reverse , 1 , degrees);
RightMotor2.rotateFor(reverse , 1 , degrees);
}
LeftMotor.resetRotation();
Brain.Timer.reset();
Brain.Screen.clearLine();
Brain.Screen.print("halfway there!");

while(Brain.timer(sec) <= 5  &&  LeftMotor.rotation(degrees) <=90 ){
LeftMotor.rotateFor(forward , 1 , degrees);
LeftMotor2.rotateFor(forward , 1 , degrees);
RightMotor.rotateFor(forward , 1 , degrees);
RightMotor2.rotateFor(forward , 1 , degrees);
}
LeftMotor.resetRotation();
Brain.Screen.clearLine();
Brain.Timer.reset();

LeftMotor.stop();
LeftMotor2.stop();
RightMotor.stop();
RightMotor2.stop();
Brain.Screen.clearLine();
Brain.Screen.print("autonomous has ended");

I’ll move this to VEXcode, you had it in the PROS section.
this code

LeftMotor.rotateFor(reverse , 1 , degrees);

gives me concern used in a while loop, constantly trying to move 1 degree in a loop, need to see what that would actually do, however, is the program even printing Brain.Screen.print(“autonomous is running”); on the screen ?

4 Likes

yes, the entire loop completes, but no motors move

void autonomous(void)
LeftMotor.spin(forward);
LeftMotor2.spin(forward);
RightMotor.spin(forward);
RightMotor2.spin(forward);

vex::task::sleep(INSERT AMOUNT OF MILLISECONDS YOU WANT PROCESS TO GO FOR)
LeftMotor.stop();
LeftMotor2.stop();
RightMotor.stop();
RightMotor2.stop();

LeftMotor.spin(rev);
LeftMotor2.spin(rev);
RightMotor.spin(rev);
RightMotor2.spin(rev);

vex::task::sleep(INSERT AMOUNT OF MILLISECONDS YOU WANT PROCESS TO GO FOR)

LeftMotor.stop();
LeftMotor2.stop();
RightMotor.stop();
RightMotor2.stop();

1 point auton

This was the first approach i took. I tried this with both vex::task::sleep(time) and wait(). This worked in a separate file, but did not in our autonomous function.

Not sure if this will help, but here is what my main looks like.

int main() {
// Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);

// Run the pre-autonomous function.
pre_auton();
autonomous();
usercontrol();
// Prevent main from exiting with an infinite loop.
while (true) {
  wait(100, msec);
}
}

I’ve been told this is configured correctly

ok, so asking the motor to rotate 1 degree in a loop like that isn’t going to work. The motor is constantly being told to start the move and it never completes. But what you are trying to do, that is move backwards for 90 degrees rotation with a 5 second timeout, can be done in a much easier way than that. For example, this non working code

while(Brain.timer(sec) <= 5  &&  LeftMotor.rotation(degrees) >= -90 ){
  LeftMotor.rotateFor(reverse , 1 , degrees);
}

can easily be replaced by this

LeftMotor.setTimeout(5, seconds);
LeftMotor.spinFor( reverse, 90, degrees );

we set a 5 second timeout on the motor (you would need to do for all motors) then you ask for a 90 degree reverse movement.
with multiple motors, you would set some as non blocking, so this

while(Brain.timer(sec) <= 5  &&  LeftMotor.rotation(degrees) >= -90 ){
  LeftMotor.rotateFor(reverse , 1 , degrees);
  LeftMotor2.rotateFor(reverse , 1 , degrees);
  RightMotor.rotateFor(reverse , 1 , degrees);
  RightMotor2.rotateFor(reverse , 1 , degrees);
}

would be replaced by this

LeftMotor.setTimeout(5, seconds);
LeftMotor2.setTimeout(5, seconds);
RightMotor.setTimeout(5, seconds);
RightMotor2.setTimeout(5, seconds);

LeftMotor.spinFor( reverse, 90, degrees, false );
LeftMotor2.spinFor( reverse, 90, degrees, false );
RightMotor.spinFor( reverse, 90, degrees, false );
RightMotor2.spinFor( reverse, 90, degrees, true );

now, if the intent of the 5 second timeout was something else like trying to run the motors for 5 seconds, that code wouldn’t work, but it did seem like you just wanted a very small reverse movement.

(edit: notice I’m using spinFor, that’s the same as rotateFor but using our preferred API)

3 Likes

@jpearman 's method should work too

how does mine not work?

also there is an Error thing at the bottom

no, that’s not correct, it should just be this.

int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

  // Run the pre-autonomous function.
  pre_auton();

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}

don’t call autonomous or usercontrol directly, they will be started by field control.

3 Likes

Ok, I will change main to remove calling those. Your recommendation for the autonomous did not work. Is this a problem that is out of my control?

Would have to see the whole project to know if you have something else going on. Is driver control working ? You can export the project and attach the zip file here if you want.

1 Like

driver control is working, all other aspects except autonomous are working. It says I cannot upload since I am a new user.

I bumped your trust level up, you should be able to upload now.

3 Likes

CompCode.zip (100.6 KB)
Here is the zip of my code

yea, ok, you have some fundamental issues with that code.

not related to your exact problem, we will get to that in a minute.
in usercontrol, you register many events in a loop, events (ie. calling buttonX.pressed etc.) must only be done once. When you connect to the field, usercontrol will often run if you start the program before connecting to field control (you also were starting it in main, see my post a few back, remove those lines of code). There’s also a good chance you may briefly go into and out of usercontrol even under field control sometimes. Anyway, you use up most resources of the brain doing this and event handlers (the thing that happens when you press the button etc.) run multiple times.

I also see things like this.

void moveLeftAuto (double time , int power) {
  //distance is in inchs
  for(int t = 0; t <= time ,t = t + .0001; ) {
  LeftMotor.spin(directionType::fwd, power , velocityUnits::pct);
  LeftMotor2.spin(directionType::fwd, power , velocityUnits::pct);
  wait(.0001 , seconds);
  }
}

time is a double, but the variable in the for loop is an integer, t cannot have 0.0001 added. You may never leave that loop. Also, minimum wait time is 1mS, that is 0.001 seconds.

Anyway, the primary issue with the code that is stopping motors is that part of usercontrol sets motor velocity to 0.

void moveRight ( int mpower , int onOff) {
  if( onOff == 1){
   // implement stoping
    RightMotor.setVelocity(0 , velocityUnits::pct);
    RightMotor2.setVelocity(0 , velocityUnits::pct);
    RightMotor.spin(forward);
    RightMotor2.spin(forward);
    return;
  }else{
    // power is on percent
    // onOff controls whether to start or stop, 0 = on, 1 = off
     RightMotor.setVelocity(mpower , velocityUnits::pct);
    RightMotor2.setVelocity(mpower , velocityUnits::pct);
    RightMotor.spin(forward);
    RightMotor2.spin(forward);
  }
}

mpower will mostly be 0 and you call that function from usercontrol.

so when autonomous runs (remember I said usercontrol will often have already run). and you ask the motor to move to 90 degrees, it is trying to use the velocity you have set, unfortunately 0.

either make sure default velocity has been set at the beginning of autonomous, or use spinTo function that includes velocity as well as position to spin to.

but clean up all that event stuff, even if that’s not currently causing problems I’m sure it will at some point.

6 Likes

Ok, thank you! I will work on fixing this. Thanks alot!