NXC question

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

NXC question

Post by h-g-t »

I have two problems at the moment and would be grateful for some assistance.

In NXT-G I could use a 'reset motor' block to zero the motor rotation counters.This cancelled out any movements the motors had made prior to that command and stopped the motors from trying to correct for these movements. Otherwise when I issued a command the motors went back to the place they were when I first switched the brick on before carrying out the new command.

In NXC I tried both the statements below and am not sure which one is most effective so at the moment am using both!

OffEx (OUT_A, RESET_ALL); // Reset the motor counters
Wait (250);

ResetSensor (SENSOR_1); // Clear the sensor on port 0
Wait (100);

I also found that they did not seem to work properly unless I put a wait statement after them. Any comments or guidance would be appreciated.

I am also having a slight problem with the 'sleep' function. Is there a command similar to the 'stay alive' block in NXT-G or do I have to make sure I specify a long enough time using the sleep command? I want to put something in the program in case I forget to set the brick to 'always on' before running the programs.
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.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC question

Post by afanofosc »

There is no need to reset a sensor if all you are trying to do is stop a motor and reset the tachometer counts for that motor. You do need to wait at least 1 ms but the motor may take longer than 1 ms to actually stop. Different programs will require different approaches to waiting so I did not pick one approach for the built-in API functions. You could write a while loop that loops until the tacho count stops changing and then reset it again (if it is not zero) followed by a 1ms wait.

Also, SENSOR_1 is not the port number. It is a macro that expands to Sensor(S1) so you should never use it (or its siblings) as a parameter to a function that expects a port number. Use S1..S4 instead.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: NXC question

Post by h-g-t »

Thank you for a quick response.

The unit I am working with uses measurements from the rotation sensors on the motors to determine parameters for motor movements in later parts of the program.

When I wrote this in NXT-G, the motors always went back to where they they were when the brick was switched on before executing the commands issued by the program. I found this in the Help section :-

"The interactive servo motors have an automatic error correction mechanism that helps your robot move very precisely. However, there may be times when you want to turn this feature off -- the Reset Motor block will let you do this."

When I included this block in between the measurements and the movements, the motors behaved as required.

When I re-wrote the program in NXC, I encountered the same program and sought a similar command.

When I tried simply resetting the rotation counters I still encountered (sorry!) unwanted motor movements.

Using the Reset All did not reliably cure this so I ended up using both commands and that worked.

However, I would prefer to know which is right, if for no better reason than I like to understand what I am doing.

The 'sleep' problem is the same; I can just put in an excessively long period to keep the brick going but would prefer to use the correct statement to switch off the sleep timer if one is available.
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.
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: NXC question

Post by timpattinson »

For resetting the tacho (rotation sensor) I use:

Code: Select all

ResetTachoCount(OUT_A);
//or
ResetAllTachoCounts(OUT_AB);
To stop the brick from falling asleep in the middle of a program I use

Code: Select all

KeepAlive();
It should reset the sleep timer back to what you set it at. If you include it in the main loop of your program or in another task you should have no problems
-Tim
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: NXC question - update

Post by h-g-t »

Thanks to all for sorting this out for me and I will now change my program. :D
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: Semrush [Bot] and 0 guests