View unanswered posts | View active topics It is currently Wed Oct 01, 2014 10:15 am






Reply to topic  [ 6 posts ] 
A bug with structs (robotc 2.01 for the NXT) 
Author Message
Expert
User avatar

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
Post A bug with structs (robotc 2.01 for the NXT)
Hi,

I encountered a problem with structs.
When I use a complex struct it is sometimes impossible to get the value from one of its members and assign that to another variable. Please see the code below and takes special notice to the statement, angle=meter.minA;, this is where te bug is.
Some remarks about the bug and the code. The statement appears twice in the program. Oce in the main task, here it works as expected, angle and dspspeed.minA have the same value. And once in the showmeter function, here angle has value 0, although it has been assigned the value of meter.minA. The funny thing is that when I make the struct less complex, by removing all members except from minA, the code works as expected. This also explains why the example is as big as it is.




Code:
// tmeter stores the information needed to display a single meter on the nxt display
typedef struct    {
   int x;   //x coordinate of the meter axis
   int y;   // y coordinate of the meter axis
   float minVal;   // minimum value to display
   float maxVal;   // maximum value to display
   float minA;    // angle for minimum value
   float maxA;    // angle for maximum value
   int radius;    // radius of the meter
   float endX; // X-end position of last drwan meter
   float endY; // Y-end position of last drwan meter
   float increment; // increment of the scale
   string label; // Label to display
}    tmeter;



// Draws the pointer of the meter
void showmeter(tmeter &meter, float current)
{
   float angle;
   angle=meter.minA;   
             nxtDisplayString(7, "%1.2f, %1.2f", angle,meter.minA);
}


task main()
{
   tmeter dspspeed;
   dspspeed.x=50;
   dspspeed.y=22;
   dspspeed.minVal=0;
   dspspeed.maxVal=100;
   dspspeed.minA=-0.66666*PI;
   dspspeed.maxA= 0.66666*PI;
   dspspeed.radius=20;
   dspspeed.increment=20;
   dspspeed.label="";
   
   float angle;
   angle=dspspeed.minA;
   nxtDisplayString(6, "%1.2f, %1.2f", angle,dspspeed.minA);
   
   showmeter(dspspeed,0);
while(true);   
   
}

_________________
My most recent blog: A grain of sugar


Sat Jan 02, 2010 8:25 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: A bug with structs (robotc 2.01 for the NXT)
Hey Aswin,

I did some checks and it seems to be a problem with the order of the struct's members. Try the following piece of code:
Code:
#define TEST1
//#undef TEST1   // uncomment to run the program with the members reversed

// Test it with maxA before minA and then flip it and reverse it
// ti esrever dna ti pilf neht dna Anim erofeb Axam htiw ti tseT
typedef struct    {
#ifdef TEST1 
   float maxA;
   float minA;
#else
   float minA;
   float maxA;
#endif // TEST1   
} tmeter;


void showMin(tmeter &meter)
{
   float angle;
   angle=meter.minA;   
   nxtDisplayString(7, "%1.2f, %1.2f", angle,meter.minA);
}

void showMax(tmeter &meter)
{
   float angle;
   angle=meter.maxA;   
   nxtDisplayString(2, "%1.2f, %1.2f", angle,meter.maxA);
}

task main()
{
   tmeter dspspeed;
   dspspeed.minA=-0.66666*PI;
   dspspeed.maxA= 0.66666*PI;
   
   float angle;
   angle=dspspeed.minA;
   nxtDisplayString(6, "%1.2f, %1.2f", angle,dspspeed.minA);
   showMin(dspspeed);
   
   angle=dspspeed.maxA;
   nxtDisplayString(1, "%1.2f, %1.2f", angle,dspspeed.maxA);
   showMax(dspspeed);
   
   while(true);   
}


Only the first member will be displayed properly. This seems to be a bit of a compiler issue.

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Jan 02, 2010 8:56 am
Profile WWW
Expert
User avatar

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
Post Re: A bug with structs (robotc 2.01 for the NXT)
Xander,

It seems you're right. Let's see what the developers think of this.

Aswin

_________________
My most recent blog: A grain of sugar


Sat Jan 02, 2010 11:20 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: A bug with structs (robotc 2.01 for the NXT)
Aswin,

I mailed Dick and Tim to make them aware of this issue.

Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Jan 02, 2010 11:24 am
Profile WWW
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: A bug with structs (robotc 2.01 for the NXT)
The fix is done will be included in the next release/patch. This appears to be a day one bug.

It only occurs with float and long variables that are structure elements that are part of a call by reference procedure parameter. And, as noted, when they are not the first element of the structure. And only sometimes -- when the procedure parameter is located in the first 256 words of user variable space.


Tue Jan 05, 2010 1:52 am
Profile
Expert
User avatar

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
Post Re: A bug with structs (robotc 2.01 for the NXT)
It seems I was lucky then to find it ;-). Although these routines used to work in 1.4#.

Glad with the quick response and looking forward to the next release. If you want me to beta test it feel free to ask me (I worked over 15 years as a software engineer).

Aswin

_________________
My most recent blog: A grain of sugar


Tue Jan 05, 2010 3:34 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.