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
17
18 std::string
19 NJointTorqueController::getClassName(const Ice::Current&) const
20 {
21 return "NJointTorqueController";
22 }
23
25 const NJointTorqueControllerConfigPtr& config,
27 {
29 RobotUnitPtr robotUnit = RobotUnitPtr::dynamicCast(prov);
30 ARMARX_CHECK_NOT_NULL(robotUnit);
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
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
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 > &)
Definition Torque.cpp:66
void setControllerTarget(const Ice::FloatSeq &, const Ice::Current &) override
Definition Torque.cpp:244
std::vector< ControlTarget1DoFActuatorTorque * > targets
Definition Torque.h:47
NJointTorqueController(RobotUnitPtr prov, const NJointTorqueControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition Torque.cpp:24
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition Torque.cpp:130
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition Torque.cpp:55
static NJointTorqueControllerConfigPtr GenerateConfigFromVariants(const StringVariantBaseMap &values)
Definition Torque.cpp:120
std::string getClassName(const Ice::Current &) const override
Definition Torque.cpp:19
void rtPreActivateController() override
This function is called before the controller is activated.
Definition Torque.cpp:49
#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...
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
::IceInternal::Handle<::armarx::WidgetDescription::Widget > WidgetPtr
NJointControllerRegistration< NJointTorqueController > registrationControllerNJointTorqueController("NJointTorqueController")
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)