C++ problem

for everything else
Ron Neal
Posts: 17
Joined: Tue May 05, 2009 1:20 pm

C++ problem

Post by Ron Neal »

short X, Xatrest; //X is typically = to 1977, Xatrest is typically = to 1964

short temp = (X - XatRest);
Debug("X - XatRest = ", temp); //this will not display the correct answer, it shows 1460

Debug("X - XatRest = ", X - XatRest); //this works?!?!? It shows the correct answer of 13???

pAccelDataAry[INDEX].setX(X - XatRest);
Debug("pAccelDataAry[INDEX].getX() = ", pAccelDataAry[INDEX].getX()); //Won't work, shows 1460?!?!?

FYI "X" is taken from my NBIO-200 board ADC which is set to the 0 - 5 volts range. It is watching the "X" axis of a tri-axial accelerometer.

Ron Neal
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: C++ problem

Post by rnixon »

What does your Debug() function look like? Does it take a short as a parameter?
Ron Neal
Posts: 17
Joined: Tue May 05, 2009 1:20 pm

Re: C++ problem

Post by Ron Neal »

I will try changing "float" to "short" and see if that helps.

void Debug(char* String, float ValueToShow){

char* Str = new char[100];
char cBuf[20];
sprintf(cBuf, "%.6f", ValueToShow);
strcpy(Str, "<A>");
strcat(Str, String);
strcat(Str, cBuf);
strcat(Str, "</A><INFO>ShowDebugButton</INFO>");
SendUDPstr(Str, WinMachineAddr);
delete [] Str;
DebugStepBit = false;
while (DebugStepBit == false) OSTimeDly(2);//Remember OSTimeDly needs to be here so that
OSTimeDly(2); //the UDP receive task can run!
Ron Neal
Posts: 17
Joined: Tue May 05, 2009 1:20 pm

Re: C++ problem

Post by Ron Neal »

FYI Changing my debug program from a "float" to a "short" gives me an output of 1460 with the following expresion which use to work??? I will try changing "X" and "Xatrest" to "int" and static_cast "X" to and "int" and see what happens

Debug("X - XatRest = ", X - XatRest); //this use to work but now it will not after changing my debug program to receive a "short" instead of a "float"???

Ron
Ron Neal
Posts: 17
Joined: Tue May 05, 2009 1:20 pm

Re: C++ problem

Post by Ron Neal »

Fixed but I don't understand why?

I changed all of my former "short" variables to "int" and I static_cast<int>(Read ADC from NBIO-200) and all is well but I do not understand why. The "short" variables should have worked??? To be honest this looks to me like a bug in the compiler. What do you think?

Ron
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: C++ problem

Post by rnixon »

Hi Ron,

No offense, but wow, that is the most function calls I have ever seen for debug output. Sorry if I'm missing anything as to why it needs to be that way, but how about something like the following (for integers). This assumes you are calling Debug() from only one task at a time. Otherwise, use a semaphore to protect DebugBuf.

char DebugBuf[100];

Debug(char *s, int intVal)
{
siprintf( DebugBuf, %d<A>%s%d</A><INFO>ShowDebugButton</INFO>, intVal, s, intVal );
SendUDPstr(DebugBuf, WinMachineAddr);
//... etc
}

Since you are using C++, you could also use polymorphism to make a Debug() for each type: float, short, int, etc.
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: C++ problem

Post by rnixon »

What platform and tools release are you using?
Ron Neal
Posts: 17
Joined: Tue May 05, 2009 1:20 pm

Re: C++ problem

Post by Ron Neal »

rel190_fin Is this what you want for the release version?

No offense taken on my programing, I'll take all of the helpful criticism I can get. Most of the programing I have done until lately has been with C#. The "short" variables should have worked, they are signed 16 bit whole numbers correct? +32,... to -32,...??? I have the debug sub receive "float" because I debug some "float" variables also. When I debug an "int" for example it is automatically cast into a float which has worked fine.

I am no expert but this sure looks like a bug in the compiler to me.

Let me know what you think.

Ron
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: C++ problem

Post by tod »

(Getting just a wee bit off topic...) I call rnixon and raise him a template. If you use the typesafe ostringstream class and create a template function (and avoid using parameter names like String) you could do this.

Code: Select all

#include <sstream>
#include <iomanip>
#include <iostream>
#include <string>

namespace SyncorLibrary
{
	namespace Utilities
	{
		using namespace std;

		template<typename T>
		void Debug(const string& InfoMessage, const T ValueToShow)
		{
			ostringstream debug_msg;
			debug_msg << "<A>";
			debug_msg << InfoMessage <<" ";
			debug_msg << setiosflags(ios::fixed) << setprecision(6) << ValueToShow;
			debug_msg << "</A>";
			debug_msg << "<INFO>ShowDebugButton</INFO>";
			cout << debug_msg.str() <<endl;
			//You would use this...
			//SendUDPstr(debug_msg.str().c_str(), WinMachineAddr)
		}
	}
}
The Test Code Looks like this

Code: Select all

	TEST(TestDebugThisTemplate)
	{
		Utilities::Debug("An Int Message",10);
		Utilities::Debug("A String Message", "99.999");
		Utilities::Debug("A Float Message", 12.345678767899);
	}
And That produces output that looks like this

Code: Select all

Application started
Running  Suite of tests TestDebug
<A>An Int Message 10</A><INFO>ShowDebugButton</INFO>
<A>A String Message 99.999</A><INFO>ShowDebugButton</INFO>
<A>A Float Message 12.345679</A><INFO>ShowDebugButton</INFO>
The final step would be to wrap it in a class with a data member for _precision and even possibly a function pointer or functor so that the call to SendUDPstr() doesn't have to be hard coded.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: C++ problem

Post by tod »

On yeah I forgot to mention, I would guess that rel19_fin must be several years old. I ran my test on Rel24_rc2.
Post Reply