How to make a motor go forward during 3 seconds, using java and programming in iterative.
If you want to use timed movements, I would strongly recommend using the TimedRobot framework instead of the IterativeRobot framework. You can find more info on the different robot frameworks here.
Feel free to cook this copy-pasta in your very own robot. ::safety::
// Autonomous 'Global' Variable
private Timer m_timer = new Timer();
@Override
public void autonomousInit() {
m_timer.reset();
m_timer.start();
}
@Override
public void autonomousPeriodic() {
// Drive Forward
if (m_timer.get() < 3.0) {
m_robotDrive.arcadeDrive(0.5, 0.0);
} else {
m_robotDrive.stopMotor();
}
}
We’re really new to FRC, and when starting, I didn’t even know that a command based system existed. Because of this, I have some old code that I wrote using mainly Threads in java. I was using raw methods and such instead of commands or the WPILIB files (mostly).
This was an example of our code, assuming you already set motors to their assigned ports and made Speed Controller Groups if needed.
left is the left SpeedControllerGroup and right is the right SpeedControllerGroup
public static void driveStraight() {
try {
left.set(-0.6); //left inverted
right.set(0.6)
Thread.sleep(3000);
left.set(0);
right.set(0);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
I KNOW this is not efficient at all, and I’m working on converting our entire code to CommandBased (I’m very new to it), but this has been working for us as of now. This is just raw code to go forward without a gyro or any sensors (we don’t have those either ).
If you want to go the command-based route (which I highly recommend, and would be more than willing to help you out with if you need it), there’s TimedCommands built right into the WPI libs that you can use. Simple specify a timeout length when constructing your TimedCommand, and then whatever you put in the execute method of that TimedCommand will run until the timeout ends.
You can read the docs about it here. (Look for the TimedCommand in the sidebar)
Direct link. You can copy links to JavaDoc HTML by right-clicking the link in the tree view and selecting “Copy link address”, or “Open in new tab” to view the full HTML page