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