CodeTheRobot

C++ Advanced · Lesson 07 of 23

Configuration structs

Grouping settings in a struct, defaults, validation and loading from a file or map.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain configuration structs in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A configuration struct gathers all settings in one place with defaults: publish rate, limits, frame names. It is easy to pass around, to print, and to compare in tests. Validate it once, when it is loaded, so the rest of the program can trust it.

In ROS 2 the values arrive as parameters that you declare and read into a struct like this. Building the struct and its validation now makes the later parameter code short and safe.

Guided lesson

Work through these steps

  1. 1.

    Read the configuration structs example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

struct ArmConfig {
// All settings of the arm driver in one type.
  std::string frame{"base_link"};
// Default coordinate frame name.
  double rate_hz{50.0};
// Default control rate.
  double max_speed{1.2};
// Default speed limit in rad/s.
};
// End of the struct.
std::optional<std::string> validate(const ArmConfig& c) {
// Return an error message, or nothing if the config is fine.
  if (c.rate_hz <= 0.0) return "rate_hz must be positive";
// Reject nonsense rates.
  if (c.max_speed <= 0.0) return "max_speed must be positive";
// Reject nonsense limits.
  return std::nullopt;
// No error.
}
// End of validate.
ArmConfig cfg; cfg.rate_hz = 100.0;
// Start from defaults and override one field.
if (auto err = validate(cfg)) std::cerr << *err << '\n';
// Report the first problem, if any.

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.

Watch it run · Values in memory

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

motor speed030x1070x1410x1890x1c40x2060x2420x2880x2c*p += 10VALUES IN MEMORY

Demo for Configuration structs. Expected result: rate_hz must be positive ok

Practice code

Now type it yourself

Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice

#include <iostream>
#include <optional>
#include <string>

struct ArmConfig {
  std::string frame{"base_link"};
  double rate_hz{50.0};
  double max_speed{1.2};
};

// TODO: return an error message for a non-positive rate_hz or max_speed, otherwise std::nullopt

int main() {
  ArmConfig bad;
  bad.rate_hz = -1.0;
  if (auto err = validate(bad)) std::cout << *err << '\n';
  ArmConfig good;
  if (!validate(good)) std::cout << "ok\n";
  return 0;
}

Expected output

rate_hz must be positive
ok
Stuck? Show the full solution (try it yourself first)
#include <iostream>
#include <optional>
#include <string>

struct ArmConfig {
  std::string frame{"base_link"};
  double rate_hz{50.0};
  double max_speed{1.2};
};

std::optional<std::string> validate(const ArmConfig& c) {
  if (c.rate_hz <= 0.0) return "rate_hz must be positive";
  if (c.max_speed <= 0.0) return "max_speed must be positive";
  return std::nullopt;
}

int main() {
  ArmConfig bad;
  bad.rate_hz = -1.0;
  if (auto err = validate(bad)) std::cout << *err << '\n';
  ArmConfig good;
  if (!validate(good)) std::cout << "ok\n";
  return 0;
}

Expected result

How you know it worked

  • After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
  • Changing the stated input produces the result you predicted.

Practical exercise

Apply the lesson

Write a function that fills ArmConfig from a std::map<std::string, std::string>, keeping defaults for missing keys and reporting a clear error for a value that is not a number. Test three inputs.

Before you continue

  • The program builds with -std=c++17 and no warnings
  • You can explain the key rule without reading the example
  • The normal and edge cases both have recorded results

If something goes wrong

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.