overcoming time limits

GPC2 script programming for Titan Two. Code examples, questions, requests.

overcoming time limits

Postby DeviateSquirrel » Thu Feb 11, 2021 6:15 pm

This script stops providing output after 30-45 seconds of use. I understand there's a limit to how long you can have a main run continue but I don't know how to work around it... or if I can work around it. I've tried putting things in functions but it didn't help. I suspect it's because they're all inside of that first if/then and the triggers are continually moving. Is there anything I can do to get it running continuously? Maybe a way to have it refresh and essentially start over every 20 seconds or so?

Code: Select all
#pragma METAINFO("GyroTest", 1, 0, "DeviateSquirrel")
 
uint16 deltaTimeX, deltaTimeY, Xts, Yts;
fix32 gyroYaw, gyroPitch;
fix32 deltaX, deltaY;
fix32 angleRange = 100.0;
fix32 deadZone = 0.5;
fix32 xAngle, yAngle, xAngle_Prev, yAngle_Prev;
fix32 button5, button8;
fix32 gyroSens = 1.0;
fix32 x, y;
 
init {
 
}
 
main {
 
    //correct values from opentrack
    //value near 0% 0.39215087890625
    //value at 50% 49.8039093017578125
    //value at 100% 100.0004730224609375
    fix32 kmgMax = 100.0004730224609375;
    fix32 kmgMin = 0.39215087890625;
 
 
    if( y != get_actual(BUTTON_8)) {
 
        y = get_actual(BUTTON_8);
 
        deltaTimeY = system_time() - Yts;
        printf("deltaTimeY: %ums", deltaTimeY);
 
        button8 = get_actual(BUTTON_8);
        yAngle =  clamp(lerp(-angleRange, angleRange, Normalize(button8, kmgMin, kmgMax, 0.0, 100.0,49.804107666015625)/100.0), -angleRange, angleRange);
 
        deltaY = yAngle - yAngle_Prev;
 
        if(deltaTimeY == 0){
            gyroPitch = gyroPitch;
            yAngle_Prev = yAngle_Prev;
        } else {
            gyroPitch = ((deltaY)/(fix32)deltaTimeY)/(2000.0/32767.0)*gyroSens;
        }
 
        yAngle_Prev = yAngle;
        Yts = system_time();
    } else if(Yts != -1){
        if((fix32)system_time() - (fix32)Yts >= (fix32)deltaTimeY + deadZone){
            gyroPitch = 0.0;
            yAngle_Prev = yAngle_Prev;
            Yts = -1;
        }
    }
 
    if(x != get_actual(BUTTON_5)) {
 
        x = get_actual(BUTTON_5);
 
        deltaTimeX = system_time() - Xts;
        printf("deltaTimeX: %ums", deltaTimeX);
 
        button5 = get_val(BUTTON_5);
        xAngle = clamp(lerp(-angleRange, angleRange, Normalize(button5, kmgMin, kmgMax, 0.0, 100.0,49.804107666015625)/100.0),-angleRange,angleRange);
 
        deltaX = xAngle - xAngle_Prev;
 
        if(deltaTimeX == 0){
            gyroYaw = gyroYaw;
            xAngle_Prev = xAngle_Prev;
        } else {
            gyroYaw = ((deltaX)/(fix32)deltaTimeX)/(2000.0/32767.0)*gyroSens;
        }
 
        xAngle_Prev = xAngle;
 
        Xts = system_time();
    } else if(Xts != -1){
        if((fix32)system_time() - (fix32)Xts >= (fix32)deltaTimeX + deadZone){
            gyroYaw = 0.0;
            xAngle_Prev = xAngle_Prev;
            Xts = -1;
        }
    }
 
    set_val(GYRO_1_X, gyroYaw);
    set_val(GYRO_1_Y, gyroPitch);
 
    //just for the graph
    set_val(PADDLE_1, xAngle);
    set_val(PADDLE_2, yAngle);
 
 
}
 
 
fix32 Interpolate(fix32 val, fix32 valmin, fix32 valmax, fix32 newLow, fix32 newHigh){
    return (newHigh - newLow) * (val - 0.5 * (valmax + valmin)) / (valmax - valmin) + 0.5 * (newHigh + newLow);
}
 
fix32 Normalize(fix32 val, fix32 valmin, fix32 valmax, fix32 newLow, fix32 newHigh, fix32 midpoint){
    fix32 m = Interpolate(midpoint, valmin, valmax, newLow, newHigh);
    if (val < m) {
        return Interpolate(val, valmin, m, newLow, midpoint);
    } else if (val > m) {
        return Interpolate(val, m, valmax, midpoint, newHigh);
    } else {
        return m;
    }
    return 0.0;
}
User avatar
DeviateSquirrel
First Sergeant
First Sergeant
 
Posts: 46
Joined: Sun Jan 17, 2021 11:07 pm

Re: overcoming time limits

Postby Scachi » Thu Feb 11, 2021 8:06 pm

I have no idea what limit of main run continue you are talking about. I don't know such a limit.

If a single run of one main loop is taking more time than *some limit I don't know* the T2 watchdog resets the T2 as it isn't responding anymore. This is the only time limit to main I know of.

