Skip to content

yiliangbetter/SensorPipeLine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ADAS Sensor Pipeline - Compile-Time Type Safety Project

A modern C++20 metaprogramming project for ADAS (Advanced Driver Assistance Systems) that provides compile-time validated sensor pipelines with type-safe units and coordinate frames.

🎯 Project Goals

This project demonstrates advanced C++ metaprogramming techniques applied to real-world robotics and ADAS systems, eliminating entire classes of runtime bugs through compile-time validation.

Key Features

  • βœ… Type-safe units - No more mixing meters with seconds
  • βœ… Coordinate frame safety - Prevents accidental frame mixing
  • βœ… Zero runtime overhead - All checks at compile time
  • βœ… ADAS-focused - Built for sensor fusion, perception, and planning

πŸ“‹ Project Phases

βœ… Phase 1: Type-Safe Units & Frames (Current)

  • Compile-time unit system (Length, Time, Velocity, Frequency, Angle)
  • Unit conversions (m ↔ km, m/s ↔ km/h, etc.)
  • Coordinate frame types (Vehicle, Lidar, Camera, Radar, Map, IMU)
  • Type-safe transforms between frames
  • Transform composition
  • Compile-time validation via static_assert

πŸ”œ Phase 2: Compile-Time Pipeline Graph

  • Sensor message types
  • Pipeline DAG representation
  • Compile-time graph validation
  • Rate and latency checking

πŸ”œ Phase 3: FSM for Behavior Planning

  • Type-safe state machines
  • Transition validation
  • Reachability analysis

πŸ”œ Phase 4: Safety Rules & Static Assertions

  • Safety-critical constraints
  • ISO 26262 inspired checks
  • Compile-time proof of safety properties

πŸ”œ Phase 5: Benchmarks & Performance

  • Runtime comparison vs. unchecked code
  • Zero-overhead validation
  • Real-world ADAS scenarios

πŸš€ Getting Started

Prerequisites

  • C++20 compiler (GCC 10+, Clang 12+, or MSVC 2019+)
  • CMake 3.20+

Building

# Clone or navigate to project directory
cd SensorPipeLine

# Create build directory
mkdir build && cd build

# Configure
cmake ..

# Build
cmake --build .

# Run demo
./phase1_demo

# Run tests
ctest --verbose

Quick Example

#include "units.hpp"
#include "frames.hpp"

using namespace sensor_pipeline::literals;

int main() {
    // βœ… Type-safe units
    auto distance = 150.5_m;
    auto time = 5.0_s;
    
    // ❌ This won't compile - different dimensions!
    // auto wrong = distance + time;
    
    // βœ… Coordinate frames
    Point3D<LidarFrame> lidar_point(10.0_m, 5.0_m, 2.0_m);
    
    // βœ… Transform between frames
    Transform<LidarFrame, VehicleFrame> T_lidar_vehicle;
    Point3D<VehicleFrame> vehicle_point = T_lidar_vehicle(lidar_point);
    
    // ❌ This won't compile - frame mismatch!
    // Point3D<VehicleFrame> wrong = lidar_point;
    
    return 0;
}

🧩 Architecture

Type-Safe Units (include/units.hpp)

// Define physical dimensions as types
struct Length {};
struct Time {};
struct Velocity {};

// Unit types with compile-time dimension checking
template<typename Dimension, typename Ratio = std::ratio<1>>
struct Unit { /* ... */ };

// Specific units
using Meters = Unit<Length, std::ratio<1>>;
using Kilometers = Unit<Length, std::kilo>;
using MetersPerSecond = Unit<Velocity, std::ratio<1>>;

// User-defined literals
auto distance = 100.0_m;
auto speed = 30.0_mps;
auto freq = 20_Hz;

Coordinate Frames (include/frames.hpp)

// Frame tags
struct VehicleFrame {};
struct LidarFrame {};
struct CameraFrame {};
struct RadarFrame {};

// Points are tagged with frames
template<Frame F>
struct Point3D {
    using frame = F;
    Meters x, y, z;
};

// Transforms between frames
template<Frame From, Frame To>
struct Transform {
    Point3D<To> operator()(const Point3D<From>& p) const;
};

// Transform composition (automatic frame chaining)
Transform<A, C> = Transform<A, B> * Transform<B, C>;

πŸ”¬ Compile-Time Guarantees

All the following errors are caught at compile time:

❌ Unit Dimension Mismatch

auto distance = 100.0_m;
auto time = 5.0_s;
auto wrong = distance + time;  // ❌ Compile error!

❌ Frame Mismatch

Point3D<LidarFrame> p1;
Point3D<VehicleFrame> p2 = p1;  // ❌ Compile error!

❌ Invalid Transform

Transform<LidarFrame, VehicleFrame> T;
Point3D<CameraFrame> camera_point;
auto result = T(camera_point);  // ❌ Compile error!

❌ Transform Chain Error

Transform<A, B> T1;
Transform<C, D> T2;
auto T3 = T1 * T2;  // ❌ Compile error! B β‰  C

πŸ§ͺ Testing

Static Assertion Tests

All type safety is validated at compile time:

// Dimension checking
static_assert(std::same_as<Meters::dimension, Length>);
static_assert(!std::same_as<Meters::dimension, Time>);

// Frame type checking
static_assert(!std::same_as<VehicleFrame, LidarFrame>);
static_assert(Frame<VehicleFrame>);

// Transform type checking
static_assert(std::same_as<
    Transform<LidarFrame, VehicleFrame>::from_frame,
    LidarFrame
>);

Runtime Tests

Numerical accuracy validation:

./compile_time_tests

πŸŽ“ C++ Metaprogramming Techniques Used

Phase 1

  • βœ… Type-level programming - Dimensions and frames as types
  • βœ… Variadic templates - Flexible unit composition
  • βœ… C++20 Concepts - Constrain template parameters
  • βœ… constexpr functions - Compile-time computations
  • βœ… static_assert - Compile-time validation
  • βœ… Type traits - is_in_frame, SameFrame
  • βœ… User-defined literals - Ergonomic syntax (100_m, 30_mps)
  • βœ… SFINAE / Concepts - Prevent invalid operations

πŸš— Real-World ADAS Applications

This design eliminates bugs commonly seen in:

Sensor Fusion

  • βœ… Prevents coordinate frame confusion (Lidar vs. Camera vs. Vehicle)
  • βœ… Ensures unit consistency (meters vs. millimeters)
  • βœ… Validates transform chains

Perception Pipelines

  • βœ… Type-safe message passing
  • βœ… Frame alignment verification
  • βœ… Rate consistency checks (future phases)

Motion Planning

  • βœ… Speed limit enforcement (type-safe velocities)
  • βœ… Safe distance calculations
  • βœ… Collision prediction with correct units

Localization

  • βœ… Map ↔ Vehicle transform validation
  • βœ… GPS coordinate safety
  • βœ… IMU integration correctness

πŸ“š References & Inspiration

  • ISO 26262 (Functional Safety for Road Vehicles)
  • ROS2 TF2 (Transform Library)
  • Modern C++ Design Patterns
  • Compile-time dimensional analysis
  • Type-safe embedded systems

🀝 Contributing

This is a personal learning project, but feedback and suggestions are welcome!

Future Enhancements

  • Support for more sensors (USS, GPS, IMU)
  • Compile-time pipeline optimization
  • Integration with ROS2 message types
  • ASIL-D safety pattern library
  • Performance benchmarking suite

πŸ“œ License

MIT License - feel free to use this for learning and inspiration.


πŸ‘€ Author

Robotics & ADAS Engineer
Exploring modern C++ metaprogramming for safety-critical systems.


πŸ”₯ Why This Matters

In ADAS and robotics, runtime errors cost lives. By moving validation to compile time:

  • πŸ›‘οΈ Zero runtime checks - No performance penalty
  • πŸ”’ Impossible states - Can't compile invalid code
  • πŸ“Š Self-documenting - Types encode domain knowledge
  • πŸš€ Faster development - Catch bugs before testing

This is C++ metaprogramming with purpose.


Phase 1 Complete βœ… - Ready for Phase 2: Pipeline Graphs

About

Compile-Time Safe ADAS Sensor Pipeline with C++20 Metaprogramming

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors