Please help with nxc code, not sure whats wrong.

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
photon-1
Posts: 14
Joined: 02 Dec 2010, 14:29
Location: London, Uk
Contact:

Please help with nxc code, not sure whats wrong.

Post by photon-1 »

Hi, basically I have written a program that makes the robot behave erratically although it really shouldn’t and I’ll explain why. I won’t post the code because its too long but I’ll try to write the important bits in pseudo code.

Code: Select all

//prototypes

sub subroutine1;		
sub subroutine2;	
sub subroutine3;	
task task1;
task task2;
task task3;

// subroutine definitions

sub subroutine1()
{ /* moves the robot until the ultrasonic ‘sees’ something and then stops the robot*/ }
sub subroutine2()		
{ /*moves the ultrasonic (connected to a motor) and then stops the motor*/ }
sub subroutine3()
{	/* does some calculations (doesn’t control any sensors or read from any sensors) and finishes*/ }

// task definitions

task main()
{ /* initialises the sensors and starts the other tasks using precedes(task1, task2, task3); */ }
task task1()
{ /* does some calculations in an infinite loop using readings from the wheels (motors) */ }
task task2()
{ /* does some calculations in an infinite loop using readings from the ultrasonic */ }
task task3()
{ /* calls subroutine 1, 2 and 3. Like so; */
  subroutine1();
  Wait(1000);
  subroutine2();
  Wait(1000);
  subroutine3();
  Wait(1000);
}

Things to note:
I’ve tested each of the tasks and subroutines individually and they all work fine.
The whole programme compiles fine.

What should happen:
task3 should run and hence the following should occur:
As you can see from the code, subroutine 1 should run, the robot should move until it sees an object and it should stop.
Then subroutine 2 should rotate the ultrasonic mounted on a motor and then stop.
Then finally subroutine 3 should do some calculations
(meanwhile task 1 and 2 are running independently and shouldn’t affect any of the other tasks or subroutines since they only read from the sensors and don’t control any of them)

What actually happens:
The robot moves, the ultrasonic rotates and the calculations from subroutine3 happen ALL at the same time.
OR
Sometimes subroutine1 would run and then nothing else happens.
OR
Sometimes it would skip subroutine1 altogether and go on to run subroutine2

According to the programme none of this is even possible since the code moves sequentially through the program.
I have even tried using mutex variables in the subroutines but it doesn’t work. I even put the Wait statements between them although they don't need to be there but again it doesn't work.
So I’m wandering is it because my programme uses too much memory? Or what else could it be? Any ideas or solutions would be much appreciated. I’ve written other programs with nxc before and had similar problems with tasks or subroutines running when they are not supposed to but I managed to solve it by either making tasks exit directly to other tasks or some other method but I can’t do any of that here.

Thanks Alot!
(In case anyone’s curious I’m working on simultaneous localisation and mapping)
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: Please help with nxc code, not sure whats wrong.

Post by spillerrec »

It is not possible to see what goes wrong without your actual code. Include your code as an attachment if it is too long to have in the post. (You might need to rename it to *.txt though.)
My only guess is that you are using motor commands which does not stall the program until they complete, this could cause a function to exit much faster than expected, even though the motors are still to complete their task.

You should however use "void" for functions instead of "sub" as "sub" is basically just for backwards compatibly with NQC iirc. It shouldn't make a difference in your program though, it just makes it more standardized and therefore easier to read for others.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
monxc
Posts: 18
Joined: 17 Oct 2011, 10:23

Re: Please help with nxc code, not sure whats wrong.

Post by monxc »

Hello,

as spillerrec says, without your actual code, it's hard to say what's really happening, but possible reasons may be that you're getting spurious readings from the ultrasonic sensor (which may cause sub 1 to quit immediately or, indeed, never quit according to your pseudo-code) or the method you're using to check the position of the ultrasonic sensor in sub 2 returns that the motor is already in the correct position, so sub 2 quits immediately - this might make it look like sub 1 or sub 2 (or both) are being skipped and may make it look like all three are running at the same time, but without seeing your code, it's hard to tell...

Good luck with it,

monxc
---111 040 150 141 166 145 040 156 157 040 151 144 145 141 040 167 150 141 164 040 111 047 155 040 144 157 151 156 147 056 056 056---
monxc
Posts: 18
Joined: 17 Oct 2011, 10:23

Re: Please help with nxc code, not sure whats wrong.

Post by monxc »

...I've just read the post by spillerrec on tasks and global variables - have a read of that, as it has the scope to cause random behaviour, too...

monxc
---111 040 150 141 166 145 040 156 157 040 151 144 145 141 040 167 150 141 164 040 111 047 155 040 144 157 151 156 147 056 056 056---
photon-1
Posts: 14
Joined: 02 Dec 2010, 14:29
Location: London, Uk
Contact:

Re: Please help with nxc code, not sure whats wrong.

Post by photon-1 »

Thanks guys,
I wanna try a few things first this weekend and if that doesn't work I'll post the code;
I was thinking because I placed the implementation of the functions/sub routines BEFORE the main task, maybe thats causing some confusion?
Anyways I'll try putting them after the main task n' if it doesn't work, will post the code...

Thanks.
photon-1
Posts: 14
Joined: 02 Dec 2010, 14:29
Location: London, Uk
Contact:

Re: Please help with nxc code, not sure whats wrong.

Post by photon-1 »

hi guys, sorry for the delay, hope you're still tuned in. I've actually figured out the problem but I haven't been able to figure out WHY it is a problem.
I've messed around with the program a lot trying to simplify it and find out whats wrong. at the moment as it is right now (as I've posted it); I've given it it test values and I've commented out some of the functions and tasks or just not started them in the program at the moment the robot does this:

turn until there is nothing in front,
move forward until there is something in front.
repeat the process.

which is what should happen. but you will notice I havent started "localise" task. However when I start the "localise" task... the robot does something like:

turn until there is nothing in front,
then nothing happens when its supposed to move forward,
then nothing happens when its supposed to turn,
then it might move forward this time,
then nothing happens again

basically it starts ignoring/skipping lines of code or sometimes nothing happens at all. So what exactly have i written in the "localise" task that is causing this kind of behaviour???
Thanks alot!
Attachments
SLAM - REFINING.nxc
(12.4 KiB) Downloaded 346 times
photon-1
Posts: 14
Joined: 02 Dec 2010, 14:29
Location: London, Uk
Contact:

Re: Please help with nxc code, not sure whats wrong.

Post by photon-1 »

just incase you missed it, the file is called "SLAM - REFINING" in the above post.

Anyways my thoughts on the problem:
I'm resetting the rotation counter for the motors continuously in the "localise" task so does this affect the "OnFwd" functions for moving the motors somehow?
The algorithm requires that I do this so I'm not sure how else I would be able to implement it if I had to remove that statement from the program.

Gosh programming robots is stress!!! :( (sorry for the rant!)


Just for completeness, I'm re-posting the code as an nxc file and a text file.
Attachments
SLAM - REFINING.txt
(12.4 KiB) Downloaded 347 times
SLAM - REFINING.nxc
(12.4 KiB) Downloaded 343 times
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: Please help with nxc code, not sure whats wrong.

Post by h-g-t »

Wow - a bit too complex for me!

Only comment I can make is that I seem to recall reading somewhere that all that sub-routines, etc have to be defined before the tasks that call them.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Please help with nxc code, not sure whats wrong.

Post by mattallen37 »

h-g-t wrote:...I seem to recall reading somewhere that all that sub-routines, etc have to be defined before the tasks that call them.
That is true, unless you prototype them as he did:

Code: Select all

sub path_plan();
sub explore();
task localise();
task map();
task navigate();
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: Please help with nxc code, not sure whats wrong.

Post by h-g-t »

OK. I stand (or rather slump) corrected.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests