Skip to content

Commit 9ca1bc7

Browse files
authored
Merge pull request #27 from manumerous/feature/add-mujoco-sim
Add robot runtime and mujoco sim
2 parents 7716c73 + 52cfc14 commit 9ca1bc7

File tree

85 files changed

+5503
-282
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+5503
-282
lines changed

.github/workflows/build_test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ jobs:
6464
6565
- name: Build All
6666
run: |
67+
export PARALLEL_JOBS=1
6768
make build-all

Makefile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ NMPC_PACKAGES := $(call find_ros2_packages,$(current_path)/humanoid_nmpc)
4848

4949
ROBOT_MODEL_PACKAGES := $(call find_ros2_packages,$(current_path)/robot_models)
5050

51+
RUNTIME_PACKAGES := $(call find_ros2_packages,$(current_path)/robot_runtime)
52+
5153
# Unified package list
52-
PACKAGES ?= $(NMPC_PACKAGES) $(ROBOT_MODEL_PACKAGES)
54+
PACKAGES ?= $(NMPC_PACKAGES) $(ROBOT_MODEL_PACKAGES) $(RUNTIME_PACKAGES)
5355

5456
############################################################
5557
# Customizable Configuration - User can override these
@@ -179,23 +181,24 @@ launch-g1-dummy-sim:
179181
source install/setup.bash && \
180182
ros2 launch g1_centroidal_mpc dummy_sim.launch.py
181183

182-
launch-wb-g1-dummy-sim:
184+
launch-g1-sim:
183185
cd ${build_dir} && \
184186
source ${ros_source_file} && \
185187
source install/setup.bash && \
186-
ros2 launch g1_wb_mpc wb_dummy_sim.launch.py
188+
ros2 launch g1_centroidal_mpc mujoco_sim.launch.py
187189

188-
launch-neo-dummy-sim:
190+
191+
launch-wb-g1-dummy-sim:
189192
cd ${build_dir} && \
190193
source ${ros_source_file} && \
191194
source install/setup.bash && \
192-
ros2 launch neo_centroidal_mpc dummy_sim.launch.py
195+
ros2 launch g1_wb_mpc dummy_sim.launch.py
193196

194-
launch-wb-neo-dummy-sim:
197+
launch-wb-g1-sim:
195198
cd ${build_dir} && \
196199
source ${ros_source_file} && \
197200
source install/setup.bash && \
198-
ros2 launch neo_wb_mpc wb_dummy_sim.launch.py
201+
ros2 launch g1_wb_mpc mujoco_sim.launch.py
199202

200203
run-ocs2-tests:
201204
echo "make sure you call 'make build-relwithdebinfo' to build the tests before running them." && \

dependencies.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ libboost-all-dev
77
libboost-filesystem-dev
88
libboost-log-dev
99
libglfw3-dev
10+
liburdfdom-dev
11+
libabsl-dev
12+
libxinerama-dev
13+
libxcursor-dev
14+
libxi-dev
1015
ninja-build
1116
ccache
1217
python3-pygame

humanoid_nmpc/humanoid_centroidal_mpc/CMakeLists.txt

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ find_package(ocs2_mpc REQUIRED)
1919
find_package(ocs2_sqp REQUIRED)
2020
find_package(ocs2_pinocchio_interface REQUIRED)
2121
find_package(humanoid_common_mpc REQUIRED)
22+
find_package(robot_model REQUIRED)
2223

2324
find_package(GTest REQUIRED)
2425

@@ -38,7 +39,7 @@ find_package(Eigen3 3.3 REQUIRED NO_MODULE)
3839
## Build ##
3940
###########
4041

41-
set(CMAKE_CXX_STANDARD 17)
42+
set(CMAKE_CXX_STANDARD 20)
4243

4344
set(FLAGS ${OCS2_CXX_FLAGS} -Wno-invalid-partial-specialization)
4445

@@ -64,6 +65,7 @@ set(dependencies
6465
ocs2_sqp
6566
ocs2_pinocchio_interface
6667
humanoid_common_mpc
68+
robot_model
6769
)
6870

6971
link_directories(
@@ -86,6 +88,7 @@ add_library(${PROJECT_NAME}
8688
src/initialization/CentroidalWeightCompInitializer.cpp
8789
src/CentroidalMpcInterface.cpp
8890
src/command/CentroidalMpcTargetTrajectoriesCalculator.cpp
91+
src/mrt/CentroidalMpcMrtJointController.cpp
8992
)
9093
ament_target_dependencies(${PROJECT_NAME}
9194
${dependencies}
@@ -111,34 +114,6 @@ target_compile_options(${PROJECT_NAME} PUBLIC ${OCS2_CXX_FLAGS})
111114
if(BUILD_TESTING)
112115
find_package(ament_cmake_gtest REQUIRED)
113116

114-
# Disable for now until best testing strategy is found.
115-
116-
# ament_add_gtest(test_dynamics_helper_functions test/testDynamicsHelperFunctions.cpp)
117-
# target_link_libraries(test_dynamics_helper_functions
118-
# ${rclcpp_LIBRARIES}
119-
# ${rmw_connext_cpp_LIBRARIES}
120-
# ${std_interfaces}
121-
# ${PROJECT_NAME}
122-
# )
123-
124-
# ament_add_gtest(test_pinocchio_frame_conversions test/testPinocchioFrameConversions.cpp)
125-
# target_link_libraries(test_pinocchio_frame_conversions
126-
# ${rclcpp_LIBRARIES}
127-
# ${rmw_connext_cpp_LIBRARIES}
128-
# ${std_interfaces}
129-
# ${PROJECT_NAME}
130-
# )
131-
132-
# This test is diabled for now. A smarter test structure working with different active joints is needed.
133-
134-
# ament_add_gtest(test_centroidal_model_conversions test/testCentroidalConversions.cpp)
135-
# target_link_libraries(test_centroidal_model_conversions
136-
# ${rclcpp_LIBRARIES}
137-
# ${rmw_connext_cpp_LIBRARIES}
138-
# ${std_interfaces}
139-
# ${PROJECT_NAME}
140-
# )
141-
142117
endif()
143118

144119
#############

humanoid_nmpc/humanoid_centroidal_mpc/include/humanoid_centroidal_mpc/CentroidalMpcInterface.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ class CentroidalMpcInterface final : public RobotInterface {
6060
* @param [in] urdfFile: The absolute path to the URDF file for the robot.
6161
* @param [in] referenceFile: The absolute path to the reference configuration file.
6262
*/
63-
CentroidalMpcInterface(const std::string& taskFile,
64-
const std::string& urdfFile,
65-
const std::string& referenceFile,
66-
const std::string& gaitFile,
67-
bool setupOCP = true);
63+
CentroidalMpcInterface(const std::string& taskFile, const std::string& urdfFile, const std::string& referenceFile, bool setupOCP = true);
6864

6965
~CentroidalMpcInterface() override = default;
7066

humanoid_nmpc/humanoid_centroidal_mpc/include/humanoid_centroidal_mpc/common/CentroidalMpcRobotModel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ class CentroidalMpcRobotModel : public MpcRobotModelBase<SCALAR_T> {
233233
return state.head(6);
234234
};
235235

236+
const CentroidalModelInfoTpl<SCALAR_T>& getCentroidalModelInfo() const { return centroidalModelInfo_; }
237+
236238
private:
237239
CentroidalMpcRobotModel(const CentroidalMpcRobotModel& rhs)
238240
: MpcRobotModelBase<SCALAR_T>(rhs),
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/******************************************************************************
2+
Copyright (c) 2025, Manuel Yves Galliker. All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of the copyright holder nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
******************************************************************************/
29+
30+
#include <ocs2_mpc/MPC_MRT_Interface.h>
31+
32+
#include "humanoid_centroidal_mpc/CentroidalMpcInterface.h"
33+
34+
#include <ocs2_ros2_interfaces/mrt/DummyObserver.h>
35+
#include <robot_model/ControllerBase.h>
36+
#include "humanoid_common_mpc/reference_manager/ProceduralMpcMotionManager.h"
37+
#include "robot_model/RobotDescription.h"
38+
39+
namespace ocs2::humanoid {
40+
41+
class CentroidalMpcMrtJointController final : public ::robot::model::ControlBase {
42+
public:
43+
/**
44+
* Constructor.
45+
*
46+
* @param [in] mpc: The underlying MPC class to be used.
47+
* @param [in] topicPrefix: The robot's name.
48+
* @param [in] mpcDesiredFrequency: The max frequency to run the mpc at.
49+
*/
50+
CentroidalMpcMrtJointController(const ::robot::model::RobotDescription& robotDescription,
51+
const ModelSettings& modelSettings,
52+
const CentroidalMpcRobotModel<scalar_t>& mpcRobotModel,
53+
MPC_BASE& mpc,
54+
PinocchioInterface pinocchioInterface,
55+
scalar_t mpcDesiredFrequency = -1,
56+
std::shared_ptr<DummyObserver> rVizVisualizerPtr = nullptr);
57+
58+
/**
59+
* Destructor.
60+
*/
61+
~CentroidalMpcMrtJointController();
62+
63+
bool ready() const { return mcpMrtInterface_.initialPolicyReceived(); }
64+
65+
/**
66+
* Handles the low level controller loop that updates the mpc observation, reads out the latest policy and sets the joint control action.
67+
*/
68+
69+
void computeJointControlAction(scalar_t time,
70+
const ::robot::model::RobotState& robotState,
71+
::robot::model::RobotJointAction& robotJointAction) override;
72+
73+
void startMpcThread(const ::robot::model::RobotState& initRobotState);
74+
75+
private:
76+
/**
77+
* Handles the MPC solver thread.
78+
*/
79+
void solverWorker();
80+
81+
/**
82+
* Method to convert the latest observation msg to a stable desired trajectory (current position, zero velocity and
83+
* acceleration)
84+
*
85+
* @param [in] msg: The observation message.
86+
*/
87+
TargetTrajectories currentObservationToResetTrajectory(const SystemObservation& currentMpcObservation);
88+
89+
void updateMpcState(vector_t& mpcState, const ::robot::model::RobotState& robotState);
90+
void updateMpcObservation(ocs2::SystemObservation& mpcObservation, const ::robot::model::RobotState& robotState);
91+
92+
MPC_MRT_Interface mcpMrtInterface_;
93+
94+
PinocchioInterface pinocchioInterface_;
95+
ocs2::SystemObservation currentMpcObservation_;
96+
std::unique_ptr<CentroidalMpcRobotModel<scalar_t>> mpcRobotModelPtr_;
97+
std::vector<size_t> mpcJointIndices_;
98+
std::vector<size_t> otherJointIndices_;
99+
100+
size_t mpcDeltaTMicroSeconds_;
101+
bool realtime_; // True if MPC is to be run as fast as possible
102+
103+
std::atomic_bool terminateThread_{false};
104+
std::jthread solver_worker_;
105+
106+
std::shared_ptr<DummyObserver> visualizerPtr_;
107+
108+
vector_t inverse_dynamics_kp_;
109+
vector_t inverse_dynamics_kd_;
110+
};
111+
112+
} // namespace ocs2::humanoid

humanoid_nmpc/humanoid_centroidal_mpc/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<depend>ocs2_pinocchio_interface</depend>
2121
<depend>ocs2_ros2_interfaces</depend>
2222
<depend>pinocchio</depend>
23+
<depend>robot_model</depend>
2324

2425
<depend>humanoid_common_mpc</depend>
2526

humanoid_nmpc/humanoid_centroidal_mpc/src/CentroidalMpcInterface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ namespace ocs2::humanoid {
6969
/******************************************************************************************************/
7070
/******************************************************************************************************/
7171

72-
CentroidalMpcInterface::CentroidalMpcInterface(
73-
const std::string& taskFile, const std::string& urdfFile, const std::string& referenceFile, const std::string& gaitFile, bool setupOCP)
72+
CentroidalMpcInterface::CentroidalMpcInterface(const std::string& taskFile,
73+
const std::string& urdfFile,
74+
const std::string& referenceFile,
75+
bool setupOCP)
7476
: taskFile_(taskFile),
7577
urdfFile_(urdfFile),
7678
referenceFile_(referenceFile),

humanoid_nmpc/humanoid_centroidal_mpc/src/command/CentroidalMpcTargetTrajectoriesCalculator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ TargetTrajectories CentroidalMpcTargetTrajectoriesCalculator::commandedVelocityT
106106

107107
vector6_t targetMomentum;
108108

109-
const Eigen::Matrix<scalar_t, 6, 6> Ab = A.template leftCols<6>();
109+
const Eigen::Matrix<scalar_t, 6, 6> Ab = A.leftCols<6>();
110110
const Eigen::Matrix<scalar_t, 6, 6> Ab_inv = computeFloatingBaseCentroidalMomentumMatrixInverse(Ab);
111111

112112
// This did not lead to meaningful commands around the z axis. Needs more investigations.

0 commit comments

Comments
 (0)