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