@@ -147,13 +147,14 @@ void PInverse(const Eigen::MatrixXd &M, Eigen::MatrixXd &M_inv,
147147 M_inv = Eigen::MatrixXd (svd.matrixV () * S_inv * svd.matrixU ().transpose ());
148148}
149149
150- void TorqueSafetyGuardFn (std::array<double , 7 > &tau_d_array, double min_torque,
151- double max_torque) {
150+ void TorqueSafetyGuardFn (std::array<double , 7 > &tau_d_array,
151+ const std::array<double , 7 > &min_torques,
152+ const std::array<double , 7 > &max_torques) {
152153 for (size_t i = 0 ; i < tau_d_array.size (); i++) {
153- if (tau_d_array[i] < min_torque ) {
154- tau_d_array[i] = min_torque ;
155- } else if (tau_d_array[i] > max_torque ) {
156- tau_d_array[i] = max_torque ;
154+ if (tau_d_array[i] < min_torques[i] ) {
155+ tau_d_array[i] = min_torques[i] ;
156+ } else if (tau_d_array[i] > max_torques[i] ) {
157+ tau_d_array[i] = max_torques[i] ;
157158 }
158159 }
159160}
@@ -419,18 +420,20 @@ void Franka::osc() {
419420 dist2joint_max = joint_max_.matrix () - q;
420421 dist2joint_min = q - joint_min_.matrix ();
421422
423+ double activation_distance = 0.25 ;
422424 for (int i = 0 ; i < 7 ; i++) {
423- if (dist2joint_max[i] < 0.25 && dist2joint_max[i] > 0.1 )
424- avoidance_force[i] += -avoidance_weights_[i] * dist2joint_max[i];
425- if (dist2joint_min[i] < 0.25 && dist2joint_min[i] > 0.1 )
426- avoidance_force[i] += avoidance_weights_[i] * dist2joint_min[i];
425+ if (dist2joint_max[i] < activation_distance) {
426+ avoidance_force[i] +=
427+ -avoidance_weights_[i] *
428+ (1 / std::max (dist2joint_max[i], 0.001 ) - 1 / activation_distance);
429+ }
430+ if (dist2joint_min[i] < activation_distance) {
431+ avoidance_force[i] +=
432+ avoidance_weights_[i] *
433+ (1 / std::max (dist2joint_min[i], 0.001 ) - 1 / activation_distance);
434+ }
427435 }
428436 tau_d << tau_d + Nullspace * avoidance_force;
429- for (int i = 0 ; i < 7 ; i++) {
430- if (dist2joint_max[i] < 0.1 && tau_d[i] > 0 .) tau_d[i] = 0 .;
431- if (dist2joint_min[i] < 0.1 && tau_d[i] < 0 .) tau_d[i] = 0 .;
432- }
433-
434437 std::array<double , 7 > tau_d_array{};
435438 Eigen::VectorXd::Map (&tau_d_array[0 ], 7 ) = tau_d;
436439
@@ -439,14 +442,16 @@ void Franka::osc() {
439442 std::chrono::high_resolution_clock::now ();
440443 auto time = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1);
441444
445+ // clamp the torques to a safe range.
446+ std::array<double , 7 > min_torques = {-10.0 , -10.0 , -10.0 , -10.0 ,
447+ -5.0 , -5.0 , -5.0 };
448+ std::array<double , 7 > max_torques = {10.0 , 10.0 , 10.0 , 10.0 , 5.0 , 5.0 , 5.0 };
449+ TorqueSafetyGuardFn (tau_d_array, min_torques, max_torques);
450+
451+ // limit the rate of change of the clamped torques.
442452 std::array<double , 7 > tau_d_rate_limited = franka::limitRate (
443453 franka::kMaxTorqueRate , tau_d_array, robot_state.tau_J_d );
444454
445- // deoxys/config/control_config.yml
446- double min_torque = -5 ;
447- double max_torque = 5 ;
448- TorqueSafetyGuardFn (tau_d_rate_limited, min_torque, max_torque);
449-
450455 return tau_d_rate_limited;
451456 });
452457}
@@ -514,16 +519,31 @@ void Franka::joint_controller() {
514519
515520 tau_d << Kp.cwiseProduct (joint_pos_error) - Kd.cwiseProduct (dq);
516521
522+ // Add joint avoidance potential
523+ Eigen::Array<double , 7 , 1 > avoidance_weights;
524+ avoidance_weights << 1 ., 1 ., 1 ., 1 ., 1 ., 10 ., 10 .;
525+ Eigen::Matrix<double , 7 , 1 > avoidance_force;
526+ avoidance_force.setZero ();
517527 Eigen::Matrix<double , 7 , 1 > dist2joint_max;
518528 Eigen::Matrix<double , 7 , 1 > dist2joint_min;
519529
520530 dist2joint_max = joint_max_.matrix () - q;
521531 dist2joint_min = q - joint_min_.matrix ();
522532
533+ double activation_distance = 0.25 ;
523534 for (int i = 0 ; i < 7 ; i++) {
524- if (dist2joint_max[i] < 0.1 && tau_d[i] > 0 .) tau_d[i] = 0 .;
525- if (dist2joint_min[i] < 0.1 && tau_d[i] < 0 .) tau_d[i] = 0 .;
535+ if (dist2joint_max[i] < activation_distance) {
536+ avoidance_force[i] +=
537+ -avoidance_weights[i] *
538+ (1 / std::max (dist2joint_max[i], 0.001 ) - 1 / activation_distance);
539+ }
540+ if (dist2joint_min[i] < activation_distance) {
541+ avoidance_force[i] +=
542+ avoidance_weights[i] *
543+ (1 / std::max (dist2joint_min[i], 0.001 ) - 1 / activation_distance);
544+ }
526545 }
546+ tau_d << tau_d + avoidance_force;
527547
528548 std::array<double , 7 > tau_d_array{};
529549 Eigen::VectorXd::Map (&tau_d_array[0 ], 7 ) = tau_d;
@@ -533,14 +553,17 @@ void Franka::joint_controller() {
533553 std::chrono::high_resolution_clock::now ();
534554 auto time = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1);
535555
556+ // deoxys/config/control_config.yml
557+ // First clamp the torques to a safe range.
558+ std::array<double , 7 > min_torques = {-10.0 , -10.0 , -10.0 , -10.0 ,
559+ -5.0 , -5.0 , -5.0 };
560+ std::array<double , 7 > max_torques = {10.0 , 10.0 , 10.0 , 10.0 , 5.0 , 5.0 , 5.0 };
561+ TorqueSafetyGuardFn (tau_d_array, min_torques, max_torques);
562+
563+ // Then limit the rate of change of the clamped torques.
536564 std::array<double , 7 > tau_d_rate_limited = franka::limitRate (
537565 franka::kMaxTorqueRate , tau_d_array, robot_state.tau_J_d );
538566
539- // deoxys/config/control_config.yml
540- double min_torque = -5 ;
541- double max_torque = 5 ;
542- TorqueSafetyGuardFn (tau_d_rate_limited, min_torque, max_torque);
543-
544567 return tau_d_rate_limited;
545568 });
546569}
0 commit comments