I have a couple questions about NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: I have a couple questions about NXC

Post by m-goldberg »

I often find it useful to use the PlayTone API call for debugging. For example if I have more than one task running, I might assign a different tone to each task and have each of the tasks play its assigned tone on entry. Sometimes, when I do this, I hear the tones in the sequence I expect, but sometimes I don't. In the latter case, this gives me an important clue for debugging.

The same technique can be applied to function calls as well. This can tell whether or not functions are being called. Again, an important clue for debugging.

Note that there are named constants for both the frequency arg and the duration arg.

Code: Select all

#define TONE_A3 220
#define TONE_AS3 233
#define TONE_B3 247
#define TONE_C4 262
#define TONE_CS4 277
#define TONE_D4 294
#define TONE_DS4 311
#define TONE_E4 330
#define TONE_F4 349
#define TONE_FS4 370
#define TONE_G4 392
#define TONE_GS4 415
#define TONE_A4 440
#define TONE_AS4 466
#define TONE_B4 494
#define TONE_C5 523
#define TONE_CS5 554
#define TONE_D5 587
#define TONE_DS5 622
#define TONE_E5 659
#define TONE_F5 698
#define TONE_FS5 740
#define TONE_G5 784
#define TONE_GS5 831
#define TONE_A5 880
#define TONE_AS5 932
#define TONE_B5 988
#define TONE_C6 1047
#define TONE_CS6 1109
#define TONE_D6 1175
#define TONE_DS6 1245
#define TONE_E6 1319
#define TONE_F6 1397
#define TONE_FS6 1480
#define TONE_G6 1568
#define TONE_GS6 1661
#define TONE_A6 1760
#define TONE_AS6 1865
#define TONE_B6 1976
#define TONE_C7 2093
#define TONE_CS7 2217
#define TONE_D7 2349
#define TONE_DS7 2489
#define TONE_E7 637
#define TONE_F7 2794
#define TONE_FS7 2960
#define TONE_G7 3136
#define TONE_GS7 3322
#define TONE_A7 3520
#define TONE_AS7 3729
#define TONE_B7 3951

Code: Select all

#define MS_10 10
#define MS_20 20
#define MS_30 30
#define MS_40 40
#define MS_50 50
#define MS_60 60
#define MS_70 70
#define MS_80 80
#define MS_90 90
#define MS_100 100
#define MS_150 150
#define MS_200 200
#define MS_250 250
#define MS_300 300
#define MS_350 350
#define MS_400 400
#define MS_450 450
#define MS_500 500
#define MS_600 600
#define MS_700 700
#define MS_800 800
#define MS_900 900
#define SEC_1 1000
#define SEC_2 2000
The above aren't all the time constants, but are probably the ones most useful to use with PlayTone.
Regards, Morton
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: I have a couple questions about NXC

Post by afanofosc »

rrawat wrote:Mr Hansen:
I find the NXT watch list most convenient
however, i cannot find the variable i want to watch in the drop down expression list.
what if i just type the variable in the expression space; will it work??
Local variables are decorated with the name of the function or task in which they are defined. Also with the nesting level, depending on whether the variable declaration is inside a block statement of some kind. Also with separators and underscores. I forgot to attach the sample I was using to grab the screenshots.

Code: Select all

float globalFloat = PI;
int globalInt = 45;
long globalLong = -28534;

task main()
{
  int i;
  while(true)
  {
    i++;
    NumOut(0, LCD_LINE1, globalFloat);
    NumOut(0, LCD_LINE2, globalInt);
    NumOut(0, LCD_LINE3, globalLong);
    NumOut(0, LCD_LINE4, i);
    for (int i=0; i<10; i++)
    {
      NumOut(0, LCD_LINE5, i);
      Wait(MS_500);
    }
    globalFloat += sqrt(Random(50));
    globalInt += i;
    globalLong += Random(10);
    Wait(SEC_1);
  }
}
The global variables are undecorated. In this example the variable "i" that is declared at the top of task main will be called __main_7qG2_i_7qG2_000 and the variable "i" that is declared inside the "for" loop will be called __main_7qG2_i_7qG2_002 (since it is nested 2 levels deep). All variables used within your program, global or local, will be listed in the drop down list. You just need to know the decoration scheme so that you can find your local variable.

Several options/features of the NXT Watch list are not fully implemented yet so don't be surprised if not everything works. Watching arrays is still very hard to do successfully. The same goes for watching a user-defined struct type.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: I have a couple questions about NXC

Post by muntoo »

Oh, I just have to try the new debugging stuff when I get back to my NXT.

-----

Light sensor blinking program:

Code: Select all

unsigned long blink[];


inline void Blink(byte port = S1, unsigned long time = 100)
{
    blink[port] = time;
}


task tBlink()
{
    ArrayInit(blink, 0, 4);

    unsigned long blink_timer[];
    ArrayInit(blink_timer, 0, 4);

    while(1)
    {
        for(byte port = S1; port <= S3; port++)
        {
            if(blink[port] > 0)
            {
                if(blink_timer[port] > 0)
                {
                    if((blink_timer[port] + blink[time]) <= CurrentTick())
                    {
                        SetSensorLight(port, false);
                        blink[port] = 0;
                        blink_timer[port] = 0;
                    }
                }
                else
                {
                    SetSensorLight(port, true);
                    blink_timer[port] = CurrentTick();
                }
            }
        }

        Yield();
    }
}


task main()
{
    start tBlink;

    // Random code...

    Blink();

    // 10 more lines of code...

    Blink();

    // etc...
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests