VEXCode Sleep help

Hello!
As I am working on a VEXCode odometry tutorial, that may be published later on, and I am stuck with one major issue. I am simulating my code utilizing lua, and I have the ability to utilize:

local m = wait(time)

And I cannot figure out how to do the same thing with waiting/sleeping in a VEXCode task. I am looking for a wait that, when called, returns the actual value the robot has waited for to make calculations slightly more precise based upon time by doing:

worldX += XVelocityWorldSpace * (m/time)

Is there anything I can utilize that allows waiting to return the actual time waited, or would I have to deal with the slight time error from the statement?
Note: By utilizing such a statement it improved the odom position accuracy from 95% to 99%, and it would be awesome if theres a way to do this in vexcode

3 Likes

couple of options.

// return time slept in mS
uint32_t
wait1( uint32_t time_mS ) {
    uint32_t start = Brain.Timer.system();
    this_thread::sleep_for(time_mS);
    return Brain.Timer.system() - start;
}

// return time slept in uS
uint32_t
wait2( uint32_t time_mS ) {
    uint64_t start = Brain.Timer.systemHighResolution();
    this_thread::sleep_for(time_mS);
    return (uint32_t)(Brain.Timer.systemHighResolution() - start);
}
6 Likes

and for odometry, checkout the recent timestamp() addition to all device classes in VEXcode that returns the time when the last message (and therefore position etc.) was received from any smart device.

5 Likes

Thank you very much! I am not really familiar with uint32_t variables, but would it just be that I declare the function in code such like this?

uint32_t wait2( uint32_t time_mS ) {
    uint64_t start = Brain.Timer.systemHighResolution();
    this_thread::sleep_for(time_mS);
    return (uint32_t)(Brain.Timer.systemHighResolution() - start); 
}

void drivercontrol(void){
uint32_t m = wait2(20);
}

Also can I utilize uint32_t in mathematical operations as well? If not, can I convert it?

2 Likes

uint32_t is just a 32-bit unsigned integer, so yes you can use it as a normal int

3 Likes

Thank you very much! That makes sense xD

@jpearman Would it be possible to have the function addded to the VEXCode api: https://api.vexcode.cloud/v5/html/index.html
The timestamp function seems awesome, but I cannot find how to use it or where to find it, even when looking on the VEXCode command reference. Are any examples available?
PS: Thank you so much for the help!

5 Likes

I don’t have a robot with me, but after trial and error is the correct usage of timestamp like this?

vexDeviceGetTimestampByIndex('A');

which probably returns a uint64_t or uint32_t as well?

1 Like

No, that’s the C API (which I guess you could use) but something like this is more appropriate.

If you had an inertial sensor

vex::inertial imu(PORT1);

then you can get the time the last data was received as

uint32_t t = imu.timestamp();

and yes, you could substitute int (or unsigned int more accurately) where I used uint32_t, it’s just habit that I tend to do that.

timestamp() is available for all smart port sensors (except vision sensor because it didn’t make much sense for that and there are some implementation issues). For three wire devices (quad encoder etc.) you need to use it on the base three wire device, so the Brain.ThreeWirePort for the internal port.

timestamp() only has mS accuracy

yea, new classes and their member functions will be added to the documentation soon, these new features were only released a few days ago as part of the 2.0 preview.

7 Likes