VEXCode get Run Time function?

Is there any function or class that can give me how long the program has been running? If there isn’t how would I go about making one? I was thinking

void startRecordingTime()
{
  while(1)
  {
    vex::task::sleep(10);
    runTime += 10;
  }
}

where runTime is gobally initialized and set to 0, but wouldn’t this prevent all other tasks from running if I call this in the start of main?

Like almost any device v5 has a system timer. You have two options…

vex::timer::system();
vex::timer::systemHighResolution();

vex::timer::system() returns the value of the system timer in milliseconds (1/1000 seconds).

vex::timer::systemHighResolution() returns the current value of the high-resolution timer (I guess that means system timer) in microseconds (1/1,000,000 seconds).

I don’t know if these timers start when the program starts or when the v5 starts. (You can figure that out by printing the value and seeing what it is.) Also you can just record the time something starts, like in your case the time the program starts.

4 Likes

yes, they are both referenced to when the program was run.

They are in fact different timer sources, the mS timer is incremented under firmware interrupt. The high resolution timer is a special timer that we read from the hardware and is divided down to make a uS timer. They should both track as everything is derived from the same master clock.

we also have a timer that gives time since power on, but I didn’t expose that in the C++ timer class. Timer class is pretty useful as it also has a simple event system, I posted this once before, but here’s a refresher.

timer       Timer;

void cb( void *arg ) {
    std::cout << "timer fired sys:" << timer::system() << " timer:" << Timer << "  \n";

    // rearm
    timer().event( cb, 1000 );    
}
 
int main()
{    
    // fire in 1 seconds time
    timer().event( cb, 1000 );
    
    while(1)
      this_thread::sleep_for(10);
}

It’s an alternate to using tasks, do something like start motors, start a timer, when timer fires perhaps stop the motors. You can rearm the timer in the event to make a periodic timer.

7 Likes