How to Print Sensor values on the Controller

I’m trying to let the controller print the real-time inertial sensor value to its screen, but when I run this code, the screen is blank. I think the screen refreshes too quickly, so the controller does not have enough time to print them. Is there a way to use a separate thread to print the sensor value?

  int error = ang;
  int prevError = 0;
  int derivative;
  Inertial.setRotation(0, degrees);
  while(abs(error) > 2){
    error = ang - Inertial.rotation(degrees);
    derivative = error - prevError;
    double mmp = kp * error + kd * derivative;
    if(mmp > 0){mmp += 5;}
    else if(mmp < 0){mmp -= 5;}
    LF.spin(forward, mmp, percent);
    LR.spin(forward, mmp, percent);
    RF.spin(reverse, mmp, percent);
    RR.spin(reverse, mmp, percent);
    prevError = error;


    Controller1.Screen.clearScreen();
    Controller1.Screen.print(Inertial.rotation(degrees));


    vex::task::sleep(20);
  }
  LF.stop();
  LR.stop();
  RF.stop();
  RR.stop();
  return 1;
}

That’s a valid concern. I think V5 only lets you send to the controller only one screen command every 50 ms.

Also, you don’t have to clear screen every time. Use set_cursor() instead:

   double r = Inertial.rotation(degrees);
   Controller1.lcd.setCursor(1, 1);
   Controller1.lcd.print("rotation=%f",r);

https://api.vexcode.cloud/v5/html/classvex_1_1controller_1_1lcd.html
https://api.vexcode.cloud/v5/html/classvex_1_1inertial.html

2 Likes

You could create a variable to count the number of loops and then only print the value after a certain number of iterations. Hint: you can use the modulus operator here. Your code might look something like if loopCount % 10 == 0 then print value. This would print every 10 iterations.

Maybe not for this situation, but you could only update the text if the value changed. The intertial sensor value is always changing during a turn so this application would not make sense. If you were printing motor temperature to the screen, this would be a valid approach.

On that note, can I ask why you want to print this value to the controller screen? I think the controller screen is one of the least effective places to do print debugging since it has a slow refresh rate and limited screen space. Maybe you could say that this gives you a wireless method of viewing certain values, but even then the values update too fast for a human eye to make sense of it until the procedure stopped. Alternatively, if you wanted to log the values and analyze it, an SD card is good for saving information then viewing it on a computer. Finally, if you have a long usb cable you could just keep it plugged into your computer (Or try using the wireless terminal, however I am not really up-to-date with its workings in vexcode)

2 Likes

Thanks. I will try that. I do have a long USB cable. Do you mean that when I connect the brain with my computer, I can directly see the sensor values on my computer?

To debug, I like writing values to the VexCode “terminal.”
Add:
#include <iostream>

In your while loop add:
std::cout << Inertial.rotation(degrees) << '\n';

A log of the values will be written to the VexCode terminal.

2 Likes