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);
}
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)