LEGO NXT C Programming

This article is automatically translated from Russian by Google Translator.

Text programming for LEGO NXT is done in C using macros and functions from the ECRobot library.

The general structure of the program:

  • First you connect the header files used in the program (standard C header files and ECRobot header files).

  • Then the constants and variables used in the program are declared.

  • These are followed by the functions ecrobot_device_initialize and ecrobot_device_terminate. The first one is called when the program starts and the second one when it stops. They usually initialize and deinitialize sensors, encoders, and other robot devices. The random number generator is initialized in ecrobot_device_initialize by default.

  • This is followed by the description of the user_1ms_isr_type2 function, which is called every millisecond.

  • Then comes the description of the task, which is executed when the program starts: TASK (OSEK_Task_Number_0). This is where most of the programming is done.

#include <string.h>
#include "kernel.h"
#include "kernel_id.h"
#include "ecrobot_interface.h"
#include "trik_studio_utils.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
U32 __interpretation_started_timestamp__ = 0;
static const float pi = 3.14159265;
void ecrobot_device_initialize(void)
{
srand(systick_get_ms());
}
void ecrobot_device_terminate(void)
{
}
/* nxtOSEK hook to be invoked from an ISR in category 2 */
void user_1ms_isr_type2(void)
{
}
/* Main task */
TASK(TASK_MAIN)
{
__interpretation_started_timestamp__ = systick_get_ms();
TerminateTask();
}

Full description of functions in English available for programming.

The code generated by TRIK Studio itself can be used as examples.