diff --git a/CMakeLists.txt b/CMakeLists.txt index 70e7bff73..75acb3a6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,9 +107,6 @@ endif() # Dependencies add_subdirectory(deps) -# Add the C++ sources subdirectory -add_subdirectory(gympp) - # ======== # IGNITION # ======== @@ -118,6 +115,11 @@ find_package(ignition-gazebo3 COMPONENTS all) option(GYMIGNITION_USE_IGNITION "Build C++ code depending on Ignition Robotics" ${ignition-gazebo3_FOUND}) +# Add the C++ sources subdirectories +add_subdirectory(cpp/gympp/base) +add_subdirectory(cpp/gympp/gazebo) +add_subdirectory(cpp/gympp/plugins) + # Add the targets depending on Ignition Robotics if(${GYMIGNITION_USE_IGNITION}) add_subdirectory(ignition) diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt index 2c6f0205c..def86865e 100644 --- a/bindings/CMakeLists.txt +++ b/bindings/CMakeLists.txt @@ -35,11 +35,13 @@ swig_add_library(${swig_name} SOURCES ${swig_name}.i) target_link_libraries(${swig_name} PUBLIC + gympp + IgnitionRobot + RobotSingleton GymFactory - IgnitionEnvironment + GazeboEnvironment RobotSingleton GazeboWrapper - IgnitionRobot ${PYTHON_LIBRARIES}) set_property(TARGET ${swig_name} PROPERTY diff --git a/bindings/gympp_bindings.i b/bindings/gympp_bindings.i index af8c57055..10002654f 100644 --- a/bindings/gympp_bindings.i +++ b/bindings/gympp_bindings.i @@ -2,15 +2,15 @@ %{ #define SWIG_FILE_WITH_INIT -#include "gympp/Common.h" -#include "gympp/Environment.h" -#include "gympp/gazebo/IgnitionEnvironment.h" +#include "gympp/base/Common.h" +#include "gympp/base/Environment.h" +#include "gympp/base/Robot.h" +#include "gympp/base/Space.h" +#include "gympp/gazebo/GazeboEnvironment.h" #include "gympp/gazebo/GazeboWrapper.h" +#include "gympp/gazebo/GymFactory.h" +#include "gympp/gazebo/Metadata.h" #include "gympp/gazebo/RobotSingleton.h" -#include "gympp/GymFactory.h" -#include "gympp/Metadata.h" -#include "gympp/Robot.h" -#include "gympp/Space.h" #include %} @@ -34,46 +34,46 @@ %template(Array4d) std::array; %template(Array6d) std::array; -%include "gympp/Common.h" -%template(BufferContainer_i) gympp::BufferContainer; -%template(BufferContainer_u) gympp::BufferContainer; -%template(BufferContainer_f) gympp::BufferContainer; -%template(BufferContainer_d) gympp::BufferContainer; -%template(get_i) gympp::data::Sample::get; -%template(get_u) gympp::data::Sample::get; -%template(get_f) gympp::data::Sample::get; -%template(get_d) gympp::data::Sample::get; -%template(getBuffer_i) gympp::data::Sample::getBuffer; -%template(getBuffer_u) gympp::data::Sample::getBuffer; -%template(getBuffer_f) gympp::data::Sample::getBuffer; -%template(getBuffer_d) gympp::data::Sample::getBuffer; +%include "gympp/base/Common.h" +%template(BufferContainer_i) gympp::base::BufferContainer; +%template(BufferContainer_u) gympp::base::BufferContainer; +%template(BufferContainer_f) gympp::base::BufferContainer; +%template(BufferContainer_d) gympp::base::BufferContainer; +%template(get_i) gympp::base::data::Sample::get; +%template(get_u) gympp::base::data::Sample::get; +%template(get_f) gympp::base::data::Sample::get; +%template(get_d) gympp::base::data::Sample::get; +%template(getBuffer_i) gympp::base::data::Sample::getBuffer; +%template(getBuffer_u) gympp::base::data::Sample::getBuffer; +%template(getBuffer_f) gympp::base::data::Sample::getBuffer; +%template(getBuffer_d) gympp::base::data::Sample::getBuffer; %include "optional.i" %template(Optional_i) std::optional; %template(Optional_u) std::optional; %template(Optional_f) std::optional; %template(Optional_d) std::optional; -%template(Optional_state) std::optional; -%template(Optional_sample) std::optional; +%template(Optional_state) std::optional; +%template(Optional_sample) std::optional; %include -%shared_ptr(gympp::spaces::Space) -%shared_ptr(gympp::spaces::Box) -%shared_ptr(gympp::spaces::Discrete) -%include "gympp/Space.h" +%shared_ptr(gympp::base::spaces::Space) +%shared_ptr(gympp::base::spaces::Box) +%shared_ptr(gympp::base::spaces::Discrete) +%include "gympp/base/Space.h" -%shared_ptr(gympp::Environment) +%shared_ptr(gympp::base::Environment) %shared_ptr(gympp::gazebo::GazeboWrapper) -%shared_ptr(gympp::gazebo::IgnitionEnvironment) +%shared_ptr(gympp::gazebo::GazeboEnvironment) %include "ignition/common/SingletonT.hh" -%ignore ignition::common::SingletonT::myself; -%template(GymFactorySingleton) ignition::common::SingletonT; +%ignore ignition::common::SingletonT::myself; +%template(GymFactorySingleton) ignition::common::SingletonT; -%include "gympp/Environment.h" +%include "gympp/base/Environment.h" %include "gympp/gazebo/GazeboWrapper.h" -%include "gympp/gazebo/IgnitionEnvironment.h" +%include "gympp/gazebo/GazeboEnvironment.h" -%extend gympp::Robot { +%extend gympp::base::Robot { bool setdt(const double dt) { return $self->setdt(std::chrono::duration(dt)); } @@ -84,24 +84,24 @@ } %include "weak_ptr.i" -%shared_ptr(gympp::Robot) -%template(RobotWeakPtr) std::weak_ptr; +%shared_ptr(gympp::base::Robot) +%template(RobotWeakPtr) std::weak_ptr; -%ignore gympp::Robot::dt; -%ignore gympp::Robot::setdt(const StepSize&); -%include "gympp/Robot.h" -%template(Vector_contact) std::vector; +%ignore gympp::base::Robot::dt; +%ignore gympp::base::Robot::setdt(const StepSize&); +%include "gympp/base/Robot.h" +%template(Vector_contact) std::vector; %inline %{ - std::shared_ptr envToIgnEnv(gympp::EnvironmentPtr env) { - return std::dynamic_pointer_cast(env); + std::shared_ptr envToGazeboEnvironment(gympp::base::EnvironmentPtr env) { + return std::dynamic_pointer_cast(env); } - std::shared_ptr envToGazeboWrapper(gympp::EnvironmentPtr env) { + std::shared_ptr envToGazeboWrapper(gympp::base::EnvironmentPtr env) { return std::dynamic_pointer_cast(env); } %} -%include "gympp/Metadata.h" -%include "gympp/GymFactory.h" +%include "gympp/gazebo/Metadata.h" +%include "gympp/gazebo/GymFactory.h" %include "gympp/gazebo/RobotSingleton.h" diff --git a/cpp/gympp/base/CMakeLists.txt b/cpp/gympp/base/CMakeLists.txt new file mode 100644 index 000000000..46d3873f4 --- /dev/null +++ b/cpp/gympp/base/CMakeLists.txt @@ -0,0 +1,91 @@ +# Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT). All rights reserved. +# This software may be modified and distributed under the terms of the +# GNU Lesser General Public License v2.1 or any later version. + +# ===== +# GYMPP +# ===== + +add_library(gympp + include/gympp/base/Environment.h + include/gympp/base/Common.h + include/gympp/base/Log.h + include/gympp/base/Random.h + include/gympp/base/Space.h + include/gympp/base/Robot.h + Space.cpp + Random.cpp) + +find_package(ignition-common3 QUIET) + +if(ignition-common3_FOUND) + target_compile_definitions(gympp PUBLIC USE_IGNITION_LOGS) + target_link_libraries(gympp PUBLIC + ignition-common3::ignition-common3) +endif() + +file(GLOB_RECURSE GYMPP_HEADERS_INSTALL include/*.h) + +if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") + set_target_properties(gympp PROPERTIES + PUBLIC_HEADER "${GYMPP_HEADERS_INSTALL}") +endif() + +target_include_directories(gympp PUBLIC + $ + $) + +# ==== +# TASK +# ==== + +add_library(Task INTERFACE) +target_sources(Task INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}/include/gympp/base/Task.h) + +target_include_directories(Task INTERFACE + $ + $) + +target_link_libraries(Task INTERFACE gympp) + +if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") + set_target_properties(Task PROPERTIES + PUBLIC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include/gympp/base/Task.h) +endif() + +# ============== +# TASK SINGLETON +# ============== + +add_library(TaskSingleton + include/gympp/base/TaskSingleton.h + TaskSingleton.cpp) + +target_include_directories(TaskSingleton PUBLIC + $ + $) + +target_link_libraries(TaskSingleton + PRIVATE + gympp + Task) + +if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") + set_target_properties(TaskSingleton PROPERTIES + PUBLIC_HEADER include/gympp/base/TaskSingleton.h) +endif() + +# =================== +# INSTALL THE TARGETS +# =================== + +if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") + install( + TARGETS gympp Task TaskSingleton + EXPORT gympp + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/gympp/base) +endif() diff --git a/gympp/src/Random.cpp b/cpp/gympp/base/Random.cpp similarity index 69% rename from gympp/src/Random.cpp rename to cpp/gympp/base/Random.cpp index aad32c4d1..118fa4d81 100644 --- a/gympp/src/Random.cpp +++ b/cpp/gympp/base/Random.cpp @@ -6,30 +6,30 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#include "gympp/Random.h" +#include "gympp/base/Random.h" #include static std::random_device rd; static size_t Seed = rd(); static std::default_random_engine Engine(Seed); -gympp::Random& gympp::Random::instance() +gympp::base::Random& gympp::base::Random::instance() { static Random random; return random; } -size_t gympp::Random::seed() +size_t gympp::base::Random::seed() { return Seed; } -std::default_random_engine& gympp::Random::engine() +std::default_random_engine& gympp::base::Random::engine() { return Engine; } -void gympp::Random::setSeed(size_t seed) +void gympp::base::Random::setSeed(size_t seed) { Seed = seed; Engine = std::default_random_engine(seed); diff --git a/gympp/src/Space.cpp b/cpp/gympp/base/Space.cpp similarity index 95% rename from gympp/src/Space.cpp rename to cpp/gympp/base/Space.cpp index e5fb3ad5c..8d8cb0373 100644 --- a/gympp/src/Space.cpp +++ b/cpp/gympp/base/Space.cpp @@ -6,9 +6,9 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#include "gympp/Space.h" -#include "gympp/Log.h" -#include "gympp/Random.h" +#include "gympp/base/Space.h" +#include "gympp/base/Log.h" +#include "gympp/base/Random.h" #include #include @@ -16,7 +16,7 @@ #include #include -using namespace gympp::spaces; +using namespace gympp::base::spaces; // === // BOX @@ -148,7 +148,7 @@ Discrete::Sample Discrete::sample() std::uniform_int_distribution<> distr(0, static_cast(pImpl->n) - 1); // Create the buffer - auto buffer = gympp::BufferContainer::type(1, Type{}); + auto buffer = gympp::base::BufferContainer::type(1, Type{}); // Fill it with data buffer[0] = distr(Random::engine()); diff --git a/ignition/src/TaskSingleton.cpp b/cpp/gympp/base/TaskSingleton.cpp similarity index 88% rename from ignition/src/TaskSingleton.cpp rename to cpp/gympp/base/TaskSingleton.cpp index e2eaeab84..0c46c0527 100644 --- a/ignition/src/TaskSingleton.cpp +++ b/cpp/gympp/base/TaskSingleton.cpp @@ -6,14 +6,14 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#include "gympp/gazebo/TaskSingleton.h" -#include "gympp/Log.h" -#include "gympp/gazebo/Task.h" +#include "gympp/base/TaskSingleton.h" +#include "gympp/base/Log.h" +#include "gympp/base/Task.h" #include #include -using namespace gympp::gazebo; +using namespace gympp::base; class TaskSingleton::Impl { @@ -22,10 +22,12 @@ class TaskSingleton::Impl }; TaskSingleton::TaskSingleton() - : pImpl{new Impl(), [](Impl* impl) { delete impl; }} + : pImpl{new Impl()} {} -gympp::gazebo::TaskSingleton& TaskSingleton::get() +TaskSingleton::~TaskSingleton()= default; + +gympp::base::TaskSingleton& TaskSingleton::get() { static TaskSingleton instance; return instance; diff --git a/gympp/include/gympp/Common.h b/cpp/gympp/base/include/gympp/base/Common.h similarity index 67% rename from gympp/include/gympp/Common.h rename to cpp/gympp/base/include/gympp/base/Common.h index 99b7b3a6b..db9e622bb 100644 --- a/gympp/include/gympp/Common.h +++ b/cpp/gympp/base/include/gympp/base/Common.h @@ -6,8 +6,8 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_COMMON -#define GYMPP_COMMON +#ifndef GYMPP_BASE_COMMON +#define GYMPP_BASE_COMMON #include #include @@ -17,30 +17,31 @@ #include namespace gympp { - - using DataSupport = double; - struct Range; - - template - struct BufferContainer - { - typedef std::vector type; - }; - - using BufferInt = BufferContainer::type; - using BufferLong = BufferContainer::type; - using BufferFloat = BufferContainer::type; - using BufferDouble = BufferContainer::type; - using GenericBuffer = std::variant; - - namespace data { - using Shape = std::vector; - - struct Sample; - } // namespace data + namespace base { + using DataSupport = double; + struct Range; + + template + struct BufferContainer + { + typedef std::vector type; + }; + + using BufferInt = BufferContainer::type; + using BufferLong = BufferContainer::type; + using BufferFloat = BufferContainer::type; + using BufferDouble = BufferContainer::type; + using GenericBuffer = std::variant; + + namespace data { + using Shape = std::vector; + + struct Sample; + } // namespace data + } // namespace base } // namespace gympp -struct gympp::data::Sample +struct gympp::base::data::Sample { GenericBuffer buffer; @@ -88,7 +89,7 @@ struct gympp::data::Sample } }; -struct gympp::Range +struct gympp::base::Range { Range(DataSupport minValue = std::numeric_limits::lowest(), DataSupport maxValue = std::numeric_limits::max()) @@ -102,4 +103,4 @@ struct gympp::Range bool contains(double value) { return (value <= max && value >= min) ? true : false; } }; -#endif // GYMPP_COMMON +#endif // GYMPP_BASE_COMMON diff --git a/gympp/include/gympp/Environment.h b/cpp/gympp/base/include/gympp/base/Environment.h similarity index 62% rename from gympp/include/gympp/Environment.h rename to cpp/gympp/base/include/gympp/base/Environment.h index 1adb805b3..17b230341 100644 --- a/gympp/include/gympp/Environment.h +++ b/cpp/gympp/base/include/gympp/base/Environment.h @@ -6,47 +6,49 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_ENVIRONMENT -#define GYMPP_ENVIRONMENT +#ifndef GYMPP_BASE_ENVIRONMENT +#define GYMPP_BASE_ENVIRONMENT #include #include #include #include -#include "gympp/Common.h" -#include "gympp/Space.h" +#include "gympp/base/Common.h" +#include "gympp/base/Space.h" namespace gympp { - class Environment; - using EnvironmentName = std::string; - using EnvironmentPtr = std::shared_ptr; + namespace base { + class Environment; + using EnvironmentName = std::string; + using EnvironmentPtr = std::shared_ptr; - struct State; - using Action = data::Sample; - using Reward = DataSupport; - using Observation = data::Sample; + struct State; + using Action = data::Sample; + using Reward = DataSupport; + using Observation = data::Sample; + } // namespace base } // namespace gympp -struct gympp::State +struct gympp::base::State { bool done; std::string info; - gympp::Reward reward; - gympp::Observation observation; + gympp::base::Reward reward; + gympp::base::Observation observation; }; // TODO: https://hub.packtpub.com/openai-gym-environments-wrappers-and-monitors-tutorial/ // These C++ and their mapping to python / julia should allow using the Wrapper method // From https://github.com/openai/gym/blob/master/gym/core.py -class gympp::Environment +class gympp::base::Environment { public: - using Action = gympp::Action; - using Reward = gympp::Reward; - using Observation = gympp::Observation; - using State = gympp::State; + using Action = gympp::base::Action; + using Reward = gympp::base::Reward; + using Observation = gympp::base::Observation; + using State = gympp::base::State; enum class RenderMode { @@ -55,11 +57,11 @@ class gympp::Environment ANSI, }; - using ActionSpace = gympp::spaces::Space; - using ObservationSpace = gympp::spaces::Space; + using ActionSpace = gympp::base::spaces::Space; + using ObservationSpace = gympp::base::spaces::Space; using ActionSpacePtr = std::shared_ptr; using ObservationSpacePtr = std::shared_ptr; - using RewardRange = gympp::Range; + using RewardRange = gympp::base::Range; public: ActionSpacePtr action_space; @@ -85,4 +87,4 @@ class gympp::Environment // TODO: close() }; -#endif // GYMPP_ENVIRONMENT +#endif // GYMPP_BASE_ENVIRONMENT diff --git a/gympp/include/gympp/Log.h b/cpp/gympp/base/include/gympp/base/Log.h similarity index 100% rename from gympp/include/gympp/Log.h rename to cpp/gympp/base/include/gympp/base/Log.h diff --git a/gympp/include/gympp/Random.h b/cpp/gympp/base/include/gympp/base/Random.h similarity index 71% rename from gympp/include/gympp/Random.h rename to cpp/gympp/base/include/gympp/base/Random.h index e1952d894..86b250842 100644 --- a/gympp/include/gympp/Random.h +++ b/cpp/gympp/base/include/gympp/base/Random.h @@ -6,16 +6,18 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_RANDOM -#define GYMPP_RANDOM +#ifndef GYMPP_BASE_RANDOM +#define GYMPP_BASE_RANDOM #include namespace gympp { - class Random; -} + namespace base { + class Random; + } // namespace base +} // namespace gympp -class gympp::Random +class gympp::base::Random { public: Random() = default; @@ -27,4 +29,4 @@ class gympp::Random static void setSeed(size_t seed); }; -#endif // GYMPP_RANDOM +#endif // GYMPP_BASE_RANDOM diff --git a/gympp/include/gympp/Robot.h b/cpp/gympp/base/include/gympp/base/Robot.h similarity index 81% rename from gympp/include/gympp/Robot.h rename to cpp/gympp/base/include/gympp/base/Robot.h index 2e36de714..33355d52c 100644 --- a/gympp/include/gympp/Robot.h +++ b/cpp/gympp/base/include/gympp/base/Robot.h @@ -6,8 +6,8 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_ROBOT_H -#define GYMPP_ROBOT_H +#ifndef GYMPP_BASE_ROBOT_H +#define GYMPP_BASE_ROBOT_H #include #include @@ -17,32 +17,36 @@ #include namespace gympp { - class Robot; - using RobotPtr = std::shared_ptr; - - struct PID; - struct Limit; - struct Pose; - struct Velocity6D; - struct ContactData; - struct Acceleration6D; - enum class JointControlMode - { - Position, - PositionInterpolated, - Velocity, - Torque, - }; - enum class JointType - { - Invalid, - Fixed, - Revolute, - Prismatic, - }; + namespace base { + class Robot; + using RobotPtr = std::shared_ptr; + + struct PID; + struct Limit; + struct Pose; + struct Velocity6D; + struct ContactData; + struct Acceleration6D; + enum class JointControlMode + { + Position, + PositionInterpolated, + Velocity, + Torque, + }; + enum class JointType + { + Invalid, + Fixed, + Revolute, + Prismatic, + Ball, + }; + } // namespace base + } // namespace gympp -struct gympp::PID +struct gympp::base::PID { PID() = default; PID(double _p, double _i, double _d) @@ -56,31 +60,38 @@ struct gympp::PID double d; }; -struct gympp::Pose +struct gympp::base::Pose { - std::array position; - std::array orientation; + Pose() = default; + Pose(std::array p, std::array o) + : position(p) + , orientation(o) + {} + + // TODO: constructor + std::array position = {0, 0, 0}; + std::array orientation = {1, 0, 0, 0}; }; -struct gympp::Velocity6D +struct gympp::base::Velocity6D { std::array linear; std::array angular; }; -struct gympp::Acceleration6D +struct gympp::base::Acceleration6D { std::array linear; std::array angular; }; -struct gympp::Limit +struct gympp::base::Limit { double min = std::numeric_limits::lowest(); double max = std::numeric_limits::max(); }; -struct gympp::ContactData +struct gympp::base::ContactData { std::string bodyA; std::string bodyB; @@ -90,7 +101,7 @@ struct gympp::ContactData std::array position; }; -class gympp::Robot +class gympp::base::Robot { public: using VectorContainer = std::vector; @@ -104,7 +115,7 @@ class gympp::Robot using JointPositions = VectorContainer; using JointVelocities = VectorContainer; - using PID = gympp::PID; + using PID = gympp::base::PID; using StepSize = std::chrono::duration; Robot() = default; @@ -194,4 +205,4 @@ class gympp::Robot virtual std::array baseWrench() = 0; }; -#endif // GYMPP_ROBOT_H +#endif // GYMPP_BASE_ROBOT_H diff --git a/gympp/include/gympp/Space.h b/cpp/gympp/base/include/gympp/base/Space.h similarity index 64% rename from gympp/include/gympp/Space.h rename to cpp/gympp/base/include/gympp/base/Space.h index 69adf1a0b..1b9e5d409 100644 --- a/gympp/include/gympp/Space.h +++ b/cpp/gympp/base/include/gympp/base/Space.h @@ -6,27 +6,29 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_SPACES_SPACE -#define GYMPP_SPACES_SPACE +#ifndef GYMPP_BASE_SPACES_SPACE +#define GYMPP_BASE_SPACES_SPACE -#include "gympp/Common.h" +#include "gympp/base/Common.h" #include #include namespace gympp { - namespace spaces { - class Box; - class Space; - class Discrete; - using SpacePtr = std::shared_ptr; - } // namespace spaces + namespace base { + namespace spaces { + class Box; + class Space; + class Discrete; + using SpacePtr = std::shared_ptr; + } // namespace spaces + } // namespace base } // namespace gympp -class gympp::spaces::Space +class gympp::base::spaces::Space { public: - using Sample = gympp::data::Sample; + using Sample = gympp::base::data::Sample; Space() = default; virtual ~Space() = default; @@ -42,13 +44,13 @@ class gympp::spaces::Space // from_jsonable }; -class gympp::spaces::Box : public gympp::spaces::Space +class gympp::base::spaces::Box : public gympp::base::spaces::Space { public: - using Shape = gympp::data::Shape; - using Buffer = typename gympp::BufferContainer::type; + using Shape = gympp::base::data::Shape; + using Buffer = typename gympp::base::BufferContainer::type; using Limit = Buffer; - using Sample = gympp::data::Sample; + using Sample = gympp::base::data::Sample; Box() = delete; Box(const DataSupport low, const DataSupport high, const Shape& shape); @@ -67,11 +69,11 @@ class gympp::spaces::Box : public gympp::spaces::Space std::unique_ptr> pImpl; }; -class gympp::spaces::Discrete : public gympp::spaces::Space +class gympp::base::spaces::Discrete : public gympp::base::spaces::Space { public: using Type = int; - using Shape = gympp::data::Shape; + using Shape = gympp::base::data::Shape; Discrete() = delete; Discrete(size_t n); @@ -88,4 +90,4 @@ class gympp::spaces::Discrete : public gympp::spaces::Space std::unique_ptr> pImpl; }; -#endif // GYMPP_SPACES_SPACE +#endif // GYMPP_BASE_SPACES_SPACE diff --git a/ignition/include/gympp/gazebo/Task.h b/cpp/gympp/base/include/gympp/base/Task.h similarity index 68% rename from ignition/include/gympp/gazebo/Task.h rename to cpp/gympp/base/include/gympp/base/Task.h index 6856c38c2..cedc11a8b 100644 --- a/ignition/include/gympp/gazebo/Task.h +++ b/cpp/gympp/base/include/gympp/base/Task.h @@ -6,24 +6,24 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_GAZEBO_TASK_H -#define GYMPP_GAZEBO_TASK_H +#ifndef GYMPP_BASE_TASK_H +#define GYMPP_BASE_TASK_H -#include "gympp/Environment.h" +#include "gympp/base/Environment.h" #include namespace gympp { - namespace gazebo { + namespace base { class Task; } // namespace gazebo } // namespace gympp -class gympp::gazebo::Task +class gympp::base::Task { public: - using Action = gympp::Environment::Action; - using Observation = gympp::Environment::Observation; - using Reward = gympp::Environment::Reward; + using Action = gympp::base::Environment::Action; + using Observation = gympp::base::Environment::Observation; + using Reward = gympp::base::Environment::Reward; virtual ~Task() = default; diff --git a/ignition/include/gympp/gazebo/TaskSingleton.h b/cpp/gympp/base/include/gympp/base/TaskSingleton.h similarity index 60% rename from ignition/include/gympp/gazebo/TaskSingleton.h rename to cpp/gympp/base/include/gympp/base/TaskSingleton.h index 07c4ca393..6f432509b 100644 --- a/ignition/include/gympp/gazebo/TaskSingleton.h +++ b/cpp/gympp/base/include/gympp/base/TaskSingleton.h @@ -6,42 +6,41 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_ROBOT_TASKSINGLETON_H -#define GYMPP_ROBOT_TASKSINGLETON_H +#ifndef GYMPP_BASE_TASKSINGLETON_H +#define GYMPP_BASE_TASKSINGLETON_H -#include #include #include #include namespace gympp { - namespace gazebo { + namespace base { class Task; class TaskSingleton; - } // namespace gazebo + } // namespace base } // namespace gympp -class gympp::gazebo::TaskSingleton +class gympp::base::TaskSingleton { private: class Impl; - std::unique_ptr> pImpl; + std::unique_ptr pImpl; protected: public: using TaskName = std::string; TaskSingleton(); - ~TaskSingleton() = default; + ~TaskSingleton(); TaskSingleton(TaskSingleton&) = delete; void operator=(const TaskSingleton&) = delete; static TaskSingleton& get(); - gympp::gazebo::Task* getTask(const TaskName& taskName); + gympp::base::Task* getTask(const TaskName& taskName); - bool storeTask(const TaskName& taskName, gympp::gazebo::Task* task); + bool storeTask(const TaskName& taskName, gympp::base::Task* task); bool removeTask(const TaskName& taskName); }; -#endif // GYMPP_ROBOT_TASKSINGLETON_H +#endif // GYMPP_BASE_TASKSINGLETON_H diff --git a/cpp/gympp/gazebo/CMakeLists.txt b/cpp/gympp/gazebo/CMakeLists.txt new file mode 100644 index 000000000..cc9215cfb --- /dev/null +++ b/cpp/gympp/gazebo/CMakeLists.txt @@ -0,0 +1,118 @@ +# Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT). All rights reserved. +# This software may be modified and distributed under the terms of the +# GNU Lesser General Public License v2.1 or any later version. + +# ============== +# IGNITION ROBOT +# ============== + +add_library(IgnitionRobot + include/gympp/gazebo/IgnitionRobot.h + IgnitionRobot.cpp) + +target_link_libraries(IgnitionRobot + PUBLIC + gympp + ignition-gazebo3::core + PRIVATE + RobotSingleton + ExtraComponents) + +set_target_properties(IgnitionRobot PROPERTIES + PUBLIC_HEADER include/gympp/gazebo/IgnitionRobot.h) + +target_include_directories(IgnitionRobot PUBLIC + $ + $) + +# =============== +# ROBOT SINGLETON +# =============== + +add_library(RobotSingleton + include/gympp/gazebo/RobotSingleton.h + RobotSingleton.cpp) + +target_link_libraries(RobotSingleton + PUBLIC + gympp + PRIVATE + ignition-gazebo3::core + ExtraComponents) + +if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") + set_target_properties(RobotSingleton PROPERTIES + PUBLIC_HEADER include/gympp/gazebo/RobotSingleton.h) +endif() + +target_include_directories(RobotSingleton PUBLIC + $ + $) + +# ================== +# GAZEBO ENVIRONMENT +# ================== + +add_library(GazeboEnvironment + include/gympp/gazebo/GazeboEnvironment.h + GazeboEnvironment.cpp) + +target_link_libraries(GazeboEnvironment + PUBLIC + gympp + GazeboWrapper + PRIVATE + Task + TaskSingleton + ignition-gazebo3::core) + +set_target_properties(GazeboEnvironment PROPERTIES + PUBLIC_HEADER include/gympp/gazebo/GazeboEnvironment.h) + +target_include_directories(GazeboEnvironment PUBLIC + $ + $) + +# =========== +# GYM FACTORY +# =========== + +set(GYMFACTORY_PUBLIC_HEADERS + include/gympp/gazebo/GymFactory.h + include/gympp/gazebo/Metadata.h) + +add_library(GymFactory + ${GYMFACTORY_PUBLIC_HEADERS} + GymFactory.cpp) + +target_link_libraries(GymFactory + PUBLIC + gympp + GazeboEnvironment + ignition-gazebo3::core) + +set_target_properties(GymFactory PROPERTIES + PUBLIC_HEADER + "${GYMFACTORY_PUBLIC_HEADERS}") + +target_include_directories(GymFactory PUBLIC + $ + $) + +# =================== +# INSTALL THE TARGETS +# =================== + +if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") + install( + TARGETS + IgnitionRobot + RobotSingleton + GazeboEnvironment + GymFactory + EXPORT gympp + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/gympp/gazebo) +endif() diff --git a/ignition/src/IgnitionEnvironment.cpp b/cpp/gympp/gazebo/GazeboEnvironment.cpp similarity index 81% rename from ignition/src/IgnitionEnvironment.cpp rename to cpp/gympp/gazebo/GazeboEnvironment.cpp index 4eb921840..6256d1354 100644 --- a/ignition/src/IgnitionEnvironment.cpp +++ b/cpp/gympp/gazebo/GazeboEnvironment.cpp @@ -6,12 +6,12 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#include "gympp/gazebo/IgnitionEnvironment.h" -#include "gympp/Log.h" -#include "gympp/Random.h" -#include "gympp/Space.h" -#include "gympp/gazebo/Task.h" -#include "gympp/gazebo/TaskSingleton.h" +#include "gympp/gazebo/GazeboEnvironment.h" +#include "gympp/base/Log.h" +#include "gympp/base/Random.h" +#include "gympp/base/Space.h" +#include "gympp/base/Task.h" +#include "gympp/base/TaskSingleton.h" #include #include @@ -22,11 +22,11 @@ using namespace gympp::gazebo; -class IgnitionEnvironment::Impl +class GazeboEnvironment::Impl { public: size_t id; - gympp::gazebo::Task* task = nullptr; + gympp::base::Task* task = nullptr; PluginData pluginData; gympp::gazebo::ModelInitData modelData; @@ -36,7 +36,7 @@ class IgnitionEnvironment::Impl // IGNITION ENVIRONMENT // ==================== -bool IgnitionEnvironment::initializeSimulation() +bool GazeboEnvironment::initializeSimulation() { // If the server is already running it means that the simulation has been already initialized if (GazeboWrapper::initialized()) { @@ -83,36 +83,36 @@ bool IgnitionEnvironment::initializeSimulation() return true; } -Task* IgnitionEnvironment::getTask() +gympp::base::Task* GazeboEnvironment::getTask() { if (!pImpl->task) { - auto& taskSingleton = TaskSingleton::get(); + auto& taskSingleton = base::TaskSingleton::get(); pImpl->task = taskSingleton.getTask(pImpl->modelData.modelName); } return pImpl->task; } -void IgnitionEnvironment::storeModelData(const gympp::gazebo::ModelInitData& modelData) +void GazeboEnvironment::storeModelData(const gympp::gazebo::ModelInitData& modelData) { pImpl->modelData = modelData; } -void IgnitionEnvironment::storePluginData(const PluginData& pluginData) +void GazeboEnvironment::storePluginData(const PluginData& pluginData) { pImpl->pluginData = pluginData; } -IgnitionEnvironment::IgnitionEnvironment(const ActionSpacePtr aSpace, - const ObservationSpacePtr oSpace, - const double agentUpdateRate, - const double realTimeFactor, - const double physicsUpdateRate) +GazeboEnvironment::GazeboEnvironment(const ActionSpacePtr aSpace, + const ObservationSpacePtr oSpace, + const double agentUpdateRate, + const double realTimeFactor, + const double physicsUpdateRate) : Environment(aSpace, oSpace) , GazeboWrapper(static_cast(physicsUpdateRate / agentUpdateRate), realTimeFactor, physicsUpdateRate) - , pImpl{new IgnitionEnvironment::Impl, [](Impl* impl) { delete impl; }} + , pImpl{new GazeboEnvironment::Impl} { gymppDebug << "Configuring gazebo for an agent running at " << agentUpdateRate << " Hz" << std::endl; @@ -127,7 +127,9 @@ IgnitionEnvironment::IgnitionEnvironment(const ActionSpacePtr aSpace, } } -std::optional IgnitionEnvironment::step(const Action& action) +GazeboEnvironment::~GazeboEnvironment() = default; + +std::optional GazeboEnvironment::step(const Action& action) { assert(action_space); assert(observation_space); @@ -192,24 +194,24 @@ std::optional IgnitionEnvironment::step(const Action return {}; } - return IgnitionEnvironment::State{task->isDone(), {}, reward.value(), observation.value()}; + return GazeboEnvironment::State{task->isDone(), {}, reward.value(), observation.value()}; } -std::vector IgnitionEnvironment::seed(size_t seed) +std::vector GazeboEnvironment::seed(size_t seed) { if (seed != 0) { - gympp::Random::setSeed(seed); + gympp::base::Random::setSeed(seed); } return {seed}; } -gympp::EnvironmentPtr IgnitionEnvironment::env() +gympp::base::EnvironmentPtr GazeboEnvironment::env() { return shared_from_this(); } -std::optional IgnitionEnvironment::reset() +std::optional GazeboEnvironment::reset() { // Check if the gazebo server is running. If reset() is executed as first method, // the server is initialized lazily. @@ -235,7 +237,7 @@ std::optional IgnitionEnvironment::reset() return task->getObservation(); } -bool IgnitionEnvironment::render(RenderMode mode) +bool GazeboEnvironment::render(RenderMode mode) { gymppDebug << "Rendering the environment" << std::endl; diff --git a/plugins/GymFactory/src/GymFactory.cpp b/cpp/gympp/gazebo/GymFactory.cpp similarity index 67% rename from plugins/GymFactory/src/GymFactory.cpp rename to cpp/gympp/gazebo/GymFactory.cpp index 2ff505af8..2599dc82f 100644 --- a/plugins/GymFactory/src/GymFactory.cpp +++ b/cpp/gympp/gazebo/GymFactory.cpp @@ -6,18 +6,19 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#include "gympp/GymFactory.h" -#include "gympp/Log.h" -#include "gympp/Metadata.h" -#include "gympp/Space.h" -#include "gympp/gazebo/IgnitionEnvironment.h" +#include "gympp/gazebo/GymFactory.h" +#include "gympp/base/Log.h" +#include "gympp/base/Space.h" +#include "gympp/gazebo/GazeboEnvironment.h" +#include "gympp/gazebo/Metadata.h" #include "sdf/Root.hh" #include #include #include -using namespace gympp; +using namespace gympp::base; +using namespace gympp::gazebo; class GymFactory::Impl { @@ -30,23 +31,23 @@ class GymFactory::Impl std::unordered_map plugins; }; -gympp::spaces::SpacePtr gympp::GymFactory::Impl::makeSpace(const SpaceMetadata& md) +gympp::base::spaces::SpacePtr GymFactory::Impl::makeSpace(const SpaceMetadata& md) { assert(md.isValid()); - gympp::spaces::SpacePtr space; + gympp::base::spaces::SpacePtr space; switch (md.type) { - case gympp::SpaceType::Box: { + case gympp::gazebo::SpaceType::Box: { if (md.dims.empty()) { - space = std::make_shared(md.low, md.high); + space = std::make_shared(md.low, md.high); } else { - space = std::make_shared(md.low[0], md.high[0], md.dims); + space = std::make_shared(md.low[0], md.high[0], md.dims); } break; } - case gympp::SpaceType::Discrete: { - space = std::make_shared(md.dims[0]); + case gympp::gazebo::SpaceType::Discrete: { + space = std::make_shared(md.dims[0]); break; } } @@ -54,11 +55,13 @@ gympp::spaces::SpacePtr gympp::GymFactory::Impl::makeSpace(const SpaceMetadata& return space; } -gympp::GymFactory::GymFactory() - : pImpl{new Impl(), [](Impl* impl) { delete impl; }} +GymFactory::GymFactory() + : pImpl{new Impl()} {} -gympp::EnvironmentPtr gympp::GymFactory::make(const std::string& envName) +GymFactory::~GymFactory() = default; + +gympp::base::EnvironmentPtr GymFactory::make(const std::string& envName) { if (!pImpl->exists(envName)) { gymppError << "Environment '" << envName << "' has never been registered" << std::endl; @@ -77,12 +80,11 @@ gympp::EnvironmentPtr gympp::GymFactory::make(const std::string& envName) } // Create the environment - auto ignGym = - std::make_shared(actionSpace, - observationSpace, - md.agentRate, - md.getPhysicsData().rtf, - 1 / md.getPhysicsData().maxStepSize); + auto ignGym = std::make_shared(actionSpace, + observationSpace, + md.agentRate, + md.getPhysicsData().rtf, + 1 / md.getPhysicsData().maxStepSize); // Setup the world if (!ignGym->setupGazeboWorld(md.worldFileName)) { @@ -117,7 +119,7 @@ gympp::EnvironmentPtr gympp::GymFactory::make(const std::string& envName) return ignGym->env(); } -bool gympp::GymFactory::registerPlugin(const PluginMetadata& md) +bool GymFactory::registerPlugin(const PluginMetadata& md) { if (!md.isValid()) { gymppError << "The plugin metadata is not valid" << std::endl; diff --git a/ignition/src/IgnitionRobot.cpp b/cpp/gympp/gazebo/IgnitionRobot.cpp similarity index 90% rename from ignition/src/IgnitionRobot.cpp rename to cpp/gympp/gazebo/IgnitionRobot.cpp index 94b06ffc0..8a3ee2502 100644 --- a/ignition/src/IgnitionRobot.cpp +++ b/cpp/gympp/gazebo/IgnitionRobot.cpp @@ -7,7 +7,7 @@ */ #include "gympp/gazebo/IgnitionRobot.h" -#include "gympp/Log.h" +#include "gympp/base/Log.h" #include "gympp/gazebo/RobotSingleton.h" #include "gympp/gazebo/components/JointPositionReset.h" #include "gympp/gazebo/components/JointVelocityReset.h" @@ -58,7 +58,7 @@ using JointEntity = ignition::gazebo::Entity; const std::string World2BaseJoint = "world2base"; const ignition::math::PID DefaultPID(1, 0.1, 0.01, 1, -1, 10000, -10000); -const gympp::JointControlMode DefaultJointControlMode = gympp::JointControlMode::Torque; +const gympp::base::JointControlMode DefaultJointControlMode = gympp::base::JointControlMode::Torque; class IgnitionRobot::Impl { @@ -67,17 +67,17 @@ class IgnitionRobot::Impl { struct { - gympp::Robot::JointPositions positions; - gympp::Robot::JointVelocities velocities; + gympp::base::Robot::JointPositions positions; + gympp::base::Robot::JointVelocities velocities; std::map references; std::map pid; - std::map controlMode; + std::map controlMode; std::map appliedForces; std::map effortLimits; } joints; struct { - std::unordered_map> contacts; + std::unordered_map> contacts; } links; } buffers; @@ -122,8 +122,8 @@ class IgnitionRobot::Impl template ComponentType& getOrCreateComponent(const ignition::gazebo::Entity entity); - static gympp::Pose fromIgnitionMath(const ignition::math::Pose3d& ignitionPose); - static ignition::math::Pose3d toIgnitionMath(const gympp::Pose& pose); + static gympp::base::Pose fromIgnitionMath(const ignition::math::Pose3d& ignitionPose); + static ignition::math::Pose3d toIgnitionMath(const gympp::base::Pose& pose); static inline std::array fromIgnitionMath(const ignition::math::Vector3d& ignitionVector) @@ -136,7 +136,7 @@ class IgnitionRobot::Impl return {vector[0], vector[1], vector[2]}; } - static inline ignition::math::PID toIgnitionMath(const gympp::PID& pid) + static inline ignition::math::PID toIgnitionMath(const gympp::base::PID& pid) { return {pid.p, pid.i, pid.d}; } @@ -243,7 +243,7 @@ void IgnitionRobot::Impl::gatherLinksContactData() + "::" + getEntityName(contactData.collision2().id()); // Extract the contact data - ContactData contact; + base::ContactData contact; contact.bodyA = scopedBodyA; contact.bodyB = scopedBodyB; contact.position[0] = contactData.position(i).x(); @@ -265,9 +265,9 @@ void IgnitionRobot::Impl::gatherLinksContactData() }); } -gympp::Pose IgnitionRobot::Impl::fromIgnitionMath(const ignition::math::Pose3d& ignitionPose) +gympp::base::Pose IgnitionRobot::Impl::fromIgnitionMath(const ignition::math::Pose3d& ignitionPose) { - gympp::Pose pose; + gympp::base::Pose pose; pose.position[0] = ignitionPose.Pos().X(); pose.position[1] = ignitionPose.Pos().Y(); pose.position[2] = ignitionPose.Pos().Z(); @@ -278,7 +278,7 @@ gympp::Pose IgnitionRobot::Impl::fromIgnitionMath(const ignition::math::Pose3d& return pose; } -ignition::math::Pose3d IgnitionRobot::Impl::toIgnitionMath(const gympp::Pose& pose) +ignition::math::Pose3d IgnitionRobot::Impl::toIgnitionMath(const gympp::base::Pose& pose) { ignition::math::Pose3d ignitionPose; ignitionPose.Pos() = @@ -306,7 +306,7 @@ ComponentTypeT& IgnitionRobot::Impl::getOrCreateComponent(const ignition::gazebo // ============== IgnitionRobot::IgnitionRobot() - : pImpl{new Impl(), [](Impl* impl) { delete impl; }} + : pImpl{new Impl()} {} IgnitionRobot::~IgnitionRobot() = default; @@ -494,12 +494,12 @@ size_t IgnitionRobot::dofs() const // GET METHODS // =========== -gympp::Robot::RobotName IgnitionRobot::name() const +gympp::base::Robot::RobotName IgnitionRobot::name() const { return pImpl->name; } -gympp::Robot::JointNames IgnitionRobot::jointNames() const +gympp::base::Robot::JointNames IgnitionRobot::jointNames() const { JointNames names; names.reserve(pImpl->joints.size()); @@ -511,40 +511,41 @@ gympp::Robot::JointNames IgnitionRobot::jointNames() const return names; } -gympp::JointType IgnitionRobot::jointType(const gympp::Robot::JointName& jointName) const +gympp::base::JointType +IgnitionRobot::jointType(const gympp::base::Robot::JointName& jointName) const { JointEntity jointEntity = pImpl->getJointEntity(jointName); if (jointEntity == ignition::gazebo::kNullEntity) { - return JointType::Invalid; + return base::JointType::Invalid; } auto typeComponent = pImpl->ecm->Component(jointEntity); assert(typeComponent); - gympp::JointType returnedType; + gympp::base::JointType returnedType; sdf::JointType type = typeComponent->Data(); switch (type) { case sdf::JointType::FIXED: - returnedType = JointType::Fixed; + returnedType = base::JointType::Fixed; break; case sdf::JointType::REVOLUTE: - returnedType = JointType::Revolute; + returnedType = base::JointType::Revolute; break; case sdf::JointType::PRISMATIC: - returnedType = JointType::Prismatic; + returnedType = base::JointType::Prismatic; break; default: gymppError << "Joint type not recognized" << std::endl; - returnedType = JointType::Invalid; + returnedType = base::JointType::Invalid; break; } return returnedType; } -double IgnitionRobot::jointForce(const gympp::Robot::JointName& jointName) const +double IgnitionRobot::jointForce(const gympp::base::Robot::JointName& jointName) const { JointEntity jointEntity = pImpl->getJointEntity(jointName); if (jointEntity == ignition::gazebo::kNullEntity) { @@ -566,7 +567,7 @@ double IgnitionRobot::jointForce(const gympp::Robot::JointName& jointName) const return jointForceComponent->Data().front(); } -double IgnitionRobot::jointPosition(const gympp::Robot::JointName& jointName) const +double IgnitionRobot::jointPosition(const gympp::base::Robot::JointName& jointName) const { JointEntity jointEntity = pImpl->getJointEntity(jointName); assert(jointEntity != ignition::gazebo::kNullEntity); @@ -592,7 +593,7 @@ double IgnitionRobot::jointPosition(const gympp::Robot::JointName& jointName) co return jointPositionComponent->Data()[0]; } -double IgnitionRobot::jointVelocity(const gympp::Robot::JointName& jointName) const +double IgnitionRobot::jointVelocity(const gympp::base::Robot::JointName& jointName) const { JointEntity jointEntity = pImpl->getJointEntity(jointName); assert(jointEntity != ignition::gazebo::kNullEntity); @@ -618,13 +619,13 @@ double IgnitionRobot::jointVelocity(const gympp::Robot::JointName& jointName) co return jointVelocityComponent->Data()[0]; } -gympp::JointControlMode -IgnitionRobot::jointControlMode(const gympp::Robot::JointName& jointName) const +gympp::base::JointControlMode +IgnitionRobot::jointControlMode(const gympp::base::Robot::JointName& jointName) const { return pImpl->buffers.joints.controlMode[jointName]; } -gympp::Robot::JointPositions IgnitionRobot::jointPositions() const +gympp::base::Robot::JointPositions IgnitionRobot::jointPositions() const { size_t i = 0; for (const auto& [jointName, _] : pImpl->joints) { @@ -634,7 +635,7 @@ gympp::Robot::JointPositions IgnitionRobot::jointPositions() const return pImpl->buffers.joints.positions; } -gympp::Robot::JointVelocities IgnitionRobot::jointVelocities() const +gympp::base::Robot::JointVelocities IgnitionRobot::jointVelocities() const { size_t i = 0; for (const auto& [jointName, _] : pImpl->joints) { @@ -644,7 +645,7 @@ gympp::Robot::JointVelocities IgnitionRobot::jointVelocities() const return pImpl->buffers.joints.velocities; } -gympp::Robot::JointPositions IgnitionRobot::initialJointPositions() const +gympp::base::Robot::JointPositions IgnitionRobot::initialJointPositions() const { JointPositions initialJointPositions; initialJointPositions.reserve(dofs()); @@ -669,7 +670,7 @@ gympp::Robot::JointPositions IgnitionRobot::initialJointPositions() const return initialJointPositions; } -double IgnitionRobot::jointEffortLimit(const gympp::Robot::JointName& jointName) const +double IgnitionRobot::jointEffortLimit(const gympp::base::Robot::JointName& jointName) const { JointEntity jointEntity = pImpl->getJointEntity(jointName); if (jointEntity == ignition::gazebo::kNullEntity) { @@ -690,7 +691,8 @@ double IgnitionRobot::jointEffortLimit(const gympp::Robot::JointName& jointName) return effortLimits[jointName]; } -gympp::Limit IgnitionRobot::jointPositionLimits(const gympp::Robot::JointName& jointName) const +gympp::base::Limit +IgnitionRobot::jointPositionLimits(const gympp::base::Robot::JointName& jointName) const { JointEntity jointEntity = pImpl->getJointEntity(jointName); assert(jointEntity != ignition::gazebo::kNullEntity); @@ -705,19 +707,20 @@ gympp::Limit IgnitionRobot::jointPositionLimits(const gympp::Robot::JointName& j return {}; } - gympp::Limit limit; + gympp::base::Limit limit; limit.min = jointAxisComponent->Data().Lower(); limit.max = jointAxisComponent->Data().Upper(); return limit; } -gympp::Robot::StepSize IgnitionRobot::dt() const +gympp::base::Robot::StepSize IgnitionRobot::dt() const { return pImpl->dt; } -gympp::Robot::PID IgnitionRobot::jointPID(const gympp::Robot::JointName& jointName) const +gympp::base::Robot::PID +IgnitionRobot::jointPID(const gympp::base::Robot::JointName& jointName) const { assert(pImpl->jointExists(jointName)); assert(pImpl->pidExists(jointName)); @@ -726,7 +729,7 @@ gympp::Robot::PID IgnitionRobot::jointPID(const gympp::Robot::JointName& jointNa return PID(pid.PGain(), pid.IGain(), pid.DGain()); } -gympp::Robot::LinkNames IgnitionRobot::linksInContact() const +gympp::base::Robot::LinkNames IgnitionRobot::linksInContact() const { // Acquire the data pImpl->gatherLinksContactData(); @@ -743,14 +746,14 @@ gympp::Robot::LinkNames IgnitionRobot::linksInContact() const return linksInContact; } -std::vector -IgnitionRobot::contactData(const gympp::Robot::LinkName& linkName) const +std::vector +IgnitionRobot::contactData(const gympp::base::Robot::LinkName& linkName) const { pImpl->gatherLinksContactData(); return pImpl->buffers.links.contacts.at(linkName); } -gympp::Robot::LinkNames IgnitionRobot::linkNames() const +gympp::base::Robot::LinkNames IgnitionRobot::linkNames() const { LinkNames names; names.reserve(pImpl->links.size()); @@ -762,7 +765,7 @@ gympp::Robot::LinkNames IgnitionRobot::linkNames() const return names; } -gympp::Pose IgnitionRobot::linkPose(const gympp::Robot::LinkName& linkName) const +gympp::base::Pose IgnitionRobot::linkPose(const gympp::base::Robot::LinkName& linkName) const { LinkEntity linkEntity = pImpl->getLinkEntity(linkName); assert(linkEntity != ignition::gazebo::kNullEntity); @@ -778,7 +781,8 @@ gympp::Pose IgnitionRobot::linkPose(const gympp::Robot::LinkName& linkName) cons return Impl::fromIgnitionMath(linkWorldPoseGazebo); } -gympp::Velocity6D IgnitionRobot::linkVelocity(const gympp::Robot::LinkName& linkName) const +gympp::base::Velocity6D +IgnitionRobot::linkVelocity(const gympp::base::Robot::LinkName& linkName) const { LinkEntity linkEntity = pImpl->getLinkEntity(linkName); assert(linkEntity != ignition::gazebo::kNullEntity); @@ -798,14 +802,15 @@ gympp::Velocity6D IgnitionRobot::linkVelocity(const gympp::Robot::LinkName& link const ignition::math::Vector3d& linkWorldAngVel = linkWorldAngVelComponent->Data(); // Fill the output data structure - gympp::Velocity6D linkWorldVelocity; + gympp::base::Velocity6D linkWorldVelocity; linkWorldVelocity.linear = Impl::fromIgnitionMath(linkWorldLinVel); linkWorldVelocity.angular = Impl::fromIgnitionMath(linkWorldAngVel); return linkWorldVelocity; } -gympp::Acceleration6D IgnitionRobot::linkAcceleration(const gympp::Robot::LinkName& linkName) const +gympp::base::Acceleration6D +IgnitionRobot::linkAcceleration(const gympp::base::Robot::LinkName& linkName) const { LinkEntity linkEntity = pImpl->getLinkEntity(linkName); assert(linkEntity != ignition::gazebo::kNullEntity); @@ -825,14 +830,15 @@ gympp::Acceleration6D IgnitionRobot::linkAcceleration(const gympp::Robot::LinkNa const ignition::math::Vector3d& linkWorldAngAcc = linkWorldAngAccComponent->Data(); // Fill the output data structure - gympp::Acceleration6D linkWorldAcceleration; + gympp::base::Acceleration6D linkWorldAcceleration; linkWorldAcceleration.linear = Impl::fromIgnitionMath(linkWorldLinAcc); linkWorldAcceleration.angular = Impl::fromIgnitionMath(linkWorldAngAcc); return linkWorldAcceleration; } -gympp::Velocity6D IgnitionRobot::linkBodyFixedVelocity(const gympp::Robot::LinkName& linkName) const +gympp::base::Velocity6D +IgnitionRobot::linkBodyFixedVelocity(const gympp::base::Robot::LinkName& linkName) const { LinkEntity linkEntity = pImpl->getLinkEntity(linkName); assert(linkEntity != ignition::gazebo::kNullEntity); @@ -852,15 +858,15 @@ gympp::Velocity6D IgnitionRobot::linkBodyFixedVelocity(const gympp::Robot::LinkN const ignition::math::Vector3d& linkBodyAngVel = linkBodyAngVelComponent->Data(); // Fill the output data structure - gympp::Velocity6D linkBodyVelocity; + gympp::base::Velocity6D linkBodyVelocity; linkBodyVelocity.linear = Impl::fromIgnitionMath(linkBodyLinVel); linkBodyVelocity.angular = Impl::fromIgnitionMath(linkBodyAngVel); return linkBodyVelocity; } -gympp::Acceleration6D -IgnitionRobot::linkBodyFixedAcceleration(const gympp::Robot::LinkName& linkName) const +gympp::base::Acceleration6D +IgnitionRobot::linkBodyFixedAcceleration(const gympp::base::Robot::LinkName& linkName) const { LinkEntity linkEntity = pImpl->getLinkEntity(linkName); assert(linkEntity != ignition::gazebo::kNullEntity); @@ -880,14 +886,14 @@ IgnitionRobot::linkBodyFixedAcceleration(const gympp::Robot::LinkName& linkName) const ignition::math::Vector3d& linkBodyAngAcc = linkBodyAngAccComponent->Data(); // Fill the output data structure - gympp::Acceleration6D linkBodyAcceleration; + gympp::base::Acceleration6D linkBodyAcceleration; linkBodyAcceleration.linear = Impl::fromIgnitionMath(linkBodyLinAcc); linkBodyAcceleration.angular = Impl::fromIgnitionMath(linkBodyAngAcc); return linkBodyAcceleration; } -bool IgnitionRobot::setdt(const gympp::Robot::StepSize& stepSize) +bool IgnitionRobot::setdt(const gympp::base::Robot::StepSize& stepSize) { pImpl->dt = stepSize; return true; @@ -897,7 +903,8 @@ bool IgnitionRobot::setdt(const gympp::Robot::StepSize& stepSize) // SET METHODS // =========== -bool IgnitionRobot::setJointForce(const gympp::Robot::JointName& jointName, const double jointForce) +bool IgnitionRobot::setJointForce(const gympp::base::Robot::JointName& jointName, + const double jointForce) { JointEntity jointEntity = pImpl->getJointEntity(jointName); if (jointEntity == ignition::gazebo::kNullEntity) { @@ -914,7 +921,7 @@ bool IgnitionRobot::setJointForce(const gympp::Robot::JointName& jointName, cons return true; } -bool IgnitionRobot::setJointEffortLimit(const gympp::Robot::JointName& jointName, +bool IgnitionRobot::setJointEffortLimit(const gympp::base::Robot::JointName& jointName, const double effortLimit) { JointEntity jointEntity = pImpl->getJointEntity(jointName); @@ -931,7 +938,7 @@ bool IgnitionRobot::setJointEffortLimit(const gympp::Robot::JointName& jointName return true; } -bool IgnitionRobot::setJointPositionTarget(const gympp::Robot::JointName& jointName, +bool IgnitionRobot::setJointPositionTarget(const gympp::base::Robot::JointName& jointName, const double jointPositionReference) { // The controller period must have been set in order to set references @@ -940,7 +947,7 @@ bool IgnitionRobot::setJointPositionTarget(const gympp::Robot::JointName& jointN return false; } - if (jointControlMode(jointName) != JointControlMode::Position) { + if (jointControlMode(jointName) != base::JointControlMode::Position) { gymppError << "Cannot set the position target of joint '" << jointName << "' not controlled in Position" << std::endl; return false; @@ -961,7 +968,7 @@ bool IgnitionRobot::setJointPositionTarget(const gympp::Robot::JointName& jointN return true; } -bool IgnitionRobot::setJointVelocityTarget(const gympp::Robot::JointName& jointName, +bool IgnitionRobot::setJointVelocityTarget(const gympp::base::Robot::JointName& jointName, const double jointVelocityReference) { // The controller period must have been set in order to set references @@ -970,7 +977,7 @@ bool IgnitionRobot::setJointVelocityTarget(const gympp::Robot::JointName& jointN return false; } - if (jointControlMode(jointName) != JointControlMode::Velocity) { + if (jointControlMode(jointName) != base::JointControlMode::Velocity) { gymppError << "Cannot set the velocity target of joint '" << jointName << "' not controlled in Velocity" << std::endl; return false; @@ -992,7 +999,7 @@ bool IgnitionRobot::setJointVelocityTarget(const gympp::Robot::JointName& jointN return true; } -bool IgnitionRobot::setJointPosition(const gympp::Robot::JointName& jointName, +bool IgnitionRobot::setJointPosition(const gympp::base::Robot::JointName& jointName, const double jointPosition) { JointEntity jointEntity = pImpl->getJointEntity(jointName); @@ -1017,7 +1024,7 @@ bool IgnitionRobot::setJointPosition(const gympp::Robot::JointName& jointName, return true; } -bool IgnitionRobot::setJointVelocity(const gympp::Robot::JointName& jointName, +bool IgnitionRobot::setJointVelocity(const gympp::base::Robot::JointName& jointName, const double jointVelocity) { JointEntity jointEntity = pImpl->getJointEntity(jointName); @@ -1042,8 +1049,8 @@ bool IgnitionRobot::setJointVelocity(const gympp::Robot::JointName& jointName, return true; } -bool IgnitionRobot::setJointControlMode(const gympp::Robot::JointName& jointName, - const JointControlMode controlMode) +bool IgnitionRobot::setJointControlMode(const gympp::base::Robot::JointName& jointName, + const base::JointControlMode controlMode) { // Clean up possible old references pImpl->buffers.joints.references.erase(jointName); @@ -1056,7 +1063,7 @@ bool IgnitionRobot::setJointControlMode(const gympp::Robot::JointName& jointName return true; } -bool IgnitionRobot::setJointPID(const gympp::Robot::JointName& jointName, const PID& pid) +bool IgnitionRobot::setJointPID(const gympp::base::Robot::JointName& jointName, const PID& pid) { JointEntity jointEntity = pImpl->getJointEntity(jointName); if (jointEntity == ignition::gazebo::kNullEntity) { @@ -1068,7 +1075,7 @@ bool IgnitionRobot::setJointPID(const gympp::Robot::JointName& jointName, const return true; } -bool IgnitionRobot::resetJoint(const gympp::Robot::JointName& jointName, +bool IgnitionRobot::resetJoint(const gympp::base::Robot::JointName& jointName, const double jointPosition, const double jointVelocity) { @@ -1101,7 +1108,7 @@ bool IgnitionRobot::resetJoint(const gympp::Robot::JointName& jointName, return true; } -bool IgnitionRobot::addExternalWrench(const gympp::Robot::LinkName& linkName, +bool IgnitionRobot::addExternalWrench(const gympp::base::Robot::LinkName& linkName, const std::array& force, const std::array& torque) { @@ -1191,10 +1198,10 @@ bool IgnitionRobot::update(const std::chrono::duration& simTime) // Use the PID to get the reference switch (pImpl->buffers.joints.controlMode[jointName]) { - case JointControlMode::Position: + case base::JointControlMode::Position: force = pid.Update(jointPosition(jointName) - reference, stepTime); break; - case JointControlMode::Velocity: + case base::JointControlMode::Velocity: force = pid.Update(jointVelocity(jointName) - reference, stepTime); break; default: @@ -1240,7 +1247,7 @@ bool IgnitionRobot::update(const std::chrono::duration& simTime) // RobotBaseFrame // ============== -gympp::Robot::LinkName IgnitionRobot::baseFrame() +gympp::base::Robot::LinkName IgnitionRobot::baseFrame() { // Get all the canonical links of the model auto candidateBaseLinks = pImpl->ecm->EntitiesByComponents( @@ -1259,7 +1266,7 @@ gympp::Robot::LinkName IgnitionRobot::baseFrame() return baseLinkName; } -bool IgnitionRobot::setBaseFrame(const gympp::Robot::LinkName& baseLink) +bool IgnitionRobot::setBaseFrame(const gympp::base::Robot::LinkName& baseLink) { if (!pImpl->linkExists(baseLink)) { gymppError << "Failed to set base frame to not existing link '" << baseLink << "'" @@ -1276,7 +1283,7 @@ bool IgnitionRobot::setBaseFrame(const gympp::Robot::LinkName& baseLink) return false; } -gympp::Pose IgnitionRobot::basePose() +gympp::base::Pose IgnitionRobot::basePose() { // Get the pose component auto* modelWorldPoseComponent = @@ -1289,7 +1296,7 @@ gympp::Pose IgnitionRobot::basePose() return Impl::fromIgnitionMath(world_H_model); } -gympp::Velocity6D IgnitionRobot::baseVelocity() +gympp::base::Velocity6D IgnitionRobot::baseVelocity() { // We can get the velocity of the base link. Since there's only a rigid // transformation between base and model frame, and the velocity is expressed @@ -1299,14 +1306,14 @@ gympp::Velocity6D IgnitionRobot::baseVelocity() const LinkName& baseLink = baseFrame(); // Get the velocity of the base link - gympp::Velocity6D baseVelocity = linkVelocity(baseLink); + gympp::base::Velocity6D baseVelocity = linkVelocity(baseLink); // Convert it to ignition math objects ignition::math::Vector3d baseLinVel = Impl::toIgnitionMath(baseVelocity.linear); ignition::math::Vector3d baseAngVel = Impl::toIgnitionMath(baseVelocity.angular); // Create the output data structure - gympp::Velocity6D modelVelocity; + gympp::base::Velocity6D modelVelocity; modelVelocity.linear = {baseLinVel.X(), baseLinVel.Y(), baseLinVel.Z()}; modelVelocity.angular = {baseAngVel.X(), baseAngVel.Y(), baseAngVel.Z()}; @@ -1362,7 +1369,7 @@ bool IgnitionRobot::resetBasePose(const std::array& position, } // Construct the desired transform between world and base - gympp::Pose pose; + gympp::base::Pose pose; pose.position = position; pose.orientation = orientation; ignition::math::Pose3d world_H_base = Impl::toIgnitionMath(pose); diff --git a/ignition/src/RobotSingleton.cpp b/cpp/gympp/gazebo/RobotSingleton.cpp similarity index 88% rename from ignition/src/RobotSingleton.cpp rename to cpp/gympp/gazebo/RobotSingleton.cpp index 6bcc5e813..7f278afa5 100644 --- a/ignition/src/RobotSingleton.cpp +++ b/cpp/gympp/gazebo/RobotSingleton.cpp @@ -7,7 +7,7 @@ */ #include "gympp/gazebo/RobotSingleton.h" -#include "gympp/Log.h" +#include "gympp/base/Log.h" #include #include @@ -18,13 +18,15 @@ using RobotName = std::string; class RobotSingleton::Impl { public: - std::unordered_map robots; + std::unordered_map robots; }; RobotSingleton::RobotSingleton() - : pImpl{new Impl(), [](Impl* impl) { delete impl; }} + : pImpl{new Impl()} {} +RobotSingleton::~RobotSingleton() = default; + RobotSingleton& RobotSingleton::get() { static RobotSingleton instance; @@ -41,7 +43,7 @@ bool RobotSingleton::exists(const std::string& robotName) const } } -std::weak_ptr RobotSingleton::getRobot(const std::string& robotName) const +std::weak_ptr RobotSingleton::getRobot(const std::string& robotName) const { if (robotName.empty()) { gymppError << "The robot name to register is empty" << std::endl; @@ -57,7 +59,7 @@ std::weak_ptr RobotSingleton::getRobot(const std::string& robotNam return pImpl->robots.at(robotName); } -bool RobotSingleton::storeRobot(RobotPtr robot) +bool RobotSingleton::storeRobot(base::RobotPtr robot) { if (!(robot && robot->valid())) { gymppError << "Trying to store an Robot pointer not valid" << std::endl; diff --git a/ignition/include/gympp/gazebo/IgnitionEnvironment.h b/cpp/gympp/gazebo/include/gympp/gazebo/GazeboEnvironment.h similarity index 54% rename from ignition/include/gympp/gazebo/IgnitionEnvironment.h rename to cpp/gympp/gazebo/include/gympp/gazebo/GazeboEnvironment.h index f92e66c5c..06d5ccaac 100644 --- a/ignition/include/gympp/gazebo/IgnitionEnvironment.h +++ b/cpp/gympp/gazebo/include/gympp/gazebo/GazeboEnvironment.h @@ -6,64 +6,64 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_GAZEBO_IGNITIONENVIRONMENT -#define GYMPP_GAZEBO_IGNITIONENVIRONMENT +#ifndef GYMPP_GAZEBO_GAZEBOENVIRONMENT +#define GYMPP_GAZEBO_GAZEBOENVIRONMENT -#include "gympp/Environment.h" +#include "gympp/base/Environment.h" #include "gympp/gazebo/GazeboWrapper.h" -#include #include #include #include namespace gympp { - class GymFactory; - namespace gazebo { + namespace base { class Task; - class IgnitionEnvironment; + } // namespace base + namespace gazebo { + class GymFactory; + class GazeboEnvironment; } // namespace gazebo } // namespace gympp -class gympp::gazebo::IgnitionEnvironment - : public gympp::Environment +class gympp::gazebo::GazeboEnvironment + : public gympp::base::Environment , public gympp::gazebo::GazeboWrapper - , public std::enable_shared_from_this + , public std::enable_shared_from_this { private: class Impl; - std::unique_ptr> pImpl; - gympp::gazebo::Task* getTask(); + std::unique_ptr pImpl; + gympp::base::Task* getTask(); - friend class gympp::GymFactory; + friend class gympp::gazebo::GymFactory; bool initializeSimulation(); void storeSDFModelFile(const std::string& modelSDF); void storeModelData(const gympp::gazebo::ModelInitData& modelData); void storePluginData(const gympp::gazebo::PluginData& pluginData); -protected: public: - using Environment = gympp::Environment; + using Environment = gympp::base::Environment; using Environment::Action; using Environment::Observation; using Environment::RenderMode; using Environment::Reward; using Environment::State; - IgnitionEnvironment() = delete; - IgnitionEnvironment(const ActionSpacePtr aSpace, - const ObservationSpacePtr oSpace, - const double agentUpdateRate, - const double realTimeFactor = 1, - const double physicsUpdateRate = 1000); - ~IgnitionEnvironment() override = default; + GazeboEnvironment() = delete; + GazeboEnvironment(const ActionSpacePtr aSpace, + const ObservationSpacePtr oSpace, + const double agentUpdateRate, + const double realTimeFactor = 1, + const double physicsUpdateRate = 1000); + ~GazeboEnvironment() override; bool render(RenderMode mode) override; std::optional reset() override; std::optional step(const Action& action) override; std::vector seed(size_t seed = 0) override; - EnvironmentPtr env(); + base::EnvironmentPtr env(); }; -#endif // GYMPP_GAZEBO_IGNITIONENVIRONMENT +#endif // GYMPP_GAZEBO_GAZEBOENVIRONMENT diff --git a/plugins/GymFactory/include/gympp/GymFactory.h b/cpp/gympp/gazebo/include/gympp/gazebo/GymFactory.h similarity index 50% rename from plugins/GymFactory/include/gympp/GymFactory.h rename to cpp/gympp/gazebo/include/gympp/gazebo/GymFactory.h index fba239c14..d112bc93c 100644 --- a/plugins/GymFactory/include/gympp/GymFactory.h +++ b/cpp/gympp/gazebo/include/gympp/gazebo/GymFactory.h @@ -6,32 +6,34 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_GYMFACTORY -#define GYMPP_GYMFACTORY +#ifndef GYMPP_GAZEBO_GYMFACTORY +#define GYMPP_GAZEBO_GYMFACTORY -#include "gympp/Environment.h" +#include "gympp/base/Environment.h" #include -#include #include #include namespace gympp { - class GymFactory; - class PluginMetadata; + namespace gazebo { + class GymFactory; + class PluginMetadata; + } // namespace gazebo } // namespace gympp -class gympp::GymFactory : public ignition::common::SingletonT +class gympp::gazebo::GymFactory : public ignition::common::SingletonT { private: class Impl; - std::unique_ptr> pImpl; + std::unique_ptr pImpl; public: GymFactory(); + ~GymFactory(); - gympp::EnvironmentPtr make(const std::string& envName); + gympp::base::EnvironmentPtr make(const std::string& envName); bool registerPlugin(const PluginMetadata& md); }; -#endif // GYMPP_GYMFACTORY +#endif // GYMPP_GAZEBO_GYMFACTORY diff --git a/ignition/include/gympp/gazebo/IgnitionRobot.h b/cpp/gympp/gazebo/include/gympp/gazebo/IgnitionRobot.h similarity index 78% rename from ignition/include/gympp/gazebo/IgnitionRobot.h rename to cpp/gympp/gazebo/include/gympp/gazebo/IgnitionRobot.h index e37ee69da..b748e1be7 100644 --- a/ignition/include/gympp/gazebo/IgnitionRobot.h +++ b/cpp/gympp/gazebo/include/gympp/gazebo/IgnitionRobot.h @@ -9,14 +9,13 @@ #ifndef GYMPP_GAZEBO_IGNITIONROBOT_H #define GYMPP_GAZEBO_IGNITIONROBOT_H -#include "gympp/Robot.h" +#include "gympp/base/Robot.h" #include #include #include #include -#include #include namespace gympp { @@ -25,11 +24,11 @@ namespace gympp { } // namespace gazebo } // namespace gympp -class gympp::gazebo::IgnitionRobot : public gympp::Robot +class gympp::gazebo::IgnitionRobot : public gympp::base::Robot { private: class Impl; - std::unique_ptr> pImpl; + std::unique_ptr pImpl; public: IgnitionRobot(); @@ -49,11 +48,11 @@ class gympp::gazebo::IgnitionRobot : public gympp::Robot RobotName name() const override; JointNames jointNames() const override; - JointType jointType(const JointName& jointName) const override; + base::JointType jointType(const JointName& jointName) const override; double jointForce(const JointName& jointName) const override; double jointPosition(const JointName& jointName) const override; double jointVelocity(const JointName& jointName) const override; - JointControlMode jointControlMode(const JointName& jointName) const override; + base::JointControlMode jointControlMode(const JointName& jointName) const override; JointPositions jointPositions() const override; JointVelocities jointVelocities() const override; @@ -61,20 +60,20 @@ class gympp::gazebo::IgnitionRobot : public gympp::Robot JointPositions initialJointPositions() const override; double jointEffortLimit(const JointName& jointName) const override; - Limit jointPositionLimits(const JointName& jointName) const override; + base::Limit jointPositionLimits(const JointName& jointName) const override; StepSize dt() const override; PID jointPID(const JointName& jointName) const override; LinkNames linksInContact() const override; - std::vector contactData(const LinkName& linkName) const override; + std::vector contactData(const LinkName& linkName) const override; LinkNames linkNames() const override; - Pose linkPose(const LinkName& linkName) const override; - Velocity6D linkVelocity(const LinkName& linkName) const override; - Acceleration6D linkAcceleration(const LinkName& linkName) const override; - Velocity6D linkBodyFixedVelocity(const LinkName& linkName) const override; - Acceleration6D linkBodyFixedAcceleration(const LinkName& linkName) const override; + base::Pose linkPose(const LinkName& linkName) const override; + base::Velocity6D linkVelocity(const LinkName& linkName) const override; + base::Acceleration6D linkAcceleration(const LinkName& linkName) const override; + base::Velocity6D linkBodyFixedVelocity(const LinkName& linkName) const override; + base::Acceleration6D linkBodyFixedAcceleration(const LinkName& linkName) const override; // =========== // SET METHODS @@ -93,7 +92,7 @@ class gympp::gazebo::IgnitionRobot : public gympp::Robot bool setJointPosition(const JointName& jointName, const double jointPosition) override; bool setJointVelocity(const JointName& jointName, const double jointVelocity) override; bool setJointControlMode(const JointName& jointName, - const JointControlMode controlMode) override; + const base::JointControlMode controlMode) override; bool setJointPID(const JointName& jointName, const PID& pid) override; @@ -113,8 +112,8 @@ class gympp::gazebo::IgnitionRobot : public gympp::Robot LinkName baseFrame() override; bool setBaseFrame(const LinkName& baseLink) override; - Pose basePose() override; - Velocity6D baseVelocity() override; + base::Pose basePose() override; + base::Velocity6D baseVelocity() override; bool setAsFloatingBase(bool isFloating) override; bool resetBasePose(const std::array& position, const std::array& orientation) override; diff --git a/plugins/GymFactory/include/gympp/Metadata.h b/cpp/gympp/gazebo/include/gympp/gazebo/Metadata.h similarity index 81% rename from plugins/GymFactory/include/gympp/Metadata.h rename to cpp/gympp/gazebo/include/gympp/gazebo/Metadata.h index e4be2c82a..5ac2bd8ac 100644 --- a/plugins/GymFactory/include/gympp/Metadata.h +++ b/cpp/gympp/gazebo/include/gympp/gazebo/Metadata.h @@ -6,38 +6,39 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_METADATA -#define GYMPP_METADATA +#ifndef GYMPP_GAZEBO_METADATA +#define GYMPP_GAZEBO_METADATA -#include "gympp/Log.h" -#include "gympp/Space.h" -#include "gympp/gazebo/IgnitionEnvironment.h" +#include "gympp/base/Log.h" +#include "gympp/base/Space.h" #include #include namespace gympp { - class GymFactory; - class SpaceMetadata; - class PluginMetadata; - - enum class SpaceType - { - Discrete, - Box - }; + namespace gazebo { + class GymFactory; + class SpaceMetadata; + class PluginMetadata; + + enum class SpaceType + { + Discrete, + Box + }; + } // namespace gazebo } // namespace gympp -class gympp::SpaceMetadata +class gympp::gazebo::SpaceMetadata { private: - friend gympp::GymFactory; + friend gympp::gazebo::GymFactory; SpaceType type; std::vector dims; - gympp::spaces::Box::Limit low; - gympp::spaces::Box::Limit high; + gympp::base::spaces::Box::Limit low; + gympp::base::spaces::Box::Limit high; bool boxSpaceValid() const { @@ -74,13 +75,13 @@ class gympp::SpaceMetadata public: inline SpaceType getType() const { return type; } inline std::vector getDimensions() const { return dims; } - inline gympp::spaces::Box::Limit getLowLimit() const { return low; } - inline gympp::spaces::Box::Limit getHighLimit() const { return high; } + inline gympp::base::spaces::Box::Limit getLowLimit() const { return low; } + inline gympp::base::spaces::Box::Limit getHighLimit() const { return high; } inline void setType(const SpaceType type) { this->type = type; } inline void setDimensions(const std::vector& dims) { this->dims = dims; } - inline void setLowLimit(const gympp::spaces::Box::Limit& limit) { this->low = limit; } - inline void setHighLimit(const gympp::spaces::Box::Limit& limit) { this->high = limit; } + inline void setLowLimit(const gympp::base::spaces::Box::Limit& limit) { this->low = limit; } + inline void setHighLimit(const gympp::base::spaces::Box::Limit& limit) { this->high = limit; } bool isValid() const { @@ -95,10 +96,10 @@ class gympp::SpaceMetadata } }; -class gympp::PluginMetadata +class gympp::gazebo::PluginMetadata { private: - friend gympp::GymFactory; + friend gympp::gazebo::GymFactory; std::string environmentName; std::string libraryName; std::string className; @@ -173,4 +174,4 @@ class gympp::PluginMetadata } }; -#endif // GYMPP_METADATA +#endif // GYMPP_GAZEBO_METADATA diff --git a/ignition/include/gympp/gazebo/RobotSingleton.h b/cpp/gympp/gazebo/include/gympp/gazebo/RobotSingleton.h similarity index 75% rename from ignition/include/gympp/gazebo/RobotSingleton.h rename to cpp/gympp/gazebo/include/gympp/gazebo/RobotSingleton.h index cb173f277..5951f281a 100644 --- a/ignition/include/gympp/gazebo/RobotSingleton.h +++ b/cpp/gympp/gazebo/include/gympp/gazebo/RobotSingleton.h @@ -9,14 +9,12 @@ #ifndef GYMPP_GAZEBO_ROBOTSINGLETON_H #define GYMPP_GAZEBO_ROBOTSINGLETON_H -#include "gympp/Robot.h" +#include "gympp/base/Robot.h" -#include #include #include namespace gympp { - class Robot; namespace gazebo { class RobotSingleton; } // namespace gazebo @@ -26,11 +24,11 @@ class gympp::gazebo::RobotSingleton { private: class Impl; - std::unique_ptr> pImpl; + std::unique_ptr pImpl; public: RobotSingleton(); - ~RobotSingleton() = default; + ~RobotSingleton(); RobotSingleton(RobotSingleton&) = delete; void operator=(const RobotSingleton&) = delete; @@ -38,9 +36,9 @@ class gympp::gazebo::RobotSingleton bool exists(const std::string& robotName) const; - std::weak_ptr getRobot(const std::string& robotName) const; + std::weak_ptr getRobot(const std::string& robotName) const; - bool storeRobot(RobotPtr robot); + bool storeRobot(base::RobotPtr robot); bool deleteRobot(const std::string& robotName); }; diff --git a/plugins/CartPole/CMakeLists.txt b/cpp/gympp/plugins/CMakeLists.txt similarity index 62% rename from plugins/CartPole/CMakeLists.txt rename to cpp/gympp/plugins/CMakeLists.txt index 0422377a7..73fbd8d5a 100644 --- a/plugins/CartPole/CMakeLists.txt +++ b/cpp/gympp/plugins/CMakeLists.txt @@ -2,6 +2,25 @@ # This software may be modified and distributed under the terms of the # GNU Lesser General Public License v2.1 or any later version. +# =============== +# PLUGIN DATABASE +# =============== + +add_library(PluginDatabase INTERFACE) +target_sources(PluginDatabase INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}/include/gympp/plugins/PluginDatabase.h) + +target_include_directories(PluginDatabase INTERFACE + $ + $) + +target_link_libraries(PluginDatabase INTERFACE gympp) + +if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") + set_target_properties(PluginDatabase PROPERTIES + PUBLIC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include/gympp/plugins/PluginDatabase.h) +endif() + # =============== # CartPole PLUGIN # =============== diff --git a/plugins/CartPole/CartPolePlugin.cpp b/cpp/gympp/plugins/CartPolePlugin.cpp similarity index 91% rename from plugins/CartPole/CartPolePlugin.cpp rename to cpp/gympp/plugins/CartPolePlugin.cpp index e09401c48..536204071 100644 --- a/plugins/CartPole/CartPolePlugin.cpp +++ b/cpp/gympp/plugins/CartPolePlugin.cpp @@ -7,12 +7,12 @@ */ #include "CartPolePlugin.h" -#include "gympp/Common.h" -#include "gympp/Log.h" -#include "gympp/Random.h" -#include "gympp/Robot.h" +#include "gympp/base/Common.h" +#include "gympp/base/Log.h" +#include "gympp/base/Random.h" +#include "gympp/base/Robot.h" +#include "gympp/base/TaskSingleton.h" #include "gympp/gazebo/RobotSingleton.h" -#include "gympp/gazebo/TaskSingleton.h" #include #include @@ -30,10 +30,10 @@ using namespace gympp::gazebo; using namespace gympp::plugins; using ActionDataType = int; -using ActionSample = gympp::BufferContainer::type; +using ActionSample = gympp::base::BufferContainer::type; using ObservationDataType = double; -using ObservationSample = gympp::BufferContainer::type; +using ObservationSample = gympp::base::BufferContainer::type; enum ObservationIndex { @@ -68,17 +68,17 @@ class CartPole::Impl std::optional action; std::string modelName; - gympp::RobotPtr robot; - static gympp::RobotPtr getRobotPtr(const std::string& robotName); + gympp::base::RobotPtr robot; + static gympp::base::RobotPtr getRobotPtr(const std::string& robotName); double getRandomThetaInRad() { std::uniform_real_distribution<> distr(-MaxTheta0Rad, MaxTheta0Rad); - return distr(gympp::Random::engine()); + return distr(gympp::base::Random::engine()); } }; -gympp::RobotPtr CartPole::Impl::getRobotPtr(const std::string& robotName) +gympp::base::RobotPtr CartPole::Impl::getRobotPtr(const std::string& robotName) { // Get the robot interface auto robotPtr = RobotSingleton::get().getRobot(robotName).lock(); @@ -115,7 +115,7 @@ CartPole::CartPole() CartPole::~CartPole() { - if (!TaskSingleton::get().removeTask(pImpl->robot->name())) { + if (!base::TaskSingleton::get().removeTask(pImpl->robot->name())) { gymppError << "Failed to unregister the Task interface"; assert(false); } @@ -141,9 +141,9 @@ void CartPole::Configure(const ignition::gazebo::Entity& entity, // Auto-register the task gymppDebug << "Registering the Task interface for robot '" << pImpl->modelName << "'" << std::endl; - auto& taskSingleton = TaskSingleton::get(); + auto& taskSingleton = base::TaskSingleton::get(); - if (!taskSingleton.storeTask(pImpl->modelName, dynamic_cast(this))) { + if (!taskSingleton.storeTask(pImpl->modelName, dynamic_cast(this))) { gymppError << "Failed to register the Task interface"; assert(false); return; @@ -309,7 +309,7 @@ bool CartPole::setAction(const Task::Action& action) return true; } -std::optional CartPole::computeReward() +std::optional CartPole::computeReward() { std::lock_guard lock(pImpl->mutex); return 1.0; @@ -318,7 +318,7 @@ std::optional CartPole::computeReward() // - std::abs(pImpl->observationBuffer[ObservationIndex::PoleVelocity]); } -std::optional CartPole::getObservation() +std::optional CartPole::getObservation() { std::lock_guard lock(pImpl->mutex); return Observation(pImpl->observationBuffer); @@ -329,4 +329,4 @@ IGNITION_ADD_PLUGIN(gympp::plugins::CartPole, gympp::plugins::CartPole::ISystemConfigure, gympp::plugins::CartPole::ISystemPreUpdate, gympp::plugins::CartPole::ISystemPostUpdate, - gympp::gazebo::Task) + gympp::base::Task) diff --git a/plugins/CartPole/CartPolePlugin.h b/cpp/gympp/plugins/CartPolePlugin.h similarity index 96% rename from plugins/CartPole/CartPolePlugin.h rename to cpp/gympp/plugins/CartPolePlugin.h index fc41a8679..e3c978ad7 100644 --- a/plugins/CartPole/CartPolePlugin.h +++ b/cpp/gympp/plugins/CartPolePlugin.h @@ -9,7 +9,7 @@ #ifndef GYMPP_PLUGINS_CARTPOLE #define GYMPP_PLUGINS_CARTPOLE -#include "gympp/gazebo/Task.h" +#include "gympp/base/Task.h" #include #include @@ -32,7 +32,7 @@ class gympp::plugins::CartPole final , public ignition::gazebo::ISystemConfigure , public ignition::gazebo::ISystemPreUpdate , public ignition::gazebo::ISystemPostUpdate - , public gympp::gazebo::Task + , public gympp::base::Task { private: class Impl; diff --git a/plugins/GymFactory/include/gympp/PluginDatabase.h b/cpp/gympp/plugins/include/gympp/plugins/PluginDatabase.h similarity index 64% rename from plugins/GymFactory/include/gympp/PluginDatabase.h rename to cpp/gympp/plugins/include/gympp/plugins/PluginDatabase.h index ca3213b0b..477e88145 100644 --- a/plugins/GymFactory/include/gympp/PluginDatabase.h +++ b/cpp/gympp/plugins/include/gympp/plugins/PluginDatabase.h @@ -6,22 +6,22 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#ifndef GYMPP_PLUGINDATABASE_H -#define GYMPP_PLUGINDATABASE_H +#ifndef GYMPP_GAZEBO_PLUGINDATABASE_H +#define GYMPP_GAZEBO_PLUGINDATABASE_H -#include "gympp/GymFactory.h" -#include "gympp/Log.h" -#include "gympp/Metadata.h" -#include "gympp/Space.h" +#include "gympp/base/Log.h" +#include "gympp/base/Space.h" #include "gympp/gazebo/GazeboWrapper.h" +#include "gympp/gazebo/GymFactory.h" +#include "gympp/gazebo/Metadata.h" class GymppPluginRegistrator_CartPole { public: GymppPluginRegistrator_CartPole() { - auto factory = gympp::GymFactory::Instance(); - gympp::PluginMetadata cartPoleMetadata; + auto factory = gympp::gazebo::GymFactory::Instance(); + gympp::gazebo::PluginMetadata cartPoleMetadata; gymppDebug << "Registering 'CartPole' plugin" << std::endl; cartPoleMetadata.setEnvironmentName("CartPole"); @@ -30,20 +30,20 @@ class GymppPluginRegistrator_CartPole cartPoleMetadata.setModelFileName("CartPole/CartPole.urdf"); cartPoleMetadata.setWorldFileName("DefaultEmptyWorld.world"); - gympp::SpaceMetadata actionSpaceMetadata; + gympp::gazebo::SpaceMetadata actionSpaceMetadata; actionSpaceMetadata.setDimensions({3}); - actionSpaceMetadata.setType(gympp::SpaceType::Discrete); + actionSpaceMetadata.setType(gympp::gazebo::SpaceType::Discrete); const double xThreshold = 2.5; const double thetaThreshold = 24; - gympp::SpaceMetadata observationSpaceMetadata; - observationSpaceMetadata.setType(gympp::SpaceType::Box); + gympp::gazebo::SpaceMetadata observationSpaceMetadata; + observationSpaceMetadata.setType(gympp::gazebo::SpaceType::Box); double maxDouble = std::numeric_limits::max(); observationSpaceMetadata.setLowLimit( - gympp::spaces::Box::Limit{-xThreshold, -maxDouble, -thetaThreshold, -maxDouble}); + gympp::base::spaces::Box::Limit{-xThreshold, -maxDouble, -thetaThreshold, -maxDouble}); observationSpaceMetadata.setHighLimit( - gympp::spaces::Box::Limit{xThreshold, maxDouble, thetaThreshold, maxDouble}); + gympp::base::spaces::Box::Limit{xThreshold, maxDouble, thetaThreshold, maxDouble}); cartPoleMetadata.setActionSpaceMetadata(actionSpaceMetadata); cartPoleMetadata.setObservationSpaceMetadata(observationSpaceMetadata); @@ -61,4 +61,4 @@ class GymppPluginRegistrator_CartPole static GymppPluginRegistrator_CartPole plugin; -#endif // GYMPP_PLUGINDATABASE_H +#endif // GYMPP_GAZEBO_PLUGINDATABASE_H diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt index df1033957..8136f865b 100644 --- a/examples/cpp/CMakeLists.txt +++ b/examples/cpp/CMakeLists.txt @@ -3,7 +3,7 @@ # GNU Lesser General Public License v2.1 or any later version. add_executable(LaunchCartPole LaunchCartPole.cpp) -target_link_libraries(LaunchCartPole PRIVATE GymFactory Clara) +target_link_libraries(LaunchCartPole PRIVATE gympp PluginDatabase GymFactory Clara) add_executable(LaunchParallelCartPole LaunchParallelCartPole.cpp) -target_link_libraries(LaunchParallelCartPole PRIVATE GymFactory Clara) +target_link_libraries(LaunchParallelCartPole PRIVATE gympp PluginDatabase GymFactory Clara) diff --git a/examples/cpp/LaunchCartPole.cpp b/examples/cpp/LaunchCartPole.cpp index 004ac267e..99f769270 100644 --- a/examples/cpp/LaunchCartPole.cpp +++ b/examples/cpp/LaunchCartPole.cpp @@ -6,12 +6,12 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#include "gympp/Common.h" -#include "gympp/Environment.h" -#include "gympp/GymFactory.h" -#include "gympp/Log.h" -#include "gympp/PluginDatabase.h" -#include "gympp/Space.h" +#include "gympp/base/Common.h" +#include "gympp/base/Environment.h" +#include "gympp/base/Log.h" +#include "gympp/base/Space.h" +#include "gympp/gazebo/GymFactory.h" +#include "gympp/plugins/PluginDatabase.h" #include "clara.hpp" @@ -28,8 +28,8 @@ #include #include -using namespace gympp; using namespace clara; +using namespace gympp::base; struct Config { @@ -67,7 +67,7 @@ int main(int argc, char* argv[]) // ========================== // Create the environment - auto env = GymFactory::Instance()->make("CartPole"); + auto env = gympp::gazebo::GymFactory::Instance()->make("CartPole"); if (!env) { gymppError << "Failed to load the CartPole environment" << std::endl; diff --git a/examples/cpp/LaunchParallelCartPole.cpp b/examples/cpp/LaunchParallelCartPole.cpp index a8a523429..7470367f1 100644 --- a/examples/cpp/LaunchParallelCartPole.cpp +++ b/examples/cpp/LaunchParallelCartPole.cpp @@ -6,12 +6,12 @@ * GNU Lesser General Public License v2.1 or any later version. */ -#include "gympp/Environment.h" -#include "gympp/GymFactory.h" -#include "gympp/Log.h" -#include "gympp/PluginDatabase.h" -#include "gympp/Random.h" -#include "gympp/Space.h" +#include "gympp/base/Environment.h" +#include "gympp/base/Log.h" +#include "gympp/base/Random.h" +#include "gympp/base/Space.h" +#include "gympp/gazebo/GymFactory.h" +#include "gympp/plugins/PluginDatabase.h" #include "clara.hpp" @@ -29,8 +29,9 @@ #include #include -using namespace gympp; using namespace clara; +using namespace gympp; +using namespace gympp::base; class Worker { @@ -60,7 +61,7 @@ class Worker bool initialize() { - m_env = GymFactory::Instance()->make(m_environmentName); + m_env = gazebo::GymFactory::Instance()->make(m_environmentName); if (!m_env) { gymppError << "Failed to create environment '" << m_environmentName << "'" << std::endl; @@ -156,7 +157,7 @@ class MasterAgent size_t m_maxEpisodes; std::vector> m_workers; std::vector m_pool; - const gympp::EnvironmentName EnvName = "CartPole"; + const gympp::base::EnvironmentName EnvName = "CartPole"; public: MasterAgent(const size_t maxEpisodes) diff --git a/gym_ignition/base/gympp_env.py b/gym_ignition/base/gympp_env.py index 81ea6dbbe..a8beb18af 100644 --- a/gym_ignition/base/gympp_env.py +++ b/gym_ignition/base/gympp_env.py @@ -13,10 +13,10 @@ class GymppEnv(gym.Env): - """Class that exposes C++ Ignition environments + """Class that exposes C++ Gazebo environments This class encapsulates environments created as C++ plugins. Plugins that implement - the provided gympp and ignition interfaces are inherently compatible with this + the provided gympp and gazebo interfaces are inherently compatible with this class if they support the C++ factory. The users of this class need to implement the following method: @@ -47,7 +47,7 @@ def __init__(self): assert(action_space and observation_space), "Failed to create spaces" @property - def gympp_env(self) -> bindings.IgnitionEnvironment: + def gympp_env(self) -> bindings.GazeboEnvironment: if self._env: return self._env @@ -155,12 +155,15 @@ def step(self, action: Action) -> State: assert observation_vector, "Failed to get the observation buffer" assert observation_vector.size() > 0, "The observation does not contain elements" + # Convert the SWIG type to a list + observation_list = list(observation_vector) + # Convert the observation to a numpy array (this is the only required copy) if isinstance(self.observation_space, gym.spaces.Box): - observation = np.array(observation_vector) + observation = np.array(observation_list) elif isinstance(self.observation_space, gym.spaces.Discrete): assert observation_vector.size() == 1, "The buffer has the wrong dimension" - observation = observation_vector[0] + observation = observation_list[0] else: assert False, "Space not supported" @@ -186,16 +189,19 @@ def reset(self) -> Observation: assert observation_vector, "Failed to get the observation buffer" assert observation_vector.size() > 0, "The observation does not contain elements" + # Convert the SWIG type to a list + observation_list = list(observation_vector) + # Convert the observation to a numpy array (this is the only required copy) if isinstance(self.observation_space, gym.spaces.Box): - observation = np.array(observation_vector) + observation = np.array(observation_list) elif isinstance(self.observation_space, gym.spaces.Discrete): assert observation_vector.size() == 1, "The buffer has the wrong dimension" - observation = observation_vector[0] + observation = observation_list[0] else: assert False, "Space not supported" - assert self.observation_space.contains(observation),\ + assert self.observation_space.contains(observation), \ "The returned observation does not belong to the space" # Return the list diff --git a/gym_ignition/robots/base/gazebo_robot.py b/gym_ignition/robots/base/gazebo_robot.py index aab9a4cb0..8488c77fa 100644 --- a/gym_ignition/robots/base/gazebo_robot.py +++ b/gym_ignition/robots/base/gazebo_robot.py @@ -104,19 +104,19 @@ def gympp_robot(self) -> bindings.Robot: # Initialize the model data model_data = bindings.ModelInitData() - model_data.setModelName(self._robot_name) - model_data.setSdfString(sdf_string) - model_data.setFixedPose(not self.is_floating_base()) - model_data.setPosition(initial_base_pose.tolist()) - model_data.setOrientation(initial_base_orientation.tolist()) + model_data.modelName = self._robot_name + model_data.sdfString = sdf_string + model_data.fixedPose = not self.is_floating_base() + model_data.position =initial_base_pose.tolist() + model_data.orientation = initial_base_orientation.tolist() if self._base_frame is not None: model_data.setBaseLink(self._base_frame) # Initialize robot controller plugin plugin_data = bindings.PluginData() - plugin_data.setLibName("RobotController") - plugin_data.setClassName("gympp::plugins::RobotController") + plugin_data.libName = "RobotController" + plugin_data.className = "gympp::plugins::RobotController" # Insert the model ok_model = self._gazebo.insertModel(model_data, plugin_data) diff --git a/gym_ignition/robots/sim/gazebo/cartpole.py b/gym_ignition/robots/sim/gazebo/cartpole.py index b0ba5b3c5..2c72aefec 100644 --- a/gym_ignition/robots/sim/gazebo/cartpole.py +++ b/gym_ignition/robots/sim/gazebo/cartpole.py @@ -20,10 +20,6 @@ def __init__(self, gazebo, model_file: str = None, **kwargs): gazebo=gazebo, controller_rate=kwargs.get("controller_rate")) - # Configure the cartpole as fixed-base robot - ok_floating = self.set_as_floating_base(False) - assert ok_floating, "Failed to set the robot as fixed base" - # Initial base position base_position = np.array([0., 0., 0.]) \ if "base_position" not in kwargs else kwargs["base_position"] diff --git a/gym_ignition/robots/sim/gazebo/panda.py b/gym_ignition/robots/sim/gazebo/panda.py index 53082a5f7..591cb95b2 100644 --- a/gym_ignition/robots/sim/gazebo/panda.py +++ b/gym_ignition/robots/sim/gazebo/panda.py @@ -20,9 +20,6 @@ def __init__(self, gazebo, model_file: str = None, **kwargs): gazebo=gazebo, controller_rate=kwargs.get("controller_rate")) - ok_floating = self.set_as_floating_base(False) - assert ok_floating, "Failed to set the robot as fixed base" - base_position = np.array([0., 0., 0.]) \ if "base_position" not in kwargs else kwargs["base_position"] diff --git a/gympp/CMakeLists.txt b/gympp/CMakeLists.txt deleted file mode 100644 index d660c5ac0..000000000 --- a/gympp/CMakeLists.txt +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT). All rights reserved. -# This software may be modified and distributed under the terms of the -# GNU Lesser General Public License v2.1 or any later version. - -add_library(gympp - include/gympp/Environment.h - include/gympp/Common.h - include/gympp/Log.h - include/gympp/Random.h - include/gympp/Space.h - include/gympp/Robot.h - src/Space.cpp - src/Random.cpp) - -find_package(ignition-common3 QUIET) - -if(ignition-common3_FOUND) - target_compile_definitions(gympp PUBLIC USE_IGNITION_LOGS) - target_link_libraries(gympp PUBLIC - ignition-common3::ignition-common3) -endif() - -file(GLOB_RECURSE GYMPP_HEADERS_INSTALL include/*.h) - -set_target_properties(gympp PROPERTIES - PUBLIC_HEADER "${GYMPP_HEADERS_INSTALL}" - POSITION_INDEPENDENT_CODE ON) - -target_include_directories(gympp PUBLIC - $ - $) - -if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") - install( - TARGETS gympp - EXPORT gympp - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/gympp) -endif() diff --git a/ignition/CMakeLists.txt b/ignition/CMakeLists.txt index 8048a0454..f92d6f338 100644 --- a/ignition/CMakeLists.txt +++ b/ignition/CMakeLists.txt @@ -18,43 +18,6 @@ target_include_directories(ExtraComponents INTERFACE target_link_libraries(ExtraComponents INTERFACE ignition-gazebo3::core) -# ===================== -# ENVIRONMENT CALLBACKS -# ===================== - -add_library(Task INTERFACE) -target_sources(Task INTERFACE - ${CMAKE_CURRENT_SOURCE_DIR}/include/gympp/gazebo/Task.h) - -target_include_directories(Task INTERFACE - $ - $) - -target_link_libraries(Task INTERFACE gympp) - -# ============== -# TASK SINGLETON -# ============== - -add_library(TaskSingleton - include/gympp/gazebo/TaskSingleton.h - src/TaskSingleton.cpp) - -target_include_directories(TaskSingleton PUBLIC - $ - $) - -target_link_libraries(TaskSingleton - PUBLIC - gympp - PRIVATE - Task) - -if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") - set_target_properties(TaskSingleton PROPERTIES - PUBLIC_HEADER include/gympp/gazebo/TaskSingleton.h) -endif() - # ============= # ECM SINGLETON # ============= @@ -103,95 +66,16 @@ target_include_directories(GazeboWrapper PUBLIC $ $) -# ==================== -# IGNITION ENVIRONMENT -# ==================== - -add_library(IgnitionEnvironment - include/gympp/gazebo/IgnitionEnvironment.h - src/IgnitionEnvironment.cpp) - -target_link_libraries(IgnitionEnvironment - PUBLIC - gympp - GazeboWrapper - PRIVATE - Task - TaskSingleton - ignition-gazebo3::core) - -set_target_properties(IgnitionEnvironment PROPERTIES - PUBLIC_HEADER include/gympp/gazebo/IgnitionEnvironment.h) - -target_include_directories(IgnitionEnvironment PUBLIC - $ - $) - -# ======================== -# IGNITION ROBOT SINGLETON -# ======================== - -# This class must be shared, otherwise plugins do not share static objects like singletons -# with the memory of the program that loaded them - -add_library(RobotSingleton - include/gympp/gazebo/RobotSingleton.h - src/RobotSingleton.cpp) - -target_link_libraries(RobotSingleton - PUBLIC - gympp - ignition-gazebo3::core) - -if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") - set_target_properties(RobotSingleton PROPERTIES - PUBLIC_HEADER include/gympp/gazebo/RobotSingleton.h) -endif() - -target_include_directories(RobotSingleton PUBLIC - $ - $) - -# ============== -# IGNITION ROBOT -# ============== - -add_library(IgnitionRobot - include/gympp/gazebo/IgnitionRobot.h - src/IgnitionRobot.cpp) - -target_link_libraries(IgnitionRobot - PUBLIC - gympp - PRIVATE - RobotSingleton - ExtraComponents) - -set_target_properties(IgnitionRobot PROPERTIES - PUBLIC_HEADER include/gympp/gazebo/IgnitionRobot.h) - -target_include_directories(IgnitionRobot PUBLIC - $ - $) - # =================== # INSTALL THE TARGETS # =================== if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") - install( - FILES include/gympp/gazebo/Task.h - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/gympp/gazebo) - install( TARGETS - IgnitionEnvironment + ExtraComponents GazeboWrapper - RobotSingleton ECMSingleton - Task - TaskSingleton - IgnitionRobot EXPORT gympp LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} diff --git a/ignition/include/gympp/gazebo/GazeboWrapper.h b/ignition/include/gympp/gazebo/GazeboWrapper.h index 686bf5cb7..495758c2e 100644 --- a/ignition/include/gympp/gazebo/GazeboWrapper.h +++ b/ignition/include/gympp/gazebo/GazeboWrapper.h @@ -45,22 +45,12 @@ struct gympp::gazebo::ModelInitData std::string modelName = ""; std::array position = {0, 0, 0}; std::array orientation = {1, 0, 0, 0}; - - inline void setFixedPose(const bool f) { fixedPose = f; } - inline void setBaseLink(const std::string& b) { baseLink = b; } - inline void setSdfString(const std::string& s) { sdfString = s; } - inline void setModelName(const std::string& m) { modelName = m; } - inline void setPosition(const std::array p) { position = p; } - inline void setOrientation(const std::array o) { orientation = o; } }; struct gympp::gazebo::PluginData { std::string libName; std::string className; - - inline void setLibName(const std::string& l) { libName = l; } - inline void setClassName(const std::string& c) { className = c; } }; class gympp::gazebo::GazeboWrapper diff --git a/ignition/src/ECMSingleton.cpp b/ignition/src/ECMSingleton.cpp index d0504e6e7..e4db34181 100644 --- a/ignition/src/ECMSingleton.cpp +++ b/ignition/src/ECMSingleton.cpp @@ -7,7 +7,7 @@ */ #include "gympp/gazebo/ECMSingleton.h" -#include "gympp/Log.h" +#include "gympp/base/Log.h" #include #include diff --git a/ignition/src/GazeboWrapper.cpp b/ignition/src/GazeboWrapper.cpp index 1877f1647..9fab3a4b0 100644 --- a/ignition/src/GazeboWrapper.cpp +++ b/ignition/src/GazeboWrapper.cpp @@ -7,7 +7,7 @@ */ #include "gympp/gazebo/GazeboWrapper.h" -#include "gympp/Log.h" +#include "gympp/base/Log.h" #include "gympp/gazebo/ECMSingleton.h" #include "gympp/gazebo/IgnitionRobot.h" #include "gympp/gazebo/RobotSingleton.h" diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index b07df9934..c26eb6d00 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -3,7 +3,5 @@ # GNU Lesser General Public License v2.1 or any later version. add_subdirectory(Physics) -add_subdirectory(CartPole) -add_subdirectory(GymFactory) add_subdirectory(ECMProvider) add_subdirectory(RobotController) diff --git a/plugins/ECMProvider/ECMProvider.cpp b/plugins/ECMProvider/ECMProvider.cpp index 8f39adfb3..083a0e55e 100644 --- a/plugins/ECMProvider/ECMProvider.cpp +++ b/plugins/ECMProvider/ECMProvider.cpp @@ -7,7 +7,7 @@ */ #include "ECMProvider.h" -#include "gympp/Log.h" +#include "gympp/base/Log.h" #include "gympp/gazebo/ECMSingleton.h" #include diff --git a/plugins/GymFactory/CMakeLists.txt b/plugins/GymFactory/CMakeLists.txt deleted file mode 100644 index ed0e84f24..000000000 --- a/plugins/GymFactory/CMakeLists.txt +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT). All rights reserved. -# This software may be modified and distributed under the terms of the -# GNU Lesser General Public License v2.1 or any later version. - -set(GYMFACTORY_PUBLIC_HEADERS - include/gympp/GymFactory.h - include/gympp/Metadata.h - include/gympp/PluginDatabase.h) - -add_library(GymFactory - ${GYMFACTORY_PUBLIC_HEADERS} - src/GymFactory.cpp) - -target_link_libraries(GymFactory - PUBLIC gympp IgnitionEnvironment - PRIVATE ignition-gazebo3::core) - -set_target_properties(GymFactory PROPERTIES - PUBLIC_HEADER - "${GYMFACTORY_PUBLIC_HEADERS}") - -target_include_directories(GymFactory PUBLIC - $ - $) - -target_compile_definitions(GymFactory PRIVATE - CARTPOLE_PLUGIN_PATH="$") - -if(NOT CMAKE_BUILD_TYPE STREQUAL "PyPI") - install( - TARGETS GymFactory - EXPORT gympp - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/gympp) -endif() diff --git a/plugins/RobotController/RobotController.cpp b/plugins/RobotController/RobotController.cpp index daacbac63..4ae341bf1 100644 --- a/plugins/RobotController/RobotController.cpp +++ b/plugins/RobotController/RobotController.cpp @@ -7,8 +7,8 @@ */ #include "RobotController.h" -#include "gympp/Log.h" -#include "gympp/Robot.h" +#include "gympp/base/Log.h" +#include "gympp/base/Robot.h" #include "gympp/gazebo/RobotSingleton.h" #include @@ -33,11 +33,11 @@ class RobotController::Impl public: std::string modelName; bool warningReported = false; - std::shared_ptr robot = nullptr; - static gympp::RobotPtr getRobotPtr(const std::string& robotName); + std::shared_ptr robot = nullptr; + static gympp::base::RobotPtr getRobotPtr(const std::string& robotName); }; -gympp::RobotPtr RobotController::Impl::getRobotPtr(const std::string& robotName) +gympp::base::RobotPtr RobotController::Impl::getRobotPtr(const std::string& robotName) { // Get the robot interface auto robotPtr = RobotSingleton::get().getRobot(robotName).lock(); diff --git a/tests/python/test_bindings.py b/tests/python/test_bindings.py index cd7356233..11dd7da79 100644 --- a/tests/python/test_bindings.py +++ b/tests/python/test_bindings.py @@ -217,7 +217,7 @@ def test_gymfactory(): assert gazebo, "Failed to get gazebo wrapper" # Get the ignition environment - ign_env = bindings.envToIgnEnv(env) + ign_env = bindings.envToGazeboEnvironment(env) assert ign_env, "Failed to get the ignition environment" # Set verbosity diff --git a/tests/python/test_environments_gympp.py b/tests/python/test_environments_gympp.py index e322e41d9..9056b82f3 100644 --- a/tests/python/test_environments_gympp.py +++ b/tests/python/test_environments_gympp.py @@ -13,6 +13,8 @@ def test_create_cpp_environment(): env = gym.make("CartPoleDiscrete-Gympp-v0") assert env, "Failed to create 'CartPoleDiscrete-Gympp-v0' environment" + env.seed(42) + action = env.action_space.sample() assert isinstance(action, int), "The sampled action is empty" diff --git a/tests/python/test_robot_controller.py b/tests/python/test_robot_controller.py index da13b2cc7..c265ef317 100644 --- a/tests/python/test_robot_controller.py +++ b/tests/python/test_robot_controller.py @@ -18,8 +18,8 @@ def test_joint_controller(): controller_rate = 500.0 plugin_data = bindings.PluginData() - plugin_data.setLibName("RobotController") - plugin_data.setClassName("gympp::plugins::RobotController") + plugin_data.libName = "RobotController" + plugin_data.className = "gympp::plugins::RobotController" # Find and load the model SDF file model_sdf_file = resource_finder.find_resource("CartPole/CartPole.urdf") @@ -28,7 +28,7 @@ def test_joint_controller(): # Initialize the model data model_data = bindings.ModelInitData() - model_data.setSdfString(model_sdf_string) + model_data.sdfString = model_sdf_string # Get the model name model_name = bindings.GazeboWrapper.getModelNameFromSDF(model_sdf_string)