|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
I'm looking at printing the value of the joystick y axis to the screen.
Code:
DriverStation *ds;
DriverStationLCD *dsLCD;
Joystick *driveController;
RobotDemo(void)
{
ds = DriverStation::GetInstance();
dsLCD = DriverStationLCD::GetInstance();
driveController = new Joystick(1);
}
void OperatorControl(void)
{
GetWatchdog().SetEnabled(true);
while (IsOperatorControl())
{
GetWatchdog().Feed();
dsLCD->Printf(DriverStationLCD::kUser_Line4, 1, driveController->GetY());
dsLCD->UpdateLCD();
}
}
Also is there string concatenation that you can add two strings together?? "Left Drive: " + driverController->GetY() |
|
#2
|
|||
|
|||
|
Re: Printing Float to Dashboard
Quote:
If you want to write a float to a string you can use some of the following methods. Code:
float my_float = 123.456f;
char line_buffer[22];
sprintf(line_buffer, "value: %.2f", my_float);
// or
#include <sstream>
float my_float = 123.456f;
std::stringstream ss("value: ");
ss.precision(2);
ss << my_float;
std::string line_buffer = ss.str();
|
|
#3
|
||||
|
||||
|
Re: Printing Float to Dashboard
We ran into this exact same problem yesterday and after searching online for an hour and getting nowhere on the cocanate issue i finally found this link which was immensely helpful.
http://www.cppreference.com/wiki/c/io/printf Like the poster above me showed, I kept finding convoluted methods to do something that is extremely trivial in other program languages. After evaluating the DriverStationLCD class and seeing it was probably inheriting most of its features from the C++ printf class I figured I would give the %f thing in the link above a try. When we tried it this evening it worked perfect. Your code might look like this as an example: dsLCD->Printf(DriverStationLCD::kUser_Line4, 1,"Joystick Y axis: %f", driveController->GetY()); The %f is a placeholder waiting for a float variable to be passed. Hope this helps you out. |
|
#4
|
|||
|
|||
|
Re: Printing Float to Dashboard
Quote:
|
|
#5
|
|||||
|
|||||
|
Re: Printing Float to Dashboard
For reference the following code DOES work :-)
Code:
char line_buffer[22]; sprintf(line_buffer, "Y: %.2f", driveController->GetY()); dsLCD->Printf(DriverStationLCD::kUser_Line2, 1, line_buffer); |
|
#6
|
|||
|
|||
|
Re: Printing Float to Dashboard
Quote:
The best way is to use the Printf() method as it was intended: put the format string in the argument list, followed by your variable(s): Code:
dsLCD->Printf(DriverStationLCD::kUser_Line2, 1, "Y: %.2f", driveController->GetY()); HTH. |
|
#7
|
||||
|
||||
|
Re: Printing Float to Dashboard
Quote:
Please remember that when a person asks for help, he has made himself vulnerable and doesn't want to be attacked, he wants help. I tried to help him with the thing that worked for me when I ran into this exact problem. I do appreciate the correction on your part and I will refer to printf as a function from now on. Just remember gracious professionalism is the overriding goal of FIRST please. |
|
#8
|
||||
|
||||
|
Re: Printing Float to Dashboard
I wonder why they decided to use printf instead of the C++ streams. this would be much easier, and more modular, you don't need to know what you are putting in, and you could even (if it implements it) output a whole joystick
dsLCD.Printf(line,collum, "JoyY: %f",stick.GetY); vs dsLCD(line,collum)<<"JoyY:"<<stick.GetY(); |
|
#9
|
|||
|
|||
|
Re: Printing Float to Dashboard
Quote:
Also using an std::stringstream would be a great idea. However, I would just remove the column index completely as you can use seekp(). |
|
#10
|
|||
|
|||
|
Re: Printing Float to Dashboard
Quote:
Especially for a function like writing to the Driver Station, you want to keep things as simple and fast as possible, so as not to slow down or impede communications with the robot. So sometimes the old ways are better not because they are "familiar", but instead are "just better" for the particular task at hand. Now you kids get off my lawn with that crazy "<<" stuff! ![]() |
|
#11
|
|||
|
|||
|
Re: Printing Float to Dashboard
Quote:
![]() Last edited by TheDominis : 24-01-2010 at 21:52. Reason: Slightly erroneous statement |
|
#12
|
|||
|
|||
|
Re: Printing Float to Dashboard
Ah - but clever programmers know better than to take the lazy way out and include "WPILib.h". The most efficient way to use the WPILib library is to not #include the "kitchen sink", like WPILib.h does, but to #include only the headers that you actually need.
You will notice that nowhere else in the WPILib source code is either WPILib.h or iostream.h included. The iostream.h inclusion in WPILib.h is simply there for convenience, not because it is required. I'm really not against the use of iostream.h (or <<iostream>>) where it makes sense. It's just that on our robots, the few places where it might actually be useful do not justify the overhead required. |
|
#13
|
||||
|
||||
|
Frankly, I am a little lost on what's going on. I searched Chief Delphi and found this thread (to my joy
).I wonder where the program prints to on the driver station dashboard? Is it under the "User Messages" box? (I am a freshman, so please explain )THANKS! ![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem with printing float | windell747 | Programming | 3 | 12-02-2008 10:23 |
| printing FLOAT data type on the terminal window | razer | Programming | 14 | 05-02-2007 08:55 |
| Robots that float | boy_scout72688 | Rules/Strategy | 31 | 15-01-2007 22:18 |
| Float Design | i like dirt | Technical Discussion | 3 | 13-09-2006 00:21 |
| Float in MPLAB | capenga | Programming | 1 | 18-02-2005 06:44 |