Skip to content

Commit b1b8bdc

Browse files
author
Hydran00
committed
matlogger is now optional
1 parent d4fc6fd commit b1b8bdc

File tree

3 files changed

+51
-74
lines changed

3 files changed

+51
-74
lines changed

cartesian_impedance_controller/CMakeLists.txt

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,38 @@ project(cartesian_impedance_controller)
33

44
set(CMAKE_CXX_STANDARD 17)
55
set(ADDITIONAL_COMPILE_OPTIONS -Wall -Wextra -Wpedantic -Wno-unused-parameter)
6+
7+
# Optional parameter to enable logging, only if MatLogger2 is found
8+
# Refer to https://github.com/ADVRHumanoids/MatLogger2 to install MatLogger2
9+
# output file is /tmp/cart_impedance_log<timestamp>.mat
10+
option(LOGGING "Enable logging" ON)
611
add_compile_options(${ADDITIONAL_COMPILE_OPTIONS})
7-
# build in release
12+
13+
# Build in release mode if not specified
814
if(NOT CMAKE_BUILD_TYPE)
915
set(CMAKE_BUILD_TYPE Release)
1016
endif()
17+
1118
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
1219
add_definitions(-DEIGEN_MPL2_ONLY)
1320
find_package(ament_cmake REQUIRED)
1421
find_package(rclcpp REQUIRED)
1522
find_package(effort_controller_base REQUIRED)
16-
# log and debug
1723
find_package(debug_msg REQUIRED)
18-
find_package(matlogger2 REQUIRED)
24+
25+
# Try to find MatLogger2 if logging is enabled
26+
if (LOGGING)
27+
find_package(matlogger2 QUIET)
28+
if (matlogger2_FOUND)
29+
message(STATUS "MatLogger2 found: Logging enabled")
30+
set(LOGGING_ENABLED TRUE)
31+
else()
32+
message(WARNING "MatLogger2 not found: Logging disabled")
33+
set(LOGGING_ENABLED FALSE)
34+
endif()
35+
else()
36+
set(LOGGING_ENABLED FALSE)
37+
endif()
1938

2039
# Convenience variable for dependencies
2140
set(THIS_PACKAGE_INCLUDE_DEPENDS
@@ -25,6 +44,11 @@ set(THIS_PACKAGE_INCLUDE_DEPENDS
2544
debug_msg
2645
)
2746

47+
if (LOGGING_ENABLED)
48+
list(APPEND THIS_PACKAGE_INCLUDE_DEPENDS matlogger2)
49+
add_compile_definitions(LOGGING)
50+
endif()
51+
2852
ament_export_dependencies(
2953
${THIS_PACKAGE_INCLUDE_DEPENDS}
3054
)
@@ -51,45 +75,31 @@ ament_target_dependencies(${PROJECT_NAME}
5175

5276
# Prevent pluginlib from using boost
5377
target_compile_definitions(${PROJECT_NAME} PUBLIC "PLUGINLIB__DISABLE_BOOST_FUNCTIONS")
54-
target_link_libraries(${PROJECT_NAME}
55-
matlogger2::matlogger2
56-
)
78+
79+
if (LOGGING_ENABLED)
80+
target_link_libraries(${PROJECT_NAME} matlogger2::matlogger2)
81+
endif()
82+
5783
#--------------------------------------------------------------------------------
5884
# Install and export
5985
#--------------------------------------------------------------------------------
6086

6187
pluginlib_export_plugin_description_file(controller_interface cartesian_impedance_controller_plugin.xml)
6288

63-
# Note: The workflow as described here https://docs.ros.org/en/foxy/How-To-Guides/Ament-CMake-Documentation.html#building-a-library
64-
# does not work for me.
65-
# I'm not sure what's the problem right now, but I need to out-comment the
66-
# lines below to achieve a correct symlink-install.
67-
68-
#ament_export_targets(my_targets_from_this_package HAS_LIBRARY_TARGET)
69-
7089
install(
7190
DIRECTORY include/
7291
DESTINATION include
7392
)
7493

7594
install(
7695
TARGETS ${PROJECT_NAME}
77-
#EXPORT my_targets_from_this_package
7896
LIBRARY DESTINATION lib
7997
ARCHIVE DESTINATION lib
8098
RUNTIME DESTINATION bin
81-
#INCLUDES DESTINATION include
8299
)
83100

84-
# Note: For the target based workflow, they seem to be superfluous.
85-
# But since that doesn't work yet, I'll add them just in case.
86-
# I took the joint_trajectory_controller as inspiration.
87101
ament_export_dependencies(${THIS_PACKAGE_INCLUDE_DEPENDS})
88-
ament_export_include_directories(
89-
include
90-
)
91-
ament_export_libraries(
92-
${PROJECT_NAME}
93-
)
102+
ament_export_include_directories(include)
103+
ament_export_libraries(${PROJECT_NAME})
94104

95105
ament_package()

cartesian_impedance_controller/include/cartesian_impedance_controller/cartesian_impedance_controller.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010
#include "effort_controller_base/Utility.h"
1111
#include "geometry_msgs/msg/pose_stamped.hpp"
1212
#include "geometry_msgs/msg/wrench_stamped.hpp"
13-
<<<<<<< Updated upstream
14-
#include <matlogger2/matlogger2.h>
1513

16-
#define DEBUG 0
17-
#define LOGGING 1
18-
=======
1914

20-
#define DEBUG 1
21-
>>>>>>> Stashed changes
15+
#define DEBUG 0
16+
#if LOGGING
17+
#include <matlogger2/matlogger2.h>
18+
#endif
2219

2320
namespace cartesian_impedance_controller {
2421

@@ -89,7 +86,9 @@ class CartesianImpedanceController
8986
rclcpp::Subscription<geometry_msgs::msg::PoseStamped>::SharedPtr
9087
m_target_frame_subscriber;
9188
rclcpp::Publisher<debug_msg::msg::Debug>::SharedPtr m_data_publisher;
89+
#if LOGGING
9290
XBot::MatLogger2::Ptr m_logger;
91+
#endif
9392
KDL::Frame m_target_frame;
9493
ctrl::Vector6D m_ft_sensor_wrench;
9594
std::string m_ft_sensor_ref_link;

cartesian_impedance_controller/src/cartesian_impedance_controller.cpp

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ CartesianImpedanceController::on_activate(
137137
m_q_starting_pose = Base::m_joint_positions.data;
138138

139139
m_target_wrench = ctrl::Vector6D::Zero();
140-
141-
m_logger = XBot::MatLogger2::MakeLogger("/tmp/my_log");
140+
#if LOGGING
141+
m_logger = XBot::MatLogger2::MakeLogger("/tmp/cart_impedance_log");
142142
m_logger->set_buffer_mode(XBot::VariableBuffer::Mode::circular_buffer);
143+
#endif
143144
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::
144145
CallbackReturn::SUCCESS;
145146
}
@@ -237,46 +238,6 @@ ctrl::VectorND CartesianImpedanceController::computeTorque() {
237238
m_dyn_solver->JntToMass(Base::m_joint_positions, M);
238239
Eigen::MatrixXd Lambda = (jac * M.data.inverse() * jac.transpose()).inverse();
239240

240-
// RCLCPP_INFO_STREAM(get_node()->get_logger(), "M: " << M.data);
241-
242-
// lambda that computes condition number
243-
auto compute_condition_number = [](const Eigen::MatrixXd &matrix) {
244-
Eigen::JacobiSVD<Eigen::MatrixXd> svd(matrix);
245-
Eigen::VectorXd singular_values = svd.singularValues();
246-
return singular_values(0) / singular_values(singular_values.size() - 1);
247-
};
248-
// RCLCPP_INFO_STREAM(
249-
// get_node()->get_logger(),
250-
// "Condition number of Jacobian: " << compute_condition_number(jac));
251-
// RCLCPP_INFO_STREAM(
252-
// get_node()->get_logger(),
253-
// "Condition number of M: " << compute_condition_number(M.data));
254-
// RCLCPP_INFO_STREAM(
255-
// get_node()->get_logger(),
256-
// "Condition number of Lambda: " << compute_condition_number(Lambda));
257-
258-
// // compue eigenvalues of M
259-
// Eigen::EigenSolver<Eigen::MatrixXd> es(M.data);
260-
// Eigen::VectorXd eigenvalues = es.eigenvalues().real();
261-
// RCLCPP_INFO_STREAM(get_node()->get_logger(),
262-
// "Eigenvalues of M: " << eigenvalues.transpose());
263-
// RCLCPP_INFO_STREAM(get_node()->get_logger(),
264-
// "Condition number of M: " << eigenvalues.maxCoeff() /
265-
// eigenvalues.minCoeff());
266-
// Eigen::EigenSolver<Eigen::MatrixXd> es3(jac);
267-
// RCLCPP_INFO_STREAM(
268-
// get_node()->get_logger(),
269-
// "Eigenvalues of Lambda: " << es3.eigenvalues().real().transpose());
270-
// RCLCPP_INFO_STREAM(
271-
// get_node()->get_logger(),
272-
// "Condition number of Lambda: " << es3.eigenvalues().real().maxCoeff() /
273-
// es3.eigenvalues().real().minCoeff());
274-
// Eigen::JacobiSVD<Eigen::MatrixXd> svd(jac);
275-
// RCLCPP_INFO_STREAM(
276-
// get_node()->get_logger(),
277-
// "Eigenvalues of Jacobian: " << svd.singularValues().transpose());
278-
// RCLCPP_INFO_STREAM(get_node()->get_logger(), "----");
279-
280241
// Compute the motion error
281242
ctrl::Vector6D motion_error = computeMotionError();
282243

@@ -366,7 +327,14 @@ ctrl::VectorND CartesianImpedanceController::computeTorque() {
366327
#endif
367328
#if LOGGING
368329
Eigen::VectorXd Force = K_d * motion_error - D_d * (jac * q_dot);
369-
330+
// lambda that computes condition number
331+
auto compute_condition_number = [](const Eigen::MatrixXd &matrix) {
332+
Eigen::JacobiSVD<Eigen::MatrixXd> svd(matrix);
333+
Eigen::VectorXd singular_values = svd.singularValues();
334+
return singular_values(0) / singular_values(singular_values.size() - 1);
335+
};
336+
m_logger->add("condition_number mass", compute_condition_number(M.data));
337+
m_logger->add("condition_number jac", compute_condition_number(jac));
370338
for (int i = 0; i < 7; i++) {
371339
m_logger->add("stiffness_" + std::to_string(i), stiffness_torque(i));
372340
m_logger->add("damping_" + std::to_string(i), damping_torque(i));

0 commit comments

Comments
 (0)