V5: Printing Motor Velocity and Temperature


#1

What’s wrong with this code? I’m trying to print real-time motor velocity and temperature to the brain and velocity to the controller screen but it’s spitting out random numbers that change randomly after “RPM:” and after “Temp:”.
Here is my code:

int flywheelStatDisp(){
Brain.Screen.setFont(vex::fontType::mono40);
while (true){
Controller.Screen.clearLine(3);
Controller.Screen.setCursor(3,0);
Controller.Screen.print(“RPM:%d”,flywheel.velocity(vex::velocityUnits::rpm));

    Brain.Screen.clearScreen();
    Brain.Screen.printAt(1,40,"RPM:%d",flywheel.velocity(vex::velocityUnits::rpm));
    Brain.Screen.printAt(1,80,"Temp:%d",flywheel.temperature(vex::percentUnits::pct));
    Brain.Screen.render();
    vex::task::sleep(20);
}

}


#2

Both


velocity

and


temperature

return


double

values.


%d

is the format character for


int

values. Try


%f

instead.


#3

It worked. Thank you so much. Is there a function that can truncate the numbers tho.


#4

Multiply by some power of 10, round it off, then divide by the same power of 10. Print that result. Usually works. Most print format strings don’t have rounding specifiers built in.


#5

you can limit the number of decimal places shown like this

Brain.Screen.printAt(1,40,"RPM: %8.3f",flywheel.velocity(vex::velocityUnits::rpm));


#6

Thank you.


#7

What do those crazy number number and letter combinations even mean you ask! Well wonder no more: www.cplusplus.com/reference/cstdio/printf/


#8

This is kinda off-topic but…

@goofnrox why in the world is there an uppercase decimal point, especially when the lowercase one looks exactly the same?


#9

Because floating point numbers include items like “INF” for infinity and “NAN” for not-a-number, which can be printed in upper or lower case.


#10

Brain.Screen.Print does not support the complete list of formats shown, only the most common ones. however, using the standard library sprintf to create a string should.