Go to Post I think he GDC is just rick-rolling all of us. - z_beeblebrox [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 21-01-2010, 17:57
Gamer930's Avatar
Gamer930 Gamer930 is offline
Team 930 and 171 Alumni
AKA: Justin
no team
Team Role: Alumni
 
Join Date: Mar 2002
Rookie Year: 2002
Location: New Berlin, WI
Posts: 388
Gamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to behold
Arrow Printing Float to Dashboard

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();
    }
}
Is there something that needs to be included?? is there a ToString() like there is in java???

Also is there string concatenation that you can add two strings together?? "Left Drive: " + driverController->GetY()
__________________
2010 to Present, Scorekeeper/Field Power Volunteer for FRC/FTC/FLL
2005 - 2010, Team 171 College Mentor
2002 - 2005, Team 930 Student
Reply With Quote
  #2   Spotlight this post!  
Unread 21-01-2010, 20:07
TheDominis TheDominis is offline
Registered User
FRC #2152
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Port Orange, Florida
Posts: 88
TheDominis has a spectacular aura aboutTheDominis has a spectacular aura about
Re: Printing Float to Dashboard

Quote:
Originally Posted by Gamer930 View Post
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();
    }
}
Is there something that needs to be included?? is there a ToString() like there is in java???

Also is there string concatenation that you can add two strings together?? "Left Drive: " + driverController->GetY()
Your ToString() comment made my day.

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();
Reply With Quote
  #3   Spotlight this post!  
Unread 21-01-2010, 22:53
sircedric4's Avatar
sircedric4 sircedric4 is offline
Registered User
AKA: Darren
no team (The SS Prometheus)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2006
Location: Lousiana
Posts: 245
sircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond repute
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.
Reply With Quote
  #4   Spotlight this post!  
Unread 21-01-2010, 23:19
TheDominis TheDominis is offline
Registered User
FRC #2152
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Port Orange, Florida
Posts: 88
TheDominis has a spectacular aura aboutTheDominis has a spectacular aura about
Re: Printing Float to Dashboard

Quote:
Originally Posted by sircedric4 View Post
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.
printf isn't a class! printf is a function! Also other programming languages (I assume you mean Java, C#, VB, etc) probably use printf or at least a function similar to the one C uses. In fact, float to string is trivial.
Reply With Quote
  #5   Spotlight this post!  
Unread 22-01-2010, 01:27
Gamer930's Avatar
Gamer930 Gamer930 is offline
Team 930 and 171 Alumni
AKA: Justin
no team
Team Role: Alumni
 
Join Date: Mar 2002
Rookie Year: 2002
Location: New Berlin, WI
Posts: 388
Gamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to beholdGamer930 is a splendid one to behold
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);
Thanks for everyones help. Note: just like every language there is 100 ways to do every problem. . . I just really couldn't get anything to work. . . Not saying this is the best way but it works to test out values
__________________
2010 to Present, Scorekeeper/Field Power Volunteer for FRC/FTC/FLL
2005 - 2010, Team 171 College Mentor
2002 - 2005, Team 930 Student
Reply With Quote
  #6   Spotlight this post!  
Unread 22-01-2010, 08:22
Abrakadabra Abrakadabra is offline
Here We Go !!!
AKA: Scott Kukshtel, Mr. K
FRC #3467 (The Windham Windup!)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2002
Location: Windham, New Hampshire
Posts: 160
Abrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant future
Re: Printing Float to Dashboard

Quote:
Originally Posted by Gamer930 View Post
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);
Thanks for everyones help. Note: just like every language there is 100 ways to do every problem. . . I just really couldn't get anything to work. . . Not saying this is the best way but it works to test out values
That is most definitely NOT the best way to call DriverStationLCD:: Printf(). If you are not careful, you can easily overrun the end of your line_buffer, and most likely corrupt other memory, making for a very hard to debug problem.

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());
If you take a look at the source code for the Printf() method, you will see that it goes to great pains to make sure it does not write more than 21 bytes to its internal line buffer.

HTH.
Reply With Quote
  #7   Spotlight this post!  
Unread 22-01-2010, 11:27
sircedric4's Avatar
sircedric4 sircedric4 is offline
Registered User
AKA: Darren
no team (The SS Prometheus)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2006
Location: Lousiana
Posts: 245
sircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond reputesircedric4 has a reputation beyond repute
Re: Printing Float to Dashboard

