I’ve been looking at using the printf() function to display joystick values, but I’m a little confused about the proper syntax and parameter usage to make the code work. I want to do the following:
Result text in IO:
X 126 Y 250 (Where 126 is the value of p1_x and 250 is the value of p1_y)
Maybe this is a holdover from the older version of printf(), but I’ve always been careful to cast (convert) any “char” types (one byte numbers) to “int” type (two byte numbers on the RC) like this:
printf( x value: %d, y value: %d
\r", (int)XAXIS, (int)YAXIS );
Otherwise you can get some huge, crazy values printing out.
Here’s the quick story about printf() on any system:
printf takes a variable number of arguments. The first one must be there and it must be a string. It’s called the “format string”. That string can have anything in it. You put a ‘%d’ each place where you want it to print a value. For each %d in the format string there must be a matching argument after the format string. They are matched up in the order the %d’s appear in the format string.
You don’t have to have any %d’s. Then you are just printing a message that’s always the same:
printf( "Starting autonomous...
\r" );
Note the
and \r. These make it go to a new line. The FRC printf() wants to see \r (I’m not clear on why) but in the rest of the world you would just use a
(“newline”). Using both makes it work in all cases.
Use %d when you want to print an int type. There are other ones, like %f for “float” types, but if you avoid using floats on the robot controller (and you *should *avoid using floats) then you won’t have a need for %f.