Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .clang-format/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ UseTab: Never
# Include
IncludeBlocks: Regroup
IncludeCategories:
# Headers containing "trajectory_generator"
- Regex: ".*trajectory_generator.*"
# Headers containing "traj_gen"
- Regex: ".*traj_gen.*"
Priority: 5
CaseSensitive: true
# Project's headers
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: ROS 2 CI

on:
push:
branches: "main"
pull_request:
branches: "main"

jobs:
build-and-test:
runs-on: ubuntu-22.04
container:
image: ros:humble

steps:
- name: Install tools
run: apt-get update && apt-get install -y git

- name: Checkout repository
uses: actions/checkout@v4
with:
path: src/trajectory_generator

- name: Install dependencies
run: |
apt-get update
apt-get install -y python3-colcon-common-extensions
rosdep update
rosdep install --from-paths src --ignore-src -y

- name: Build package
run: |
. /opt/ros/humble/setup.sh
colcon build --packages-select traj_gen --cmake-args -DBUILD_TESTING=ON

- name: Run tests
run: |
. /opt/ros/humble/setup.sh
colcon test --packages-select traj_gen
colcon test-result --all
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.14)
project(trajectory_generator)
project(traj_gen)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -14,7 +14,7 @@ find_package(ament_cmake_gtest REQUIRED)

# library
add_library(${PROJECT_NAME} SHARED
src/trajectory_generator.cpp
src/common.cpp
src/spline.cpp
)

Expand All @@ -26,7 +26,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC
)

target_compile_definitions(${PROJECT_NAME}
PRIVATE "TRAJECTORY_GENERATOR_BUILDING_LIBRARY"
PRIVATE "TRAJ_GEN_BUILDING_LIBRARY"
)

# install setting
Expand Down Expand Up @@ -80,14 +80,14 @@ if(BUILD_TESTING)

find_package(GTest REQUIRED)

