joint_wall.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022 Franka Emika GmbH
3 
4 #include <ros/ros.h>
5 
6 #include <cmath>
7 #include <iostream>
8 #include <sstream>
9 
11 
12 JointWall::JointWall(const double soft_upper_joint_position_limit,
13  const double soft_lower_joint_position_limit,
14  const double PD_zone_width, // NOLINT (readability-identifier-naming)
15  const double D_zone_width, // NOLINT (readability-identifier-naming)
16  const double PD_zone_stiffness, // NOLINT (readability-identifier-naming)
17  const double PD_zone_damping, // NOLINT (readability-identifier-naming)
18  const double D_zone_damping) // NOLINT (readability-identifier-naming)
19  : soft_upper_joint_position_limit_(soft_upper_joint_position_limit),
20  soft_lower_joint_position_limit_(soft_lower_joint_position_limit),
21  PD_zone_width_(PD_zone_width),
22  D_zone_width_(D_zone_width),
23  PD_zone_stiffness_(PD_zone_stiffness),
24  PD_zone_damping_(PD_zone_damping),
25  D_zone_damping_(D_zone_damping) {
26  PD_zone_width_ = positiveCheck(PD_zone_width);
27  D_zone_width_ = positiveCheck(D_zone_width);
28  PD_zone_stiffness_ = positiveCheck(PD_zone_stiffness);
29  PD_zone_damping_ = positiveCheck(PD_zone_damping);
30  D_zone_damping_ = positiveCheck(D_zone_damping);
31 };
32 
33 double JointWall::computeTorque(const double q, const double dq) {
34  init(q, dq);
35  adjustMovingWall(q, dq);
36 
37  double D_zone_boundary_max = // NOLINT (readability-identifier-naming)
39  double PD_zone_boundary_max = // NOLINT (readability-identifier-naming)
41  double D_zone_boundary_min = // NOLINT (readability-identifier-naming)
43  double PD_zone_boundary_min = // NOLINT (readability-identifier-naming)
45 
46  double torque = 0;
47  if (inRange(D_zone_boundary_max, PD_zone_boundary_max, q) ||
48  inRange(PD_zone_boundary_min, D_zone_boundary_min, q)) {
49  // In D zone
50  torque = -D_zone_damping_ * dq;
51  } else if (q > PD_zone_boundary_max) {
52  // In PD zone max
53  torque = -PD_zone_damping_ * dq + PD_zone_stiffness_ * (PD_zone_boundary_max - q);
54  } else if (q < PD_zone_boundary_min) {
55  // In PD zone min
56  torque = -PD_zone_damping_ * dq + PD_zone_stiffness_ * (PD_zone_boundary_min - q);
57  }
58 
59  return torque;
60 }
61 
63  initialized_ = false;
64 }
65 
66 bool JointWall::inRange(double low, double high, double x) {
67  return (low <= x && x <= high);
68 };
69 
70 void JointWall::init(const double q, const double dq) {
71  if (initialized_) {
72  return;
73  }
74 
75  if (q < soft_lower_joint_position_limit_ || q > soft_upper_joint_position_limit_) {
76  std::stringstream ss;
77  ss << "q " << q << " is beyond the joint wall: [" << soft_lower_joint_position_limit_ << ", "
78  << soft_upper_joint_position_limit_ << "]";
79  throw std::runtime_error(ss.str().c_str());
80  }
81 
82  switch (getMotionInWall(q, dq)) {
84  moving_wall_ = false;
85  break;
90  moving_wall_ = true;
91  break;
95  fabs(q - soft_upper_joint_position_limit_) / (PD_zone_width_ + D_zone_width_);
96  moving_wall_ = true;
97  break;
98  }
99  initialized_ = true;
100 }
101 
102 void JointWall::adjustMovingWall(const double q, const double dq) {
103  if (!moving_wall_) {
104  return;
105  }
106  double new_scale;
107  switch (getMotionInWall(q, dq)) {
109  moving_wall_ = false;
110  zone_width_scale_ = 1;
111  break;
114  zone_width_scale_ = fmax(zone_width_scale_, new_scale);
115  break;
118  zone_width_scale_ = fmax(zone_width_scale_, new_scale);
119  break;
122  break;
123  }
124 }
125 
126 double JointWall::positiveCheck(double value) {
127  if (value < 0) {
129  1, "JointWall expects positive parameters, but got negative. Using its absolute value.");
130  value = fabs(value);
131  }
132  return value;
133 }
134 
135 JointWall::MotionInWall JointWall::getMotionInWall(const double q, const double dq) const {
136  double D_zone_boundary_max = // NOLINT (readability-identifier-naming)
138  double D_zone_boundary_min = // NOLINT (readability-identifier-naming)
140  if (q < D_zone_boundary_min) {
141  if (dq <= 0) {
143  }
145  }
146  if (q > D_zone_boundary_max) {
147  if (dq >= 0) {
149  }
151  }
153 }
154 
155 } // namespace franka_example_controllers
MotionInWall getMotionInWall(double q, double dq) const
Checks the motion type in joint wall.
Definition: joint_wall.cpp:135
void reset()
Resets the initialized flag to false.
Definition: joint_wall.cpp:62
static bool inRange(double low, double high, double x)
Checks if x is in range [low, high].
Definition: joint_wall.cpp:66
#define ROS_WARN_THROTTLE(period,...)
void adjustMovingWall(double q, double dq)
Moves the wall with given state if the state is initialized inside the wall.
Definition: joint_wall.cpp:102
Contains functions for calculating torques generated by virtual walls.
double computeTorque(double q, double dq)
Computes the torque with given q and dq.
Definition: joint_wall.cpp:33
MotionInWall
Indicates the status of moving wall, which occurs after initializing the state inside a wall...
Definition: joint_wall.h:68
static double positiveCheck(double value)
Check if the input is positive number, if not print error and return its abs.
Definition: joint_wall.cpp:126
void init(double q, double dq)
Initializes the joint wall computation with initial states.
Definition: joint_wall.cpp:70
JointWall()=delete
Creates a JointWall instance with default params.


franka_example_controllers
Author(s): Franka Emika GmbH
autogenerated on Mon Sep 19 2022 03:06:01