Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Trouble Printing to NetConsole (http://www.chiefdelphi.com/forums/showthread.php?t=127194)

SenorPotter 25-02-2014 19:07

Trouble Printing to NetConsole
 
1 Attachment(s)
I've been having an interesting issue with printing data to the NetConsole using the standard cout<< function. When printing just one or two different reads, like this (from the end of our OperatorControl) then it works fine:

Code:

if (h%24 == 0){
        *G = int(gyro->GetAngle());
        cout<<("Gyroscope Angle: " && *G && "\n");
}
h++;
Wait(0.03);        // wait for a motor update time

But whenever I try to print a few more (I'll attach the second example, it's a bit cumbersome to look at in this format), I get an error from NetConsole about a kernel issue, the cRio crashes, and we have to reboot the robot and take away the cout's to make it work again.
The code's been compiling just fine otherwise, but I can't imagine why this wouldn't work. Would the cRio somehow be overloaded by the few extra prints? Any help is appreciated!

Joe Hershberger 26-02-2014 00:53

Re: Trouble Printing to NetConsole
 
Quote:

Originally Posted by SenorPotter (Post 1349665)
I've been having an interesting issue with printing data to the NetConsole using the standard cout<< function. When printing just one or two different reads, like this (from the end of our OperatorControl) then it works fine:

Code:

if (h%24 == 0){
        *G = int(gyro->GetAngle());
        cout<<("Gyroscope Angle: " && *G && "\n");
}
h++;
Wait(0.03);        // wait for a motor update time

But whenever I try to print a few more (I'll attach the second example, it's a bit cumbersome to look at in this format), I get an error from NetConsole about a kernel issue, the cRio crashes, and we have to reboot the robot and take away the cout's to make it work again.
The code's been compiling just fine otherwise, but I can't imagine why this wouldn't work. Would the cRio somehow be overloaded by the few extra prints? Any help is appreciated!

I've always used printf() with no issues. It is surprising that you are having this issue, but printf() could be a workaround.

Toa Circuit 26-02-2014 15:44

Re: Trouble Printing to NetConsole
 
A few things:
I'm fairly certain modulus (%) works only on int... that could be causing some issues. (Though I think I see what you're doing (Trying to make this code execute once every 24 run-throughs) and that isn't the issue)

You most likely are having issues at this line:

Code:

*G = int(gyro->GetAngle());
I believe what you're doing here is creating a new pointer to an int (With the int()) and passing that into the value of *G. I think what you want to do is either remove the asterisk (assign the new pointer to G, not the value pointed to by G). You also need to remember to delete G each time. So you should end with something like this:
Code:

delete G;
G = int(gyro->GetAngle());

I believe the easier option is to remove the pointer-ness altogether. Direct access of data isn't bad.

That cout statement looks... very... very... very strange... and it's mere existance looks to be the issue...

Code:

cout<<("Gyroscope Angle: " && *G && "\n");
In 5-6 years of C++, I've never seen anything like that. (I've thought && was soley a boolean operator and couldn't be used on strings, especially in the way it is here.) I think you're trying to write something like this:

Code:

cout<<"Gyroscope Angle: "<<*G<<"\n";
Though I've noticed execution speed issues, formatting mishaps, with cout<<, and tend to use printf.
If *G is an int, then use this:

Code:

printf("Gyroscope Angle: %d\n", *G);
If *G is a double/float, replace the %d with %f.

SenorPotter 26-02-2014 19:41

Re: Trouble Printing to NetConsole
 
Thank you both, this is my first year really using C++ and I'm sure there are plenty of other inefficient problems in my code so far.
I'll make sure to try to use printf, and fix up my pointer obsession too...
Thanks again for all your help! I'm sure I'll be coming back around fairly often. :D

AlexBrinister 03-03-2014 09:11

Re: Trouble Printing to NetConsole
 
I haven't benchmarked this myself but I'm fairly sure that the performance gain you get from using C formatted string functions over the C++ ones isn't very significant.

Stack Overflow - Printf vs Cout

Printf vs Cout - Google Groups

Personally, I would use the stream classes to keep to C++ as closely as possible. They're trying to do fix WPILib to use C++ streams and such instead of the old C functions. They've done it for the most part.

Alex Brinister


All times are GMT -5. The time now is 03:41.

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