add_executable(unit_test_trajectory_generator
test/unit_test_trajectory_generator.cpp
add_executable(unit_test_traj_gen
test/unit_test_traj_gen.cpp
)
target_link_libraries(unit_test_trajectory_generator
target_link_libraries(unit_test_traj_gen
${PROJECT_NAME}
GTest::gtest
)

include(GoogleTest)
gtest_discover_tests(unit_test_trajectory_generator)
gtest_discover_tests(unit_test_traj_gen)
endif()
116 changes: 102 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,123 @@
# trajectory_generator

## How to Install
[![Build Status](https://github.com/MasazumiImai/trajectory_generator/actions/workflows/build.yml/badge.svg)](https://github.com/MasazumiImai/trajectory_generator/actions)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

```bash
mkdir -p ~/ros2_ws/src && ~/ros2_ws/src
git clone https://github.com/MasazumiImai/trajectory_generator.git
```
A lightweight and extensible C++ library for generating and interpolating trajectories for robotics. It provides robust interpolation for N-dimensional vectors (e.g., joint angles, task-space positions) and SO(3) orientations (quaternions).

This package can be used as a standalone C++ library or integrated seamlessly as a ROS 2 (`ament_cmake`) package.

## Features

## How to Build
* **N-Dimensional Vector Trajectory:** Generate smooth spline trajectories with position, velocity, and acceleration constraints.
* **SO(3) Orientation Trajectory:** Perform accurate quaternion interpolation in the SO(3) space using Exponential and Logarithmic maps (`expMap` / `logMap`).
* **ROS 2 Ready:** Fully compatible with the ROS 2 build system (`colcon`).

### C++
## Installation & Build

### Prerequisites

* C++ 17 or higher
* Eigen3
* ROS 2 (Optional, for `colcon` build)

### As a Standalone C++ Library

```bash
git clone https://github.com/MasazumiImai/trajectory_generator.git
cd trajectory_generator
mkdir -p build && cd build
cmake .. && make && sudo make install
mkdir build && cd build
cmake ..
make
sudo make install
```

### ROS 2
### As a ROS 2 Package

```bash
mkdir -p ~/ros2_ws/src && cd ~/ros2_ws/src
git clone https://github.com/MasazumiImai/trajectory_generator.git
cd ~/ros2_ws
colcon build --packages-select trajectory_generator && . install/setup.bash
colcon build --packages-select traj_gen
source install/setup.bash
```

## How to Test
## Quick Start / Usage

### For Pure C++ Projects

If you are using standard CMake without ROS 2, simply find the package and link it in your `CMakeLists.txt`:

```CMake
find_package(traj_gen REQUIRED)

add_executable(your_app src/main.cpp)
target_link_libraries(your_app traj_gen::traj_gen)
```

### For ROS 2 Projects

If you are using this library within a ROS 2 package, add the dependency to your `package.xml`:

```xml
<depend>traj_gen</depend>
````

### C++
Then, configure your `CMakeLists.txt`:

```CMake
find_package(traj_gen REQUIRED)

add_executable(your_node src/your_node.cpp)
ament_target_dependencies(your_node traj_gen)
```

### C++ Example

Include the master header to access all trajectory generation features.

```C++
#include <iostream>
#include <traj_gen/traj_gen.hpp>

int main() {
// 1. Define constraints
int dof = 3;

traj_gen::VectorStateConstraint start;
start.time = 0.0;
start.position = Eigen::VectorXd::Zero(dof);
start.velocity = Eigen::VectorXd::Zero(dof);

traj_gen::VectorStateConstraint end;
end.time = 5.0;
end.position = Eigen::VectorXd::Ones(dof) * 1.5;
end.velocity = Eigen::VectorXd::Zero(dof);

std::vector<traj_gen::VectorStateConstraint> constraints = traj_gen::createBoundaryConditions(start, end);

// 2. Generate Spline Trajectory
std::shared_ptr<traj_gen::VectorTrajectoryBase> planner = std::make_shared<traj_gen::VectorSpline>(constraints, dof);

// 3. Get state at specific time
double current_time = 2.5;
Eigen::VectorXd pos = planner->getPosition(current_time);
Eigen::VectorXd vel = planner->getVelocity(current_time);

std::cout << "Position at t=2.5: \n" << pos << std::endl;

return 0;
}
```

## Testing

If you want to run the unit tests:

```bash
cd trajectory_generator
mkdir -p build && cd build
cmake .. -DBUILD_TESTING=ON && make
cmake .. -DBUILD_TESTING=ON
make
ctest -V
```
47 changes: 47 additions & 0 deletions include/traj_gen/base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2025 Masazumi Imai
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TRAJ_GEN__BASE_HPP_
#define TRAJ_GEN__BASE_HPP_

#include <Eigen/Dense>

#include "traj_gen/visibility_control.h"

namespace traj_gen
{

class TRAJ_GEN_PUBLIC VectorTrajectoryBase
{
public:
virtual ~VectorTrajectoryBase() = default;

virtual Eigen::VectorXd getPosition(double time) = 0;

virtual Eigen::VectorXd getVelocity(double time) = 0;
};

class TRAJ_GEN_PUBLIC OrientationTrajectoryBase
{
public:
virtual ~OrientationTrajectoryBase() = default;

virtual Eigen::Quaterniond getOrientation(double time) = 0;

virtual Eigen::Vector3d getAngularVelocity(double time) = 0;
};

} // namespace traj_gen

#endif // TRAJ_GEN__BASE_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TRAJECTORY_GENERATOR__TRAJECTORY_GENERATOR_HPP_
#define TRAJECTORY_GENERATOR__TRAJECTORY_GENERATOR_HPP_
#ifndef TRAJ_GEN__COMMON_HPP_
#define TRAJ_GEN__COMMON_HPP_

#include <Eigen/Dense>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "trajectory_generator/visibility_control.hpp"
#include "traj_gen/visibility_control.h"

namespace trajectory_generator
namespace traj_gen
{

/**
Expand All @@ -41,14 +38,14 @@ struct VectorStateConstraint
/**
* @brief Create a list of constraints with boundary conditions (start and end points) for vectors.
*/
TRAJECTORY_GENERATOR_PUBLIC
TRAJ_GEN_PUBLIC
std::vector<VectorStateConstraint> createBoundaryConditions(
const VectorStateConstraint & start_constraint, const VectorStateConstraint & end_constraint);

/**
* @brief Add a new constraint to the existing constraint list.
*/
TRAJECTORY_GENERATOR_PUBLIC
TRAJ_GEN_PUBLIC
void addConstraint(
std::vector<VectorStateConstraint> & constraints, const VectorStateConstraint & new_constraint);

Expand All @@ -66,25 +63,25 @@ struct AngularStateConstraint
/**
* @brief Create a list of constraints with boundary conditions (start and end points) for orientation.
*/
TRAJECTORY_GENERATOR_PUBLIC
TRAJ_GEN_PUBLIC
std::vector<AngularStateConstraint> createBoundaryConditions(
const AngularStateConstraint & start_constraint, const AngularStateConstraint & end_constraint);

/**
* @brief Add a new constraint to the existing constraint list.
*/
TRAJECTORY_GENERATOR_PUBLIC
TRAJ_GEN_PUBLIC
void addConstraint(
std::vector<AngularStateConstraint> & constraints, const AngularStateConstraint & new_constraint);

// --- Mathematical utility functions for quaternion calculations ---

TRAJECTORY_GENERATOR_PUBLIC
TRAJ_GEN_PUBLIC
Eigen::Quaterniond expMap(const Eigen::Vector3d & omega);

TRAJECTORY_GENERATOR_PUBLIC
TRAJ_GEN_PUBLIC
Eigen::Vector3d logMap(const Eigen::Quaterniond & q);

} // namespace trajectory_generator
} // namespace traj_gen

#endif // TRAJECTORY_GENERATOR__TRAJECTORY_GENERATOR_HPP_
#endif // TRAJ_GEN__COMMON_HPP_
Loading
Loading