I’m trying to make a function that would run every 5 seconds, I wanted to do this by using Brain.Timer and checking if the output is a multiple of 5. Problem is that it only outputs for about 5 seconds and after that, it cannot do the check.
Here is the relevant part of my code for the check (I did not include the drive code and other irrelevant things in the User loop)
void User(){
while(true){
std::cout << int(Brain.Timer);
std::cout << " || ";
if (int(Brain.Timer) % 5 == 0) {
//will run if timer is a multiple of 5
}
}
int main(){
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
Brain.setTimer(0, seconds);
std::cout << "SDcard -->";
std::cout << Brain.SDcard.isInserted();
std::cout << " || ";
Brain.Screen.drawImageFromFile("background.bmp", -10, -10);
Competition.drivercontrol(User);
Competition.autonomous(Autonomous);
}
Here is what it outputs
SDcard -->0 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 030 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 ||
031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 031 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 ||
033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 ||
033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 033 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 ||
034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034 || 034
1 Like
Instead of checking the timer, you can instead just tell the program to wait for 5 seconds. Not only does this solve your problem, but it also prevents the brain from overworking when basically anything you have to do doesn’t need to be ran 10s of thousands of times per second, and it allows other tasks to run.
Ex:
while(true) {
// Your code here
wait(5, sec); // or wait(5000, msec);
}
Also, you probably want to add a new line to the end of your logs (\n
). This tells the brain you are done logging and to send the text to your computer, and looks more organized
Ex:
std::cout << “Hello World\n”;
// Or
std::cout << 1234 << “\n”;
// Or
std::cout << “Hello World” << std::endl;
Output:
Hello World
1234
Hello World
1 Like
I probably should have mentioned it in the post but inside the User loop is all of the other code for the robot to be able to drive and accept input so I can’t use the wait command since it stops the code all together
1 Like
Always good to debug when things like this happen. One thing to do is verify your assumption that the timer returns seconds. I’m pretty sure it reports milliseconds so you are trying to write to SD every 5 milliseconds which likely exceeds the transfer rate.
1 Like
The SD card print is just there to tell me if it’s inserted for a different thing. The Brain.Timer printout goes directly to the serial terminal on my laptop screen while the code runs. The problem is that it stops outputting after about 5 seconds.
1 Like
After looking at the vexcode docs for timers, it looks like you’re using the timer wrong. You’re doing int(Brain.Timer)
, which converts the Timer object to an int instead of actually returning the time. Instead you need to do Brain.Timer.time(sec)
to get the timer’s time in seconds.
Ok so you were right about the time being in milliseconds but it still stops printing after about 0.007 seconds and takes like 2 seconds to do so. I think the issue is that what you said earlier and that I’m overworking the brain by making it print all that out in less than a thousandth of a second. I just don’t know how I would stop it from printing out / checking so much.
1 Like
How about:
void User(){
int lastTime = 0;
while(true){
// Don't write to std out every time thru the loop
//std::cout << int(Brain.Timer);
//std::cout << " || ";
int curTime = (int)Brain.Timer;
if (curTime - lastTime >= 5000) {
lastTime = curTime;
// Do the stuff you want to do every 5000 milliseconds
}
// Presumably you have a wait(20, msec); or similar at the end of the loop
}
}
5 Likes
Tried this and it didn’t work with it being set to go every 5000 miliseconds but did work when I set it to 2, I set it to print out the int(brain.timer) and it still only outputs the first ~7 milliseconds. We are back to the root problem of the brain.timer function just stops returning stuff a bit after the code is started no matter how it is used.
1 Like
i just tested this code:
int main() {
// Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);
// Run the pre-autonomous function.
pre_auton();
int lastTime = 0;
// Prevent main from exiting with an infinite loop.
while (true) {
int curTime = (int)Brain.Timer;
if (curTime - lastTime >= 5000) {
lastTime = curTime;
// Do the stuff you want to do every 5000 milliseconds
printf("This happened at %d\n",curTime);
}
wait(100, msec);
}
}
And see output:
This happened at 15099
This happened at 20099
This happened at 25099
This happened at 30099
This happened at 35099
This happened at 40099
4 Likes
you are probably just saturating the std::cout stream, you cannot expect to print every few mS, especially if you are doing that wirelessly which has extremely limited bandwidth.
5 Likes
I’ll try to run this code separately from all the other code I have tomorrow and see if it works, the one difference I see though is that I had the while loop inside the user control function instead of inside main but I don’t know what effect that would have.
1 Like
Just tested, works now for some reason, thanks for the help
1 Like