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