Skip to main content

Auto-Generate Main Function (XRobotMain)

xrobot_gen_main extracts constructor parameters from the MANIFEST sections in module headers and generates the unified entry function XRobotMain.


1. What is XRobotMain?

XRobotMain is a unified main function that:

  • Instantiates each module (based on MANIFEST parameters)
  • Builds dependency relationships between modules
  • Periodically calls each module's Monitor() method

The generated file is standard C++ code and can be compiled and used directly.


2. Quick Start

In a valid module directory structure, simply run:

xrobot_gen_main

Sample output:

Discovered modules: BlinkLED
[INFO] Successfully parsed manifest for BlinkLED
[INFO] Writing configuration to User/xrobot.yaml
[SUCCESS] Generated entry file: User/xrobot_main.hpp

Generation usually produces two files:

  • User/xrobot.yaml: module parameter configuration
  • User/xrobot_main.hpp: auto-generated main function source code

3. Modify Module Constructor Parameters

Open User/xrobot.yaml:

global_settings:
monitor_sleep_ms: 1000

modules:
- name: BlinkLED
constructor_args:
blink_cycle: 250

Adjust the parameters and rerun xrobot_gen_main to regenerate the main function.


4. Use an Existing Configuration File

If you already have a xrobot.yaml file, you can specify it explicitly:

xrobot_gen_main --config User/xrobot.yaml

This avoids re-scanning modules or overwriting existing settings.


5. Support for Template Parameters and Instance IDs

If a module's MANIFEST includes template_args, they will also be generated:

- name: PID
constructor_args:
kp: 1.0
ki: 0.2
template_args:
T: float
id: pid_left

Generated code will look like:

static PID<float> pid_left(hw, appmgr, 1.0, 0.2);

6. What Does the Generated Main Function Look Like?

#include "app_framework.hpp"
#include "libxr.hpp"

// Module headers
#include "BlinkLED.hpp"

static void XRobotMain(LibXR::HardwareContainer &hw) {
using namespace LibXR;
ApplicationManager appmgr;

// Auto-generated module instantiations
static BlinkLED blinkled(hw, appmgr, 250);

while (true) {
appmgr.MonitorAll();
Thread::Sleep(1000);
}
}

7. Typical Flow

  1. Ensure each module header includes a === MODULE MANIFEST comment block
  2. Run xrobot_gen_main to generate configuration and main function
  3. Manually tweak YAML parameter values
  4. Rerun the tool to regenerate the main function
  5. Call XRobotMain() in your project to initialize the system