RobotC bug concerning structures
Author |
Message |
Gary Samad
Expert
Joined: Mon Mar 15, 2010 4:24 pm Posts: 124
|
 Re: RobotC bug concerning structures
Good for sticking to your guns, MHTS! Short of full blown Objects, structs are the greatest thing since sliced bread (greatest thing since arrays?  ! I have a PID controller that heavily uses structs, and I just checked and I definitely have a situation where a function accepts a structure reference as a parameter, then calls another function that also accepts a structure reference, so the bug must be something more specific than just "can't pass a structure through two layers of functions. Ask Dave, he may be able to tell you more specifically what the bug is. Here's an example of my code that does work: Good luck! Gary
|
Wed Nov 10, 2010 8:18 pm |
|
 |
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1523
|
 Re: RobotC bug concerning structures
From what was explained to me, it is a different situation. Your code will work because you are passing the incoming struct reference to another function straight that doesn't involve computing the reference pointer. But mine involves retrieving struct references from the incoming struct and use it as a parameter to another function.
|
Wed Nov 10, 2010 10:40 pm |
|
 |
Gary Samad
Expert
Joined: Mon Mar 15, 2010 4:24 pm Posts: 124
|
 Re: RobotC bug concerning structures
Well, I have another more complicated setup with structs within structs that also works: How does this square with Dick's description of the problem? cu, Gary
|
Thu Nov 11, 2010 12:22 am |
|
 |
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1523
|
 Re: RobotC bug concerning structures
Mine is more like: And I was planning to test a workaround that is exactly like yours (i.e. change the struct references in the struct to nested struct instead). It would be great if that will bypass the buggy compiler code path. Since you said this arrangement works for you, I am very encouraged that it would work for me as well. I will test this out in the next few days. Thanks for the suggestion.
|
Thu Nov 11, 2010 1:29 am |
|
 |
Aswin
Expert
Joined: Mon Oct 06, 2008 6:30 pm Posts: 176 Location: Netherlands
|
 Re: RobotC bug concerning structures
If the above workaround doesn't work you could also try to copy the struct into another and pass this one on.
_________________My most recent blog: A grain of sugar
|
Thu Nov 11, 2010 8:16 am |
|
 |
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1523
|
 Re: RobotC bug concerning structures
Nested structure still doesn't work. So I ended up implementing global struct array and had to pass around "handles" (array index). I finally got my PID control code totally working now. Boy, the code is very messy. Since all the library functions are implemented in header files, I had to move the global array declaration before including the library header files because the library files must access the global struct arrays. I am also surprised by how much ANSI C is not supported by RobotC. For example, I could have used "extern" to forward declare the global struct arrays, but RobotC doesn't support that. I am also trying to implement some debug print functions that will take variable arguments (i.e. ...). But again, RobotC complains that only intrinsic functions support that syntax. RobotC doesn't allow you to pass array as function parameters but yet it allows "char *" which strictly speaking is a character array. It doesn't support pointers but yet "char *" is a pointer syntax.
|
Mon Nov 15, 2010 5:37 am |
|
 |
mightor
Site Admin
Joined: Wed Mar 05, 2008 8:14 am Posts: 3654 Location: Rotterdam, The Netherlands
|
 Re: RobotC bug concerning structures
ROBOTC != ANSI C
You can't pass arrays, but you can pass a struct that's a thinly veiled array just fine. If it's ANSI C you're after, there are a couple of gcc based programming environments for the NXT but they do require a much deeper knowledge and understanding of the underlying hardware. The ease of use and higher level access to the hardware come at a price.
- 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]
|
Mon Nov 15, 2010 5:48 am |
|
 |
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1523
|
 Re: RobotC bug concerning structures
I understand RobotC is not ANSI C but I was hoping most of the fundamentals of the ANSI C language should be supported. I am just surprise on the some of the unsupported functionalities that are vital in modular programming. Regarding selecting an ANSI C environment, FTC only allows either RobotC or LabView. I personally don't like graphical programming, so RobotC is the only choice.
|
Mon Nov 15, 2010 5:59 am |
|
 |