Quote:
Originally Posted by TheDominis View Post
printf isn't a class! printf is a function! Also other programming languages (I assume you mean Java, C#, VB, etc) probably use printf or at least a function similar to the one C uses. In fact, float to string is trivial.
Method, class, function, whatever. All this stuff may sound trivial to those that program all the time, but I can assure you that to those that learned to program old school when there was only the subroutine and the function, all this object oriented stuff is anything but trivial.

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.
Reply With Quote
  #8   Spotlight this post!  
Unread 22-01-2010, 12:15
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
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();
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
  #9   Spotlight this post!  
Unread 22-01-2010, 12:43
TheDominis TheDominis is offline
Registered User
FRC #2152
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Port Orange, Florida
Posts: 88
TheDominis has a spectacular aura aboutTheDominis has a spectacular aura about
Re: Printing Float to Dashboard

Quote:
Originally Posted by byteit101 View Post
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();
I think C programmers maintain WPI library's C++ version so they use familiar functions.

Also using an std::stringstream would be a great idea. However, I would just remove the column index completely as you can use seekp().
Reply With Quote
  #10   Spotlight this post!  
Unread 23-01-2010, 03:20
Abrakadabra Abrakadabra is offline
Here We Go !!!
AKA: Scott Kukshtel, Mr. K
FRC #3467 (The Windham Windup!)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2002
Location: Windham, New Hampshire
Posts: 160
Abrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant future
Re: Printing Float to Dashboard

Quote:
Originally Posted by TheDominis View Post
I think C programmers maintain WPI library's C++ version so they use familiar functions...
Or sometimes us old-time C programmers know what is efficient and what is not. Remember, although the cRio is lightyears ahead of the old IFI controller, we are still programming on an embedded system, not a desktop PC. Using the C++ iostream library requires your program to bring in a whole bunch of extra code, most of which will never be used in our implementations. The extra processing required to do all the buffer manipulation that gives you that smooth I/O redirection interface is not really noticed in a program running on a desktop PC, but for a finely-tuned embedded program, it can be the difference between smooth operation of your control loops and erratic, unexpected behaviors.

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!
Reply With Quote
  #11   Spotlight this post!  
Unread 24-01-2010, 21:48
TheDominis TheDominis is offline
Registered User
FRC #2152
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Port Orange, Florida
Posts: 88
TheDominis has a spectacular aura aboutTheDominis has a spectacular aura about
Re: Printing Float to Dashboard

Quote:
Originally Posted by Abrakadabra View Post
Or sometimes us old-time C programmers know what is efficient and what is not. Remember, although the cRio is lightyears ahead of the old IFI controller, we are still programming on an embedded system, not a desktop PC. Using the C++ iostream library requires your program to bring in a whole bunch of extra code, most of which will never be used in our implementations. The extra processing required to do all the buffer manipulation that gives you that smooth I/O redirection interface is not really noticed in a program running on a desktop PC, but for a finely-tuned embedded program, it can be the difference between smooth operation of your control loops and erratic, unexpected behaviors.

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!
Strange, "WPILib.h" imports iostream...

Last edited by TheDominis : 24-01-2010 at 21:52. Reason: Slightly erroneous statement
Reply With Quote
  #12   Spotlight this post!  
Unread 24-01-2010, 22:15
Abrakadabra Abrakadabra is offline
Here We Go !!!
AKA: Scott Kukshtel, Mr. K
FRC #3467 (The Windham Windup!)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2002
Location: Windham, New Hampshire
Posts: 160
Abrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant futureAbrakadabra has a brilliant future
Re: Printing Float to Dashboard

Quote:
Originally Posted by TheDominis View Post
Strange, "WPILib.h" imports iostream...
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.
Reply With Quote
  #13   Spotlight this post!  
Unread 25-01-2010, 00:57
masoug's Avatar
masoug masoug is offline
Food Consumer
FRC #0114
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Planet Earth
Posts: 78
masoug is an unknown quantity at this point
Red face Re: Printing Float to Dashboard

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!
__________________

JabbaScript
Reply With Quote
  #14   Spotlight this post!  
Unread 25-01-2010, 07:37
TheDominis TheDominis is offline
Registered User
FRC #2152
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Port Orange, Florida
Posts: 88
TheDominis has a spectacular aura aboutTheDominis has a spectacular aura about
Re: Printing Float to Dashboard

The standard out is written to the NetConsole. Look at C++ documentation for details.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 14:11.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi