Torque.cpp
Go to the documentation of this file.
1 #include "Torque.h"
2 
3 #include <VirtualRobot/Nodes/RobotNode.h>
4 #include <VirtualRobot/Robot.h>
5 
7 
11 
13 {
14 
16  registrationControllerNJointTorqueController("NJointTorqueController");
17 
18  std::string
19  NJointTorqueController::getClassName(const Ice::Current&) const
20  {
21  return "NJointTorqueController";
22  }
23 
25  const NJointTorqueControllerConfigPtr& config,
26  const VirtualRobot::RobotPtr& r)
27  {
29  RobotUnitPtr robotUnit = RobotUnitPtr::dynamicCast(prov);
30  ARMARX_CHECK_NOT_NULL(robotUnit);
31  ARMARX_CHECK_NOT_NULL(config);
32  ARMARX_CHECK_EXPRESSION(!config->jointNames.empty());
33  for (auto& jointName : config->jointNames)
34  {
35  auto node = r->getRobotNode(jointName);
36  ARMARX_CHECK_EXPRESSION(node) << jointName;
37 
38  ControlTargetBase* ct = useControlTarget(jointName, ControlModes::Torque1DoF);
40  targets.push_back(ct->asA<ControlTarget1DoFActuatorTorque>());
41  }
42  maxTorque = config->maxTorqueNm;
44  target.targetTorques = Ice::FloatSeq(config->jointNames.size(), 0.0f);
45  this->reinitTripleBuffer(target);
46  }
47 
48  void
50  {
51  // targetTorque = 0.0;
52  }
53 
54  void
55  NJointTorqueController::rtRun(const IceUtil::Time& sensorValuesTimestamp,
56  const IceUtil::Time& timeSinceLastIteration)
57  {
58  for (size_t i = 0; i < targets.size(); ++i)
59  {
60  targets.at(i)->torque =
61  std::clamp(rtGetControlStruct().targetTorques.at(i), -maxTorque, maxTorque);
62  }
63  }
64 
67  const VirtualRobot::RobotPtr& robot,
68  const std::map<std::string, ConstControlDevicePtr>& controlDevices,
69  const std::map<std::string, ConstSensorDevicePtr>&)
70  {
71  using namespace armarx::WidgetDescription;
72  HBoxLayoutPtr layout = new HBoxLayout;
73 
74 
75  ::armarx::WidgetDescription::WidgetSeq widgets;
76  auto addSlider = [&](const std::string& label, float min, float max, float defaultValue)
77  {
78  widgets.emplace_back(new Label(false, label));
79  {
80  FloatSliderPtr c_x = new FloatSlider;
81  c_x->name = label;
82  c_x->min = min;
83  c_x->defaultValue = defaultValue;
84  c_x->max = max;
85  widgets.emplace_back(c_x);
86  }
87  };
88 
89  LabelPtr label = new Label;
90  label->text = "joint names";
91  layout->children.emplace_back(label);
92  StringComboBoxPtr box = new StringComboBox;
93  box->defaultIndex = 0;
94  box->multiSelect = true;
95 
96  for (auto& c : controlDevices)
97  {
98  if (c.second->hasJointController(ControlModes::ZeroTorque1DoF))
99  {
100  box->options.push_back(c.first);
101  }
102  }
103 
104  box->name = "jointNames";
105  layout->children.emplace_back(box);
106 
107  addSlider("maxTorque", 0, 100, 10);
108 
109  layout->children.insert(layout->children.end(), widgets.begin(), widgets.end());
110 
111  // auto sliders = createSliders(0, 0, 0, 1, 0.1, 0.01, 10.0f, 10.0f, 0.0f);
112  // layout->children.insert(layout->children.end(),
113  // sliders.begin(),
114  // sliders.end());
115  // layout->children.emplace_back(new HSpacer);
116  return layout;
117  }
118 
119  NJointTorqueControllerConfigPtr
121  {
122  auto var = VariantPtr::dynamicCast(values.at("jointNames"));
123  ARMARX_CHECK_EXPRESSION(var) << "jointNames";
124  return new NJointTorqueControllerConfig{
125  var->get<SingleTypeVariantList>()->toStdVector<std::string>(),
126  values.at("maxTorque")->getFloat()};
127  }
128 
129  void
131  {
132  }
133 
134  //::armarx::WidgetDescription::WidgetSeq NJointTorqueController::createSliders(float Kp, float Ki, float Kd, float accelerationGain,
135  // float deadZone, float decay, float maxAcceleration, float maxJerk, float torqueToCurrent)
136  //{
137  // using namespace armarx::WidgetDescription;
138  // ::armarx::WidgetDescription::WidgetSeq widgets;
139  // auto addSlider = [&](const std::string & label, float min, float max, float defaultValue)
140  // {
141  // widgets.emplace_back(new Label(false, label));
142  // {
143  // FloatSliderPtr c_x = new FloatSlider;
144  // c_x->name = label;
145  // c_x->min = min;
146  // c_x->defaultValue = defaultValue;
147  // c_x->max = max;
148  // widgets.emplace_back(c_x);
149  // }
150  // };
151 
152  // addSlider("Kp", 0, 20, Kp);
153  // addSlider("Ki", 0, 10, Ki);
154  // addSlider("Kd", 0, 4, Kd);
155  // addSlider("accelerationGain", 0, 10, accelerationGain);
156  // addSlider("deadZone", 0, 2, deadZone);
157  // addSlider("decay", 0, 0.05, decay);
158  // addSlider("maxAcceleration", 0, 40, maxAcceleration);
159  // addSlider("maxJerk", 0, 400, maxJerk);
160  // addSlider("torqueToCurrent", 0, 2, torqueToCurrent);
161  // return widgets;
162  //}
163 
164  //void NJointTorqueController::onPublish(const SensorAndControl&, const DebugDrawerInterfacePrx&, const DebugObserverInterfacePrx& dd)
165  //{
166  // StringVariantBaseMap map;
167  // map["currentTarget"] = new Variant((float)currentTarget * 0.001);
168  // map["velocityTarget"] = new Variant((float)velocityTarget);
169  // map["currentPIDError"] = new Variant((float)currentPIDError);
170  // map["currentPIDIntegral"] = new Variant((float)currentPIDIntegral);
171  // map["currentAcceleration"] = new Variant((float)currentAcceleration);
172  // map["currentJerk"] = new Variant((float)currentJerk);
173  // dd->setDebugChannel("NJointTorqueController", map);
174  //}
175 
176  //WidgetDescription::StringWidgetDictionary NJointTorqueController::getFunctionDescriptions(const Ice::Current&) const
177  //{
178  // using namespace armarx::WidgetDescription;
179 
180 
181  // HBoxLayoutPtr configLayout = new HBoxLayout;
182  // auto sliders = createSliders(Kp, Ki, Kd, accelerationGain, deadZone, decay, maxAcceleration, maxJerk, torqueToCurrent);
183  // configLayout->children.insert(configLayout->children.end(),
184  // sliders.begin(),
185  // sliders.end());
186 
187  // HBoxLayoutPtr targetsLayout = new HBoxLayout;
188  // ::armarx::WidgetDescription::WidgetSeq widgets;
189  // auto addSlider = [&](const std::string & label, float min, float max, float defaultValue)
190  // {
191  // widgets.emplace_back(new Label(false, label));
192  // {
193  // FloatSliderPtr c_x = new FloatSlider;
194  // c_x->name = label;
195  // c_x->min = min;
196  // c_x->defaultValue = defaultValue;
197  // c_x->max = max;
198  // widgets.emplace_back(c_x);
199  // }
200  // };
201 
202  // addSlider("targetTorque", -5, 5, 0);
203  // targetsLayout->children.insert(targetsLayout->children.end(),
204  // widgets.begin(),
205  // widgets.end());
206  // return {{"ControllerConfig", configLayout},
207  // {"ControllerTarget", targetsLayout}
208  // };
209  //}
210 
211  //void NJointTorqueController::callDescribedFunction(const std::string& name, const StringVariantBaseMap& valueMap, const Ice::Current&)
212  //{
213  // if (name == "ControllerConfig")
214  // {
215  // Kp = valueMap.at("Kp")->getFloat();
216  // ARMARX_INFO << VAROUT(Kp);
217  // Ki = valueMap.at("Ki")->getFloat();
218  // Kd = valueMap.at("Kd")->getFloat();
219  // accelerationGain = valueMap.at("accelerationGain")->getFloat();
220  // deadZone = valueMap.at("deadZone")->getFloat();
221  // decay = valueMap.at("decay")->getFloat();
222  // maxAcceleration = valueMap.at("maxAcceleration")->getFloat();
223  // maxJerk = valueMap.at("maxJerk")->getFloat();
224  // torqueToCurrent = valueMap.at("torqueToCurrent")->getFloat();
225  // std::stringstream s;
226  // for (auto& p : valueMap)
227  // {
228  // auto var = VariantPtr::dynamicCast(p.second);
229  // s << p.first << ": " << (var ? var->getOutputValueOnly() : "NULL") << "\n";
230  // }
231  // ARMARX_INFO << "Setting new config: " << s.str();
232  // }
233  // else if (name == "ControllerTarget")
234  // {
235  // targetTorque = valueMap.at("targetTorque")->getFloat();
236  // }
237  // else
238  // {
239  // ARMARX_WARNING << "Unknown function name called: " << name;
240  // }
241  //}
242 
243  void
244  NJointTorqueController::setControllerTarget(const Ice::FloatSeq& targets, const Ice::Current&)
245  {
247  this->getWriterControlStruct().targetTorques = targets;
249  }
250 
251 } // namespace armarx::control::njoint_controller::joint_space
armarx::NJointControllerWithTripleBuffer< NJointTorqueControllerTarget >::reinitTripleBuffer
void reinitTripleBuffer(const NJointTorqueControllerTarget &initial)
Definition: NJointControllerWithTripleBuffer.h:68
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
SingleTypeVariantList.h
armarx::NJointControllerRegistration
Definition: NJointControllerRegistry.h:74
armarx::NJointControllerWithTripleBuffer< NJointTorqueControllerTarget >::rtGetControlStruct
const NJointTorqueControllerTarget & rtGetControlStruct() const
Definition: NJointControllerWithTripleBuffer.h:32
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:110
armarx::control::njoint_controller::joint_space::NJointTorqueController::GenerateConfigFromVariants
static NJointTorqueControllerConfigPtr GenerateConfigFromVariants(const StringVariantBaseMap &values)
Definition: Torque.cpp:120
armarx::control::njoint_controller::joint_space::NJointTorqueController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: Torque.cpp:19
armarx::ControlTargetBase
Brief description of class JointControlTargetBase.
Definition: ControlTargetBase.h:47
RobotUnit.h
armarx::control::njoint_controller::joint_space::NJointTorqueController::NJointTorqueController
NJointTorqueController(RobotUnitPtr prov, const NJointTorqueControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: Torque.cpp:24
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:668
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:297
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::control::njoint_controller::joint_space::NJointTorqueController::targets
std::vector< ControlTarget1DoFActuatorTorque * > targets
Definition: Torque.h:47
armarx::NJointControllerWithTripleBuffer< NJointTorqueControllerTarget >::getWriterControlStruct
NJointTorqueControllerTarget & getWriterControlStruct()
Definition: NJointControllerWithTripleBuffer.h:54
armarx::NJointControllerWithTripleBuffer< NJointTorqueControllerTarget >::LockGuardType
std::lock_guard< std::recursive_mutex > LockGuardType
Definition: NJointControllerWithTripleBuffer.h:14
clamp
double clamp(double x, double a, double b)
Definition: point.hpp:136
IceInternal::Handle
Definition: forward_declarations.h:8
visionx::voxelgrid::Label
uint32_t Label
Type of an object label.
Definition: types.h:6
armarx::WidgetDescription
Definition: DefaultWidgetDescriptions.cpp:27
armarx::control::njoint_controller::joint_space::NJointTorqueController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: Torque.cpp:55
armarx::control::njoint_controller::joint_space
Definition: Torque.cpp:12
armarx::RemoteGui::Client::FloatSlider
Definition: Widgets.h:107
armarx::SingleTypeVariantList
The SingleTypeVariantList class is a subclass of VariantContainer and is comparable to a std::vector<...
Definition: SingleTypeVariantList.h:46
armarx::NJointControllerWithTripleBuffer< NJointTorqueControllerTarget >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
armarx::control::njoint_controller::joint_space::registrationControllerNJointTorqueController
NJointControllerRegistration< NJointTorqueController > registrationControllerNJointTorqueController("NJointTorqueController")
ControlTarget1DoFActuator.h
armarx::control::njoint_controller::joint_space::NJointTorqueController::GenerateConfigDescription
static WidgetDescription::WidgetPtr GenerateConfigDescription(const VirtualRobot::RobotPtr &, const std::map< std::string, ConstControlDevicePtr > &, const std::map< std::string, ConstSensorDevicePtr > &)
Definition: Torque.cpp:66
armarx::NJointControllerWithTripleBuffer< NJointTorqueControllerTarget >::controlDataMutex
MutexType controlDataMutex
Definition: NJointControllerWithTripleBuffer.h:73
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::control::njoint_controller::joint_space::NJointTorqueController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: Torque.cpp:49
armarx::RemoteGui::Client::HBoxLayout
Definition: Widgets.h:160
NJointController.h
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
IceUtil::Handle< class RobotUnit >
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:327
armarx::control::njoint_controller::joint_space::NJointTorqueControllerTarget
Definition: Torque.h:10
armarx::control::njoint_controller::joint_space::NJointTorqueController::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: Torque.cpp:130
armarx::control::njoint_controller::joint_space::NJointTorqueController::setControllerTarget
void setControllerTarget(const Ice::FloatSeq &, const Ice::Current &) override
Definition: Torque.cpp:244
armarx::control::njoint_controller::joint_space::NJointTorqueController::maxTorque
float maxTorque
Definition: Torque.h:48
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
Torque.h