To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 12810
12809  |  12811
Subject: 
Re: help with NQC
Newsgroups: 
lugnet.robotics
Date: 
Wed, 18 Oct 2000 04:09:03 GMT
Original-From: 
Steve Baker <sjbaker1@airmailSPAMLESS.net>
Reply-To: 
sjbaker1@airmail.netANTISPAM
Viewed: 
824 times
  
Giacomo wrote:

1) Where can i find some example of NQC programs ,just to see how they are
written;

I thought there were a bunch of them on the NQC homepage - if you can't find
them, email me and I'll send you a bunch of NQC programs I've been playing with
and whatever examples I havn't "modified to death".

2)What are the differences between Functions and Subroutines ? (why trere
are also Functions, subroutins are'nt better, es  less memory use)

Functions are 'inline' code - meaning that the code for the function
is inserted into your main program every time it's called.  That
means that they consume much more memory if they are called more than
once.

Subroutines are 'true' subroutines - just like 'real' C code.  The snag
is that the very limited Lego 'bytecode' into which NQC compiles your
program imposes some rather nasty limitations. As far as I can tell:

  * Subroutines can't have arguments (!! Eeekkk!!!)

  * There is some complicated restriction over local
    variables defined within them...notably, you can't
    have locals if the subroutine is used in more than
    one task....well...maybe - it depends on what
    RCX/Scout/RCX2/CyberMaster you are using...Icky!

  * Subroutines can't call other subroutines (!!!)

The NQC manual advises you not to use subroutines unless you
absolutely have to for space reasons.

Remember, this is "Not Quite C" !

3)What are the Assignments ? (+=, -=, /=..)

   x += expression ;

...is exactly the same as:

   x = x + expression ;

...and is probably a little more efficient. Replace '+' in the example above
with just about any of the usual math operators...same deal.


4)The #include, #define, #if definition, #ifdef symbol, #ifndef
symbol,#else, #elif condition, #endif  : what they do ?

These are things that instruct the NQC compiler to do things AS IT
COMPILES
THE PROGRAM.  This is a big subject for an email - and you should probably
find some kind of standard C language tutorial/book to learn these things.

However, here is a 'quicky' tutorial to get you going...

So, the '#define' command:

#define FOOBAR  PlayTone ( 660, 10 )

...tells the compiler that from that point on, every time it sees the
word 'FOOBAR' in the program, it'll insert the string of characters
on the remainder of that line...so, you could later type:

  if ( SENSOR_1 > 50 )
    FOOBAR ;

And the compiler would see:

  if ( SENSOR_1 > 50 )
    PlayTone ( 660, 10 ) ;

You can use this to make your program more readable:

  #define REVOLUTIONS      *16  /* 16 ticks of a rotation sensor */
  #define GUN_MOTOR        OUT_A
  #define FIRE_MISSILE     {OnFwd(GUN_MOTOR);Wait(5);Off(GUN_MOTOR);}

  if ( ROTATION_SENSOR > 5 REVOLUTIONS )
    FIRE_MISSILE

But be careful - the compiler is VERY literal-minded!

  #define SEVEN  3+4

  x = SEVEN * 2 ;

...sets X to eleven - not fourteen as you *might* expect!  It would be better
to write:

  #define SEVEN (3+4)

...which works as you might expect.

Also just as 'if(condition)' switches the processor to execute
or skip the next block of code, so '#if condition' causes to compiler to
either compile or not compile the following block of code.  This can be
used in MANY ways to allow you to maintain your code more easily.

For example:

    if ( SENSOR_2 > 50 )
    {
      Off ( OUT_A ) ;
  #ifdef NOISY_ROBOT
      PlayTone ( 440, 1 ) ;
  #endif
    }

this says that if the sensor is hit - then turn off the motor and
then *IF* the symbol 'NOISY_ROBOT' is defined in the program, then
the compiler should compile the code to generate a 440Hz tone - but
if that symbol is not define then that PlayTone command is simply
discarded AT COMPILE TIME - so it takes up no space in the RCX.

How do you define that symbol?   Well, there are two ways:

1)  Inside your program:

     #define NOISY_ROBOT

2) On the command line of the NQC compiler:

     nqc -DNOISY_ROBOT

The other important one is '#include' :

   #include "my_file.h"

..tells the compiler to compile the entire contents of the file "my_file.h" into
the current program at this point.  This is *mainly* used to allow you to
write a set of common '#define' symbols and use them in many programs that you
write.

5)Events :how can I use them ? where are they useful than choosing other
system ?

I'm just reading about that - they are new in RCX 2.0 firmware and I only
upgraded last night!   I don't *think* they are desperately useful - but
I confess I havn't played with them yet - so you should probably ignore
that comment.

--
Steve Baker   HomeEmail: <sjbaker1@airmail.net>
              WorkEmail: <sjbaker@link.com>
              HomePage : http://web2.airmail.net/sjbaker1
              Projects : http://plib.sourceforge.net
                         http://tuxaqfh.sourceforge.net
                         http://tuxkart.sourceforge.net
                         http://prettypoly.sourceforge.net



Message is in Reply To:
  help with NQC
 
Finally i decided to use NQC, the Lego Programmer was too much limited, and others system to program the RCX where still limited;in the other hand ,however, i do not want to get under the firmware. Ok ,i downloaded the firmware 2.0 ,the NQC+Manual (...) (24 years ago, 17-Oct-00, to lugnet.robotics)

4 Messages in This Thread:



Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR