Autonomous Moving Backwards

Can you guys help me with making our robot drive backwards and turn right in autonomous mode

What sensors are you using? What language?

Generally it’s done by creating commands to drive or turn in X direction for a set distance (via encoder) or time, then chaining those commands together in a command group. This only applies in the text languages, I’m not sure how it works in LabVIEW

We use java for our programming
jaruar1 is our left drive motor
jaguar2 is our right drive motor

Unless your team has used them already you should have E4P US Digital Optical Encoders from previous years’ Kit of Parts. Mounting this on a drive shaft would allow you to detect the shaft’s rotation to calculate the distance or speed the robot is travelling at. You can use this to tell your robot to drive a distance autonomous and then stop once it reaches that distance.

There is a gyro and an accelerometer included in the Kit of Parts. The gyro is an analog sensor that detects change in angle. Mounting this on the robot parallel to the ground allows you to tell the robot to turn until it reaches 90 degrees (right.)

If you do not use an encoder, gyro, or any other sensor to detect the robot position then the only alternative is a timer based autonomous. You can specify through logic (while statement for SimpleRobot & if then statement for IterativeRobot) to turn on the motors at specific times and turn it off when it shouldn’t be running.

Example code of how a timer based autonomous could run. In the example the robot drives backwards for 1 second, sits still for 1 second, and then turn right in place for 1 second.


Timer timer = new Timer();
Jaguar leftDrive = new Jaguar(1);
Jaguar rightDrive = new Jaguar(2);

autonomousInit() {
  timer.start();
  timer.reset();
}

autonomousPeriodic() {
  double currentTime = timer.get() * MathUtils.pow(10, -6);
  if (currentTime < 2) { //if time is less than 2 seconds
    leftDrive.set(-0.5); //drive left motors backwards
    rightDrive.set(-0.5); //drive right motors backwards
  } else if (currentTime  >= 2 && currentTime < 3) { //time between 2&3 secs.
      leftDrive.set(0);
      rightDrive.set(0);
  } else if (currentTime >= 3 && currentTime < 4) { //time between 3&4 secs.
     leftDrive.set(0.5); //set left forward to turn right
     rightDrive.set(-0.5); //set right backward to turn right in place
  }
}

Just out of curiosity Magi, what does MathUtils.pow(10, 6) do?

It raises the first parameter (10) to the second parameter (-6) power. So the result is 10^-6. I probably should use scientific notation instead. I am multiplying the time by this assuming the javadocs is correct that the timer returns the time in micro seconds.

You should probably multiply it by 10**-6** (if you want seconds).

Oops, fixed it now.

Our team is not using encoders for the drive system only the shooter this year. I just want the robot to drive backwards when the first 3 shots are fired. That takes about 9 seconds i have it for and then it will switch to low gear. then i want it to just drive straight back for the rest of the autonomous period.

In that case, you would want something similar to Magi’s example, since that’s based on time.

You could also create a commands for driving, shooting, etc. and then string them together in a command group and have them execute in-order. That would require a bit less tuning to get right, though it might take longer to initially program.

Be careful that you do not cross the center-of-the-field line