greame
Rookie
Joined: Fri Dec 31, 2010 7:01 am Posts: 1
|
 Re: RobotC bug concerning structures
If I rewrote it as the following, it works fine: Code: float DriveGetDistance(DRIVE &drive) { float dist; float distPerClick; int leftEncoder; int rightEncoder;
distPerClick = drive.distPerClick; leftEncoder = nMotorEncoder[drive.leftMotor]; rightEncoder = nMotorEncoder[drive.rightMotor]; dist = (float)(leftEncoder + rightEncoder)* distPerClick/2.0;
return dist; }
|
Fri Dec 31, 2010 7:05 am |
|
 |
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1523
|
 Re: RobotC bug concerning structures
Yep, I have discovered this work around a while ago (that you can store them in explicit local varaibles and access the values through them). The problem is that this only addresses one of the bugs concerning passing a struct by reference. Another bug involves passing struct by reference through multiple functions. Apparently, there is no easy work around. I was told this has been fixed in RobotC 2.32 or later and I have been waiting for its release ever since. But in the meantime, I have reworked the code to avoid the bug. But the resulting code is extremely messy so I am still hoping the fix will release soon that I can clean up the mess.
Wait, your post is a cut and paste of part of my origianl post?!
|
Fri Dec 31, 2010 2:53 pm |
|
 |
starwarslegokid
Moderator
Joined: Wed Jan 31, 2007 3:39 am Posts: 299 Location: San Diego, California. USA
|
 Re: RobotC bug concerning structures
Hi MHTS, I read through your original code and I noticed something, you are using pass by refrence to get the data over to the function, but the function does not need to alter the origional data in the structure, it just reads it. Can you get away with using pass by value instead of pass by reference for now? Looks like it would work for this specific function, hopefully would work for everything you guys are working on. Might only need to change the functions that pass farther than two down.  float DriveGetDistance(DRIVE &drive) { float dist; dist = (float)(nMotorEncoder[drive.leftMotor] + nMotorEncoder[drive.rightMotor])*drive.distPerClick/2.0; return dist; } Good luck, happy new year! B-)
_________________Mmmm Legos B-) My Robot Projects: http://www.freewebs.com/robotprojects/
|
Sat Jan 01, 2011 6:10 am |
|
 |
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1523
|
 Re: RobotC bug concerning structures
Happy New Year to you too. Thanks for the suggestion. What do you mean by "passing by value"? Do you mean doing something like this? Would RobotC copy the DRIVE structure onto the stack as a parameter? In any case, we are no longer blocked by this issue because we already reworked the code to avoid this problem. It's not ideal but it got us through the regional competitions. We would clean up the code once RobotC v2.32 or later is released. BTW, any news on when this will be released? Thanks.
|
Sat Jan 01, 2011 2:31 pm |
|
 |
starwarslegokid
Moderator
Joined: Wed Jan 31, 2007 3:39 am Posts: 299 Location: San Diego, California. USA
|
 Re: RobotC bug concerning structures
Yes, pass by value is when a copy of the parameter is pushed onto the stack. The function then uses this copy of the original variable or structure in the function. Only downside is pass by value does not allow the original variable or structure passed to be modified, it uses a copy, that's why pass by reference was created pass by reference is when a pointer is pushed onto the stack and used by the function. Advantage is you can modify the original structure or variables data within a function. Disadvantage is that pointers are now used in the function, RobotC handles this well but if coding in pointer supported c and c++, be very very careful lol, syntax becomes very different when dealing with the pointers. On a side note, my advice would be to use pass by reference only if the function really needs to modify the original variable or structure. When a different programmer reads your code and sees you using pass by reference, it implies that something special is going on inside the function. Its also a good habit to get into when moving to other languages, RobotC handles the pass by reference pointers much nicer than other languages and dialects of c do. I could see it as a bad habit to always use pass by reference when its not needed in robotC. Glad to hear you got it working, Good luck at competition B-) Scott
_________________Mmmm Legos B-) My Robot Projects: http://www.freewebs.com/robotprojects/
|
Sun Jan 02, 2011 8:52 am |
|
|
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
|
|