|
Re: Autonomous in iterative robot
Quote:
Originally Posted by Oromus
One of the simplest and easiest ways to have logic for autonomous not run at the same time is to use Thread.sleep(). It lets you run code, have the code halt for a set amount of milliseconds/seconds, then proceed with more code. An example:
Code:
public void autonomousInit() {
try {
leftMotor.set(1);
rightMotor.set(1);
Thread.sleep(1000); //Thread.sleep() takes milliseconds, so 1000 milliseconds = 1 second
leftMotor.set(0);
rightMotor.set(0);
} catch (InterruptedException e) { //Thread.sleep() can throw an InterruptedException, so we have to catch it
e.printStackTrace();
}
}
This code makes the robot drive forward for one second and then stop. The only potential issues with this is that Thread.sleep() halts all code execution on that Thread, so if there is other code that needs to be called constantly you'll either need to not use Thread.sleep() or do multi-threading. In a simple autonomous like this, though, Thread.sleep() should fit your needs perfectly. Just remember that no code after Thread.sleep() will be called until after the delay. Hope this helps!
Also, in the event you want to do multi-threading, let me know and I'll help you out there 
|
Be careful with this. If you are doing this (putting large delays) inside of an IterativeRobot or Command-based project, you will not get good results as you are delaying the control loops, which are called every 20ms. You also wouldn't want to put it in autonomousInit(); use autonomousPeriodic() to control your autonomous routine instead. This style, using a sleep or delay command, works in SampleRobot projects.
OP, I would recommend going with the state machine approach that pblankenbaker describes. It closely mirrors what you are currently doing but adds more context and manageability.
__________________
2014 Las Vegas (Winners with 987, 2478; Excellence in Engineering)
2014 San Diego (Finalists with 987, 3250; Quality Award)
2013 Inland Empire (Winners with 1538, 968; Excellence in Engineering Award)
2013 San Diego (Finalists with 2984, 4322; Creativity Award)
2012 Las Vegas (Finalists with 2034, 3187; Quality Award)
|