In VEXcode Pro V5, I know that the program is running a loop in robot-config.cpp which takes in the controller input, but it requires me to turn on robot expert mode, which means it won’t auto-generate the code for me anymore.
How would you run a loop side by side with the controller one in the background?
My purpose currently for the loop is to print the robot temps to the Brain screen.
I think you can just put something like “Brain.Screen.print(motorname.temp());” in your driver control loop. The formatting is probably wrong, but it’s around that ballpark. Don’t forget to do “Brain.Screen.Newline();” too.
Yeah, just do something in your driver control function like
while (true) {
Brain.clearscreen();
Brain.setCursor(1, 1);
Brain.print(motor.temp());
vex::task::sleep(2000); // At least in IQ, this function takes milliseconds
}
I don’t know any of the real functions, so you’ll have to find them in the API. That is to say, this is pseudocode.
I didn’t even know that there was a Newline function on the Brain! I’ve just been using Brain.Screen.setCursor(row, column) and then Brain.Screen.print().
No. While loops do not stop code from running any more than drive forward commands. Are you ever surprised that the brain can drive forward while also getting input from your controller? What happens is you have two groups of code running simultaneously. On one hand, you have your controller input code, which VEX generates, which allows you to drive your robot with the controller. On the other hand, you have your driver control function, which ecutes commands in the order it receives them. So when you have code like this:
Are you surprised when it waits until the while loop ends, I.e the condition is no longer true, to execute the commands outside of and after the while loop? If that’s what you mean “stop the code from running”, yes it will do that, but I don’t think that’s a problem. What code specifically are you concerned will not run, and could you please paste it in here?
If the condition in your while loop parentheses is true, the condition will always evaluate to true, the code after the while loop will never run. The exception is if you put the loop inside your driver_control() function, the field control will end the function at the end of driver control and the loop will therefore cut out. Any code in the driver_control() function and after the while (true) loop will never run.
Remember to clear your screen at the beginning of every iteration of the loop, or your text will overwrite itself or something like that.
Remember to have a wait in every iteration of the loop. I thinkvex::task::sleep(3000); will work. If it does, the input parameter is in milliseconds.
It is good to note that some printing/outputting commands are yielding
For instance, telling your controller to vibrate may cause the code to halt for roughly 100 milliseconds until the controller finishes vibrating. This may or may not be the case for the other commands, but it is always best to be safe when it comes to possible yielding commands. You should consider multithreading instead:
int print_to_brain_screen_consecutively(){
while(1){
//do other stuff
vex::task:sleep(3000);
}
return 1;
}
int main(){
vex::task brainScreen(print_to_brain_screen_consecutively);
while(1){
//do stuff
vex::task:sleep(20);
}
return 1;
}
Why did you put a loop in your robot configuration file? No no no, put it in your main.cpp file.
My thread skills are rusty but you could run the temp monitor in a separate thread so that it is not affected by the Auton or Driver Control methods. In Blocks you would simply use another When Started primary block but in VexCodePro it takes a bit more.
//Declare a separate thread for temp monitoring
vex::thread TempMonitorThread;
//My demo motor
motor motor1 = motor(PORT1,ratio18_1, false);
// Create your temp monitoring function with a while loop and a wait delay
void MonitorMotorTemp(void){
while(true){
Brain.Screen.printAt(10,10, "L1 Temp: %d ", motor1.temperature(temperatureUnits::celsius));
wait(1, sec); // <- Important: At least 20 ms but 1 second is fine.
}
}
//Boilerplate competition functions...
void pre_auton(void) {
vexcodeInit();
}
void autonomous(void) {
motor1.spinTo(90, deg);
}
void usercontrol(void) {
while (1) {
//Driver control
wait(20, msec);
}
}
int main() {
// Initialize the temp monitoring thread in the main function
// which will run separate from the Auton and Driver control
TempMonitorThread = thread(MonitorMotorTemp);
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);
pre_auton();
while (true) {
wait(100, msec);
}
}
Definitely a couple of solutions here…
Connor had a good example of exactly what I would do if I needed another loop, and I do that in my code for a few things. However, you don’t need an additional loop at all for printing motor temperatures. Just drop that print code you pasted in one of your replies into your driver loop, and it will have zero impact on your loop. That should work. Go ahead and test it even if you’re worried about it not working, won’t break anything.
You mentioned a 3 second wait time… I’m not sure whats thats for but I don’t think you need it. Just drop in your print commands into driver control. The commands you have are instant and won’t cause any problems, and they don’t need a wait time. I think you’re over complicating it.
Yeah definitely a good idea to multithread, although the print command’s don’t take any time, so for this purpose they should be fine to drop their print code in the driver loop. May be worth noting it won’t update in auton if it’s in the driver loop, but that’s not so important.
and yeah… don’t put that stuff in robot-config.cpp xD
You need it if you’re putting it in a separate loop because without it the loop will run a zillion times a second and your program will time out. It doesn’t have to be three seconds, but it’s not important enough to shrink the wait time to 20 msec, or anything. You won’t miss much in three seconds.