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