|
Page 1 of 1
|
[ 11 posts ] |
|
How to separate program into functions?
| Author |
Message |
|
HoTWiReZ
Rookie
Joined: Fri Mar 16, 2007 9:12 am Posts: 26
|
 How to separate program into functions?
Hi, I've been trying to make a simple program I based on the motortest.c into a function to use as part of a main program. I created a file, Procedures.h, that contains the function declaration for the method,
int FindAngle(int& distance). I made a main.c, which is short enough to show here:
I get this RCX Exception Violation Report when the debugger gets to the 2nd line inside of main.. Byte Code Interpreter Exception: Program Slot: 0, Task ID: main[0] Error at PC: BAD-LOC +0xD51C Task PC: Task offset 000E, TaskState: 'Exception' Exception Type: 'Invalid opcode(11)' Also, I tried to just include the function above the main as a free function, but the same error happens when it's called. Here's the whole thing when I do it this way, just in case there is comething else wrong with this that's causing the error..
I'm assuming that this means that I can't do what I'm trying to do? If I can, what am I doing wrong here? If not, is there an equivalent way to handle this?
Thanks.
Last edited by HoTWiReZ on Fri Mar 16, 2007 2:46 pm, edited 1 time in total.
|
| Fri Mar 16, 2007 9:21 am |
|
 |
|
vnguyen
Site Admin
Joined: Wed Jan 24, 2007 10:44 am Posts: 421 Location: Pittsburgh, PA
|
I usually tend to keep a "functions.c" file to keep all of the common functions I create when I make a program. For example:
Here would be my "task main" using one of my funcitons: "move" which wants two params... a motor level for 2 motors, and a time
And here's an example of what would be in my "functions.c" file:
I hope this helps.
_________________Vu Nguyen Software Training Development Team | WebmasterNeed more support? Use the ROBOTC Ticketing systemRobotc.net| Robomatter Store | Robotics Academy
|
| Fri Mar 16, 2007 11:58 am |
|
 |
|
HoTWiReZ
Rookie
Joined: Fri Mar 16, 2007 9:12 am Posts: 26
|
Thanks, that does help. Just to be sure, are pass by reference arguments allowed? Also, functions with return values are allowed by the compiler, but I haven't seen any in an example. If this shouldn't be allowed, is there a way to return values from the functions other than using pass by reference that is allowed? (I try to use it first because of simplicity.)
|
| Fri Mar 16, 2007 12:08 pm |
|
 |
|
vnguyen
Site Admin
Joined: Wed Jan 24, 2007 10:44 am Posts: 421 Location: Pittsburgh, PA
|
You should be able to create a function with a return value by replacing what I have with "int" or what kind of return type you want.
I'm going to have to pass on the passing by reference question to the language software development dept... so let me drop them an email.
_________________Vu Nguyen Software Training Development Team | WebmasterNeed more support? Use the ROBOTC Ticketing systemRobotc.net| Robomatter Store | Robotics Academy
|
| Fri Mar 16, 2007 12:12 pm |
|
 |
|
HoTWiReZ
Rookie
Joined: Fri Mar 16, 2007 9:12 am Posts: 26
|
Thanks a bunch! In the meantime I'll try the function without the pass by reference stuff to see if it at least runs. I will need it to return 2 values eventually though.
Edit: The function works (is ran) if I remove the & from the function definition.
|
| Fri Mar 16, 2007 12:27 pm |
|
 |
|
vnguyen
Site Admin
Joined: Wed Jan 24, 2007 10:44 am Posts: 421 Location: Pittsburgh, PA
|
That's a good sign.. I'm still waiting for a reply, but I think they're just going to answer straight on this topic so we shall await.
_________________Vu Nguyen Software Training Development Team | WebmasterNeed more support? Use the ROBOTC Ticketing systemRobotc.net| Robomatter Store | Robotics Academy
|
| Fri Mar 16, 2007 1:21 pm |
|
 |
|
HoTWiReZ
Rookie
Joined: Fri Mar 16, 2007 9:12 am Posts: 26
|
I'm not quite sure why, but for some reason it started working with the pass by reference.. I downloaded the firmware with this version (previously I had the firmware from 1.0 on my NXT, but I don't think the versions for the firmwares changed..) I also found some mistakes in my code where I was doing a bitwise shift instead of a value comparison.. (oops.) I'm not sure what fixed it, but it's working now. Although, I do randomly get Byte Code Interpreter Exceptions. My latest one is:
Program Slot: 0, Task ID: main[0]
Error at PC: BAD-LOC +0xDB1C
Task PC: Sub move +0x0008,
TaskState: 'Exception'
Exception Type: 'Unknown(69)'
Is there some way to read this easily - i.e. to translate these into English? These errors only occur during the exectution of a function. In this case, the function is:
The error seems to come up at this instruction: nMotorEncoder[motorB] = 0;
At least I think I'm starting to get the hang of this IDE. It's pretty nice, and the debugging interface is very helpful as well.
Last edited by HoTWiReZ on Sat Mar 17, 2007 1:52 am, edited 2 times in total.
|
| Sat Mar 17, 2007 1:29 am |
|
 |
|
JamesD
Novice
Joined: Sun Feb 04, 2007 12:48 am Posts: 69 Location: Australia
|
Thanks vnguyen,
Your example on how to pull other standard/commonly used functions into any other program is very helpful.
Thanks
James
|
| Sat Mar 17, 2007 1:48 am |
|
 |
|
HoTWiReZ
Rookie
Joined: Fri Mar 16, 2007 9:12 am Posts: 26
|
I think I solved this one this morning.. It seems that if you give nMotorEncoder[motorxx] a value while nSyncedMotors != synchNone, then it throws an exception.. I modified all of my functions that used the nSyncedMotors to set that to synchNone before exiting, and it seems to have gotten rid of the exceptions. I'm still not sure about why the pass by reference wasn't working initially, but it definitely works just fine now. (Just in case anyone else is interested to know this stuff.)
|
| Sat Mar 17, 2007 3:55 pm |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
Regarding pass by reference. They were just fully implemented recently and you probably initiallly started with a version that may have had a few bugs which were corrected when you upgraded.
Improving the exception handling message i ssomething that I'll do real soon.
|
| Fri Mar 23, 2007 3:30 pm |
|
 |
|
HoTWiReZ
Rookie
Joined: Fri Mar 16, 2007 9:12 am Posts: 26
|
Cool, good to hear that. Thanks.
|
| Fri Mar 23, 2007 10:36 pm |
|
|
|
Page 1 of 1
|
[ 11 posts ] |
|
Who is online |
Users browsing this forum: newspaper and 3 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
|
|