To the specific issue you mentioend
TeleopPeriodic forms the guts of what’s called “Superloop” architecture in embedded software.
The key to keep in mind is that programming for a robot requires a slightly different mentality than “usual” desktop PC programs. Inside of teleop periodic, teams get to provide a list of things to do every 20ms.
The while
loop in your code currently says “Every 20ms, stop doing anything else until we detect blue” . What I’m strongly suspecting you actually want is “Every 20ms, if we see blue, stop. Otherwise, spin”.
Broader Discussion
The following is some psuedocode of how the robot is using the code you’ve written. It’s definitely inaccurate, but might be useful in forming a mental model of howteleopPeriodic()
gets used.
public static void main(){
robotInit();
while(true){ //The proverbial "super loop"
readInfoFromDS();
if(dsSaysWeAreInTeleop()){
teleopPeriodic();
}
// Other else-if's for other modes here
sendBatteryVoltageValueToDS();
delayFor20ms();
}
}
The key - the robot will already call teleopPeriodic inside of a loop. teleopPeriodic can’t “block” - it has to “do its thing” fast and get out of the way, because there’s other things the robot needs to do.
Note that this is just one solution to a much broader problem in computer science. The problem, simply stated, is “What do you do when you have fewer processors than things you want to calculate?”. Operating systems, like windows and Linux, have very specific and complex solutions to let multiple programs run “at once”. For embedded software, like a robot, we have the opportunity to be a bit more precise as to which pieces of code run when.
There are programming mechanisms called “threads” which can be used to allow pieces of code to run simultaneously (or, nearly simultaneously). However, I’d recommend staying away from these if you can. It’s easy to get code running in parallel, but gets very hard, very quickly, to have those chunks of code pass data back and forth.