Have you checked if there is some message in the output panel when your script stops responding ?
Remove your current debug output to see if there is some error message that may get lost during your current text output.
Doing a lot of printf will cause gtuner iv (not sure of what exactly happens then) stop reacting for a short time, not showing output and the whole processing of gtuner IV may loose steps. Did you run your script through Test % Debug or from a real " T2 slot ?
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: overcoming time limits

Postby DeviateSquirrel » Thu Feb 11, 2021 8:42 pm

My mistake then. I thought I read in a couple of posts about issues with keeping the main loop running.

I pulled the printf statements and the same thing happens with no messages in the Output Panel. I don't see a Test & Debug option in Gtuner. Where is that? I get the same result running it from a memory slot on the Titan Two.

This is script is trying to measure the velocity of an input (how quickly it's changing). Ultimately BUTTON_5 and BUTTON_8 will be replaced by yaw and pitch from a headlook through KMG Capture and will plug into the GYRO_1_X and GYRO_2_X outputs. I tried taking the yaw/pitch data straight into ACCEL_1_X and ACCEL_1_Y but it seems Elite Dangerous wants the gyro as well (or instead, not sure yet). There's some stuff in the front end that recenters the output from the headlook (coming from Opentrack) and Normalizes and Interporlates to get between a pair of angles and recenter where 50% is located.

Right now it starts one of two ways; either I get gyroPitch and gyroYaw outputs right away, or they stay at zero for between 10-20 seconds and then start to show data. Either way, about 30 seconds later they both go to zero. Then, another 30 seconds and they start showing data again. This cycle repeats. For testing, I'm pulling the triggers from 10-90% or so continuously.

Thoughts on where I've gone wrong? Thanks.

edit: Is Test & Debug the same as Compile? If so, it succeeds with 0 warnings.
User avatar
DeviateSquirrel
First Sergeant
First Sergeant
 
Posts: 46
Joined: Sun Jan 17, 2021 11:07 pm

Re: overcoming time limits

Postby Scachi » Thu Feb 11, 2021 9:58 pm

test and debug is that green play button in the top menu of gtuner iv.
you can run a script directly in gtuner iv without having to put it manually onto a T2 slot first.

are you sure your timing math is correct or your values are in the correct range for your variable types ?
i get some wierd values in the output panel when running your code:
Code: Select all
GVM Output: deltaTimeX: 2ms
GVM Output: deltaTimeX: 2ms
GVM Output: deltaTimeX: 1ms
GVM Output: deltaTimeX: 52270ms
GVM Output: deltaTimeX: 52271ms
GVM Output: deltaTimeX: 52273ms
GVM Output: deltaTimeX: 52278ms
GVM Output: deltaTimeX: 1ms
GVM Output: deltaTimeX: 1ms
GVM Output: deltaTimeX: 52283ms
GVM Output: deltaTimeX: 52291ms
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: overcoming time limits

Postby DeviateSquirrel » Thu Feb 11, 2021 10:54 pm

Gotcha. Yes, that's what I've been doing to test things out.

I get the same sort of values and I don't know why. Those numbers start to get into the 20k range and tick up to 65k or so and then drop back to the 1s and 2s and occasional oddballs.

I'm using fix32 for most things and had been using uint16 for time but I just checked https://www.consoletuner.com/wiki/index ... ystem_time and set it to uint32. No changes, however.

This is the meat of the time portion of the code with deltaTimeY and Yts both as uint32 (deadZone is fix32 and set at 0.5). I pulled the bones from J2Kbr's post here -- viewtopic.php?f=26&t=7262#p53836

There's another if/then beneath this one for the other axis. I had both inputs under the same conditional initially but found that it only updated one at a time but not both in the same run so I split it so they'd both update.

Code: Select all
 
if( y != get_actual(BUTTON_8)) {
        y = get_actual(BUTTON_8);
        deltaTimeY = system_time() - Yts;
                // bulk of code here
                } else if(Yts != -1) {
        if((fix32)system_time() - (fix32)Yts >= (fix32)deltaTimeY + deadZone){
            gyroPitch = 0.0;
            yAngle_Prev = yAngle_Prev;
            Yts = -1;
        }
    }
 
User avatar
DeviateSquirrel
First Sergeant
First Sergeant
 
Posts: 46
Joined: Sun Jan 17, 2021 11:07 pm

Re: overcoming time limits

Postby Scachi » Thu Feb 11, 2021 11:14 pm

65k sound like hitting the max of that uint16.
sytem_time() is defined to return uint32 .
no idea how good it works to cast system_time to fix32 and using uint32 as uint16 and cast that to fix32 again.
perhaps some "clamp" is required or some factor to keep your values in a working range.

You can check the datatype page to see the types range if you need to adjust something to not get crazy values.
https://www.consoletuner.com/wiki/index ... data_types
Also check each functions return type of the data to use the correct type for them of their documentation page or in gtuner IV when you write the functions name
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: overcoming time limits

Postby DeviateSquirrel » Thu Feb 11, 2021 11:57 pm

And there you go... now it should run for 11930.46470833 hours! I think my gaming will be done by then, ;)

Thanks for your help!

BTW, I figured out why I was getting odd numbers occasionally. When the ELSE statement is satisfied the Xts value goes to -1 which in the next loop gets subtracted from system_time(). I added another loop in middle to catch the -1 so it won't happen anymore.
User avatar
DeviateSquirrel
First Sergeant
First Sergeant
 
Posts: 46
Joined: Sun Jan 17, 2021 11:07 pm


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 70 guests