ZeroTorqueOrVelocity.cpp
Go to the documentation of this file.
1 #include "ZeroTorqueOrVelocity.h"
2 
3 #include <VirtualRobot/Nodes/RobotNode.h>
4 #include <VirtualRobot/Robot.h>
5 
8 
11 
13 {
14 
15  NJointControllerRegistration<NJointZeroTorqueOrVelocityController>
17  "NJointZeroTorqueOrVelocityController");
18 
19  std::string
21  {
22  return "NJointZeroTorqueOrVelocityController";
23  }
24 
26  RobotUnitPtr prov,
27  const NJointZeroTorqueOrVelocityControllerConfigPtr& config,
28  const VirtualRobot::RobotPtr& r)
29  {
31  RobotUnitPtr robotUnit = RobotUnitPtr::dynamicCast(prov);
32  ARMARX_CHECK_NOT_NULL(robotUnit);
33  ARMARX_CHECK_NOT_NULL(config);
35  not(config->jointNamesZeroTorque.empty() and config->jointNamesZeroVelocity.empty()));
36  // ARMARX_CHECK_EXPRESSION(!config->jointNamesZeroVelocity.empty());
37 
38  for (auto& jointName : config->jointNamesZeroTorque)
39  {
40  if (std::find(config->jointNamesZeroVelocity.begin(),
41  config->jointNamesZeroVelocity.end(),
42  jointName) != config->jointNamesZeroVelocity.end())
43  {
44  ARMARX_ERROR << jointName << " exists in both torque and velocity controlled mode";
45  }
46  auto node = r->getRobotNode(jointName);
47  ARMARX_CHECK_EXPRESSION(node) << jointName;
48 
49  ControlTargetBase* ct = useControlTarget(jointName, ControlModes::ZeroTorque1DoF);
51  targetsTorque.push_back(ct->asA<ControlTarget1DoFActuatorZeroTorque>());
52  torqueControlled.push_back(true);
53  }
54  for (auto& jointName : config->jointNamesZeroVelocity)
55  {
56  auto node = r->getRobotNode(jointName);
57  ARMARX_CHECK_EXPRESSION(node) << jointName;
58 
59  ControlTargetBase* ct = useControlTarget(jointName, ControlModes::ZeroVelocity1DoF);
61  targetsVel.push_back(ct->asA<ControlTarget1DoFActuatorZeroVelocity>());
62  torqueControlled.push_back(false);
63  }
64  size_t numJoints =
65  config->jointNamesZeroTorque.size() + config->jointNamesZeroVelocity.size();
66  ARMARX_CHECK_EQUAL(numJoints, torqueControlled.size());
67 
68  maxTorque = config->maxTorque;
69  maxVelocity = config->maxVelocity;
70 
72  target.targetTorqueOrVelocity = Ice::FloatSeq(numJoints, 0.0f);
73  this->reinitTripleBuffer(target);
74  }
75 
76  void
78  {
79 
80  // targetTorque = 0.0;
81  }
82 
83  void
85  const IceUtil::Time& timeSinceLastIteration)
86  {
87  size_t torqueIndex = 0;
88  size_t velIndex = 0;
89  for (size_t i = 0; i < torqueControlled.size(); ++i)
90  {
91  if (torqueControlled.at(i))
92  {
93  targetsTorque.at(torqueIndex)->torque = math::MathUtils::LimitTo(
94  rtGetControlStruct().targetTorqueOrVelocity.at(i), maxTorque);
95  torqueIndex++;
96  }
97  else
98  {
99  targetsVel.at(velIndex)->velocity = math::MathUtils::LimitTo(
100  rtGetControlStruct().targetTorqueOrVelocity.at(i), maxVelocity);
101  velIndex++;
102  }
103  }
104  }
105 
108  const VirtualRobot::RobotPtr& robot,
109  const std::map<std::string, ConstControlDevicePtr>& controlDevices,
110  const std::map<std::string, ConstSensorDevicePtr>&)
111  {
112  using namespace armarx::WidgetDescription;
113  HBoxLayoutPtr layout = new HBoxLayout;
114 
115 
116  ::armarx::WidgetDescription::WidgetSeq widgets;
117  auto addSlider = [&](const std::string& label, float min, float max, float defaultValue)
118  {
119  widgets.emplace_back(new Label(false, label));
120  {
121  FloatSliderPtr c_x = new FloatSlider;
122  c_x->name = label;
123  c_x->min = min;
124  c_x->defaultValue = defaultValue;
125  c_x->max = max;
126  widgets.emplace_back(c_x);
127  }
128  };
129 
130  /// joint names
131  LabelPtr label = new Label;
132  label->text = "zero torque joints";
133  layout->children.emplace_back(label);
134 
135  StringComboBoxPtr box = new StringComboBox;
136  box->name = "jointNamesZeroTorque";
137  box->defaultIndex = 0;
138  box->multiSelect = true;
139 
140  for (auto& c : controlDevices)
141  {
142  if (c.second->hasJointController(ControlModes::ZeroTorque1DoF))
143  {
144  box->options.push_back(c.first);
145  }
146  }
147  layout->children.emplace_back(box);
148 
149 
150  /// joint control mode
151  LabelPtr labelControlMode = new Label;
152  labelControlMode->text = "zero velocity joints";
153  layout->children.emplace_back(labelControlMode);
154 
155  StringComboBoxPtr boxControlMode = new StringComboBox;
156  boxControlMode->name = "jointNamesZeroVelocity";
157  boxControlMode->defaultIndex = 0;
158  boxControlMode->multiSelect = true;
159 
160  for (auto& c : controlDevices)
161  {
162  if (c.second->hasJointController(ControlModes::ZeroVelocity1DoF))
163  {
164  boxControlMode->options.push_back(c.first);
165  }
166  }
167  layout->children.emplace_back(boxControlMode);
168 
169 
170  addSlider("maxTorque", 0, 100, 10);
171  addSlider("maxVeloicty", 0, 3.14, 2.0);
172 
173  layout->children.insert(layout->children.end(), widgets.begin(), widgets.end());
174 
175 
176  // auto sliders = createSliders(0, 0, 0, 1, 0.1, 0.01, 10.0f, 10.0f, 0.0f);
177  // layout->children.insert(layout->children.end(),
178  // sliders.begin(),
179  // sliders.end());
180  // layout->children.emplace_back(new HSpacer);
181  return layout;
182  }
183 
184  NJointZeroTorqueOrVelocityControllerConfigPtr
187  {
188  auto varZeroTorque = VariantPtr::dynamicCast(values.at("jointNamesZeroTorque"));
189  ARMARX_CHECK_EXPRESSION(varZeroTorque) << "jointNamesZeroTorque";
190  auto varZeroVelocity = VariantPtr::dynamicCast(values.at("jointNamesZeroVelocity"));
191  ARMARX_CHECK_EXPRESSION(varZeroVelocity) << "jointNamesZeroVelocity";
192  for (auto& pair : values)
193  {
194  std::cout << pair.first;
195  }
196 
197  ARMARX_TRACE;
199  varZeroTorque->get<SingleTypeVariantList>()->toStdVector<std::string>(),
200  varZeroVelocity->get<SingleTypeVariantList>()->toStdVector<std::string>(),
201  values.at("maxTorque")->getFloat(),
202  values.at("maxVeloicty")->getFloat()};
203  }
204 
205  void
207  {
208  }
209 
210  //::armarx::WidgetDescription::WidgetSeq NJointZeroTorqueOrVelocityController::createSliders(float Kp, float Ki, float Kd, float accelerationGain,
211  // float deadZone, float decay, float maxAcceleration, float maxJerk, float torqueToCurrent)
212  //{
213  // using namespace armarx::WidgetDescription;
214  // ::armarx::WidgetDescription::WidgetSeq widgets;
215  // auto addSlider = [&](const std::string & label, float min, float max, float defaultValue)
216  // {
217  // widgets.emplace_back(new Label(false, label));
218  // {
219  // FloatSliderPtr c_x = new FloatSlider;
220  // c_x->name = label;
221  // c_x->min = min;
222  // c_x->defaultValue = defaultValue;
223  // c_x->max = max;
224  // widgets.emplace_back(c_x);
225  // }
226  // };
227 
228  // addSlider("Kp", 0, 20, Kp);
229  // addSlider("Ki", 0, 10, Ki);
230  // addSlider("Kd", 0, 4, Kd);
231  // addSlider("accelerationGain", 0, 10, accelerationGain);
232  // addSlider("deadZone", 0, 2, deadZone);
233  // addSlider("decay", 0, 0.05, decay);
234  // addSlider("maxAcceleration", 0, 40, maxAcceleration);
235  // addSlider("maxJerk", 0, 400, maxJerk);
236  // addSlider("torqueToCurrent", 0, 2, torqueToCurrent);
237  // return widgets;
238  //}
239 
240  //void NJointZeroTorqueOrVelocityController::onPublish(const SensorAndControl&, const DebugDrawerInterfacePrx&, const DebugObserverInterfacePrx& dd)
241  //{
242  // StringVariantBaseMap map;
243  // map["currentTarget"] = new Variant((float)currentTarget * 0.001);
244  // map["velocityTarget"] = new Variant((float)velocityTarget);
245  // map["currentPIDError"] = new Variant((float)currentPIDError);
246  // map["currentPIDIntegral"] = new Variant((float)currentPIDIntegral);
247  // map["currentAcceleration"] = new Variant((float)currentAcceleration);
248  // map["currentJerk"] = new Variant((float)currentJerk);
249  // dd->setDebugChannel("NJointZeroTorqueOrVelocityController", map);
250  //}
251 
252  //WidgetDescription::StringWidgetDictionary NJointZeroTorqueOrVelocityController::getFunctionDescriptions(const Ice::Current&) const
253  //{
254  // using namespace armarx::WidgetDescription;
255 
256 
257  // HBoxLayoutPtr configLayout = new HBoxLayout;
258  // auto sliders = createSliders(Kp, Ki, Kd, accelerationGain, deadZone, decay, maxAcceleration, maxJerk, torqueToCurrent);
259  // configLayout->children.insert(configLayout->children.end(),
260  // sliders.begin(),
261  // sliders.end());
262 
263 
264  // HBoxLayoutPtr targetsLayout = new HBoxLayout;
265  // ::armarx::WidgetDescription::WidgetSeq widgets;
266  // auto addSlider = [&](const std::string & label, float min, float max, float defaultValue)
267  // {
268  // widgets.emplace_back(new Label(false, label));
269  // {
270  // FloatSliderPtr c_x = new FloatSlider;
271  // c_x->name = label;
272  // c_x->min = min;
273  // c_x->defaultValue = defaultValue;
274  // c_x->max = max;
275  // widgets.emplace_back(c_x);
276  // }
277  // };
278 
279  // addSlider("targetTorque", -5, 5, 0);
280  // targetsLayout->children.insert(targetsLayout->children.end(),
281  // widgets.begin(),
282  // widgets.end());
283  // return {{"ControllerConfig", configLayout},
284  // {"ControllerTarget", targetsLayout}
285  // };
286  //}
287 
288  //void NJointZeroTorqueOrVelocityController::callDescribedFunction(const std::string& name, const StringVariantBaseMap& valueMap, const Ice::Current&)
289  //{
290  // if (name == "ControllerConfig")
291  // {
292  // Kp = valueMap.at("Kp")->getFloat();
293  // ARMARX_INFO << VAROUT(Kp);
294  // Ki = valueMap.at("Ki")->getFloat();
295  // Kd = valueMap.at("Kd")->getFloat();
296  // accelerationGain = valueMap.at("accelerationGain")->getFloat();
297  // deadZone = valueMap.at("deadZone")->getFloat();
298  // decay = valueMap.at("decay")->getFloat();
299  // maxAcceleration = valueMap.at("maxAcceleration")->getFloat();
300  // maxJerk = valueMap.at("maxJerk")->getFloat();
301  // torqueToCurrent = valueMap.at("torqueToCurrent")->getFloat();
302  // std::stringstream s;
303  // for (auto& p : valueMap)
304  // {
305  // auto var = VariantPtr::dynamicCast(p.second);
306  // s << p.first << ": " << (var ? var->getOutputValueOnly() : "NULL") << "\n";
307  // }
308  // ARMARX_INFO << "Setting new config: " << s.str();
309  // }
310  // else if (name == "ControllerTarget")
311  // {
312  // targetTorque = valueMap.at("targetTorque")->getFloat();
313  // }
314  // else
315  // {
316  // ARMARX_WARNING << "Unknown function name called: " << name;
317  // }
318  //}
319 
320  void
322  const Ice::Current&)
323  {
325  this->getWriterControlStruct().targetTorqueOrVelocity = targets;
327  }
328 } // namespace armarx::control::njoint_controller::joint_space
armarx::NJointControllerWithTripleBuffer< NJointZeroTorqueOrVelocityControllerTarget >::reinitTripleBuffer
void reinitTripleBuffer(const NJointZeroTorqueOrVelocityControllerTarget &initial)
Definition: NJointControllerWithTripleBuffer.h:68
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: ZeroTorqueOrVelocity.cpp:206
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityControllerTarget
Definition: ZeroTorqueOrVelocity.h:14
SingleTypeVariantList.h
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::NJointZeroTorqueOrVelocityController
NJointZeroTorqueOrVelocityController(RobotUnitPtr prov, const NJointZeroTorqueOrVelocityControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: ZeroTorqueOrVelocity.cpp:25
MathUtils.h
armarx::NJointControllerWithTripleBuffer< NJointZeroTorqueOrVelocityControllerTarget >::rtGetControlStruct
const NJointZeroTorqueOrVelocityControllerTarget & rtGetControlStruct() const
Definition: NJointControllerWithTripleBuffer.h:32
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
ZeroTorqueOrVelocity.h
armarx::ControlTargetBase
Brief description of class JointControlTargetBase.
Definition: ControlTargetBase.h:47
RobotUnit.h
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::targetsTorque
std::vector< ControlTarget1DoFActuatorZeroTorque * > targetsTorque
Definition: ZeroTorqueOrVelocity.h:55
armarx::NJointControllerBase::useControlTarget
ControlTargetBase * useControlTarget(const std::string &deviceName, const std::string &controlMode)
Declares to calculate the ControlTarget for the given ControlDevice in the given ControlMode when rtR...
Definition: NJointController.cpp:410
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::NJointControllerWithTripleBuffer< NJointZeroTorqueOrVelocityControllerTarget >::getWriterControlStruct
NJointZeroTorqueOrVelocityControllerTarget & getWriterControlStruct()
Definition: NJointControllerWithTripleBuffer.h:54
armarx::NJointControllerWithTripleBuffer< NJointZeroTorqueOrVelocityControllerTarget >::LockGuardType
std::lock_guard< std::recursive_mutex > LockGuardType
Definition: NJointControllerWithTripleBuffer.h:14
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::GenerateConfigFromVariants
static NJointZeroTorqueOrVelocityControllerConfigPtr GenerateConfigFromVariants(const StringVariantBaseMap &values)
Definition: ZeroTorqueOrVelocity.cpp:185
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: ZeroTorqueOrVelocity.cpp:84
IceInternal::Handle
Definition: forward_declarations.h:8
visionx::voxelgrid::Label
uint32_t Label
Type of an object label.
Definition: types.h:7
armarx::WidgetDescription
Definition: DefaultWidgetDescriptions.cpp:27
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::setControllerTarget
void setControllerTarget(const Ice::FloatSeq &, const Ice::Current &) override
Definition: ZeroTorqueOrVelocity.cpp:321
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::control::njoint_controller::joint_space
Definition: Torque.cpp:9
armarx::RemoteGui::Client::FloatSlider
Definition: Widgets.h:107
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::GenerateConfigDescription
static WidgetDescription::WidgetPtr GenerateConfigDescription(const VirtualRobot::RobotPtr &, const std::map< std::string, ConstControlDevicePtr > &, const std::map< std::string, ConstSensorDevicePtr > &)
Definition: ZeroTorqueOrVelocity.cpp:107
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::maxTorque
float maxTorque
Definition: ZeroTorqueOrVelocity.h:57
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: ZeroTorqueOrVelocity.cpp:77
armarx::SingleTypeVariantList
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
Definition: SingleTypeVariantList.h:47
armarx::NJointControllerWithTripleBuffer< NJointZeroTorqueOrVelocityControllerTarget >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
armarx::NJointControllerWithTripleBuffer< NJointZeroTorqueOrVelocityControllerTarget >::controlDataMutex
MutexType controlDataMutex
Definition: NJointControllerWithTripleBuffer.h:73
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::maxVelocity
float maxVelocity
Definition: ZeroTorqueOrVelocity.h:58
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: ZeroTorqueOrVelocity.cpp:20
armarx::RemoteGui::Client::HBoxLayout
Definition: Widgets.h:160
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::targetsVel
std::vector< ControlTarget1DoFActuatorZeroVelocity * > targetsVel
Definition: ZeroTorqueOrVelocity.h:56
armarx::math::MathUtils::LimitTo
static double LimitTo(double value, double absThreshold)
Definition: MathUtils.h:56
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
armarx::control::NJointZeroTorqueOrVelocityControllerConfig
Definition: ControllerInterface.ice:51
armarx::control::njoint_controller::joint_space::NJointZeroTorqueOrVelocityController::torqueControlled
std::vector< bool > torqueControlled
Definition: ZeroTorqueOrVelocity.h:59
Logging.h
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::control::njoint_controller::joint_space::registrationControllerNJointZeroTorqueOrVelocityController
NJointControllerRegistration< NJointZeroTorqueOrVelocityController > registrationControllerNJointZeroTorqueOrVelocityController("NJointZeroTorqueOrVelocityController")
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18