MCPcopy Create free account
hub / github.com/arjunskumar/Robotics_CPP_Notes

github.com/arjunskumar/Robotics_CPP_Notes @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
17 symbols 35 edges 3 files 6 documented · 35% updated 2mo ago★ 323

Browse by type

Functions 13 Types & classes 4
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Robotics & AI C++17 Notes

C++ Intro

Welcome to our Modern C++ Course, specifically tailored for Engineers focusing on Robotics.

Table of Contents

  1. Module 01: Foundations & Compilation
  2. Module 02: Variables, Types & Constants
  3. Module 03: I/O & Arithmetic
  4. Module 04: Control Flow
  5. Module 05: Loops & Iteration
  6. Module 06: Structs & Enums
  7. Module 07: Memory & Ownership
  8. Module 08: Functions & Lambdas
  9. Module 09: Project Structure & CMake
  10. Module 10: Classes & Encapsulation
  11. Module 11: Templates & Polymorphism
  12. Module 12: Concurrency
  13. Module 13: Production Deployment & Tooling
  14. Module 14: Advanced Utilities (variant, any, tuple)
  15. Module 15: Performance & Profiling Checklist
  16. Module 16: Capstone — Multi-Threaded Sensor Pipeline
  17. Appendix A: Real Robotics Failure Case Studies
  18. Appendix B: Production Design Patterns
  19. Appendix C: Testing Discipline
  20. Appendix D: ROS 2 Senior Insights

Module 01 — What is C++? Compilation & Your First Program

Phase: Foundations

C++ is a high-performance, compiled language created by Bjarne Stroustrup. In robotics, it is the industry standard for real-time systems, providing "zero-cost abstractions" that allow for high-level code with maximum hardware efficiency.

[!NOTE] Design Philosophy: This module is designed to demystify the "black box" of how code reaches hardware. Robotics involves diverse hardware (ARM/x86); if you don't understand the Linker vs. Compiler, you will be paralyzed by the first library error you hit in ROS.

[!NOTE] Modern C++ (C++17): This course focuses specifically on the C++17 Standard, which introduced critical robotics features like std::optional, std::string_view, and structured bindings.

In the world of Robotics, C++ is the undisputed king. It provides zero-cost abstractions—high-level features that compile into highly efficient machine code. When processing point clouds from a LIDAR at 10Hz or calculating inverse kinematics for a robot arm in under a millisecond, performance is critical.

Who Uses This In The Real World?

  • Tesla Autopilot — Real-time sensor fusion and vehicle control systems.
  • Open Robotics (ROS) — ROS 2 core is written in C++ for real-time distributed systems.
  • Boston Dynamics — Control software for Atlas and Spot.
  • NVIDIA Isaac — Accelerated robotics simulation and AI pipelines.

How C++ Code Becomes a Robot Action (Compilation Pipeline)

Before we write code, it's crucial to understand how your text becomes a robot action.

Stage Action Intermediate Flag
1. Preprocessor Resolves #include and #define (textual copy-paste). g++ -E robot.cpp -o robot.i
2. Compiler Translates C++ to Assembly (CPU-specific logic). g++ -S robot.cpp -o robot.s
3. Assembler Converts Assembly to Binary Machine Code (Object Files). g++ -c robot.cpp -o robot.o
4. Linker Combines .o files and libraries into a final executable. g++ -o robot_init robot.o

Your First Robot Program

Let's write a simple program that outputs the status of a robot.

Code Example

#include <iostream>

int main() {
    std::cout << "[Robot System] Initializing sensors... OK\n";
    std::cout << "[Robot System] Ready for C++17 Robotics!\n";
    return 0;
}

Anatomy of the Program (Key Terminology)

  • #include: A Preprocessor Directive. Think of it as a command to copy-paste. Before the compiler even sees your code, the preprocessor finds the file iostream and pastes its entire contents directly into your script.
  • <iostream>: The C++ Standard Input/Output Stream library. It contains all the definitions needed for printing text to a screen or reading from a keyboard. (Without this, the compiler won't know what std::cout is!).
  • int main(): The Entry Point. When the Operating System (like Linux/Ubuntu running on your robot) executes your program, it blindly searches for a function called main() to start running.
    • The int keyword signifies that this function must return an integer to the OS when it completes its run.
  • std::cout: The Standard Character Output.
    • std:: is the Namespace. A namespace is like a folder that groups all Standard C++ features together so their names don't clash with variables in your own code (e.g., if you had a custom variable also named cout).
    • cout stands for character output (the console terminal).
  • <<: The Stream Insertion Operator (or "Put" operator). It takes the text on its right and "pushes" it into the cout stream on its left to be printed.
  • return 0;: This tells the Operating System that the program finished successfully. Any non-zero value (e.g., return 1; or return -1;) acts as an exit code signalling that an error or crash occurred.

Compile and Run (All stages at once):

g++ -std=c++17 -Wall -Wextra -o robot_init robot.cpp
./robot_init

Bad Practice — Avoid

// Compiling without warnings — hides critical bugs in your robot's logic
g++ robot.cpp -o robot_init

// using namespace std; — pollutes the global namespace. 
// Can lead to mysterious naming collisions in large ROS projects.
using namespace std;
cout << "Starting motors..."; 

// C-style printf — No type safety (can crash if types don't match)
int battery = 80;
printf("%s", battery); 

Good Practice

#include <iostream>

int main() {
    std::cout << "Starting motors...\n";  // Qualify with std::
    // \n is faster than std::endl (endl forces the OS to "flush the buffer" and draw to the screen immediately, causing massive I/O delays in loops)
    return 0;
}

Industry Best Practice

  • Namespace Safety: Avoid using namespace std; in headers or shared libraries. It pollutes the global namespace and causes naming collisions. In small .cpp files or local examples, it is often acceptable for brevity.
  • Smart Output: Avoid std::endl inside high-frequency sensor loops (e.g., 200Hz) because it forces a "buffer flush," causing massive I/O delays. Use \n instead. Use std::endl intentionally when you need to ensure a log message is written immediately (like right before a potential crash).
  • RAII Beyond Memory: C++ uses the RAII pattern for more than just memory. For example, std::lock_guard (introduced later) automatically unlocks a mutex when the object goes out of scope, preventing deadlocks in multi-threaded robot nodes.
  • Strict Warnings: Always compile with -std=c++17 -Wall -Wextra -Wconversion -Werror.
  • Use clang-format for automatic formatting.
  • Enable AddressSanitizer from day one: -fsanitize=address. It catches memory bugs instantly before they crash your actual robot.

Warning

Every statement ends with ; — except lines with { }. One missing semicolon can produce 20 error messages. Always fix the FIRST error and recompile.

Beyond g++: Build Systems (CMake)

While compiling with g++ works for a single file, real robotics projects (like ROS packages) have hundreds of files. Instead of manually typing long compilation commands, the industry uses CMake.

CMake is a "meta-build system". You write a CMakeLists.txt file that describes your project, and CMake automatically generates the complex Makefiles for you.

A basic CMakeLists.txt for our robot program:

cmake_minimum_required(VERSION 3.16)
project(RobotProject)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(robot_init robot.cpp)
target_compile_options(robot_init PRIVATE -Wall -Wextra -Wconversion -Werror)

Understanding C++ Errors

C++ compiler errors are notoriously terrifying to beginners, but they follow predictable patterns.

The Golden Rule of C++ Debugging

  1. Scroll to the VERY TOP of the error output.
  2. Fix the first error.
  3. Recompile.
  4. (Often, one missing semicolon causes 50 subsequent cascade errors. Do not try to fix error #50!).

**Commo

Core symbols most depended-on inside this repo

Shape

Method 11
Class 3
Function 2
Enum 1

Languages

C++100%

Modules by API surface

capstone/sensor_pipeline.hpp15 symbols
capstone/test_pipeline.cpp1 symbols
capstone/main.cpp1 symbols

For agents

$ claude mcp add Robotics_CPP_Notes \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page