V5: Printing Motor Velocity and Temperature

  1. 4 weeks ago
    Edited 4 weeks ago by Ben.162A

    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. jpearman

    Dec 18 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888
    Edited 4 weeks ago by jpearman

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

    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. goofnrox

    Dec 19 Jenison MI Jenison Robotics
    Edited 4 weeks ago by goofnrox

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

  8. Gameoa

    Dec 19 lemme think about it... 37409A

    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. Dave Flowerday

    Dec 19 Event Partner, VEX Tech Support North Barrington, IL

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

    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. jpearman

    Dec 19 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

    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.

 

or Sign Up to reply!