Please help with nxc code, not sure whats wrong.

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
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 »

Hey, I just learned it within the last couple months... everything I know about programming I had to learn or figure out at some point, same as you ;)
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
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 »

yeh i tried to simplify the program as much as I could b4 posting it. Anyways I'm pretty sure the problem is with that localise task where I'm resetting the rotation counter continuously:

Code: Select all

task localise()
{

 while(1)
 {
  //some calculations here
  
  ResetRotationCount(OUT_AC);    // problem statement!!

  //some calculations here
 }

}
I THINK the problem is that I'm continuously resetting the counter for motors A and C in the localise task and using the OnFWD() and Off() functions to control the motors in other tasks and for some reason the program doesn't like this. I think this because the third motor B always works fine but the robot either doesn't move when its supposed to or doesn't stop when its supposed to (motors A and C) so it must be skipping those functions. I will try to find a way around it but in the mean time if anyone else has any ideas I'd be very grateful.

Thanks.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

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

Post by afanofosc »

Yes, I think you have isolated the problem.

Basically it is that you have no wait statements and you are issuing motor control statements for the same motor on two different tasks. Under the hood these motor control functions simply set fields in the Output module IOMap structure. Only once the Output module gets a chance to process the current values in its IOMap structure will anything actually happen with the motors. So if the code running in your program executes two different motor control functions before the Command module pauses to give other modules (including the Output module) a chance to execute then the second command may overwrite fields in the Output module IOMap structure that will change how the Output module controls the motors when it eventually gets a chance to do its job.

Adding a Wait() or a Yield() to your tasks while loop may not be sufficient to fix your problem since the call to ResetRotationCount could still overwrite what the OnFwd or OnRev commands put into the Output module IOMap structure before those values are ever processed by the Output module. To help fix this problem I think I may need to change the API functions so that they read the existing IOMap values or possibly have the Reset* functions Yield before fiddling with the output module IOMap and then Yield afterward to ensure that the reset is processed.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

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

Post by afanofosc »

With the existing API function behavior you can work around the issue by putting a Yield() before any Reset* function call. You can similarly Yield() prior to calling motor control routines in all tasks if you need to make sure that the reset (or any kind of motor control API function) on the other task is executed before the OnFwd on the current task.

It is normally best for infinite while loops in multi-threaded programs like yours to use Wait() commands to define varying levels of importance for each of the concurrent tasks. The most important task executes more often than the least important task by Wait()-ing fewer milliseconds each time through the infinite while loop. The least important task waits the longest each time through the loop.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
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 very much for the detailed explanation John. I'm sure I'll be able to get it working now.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests