@@ -26,6 +26,7 @@ CartesianImpedanceController::on_init() {
2626 auto_declare<double >(" stiffness.rot_x" , default_rot_stiff);
2727 auto_declare<double >(" stiffness.rot_y" , default_rot_stiff);
2828 auto_declare<double >(" stiffness.rot_z" , default_rot_stiff);
29+ auto_declare<double >(" max_impedance_force" , 70.0 ); // TODO
2930
3031 return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::
3132 CallbackReturn::SUCCESS;
@@ -72,8 +73,8 @@ CartesianImpedanceController::on_configure(
7273 tmp[4 ] = 2 * sqrt (tmp[4 ]);
7374 tmp[5 ] = 2 * sqrt (tmp[5 ]);
7475
75- // m_cartesian_damping = tmp.asDiagonal();
76-
76+ m_max_impendance_force =
77+ get_node ()-> get_parameter ( " max_impedance_force " ). as_double (); // TODO
7778 // Set nullspace stiffness
7879 m_null_space_stiffness =
7980 get_node ()->get_parameter (" nullspace_stiffness" ).as_double ();
@@ -136,7 +137,10 @@ CartesianImpedanceController::on_activate(
136137 m_q_starting_pose = Base::m_joint_positions.data ;
137138
138139 m_target_wrench = ctrl::Vector6D::Zero ();
139-
140+ #if LOGGING
141+ m_logger = XBot::MatLogger2::MakeLogger (" /tmp/cart_impedance_log" );
142+ m_logger->set_buffer_mode (XBot::VariableBuffer::Mode::circular_buffer);
143+ #endif
140144 return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::
141145 CallbackReturn::SUCCESS;
142146}
@@ -154,8 +158,9 @@ CartesianImpedanceController::on_deactivate(
154158 CallbackReturn::SUCCESS;
155159}
156160
157- controller_interface::return_type CartesianImpedanceController::update (
158- const rclcpp::Time &time, const rclcpp::Duration &period) {
161+ controller_interface::return_type
162+ CartesianImpedanceController::update (const rclcpp::Time &time,
163+ const rclcpp::Duration &period) {
159164 // Update joint states
160165 Base::updateJointStates ();
161166
@@ -183,7 +188,7 @@ ctrl::Vector6D CartesianImpedanceController::computeMotionError() {
183188 // Use Rodrigues Vector for a compact representation of orientation errors
184189 // Only for angles within [0,Pi)
185190 KDL::Vector rot_axis = KDL::Vector::Zero ();
186- double angle = error_kdl.M .GetRotAngle (rot_axis); // rot_axis is normalized
191+ double angle = error_kdl.M .GetRotAngle (rot_axis); // rot_axis is normalized
187192 double distance = error_kdl.p .Normalize ();
188193
189194 // Clamp maximal tolerated error.
@@ -209,6 +214,11 @@ ctrl::Vector6D CartesianImpedanceController::computeMotionError() {
209214}
210215
211216ctrl::VectorND CartesianImpedanceController::computeTorque () {
217+ // Redefine joints velocities in Eigen format
218+ ctrl::VectorND q = Base::m_joint_positions.data ;
219+ ctrl::VectorND q_dot = Base::m_joint_velocities.data ;
220+ ctrl::VectorND q_null_space (Base::m_joint_number);
221+
212222 // Compute the forward kinematics
213223 Base::m_fk_solver->JntToCart (Base::m_joint_positions, m_current_frame);
214224
@@ -226,14 +236,8 @@ ctrl::VectorND CartesianImpedanceController::computeTorque() {
226236
227237 KDL::JntSpaceInertiaMatrix M (Base::m_joint_number);
228238 m_dyn_solver->JntToMass (Base::m_joint_positions, M);
229-
230239 Eigen::MatrixXd Lambda = (jac * M.data .inverse () * jac.transpose ()).inverse ();
231240
232- // Redefine joints velocities in Eigen format
233- ctrl::VectorND q = Base::m_joint_positions.data ;
234- ctrl::VectorND q_dot = Base::m_joint_velocities.data ;
235- ctrl::VectorND q_null_space (Base::m_joint_number);
236-
237241 // Compute the motion error
238242 ctrl::Vector6D motion_error = computeMotionError ();
239243
@@ -254,12 +258,12 @@ ctrl::VectorND CartesianImpedanceController::computeTorque() {
254258 Base::displayInBaseLink (m_cartesian_stiffness, Base::m_end_effector_link);
255259
256260 Eigen::MatrixXd K_d = base_link_stiffness;
257- Eigen::VectorXd damping_correction = 5 .0 * Eigen::VectorXd::Ones (6 );
261+ Eigen::VectorXd damping_correction = 3 .0 * Eigen::VectorXd::Ones (6 );
258262 auto D_d = compute_correct_damping (Lambda, K_d, 1.0 );
259263
260264 // add a small damping correction to the diagonal of D_d to account for model
261265 // inaccuracies, remove this loop if you face strange behavior
262- for (int i = 5 ; i < 6 ; i++) {
266+ for (int i = 3 ; i < 6 ; i++) {
263267 D_d (i, i) = D_d (i, i) + damping_correction (3 );
264268 }
265269 Eigen::VectorXd stiffness_torque = jac.transpose () * (K_d * motion_error);
@@ -320,6 +324,26 @@ ctrl::VectorND CartesianImpedanceController::computeTorque() {
320324 }
321325 }
322326 m_data_publisher->publish (debug_msg);
327+ #endif
328+ #if LOGGING
329+ Eigen::VectorXd Force = K_d * motion_error - D_d * (jac * q_dot);
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));
338+ for (int i = 0 ; i < 7 ; i++) {
339+ m_logger->add (" stiffness_" + std::to_string (i), stiffness_torque (i));
340+ m_logger->add (" damping_" + std::to_string (i), damping_torque (i));
341+ m_logger->add (" coriolis_" + std::to_string (i), tau_coriolis (i));
342+ m_logger->add (" nullspace_" + std::to_string (i), tau_null (i));
343+ if (i < 6 ) {
344+ m_logger->add (" impedance_force_" + std::to_string (i), Force (i));
345+ }
346+ }
323347#endif
324348 // Compute the torque to achieve the desired force
325349 tau_ext = jac.transpose () * m_target_wrench;
@@ -365,7 +389,7 @@ void CartesianImpedanceController::targetFrameCallback(
365389 KDL::Vector (target->pose .position .x , target->pose .position .y ,
366390 target->pose .position .z ));
367391}
368- } // namespace cartesian_impedance_controller
392+ } // namespace cartesian_impedance_controller
369393
370394// Pluginlib
371395#include < pluginlib/class_list_macros.hpp>
0 commit comments