When I print to screen the driver input control lags

So…when I run the code, it prints all 4 motor temperatures and velocity along with the battery percentage. But the driver input control has 1-2 second of lag between when I move the joysticks to when the robot actually moves. I commented all the code that prints to the controller and the lag disappears. How can I print to the controller and not have input lag at the same time?

// VEX V5 C++ Project
#include "vex.h"
using namespace vex;

//#region config_globals
vex::brain      Brain;
vex::motor      FrontLeft(vex::PORT1, vex::gearSetting::ratio18_1, false);
vex::motor      FrontRight(vex::PORT10, vex::gearSetting::ratio18_1, true);
vex::motor      BackLeft(vex::PORT11, vex::gearSetting::ratio18_1, false);
vex::motor      BackRight(vex::PORT20, vex::gearSetting::ratio18_1, true);
vex::controller Controller1(vex::controllerType::primary);
//#endregion config_globals

int main(void) {
    while(1){
    
    Controller1.Screen.setCursor(1,1);
    Controller1.Screen.print(FrontLeft.temperature(celsius));
    Controller1.Screen.print(" ");
    Controller1.Screen.setCursor(1,12);
    Controller1.Screen.print(FrontLeft.velocity(pct));
    Controller1.Screen.print(" ");
    
    Controller1.Screen.setCursor(2,1);
    Controller1.Screen.print(FrontLeft.temperature(celsius));
    Controller1.Screen.print(" ");
    Controller1.Screen.setCursor(2,12);
    Controller1.Screen.print(FrontLeft.velocity(pct));
    Controller1.Screen.print(" ");
    
    Controller1.Screen.setCursor(3,2);
    Controller1.Screen.print("Battery % full", (Brain.Battery.capacity())*100); 
    
    if(Controller1.Axis2.value() > 1){//Move Forward
        FrontLeft.spin(forward, 75, vex::velocityUnits(pct));
        FrontRight.spin(forward, 75, vex::velocityUnits(pct));
        BackLeft.spin(forward, 75, vex::velocityUnits(pct));
        BackRight.spin(forward, 75, vex::velocityUnits(pct));
    }
    else if(Controller1.Axis2.value() < -1){//Move Backward
        FrontLeft.spin(forward, 75, vex::velocityUnits(pct));
        FrontRight.spin(forward, 75, vex::velocityUnits(pct));
        BackLeft.spin(forward, 75, vex::velocityUnits(pct));
        BackRight.spin(forward, 75, vex::velocityUnits(pct));
    }
    else if(Controller1.Axis4.value() > 1){//Turn Left
        FrontLeft.spin(reverse, 75, vex::velocityUnits(pct));
        FrontRight.spin(forward, 75, vex::velocityUnits(pct));
        BackLeft.spin(reverse, 75, vex::velocityUnits(pct));
        BackRight.spin(forward, 75, vex::velocityUnits(pct));
    }
    else if(Controller1.Axis4.value() < -1){//Turn Right
        FrontLeft.spin(forward, 75, vex::velocityUnits(pct));
        FrontRight.spin(reverse, 75, vex::velocityUnits(pct));
        BackLeft.spin(forward, 75, vex::velocityUnits(pct));
        BackRight.spin(reverse, 75, vex::velocityUnits(pct));
    }
    else{
        FrontLeft.stop();
        FrontRight.stop();
        BackLeft.stop();
        BackRight.stop();
    }
    vex::sleep(1);
   }
 }

Why do you need to continually (every 20ms) transmit motor temperatures and print it on the controller?

What this should tell you is that as an operation - sending data from brain to controller and printing it is an expensive operation.

the sleep function currently is 1ms
I copied and pasted the modified code but I haven’t downloaded the new code yet
it was tested with 1ms I changed it to 20ms so later when I do download the modified code I can see if there is a difference
but 1ms for driver control has the best response I just don’t know how to not make the controller screen update that quickly

Like @lacsap said, you shouldn’t need to update every 20 ms. If you need to keep such constant track of your motor temperatures, something is wrong.

My suggestion is make a separate task that updates every 3 seconds. Plenty of processing power is left to do the driver input. Then, program a button on the controller (one of the direction buttons would be my preference) to do a forced update of the controller screen. That way, if you just need to do a cursory glance during a match, you can without burning out your processing power, and if you really need it updated, then it’s not like you’re going to be driving while looking at the controller screen.

so something like

if(Controller1.ButtonA.pressing(){
Controller1.Screen.setCursor(1,1);
Controller1.Screen.print(FrontLeft.temperature(celsius));
Controller1.Screen.print(" “);
Controller1.Screen.setCursor(1,12);
Controller1.Screen.print(FrontLeft.velocity(pct));
Controller1.Screen.print(” ");

Sure. That isn’t how I would do it, but it would probably work.

well…the compile worked I just need to download it tomorrow since I don’t have my robotics class today.

Controller printing has some enforced delays due to wireless bandwidth.
If you try and print to the controller without at least a 50mS delay (when wireless, tethered is less), VEXcode will add the delay internally using task::sleep. The best approach is to run controller printing in a separate thread, that way only the printing thread is forced to sleep.

Lies programmers tell themselves:

  • Memory is infinite
  • Bandwidth is infinite
  • Latency is zero