From aa49aaa1b93b8c020c32b8c47f24736b7dafd692 Mon Sep 17 00:00:00 2001 From: Gerry Chen Date: Wed, 22 Dec 2021 14:57:32 -0500 Subject: [PATCH 1/4] example standalone cmake project --- examples/cmake_project_example/CMakeLists.txt | 25 +++++++++ examples/cmake_project_example/main.cpp | 51 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 examples/cmake_project_example/CMakeLists.txt create mode 100644 examples/cmake_project_example/main.cpp diff --git a/examples/cmake_project_example/CMakeLists.txt b/examples/cmake_project_example/CMakeLists.txt new file mode 100644 index 000000000..5f0c5721d --- /dev/null +++ b/examples/cmake_project_example/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.0) +project(example C CXX) + +# gtsam +find_package(GTSAM REQUIRED) + +# gtdynamics +find_path(gtdynamics_include gtdynamics) +find_library(gtdynamics_lib gtdynamics) +# these next 3 lines are temporary until we create a proper cmake module for gtdynamics. +set(SDFormat_VERSION 10) +find_package(sdformat${SDFormat_VERSION} REQUIRED) +list(APPEND gtdynamics_lib ${SDFormat_LIBRARIES}) + +# Build Executable +add_executable(${PROJECT_NAME} main.cpp) +target_link_libraries(${PROJECT_NAME} PUBLIC ${gtdynamics_lib} gtsam) +target_include_directories(${PROJECT_NAME} PUBLIC ${gtdynamics_include}) + +# Convenience cmake command to build and run in one command with `make -j8 example.run`. +add_custom_target( + ${PROJECT_NAME}.run + COMMAND ./${PROJECT_NAME} + DEPENDS ${PROJECT_NAME} + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/examples/cmake_project_example/main.cpp b/examples/cmake_project_example/main.cpp new file mode 100644 index 000000000..3d43a4a91 --- /dev/null +++ b/examples/cmake_project_example/main.cpp @@ -0,0 +1,51 @@ +/** + * @file main.cpp + * @brief gtdynamics cmake Project Example + * This code demonstrates a sample project that imports gtdynamics with cmake. + * This cpp file should be the same as `example_forward_dynamics/main.cpp`. + */ + +#include +#include +#include +#include +#include +#include + +using namespace gtdynamics; +int main(int argc, char** argv) { + // Load the robot + auto simple_rr = CreateRobotFromFile( + kSdfPath + std::string("test/simple_rr.sdf"), "simple_rr_sdf"); + simple_rr.print(); + + gtsam::Vector3 gravity(0, 0, -9.8); + + // Build a nonlinear factor graph of kinodynamics constraints. + auto graph_builder = DynamicsGraph(gravity); + auto kdfg = graph_builder.dynamicsFactorGraph(simple_rr, + 0 // timestep + ); + + // Specify the forward dynamics priors and add them to the factor graph. + gtsam::Values known_values; + for (auto&& joint : simple_rr.joints()) { + int j = joint->id(); + InsertJointAngle(&known_values, j, 0, 0.0); + InsertJointVel(&known_values, j, 0, 0.0); + InsertTorque(&known_values, j, 0, 0.0); + } + auto fd_priors = + graph_builder.forwardDynamicsPriors(simple_rr, 0, known_values); + kdfg.add(fd_priors); + + // Initialize solution. + auto init_values = ZeroValues(simple_rr, 0); + + // Compute the forward dynamics. + gtsam::LevenbergMarquardtOptimizer optimizer(kdfg, init_values); + gtsam::Values results = optimizer.optimize(); + graph_builder.printValues(results); + + return 0; +} From 1822336d3ce019d14b1b58594350f2acbfd97e52 Mon Sep 17 00:00:00 2001 From: Gerry Chen Date: Wed, 22 Dec 2021 15:24:42 -0500 Subject: [PATCH 2/4] update README examples instructions --- README.md | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 044f36466..49ac7bedb 100644 --- a/README.md +++ b/README.md @@ -84,28 +84,36 @@ $ make check ``` ## Running Examples +The `examples` directory contains various example projects where the entire project is contained inside the gtdynamics repo. Use these as templates when your project is relatively simple and can live inside your gtdynamics directory. -The `/examples` directory contains example projects that demonstrate how to include GTDynamics in your application. To run an example, ensure that the `CMAKE_PREFIX_PATH` is set to the GTDynamics install directory. - -1. Navigate to the example's subdirectory and create a build directory. e.g. +Run these examples with ```sh -cd GTDynamics/examples/example_forward_dynamics -mkdir build; cd build +$ make example_XXX.run ``` +where `XXX` corresponds to a folder name in `examples`. For example, `make example_forward_dynamics.run`. + +## Using GTDynamics in an external project with cmake +The `/examples/cmake_project_example` directory contains an example cmake project that demonstrates how to include GTDynamics in your application. Use this as a template when you want your project to be not inside the gtdynamics directory (e.g. separate git repo, ROS, personal libraries, etc). To test building the project: + +1. Navigate to the example's subdirectory and create a build directory. e.g. + ```sh + cd GTDynamics/examples/cmake_project_example + mkdir build; cd build + ``` 2. Make the example. -If GTDynamics was installed to `~/JohnDoe/gtdynamics_install`, then run the cmake command with: + If GTDynamics was installed to `~/JohnDoe/GTDynamics/install`, then run the cmake command with: -```sh -cmake -DCMAKE_PREFIX_PATH=~/JohnDoe/gtdynamics_install .. -make -``` + ```sh + cmake -DCMAKE_PREFIX_PATH=~/JohnDoe/GTDynamics/install .. + make + ``` 3. Run the example! -```sh -./exec -``` + ```sh + ./example + ``` ## Python Wrapper (Recommended use case) From 1cfe859eb774c27a4492448eb9677353bb21a4ec Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Sun, 15 Oct 2023 20:42:59 -0400 Subject: [PATCH 3/4] fix cmake and cpp files to compile with latest GTD version --- examples/cmake_project_example/CMakeLists.txt | 9 +-------- examples/cmake_project_example/main.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/examples/cmake_project_example/CMakeLists.txt b/examples/cmake_project_example/CMakeLists.txt index 5f0c5721d..269384f82 100644 --- a/examples/cmake_project_example/CMakeLists.txt +++ b/examples/cmake_project_example/CMakeLists.txt @@ -8,7 +8,7 @@ find_package(GTSAM REQUIRED) find_path(gtdynamics_include gtdynamics) find_library(gtdynamics_lib gtdynamics) # these next 3 lines are temporary until we create a proper cmake module for gtdynamics. -set(SDFormat_VERSION 10) +set(SDFormat_VERSION 12) find_package(sdformat${SDFormat_VERSION} REQUIRED) list(APPEND gtdynamics_lib ${SDFormat_LIBRARIES}) @@ -16,10 +16,3 @@ list(APPEND gtdynamics_lib ${SDFormat_LIBRARIES}) add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} PUBLIC ${gtdynamics_lib} gtsam) target_include_directories(${PROJECT_NAME} PUBLIC ${gtdynamics_include}) - -# Convenience cmake command to build and run in one command with `make -j8 example.run`. -add_custom_target( - ${PROJECT_NAME}.run - COMMAND ./${PROJECT_NAME} - DEPENDS ${PROJECT_NAME} - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/examples/cmake_project_example/main.cpp b/examples/cmake_project_example/main.cpp index 3d43a4a91..d9a4c7e87 100644 --- a/examples/cmake_project_example/main.cpp +++ b/examples/cmake_project_example/main.cpp @@ -1,4 +1,4 @@ -/** +/** * @file main.cpp * @brief gtdynamics cmake Project Example * This code demonstrates a sample project that imports gtdynamics with cmake. @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include @@ -40,7 +40,8 @@ int main(int argc, char** argv) { kdfg.add(fd_priors); // Initialize solution. - auto init_values = ZeroValues(simple_rr, 0); + Initializer initializer; + auto init_values = initializer.ZeroValues(simple_rr, 0); // Compute the forward dynamics. gtsam::LevenbergMarquardtOptimizer optimizer(kdfg, init_values); From ba2f2d08964efe5f8831fb340471669855e714af Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Sun, 15 Oct 2023 20:43:22 -0400 Subject: [PATCH 4/4] update README to show the utility of the cmake_project_example directory --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9de42be64..bb210c6ec 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,12 @@ $ make check ``` ## Running Examples -The `examples` directory contains various example projects where the entire project is contained inside the gtdynamics repo. Use these as templates when your project is relatively simple and can live inside your gtdynamics directory. + +The `examples` directory contains various full example projects demonstrating the use of GTDynamics for various robotic applications. + +We recommend going through the examples to get a better understanding of how to use GTDynamics for your own use cases. + +*NOTE* The examples are made to run within the GTDynamics source folder and are not standalone projects. Run these examples with ```sh @@ -92,8 +97,13 @@ $ make example_XXX.run ``` where `XXX` corresponds to a folder name in `examples`. For example, `make example_forward_dynamics.run`. -## Using GTDynamics in an external project with cmake -The `/examples/cmake_project_example` directory contains an example cmake project that demonstrates how to include GTDynamics in your application. Use this as a template when you want your project to be not inside the gtdynamics directory (e.g. separate git repo, ROS, personal libraries, etc). To test building the project: +## Including GTDynamics With CMake + +The `examples/cmake_project_example` directory contains an example CMake-based project that demonstrates how to include GTDynamics in your application. + +Use this as a template when you want to set up your own project that uses GTDynamics (e.g. separate git repo, ROS, personal libraries, etc). + +To build the project: 1. Navigate to the example's subdirectory and create a build directory. e.g. ```sh