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>
17  registrationControllerNJointZeroTorqueController("NJointZeroTorqueController");
18 
19  std::string
20  NJointZeroTorqueController::getClassName(const Ice::Current&) const
21  {
22  return "NJointZeroTorqueController";
23  }
24 
26  RobotUnitPtr prov,
27  const NJointZeroTorqueControllerConfigPtr& config,
28  const VirtualRobot::RobotPtr& r)
29  {
31  RobotUnitPtr robotUnit = RobotUnitPtr::dynamicCast(prov);
32  ARMARX_CHECK_NOT_NULL(robotUnit);
33  ARMARX_CHECK_NOT_NULL(config);
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
135  {
136  }
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
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:110
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:668
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:297
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::control::njoint_controller::joint_space::NJointZeroTorqueController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: ZeroTorque.cpp:51
armarx::control::njoint_controller::joint_space::NJointZeroTorqueController::NJointZeroTorqueController
NJointZeroTorqueController(RobotUnitPtr prov, const NJointZeroTorqueControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: ZeroTorque.cpp:25
armarx::control::njoint_controller::joint_space::NJointZeroTorqueController::setControllerTarget
void setControllerTarget(const Ice::FloatSeq &, const Ice::Current &) override
Definition: ZeroTorque.cpp:249
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:6
ZeroTorque.h
armarx::WidgetDescription
Definition: DefaultWidgetDescriptions.cpp:27
armarx::control::njoint_controller::joint_space
Definition: Torque.cpp:12
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:46
armarx::NJointControllerWithTripleBuffer< NJointZeroTorqueControllerTarget >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
ControlTarget1DoFActuator.h
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:134
armarx::control::njoint_controller::joint_space::NJointZeroTorqueController::GenerateConfigFromVariants
static NJointZeroTorqueControllerConfigPtr GenerateConfigFromVariants(const StringVariantBaseMap &values)
Definition: ZeroTorque.cpp:124
armarx::control::njoint_controller::joint_space::NJointZeroTorqueController::targets
std::vector< ControlTarget1DoFActuatorZeroTorque * > targets
Definition: ZeroTorque.h:54
armarx::RemoteGui::Client::HBoxLayout
Definition: Widgets.h:160
armarx::control::njoint_controller::joint_space::NJointZeroTorqueController::maxTorque
float maxTorque
Definition: ZeroTorque.h:55
NJointController.h
armarx::math::MathUtils::LimitTo
static double LimitTo(double value, double absThreshold)
Definition: MathUtils.h:71
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
IceUtil::Handle< class RobotUnit >
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:327
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:58
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:70
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::control::njoint_controller::joint_space::NJointZeroTorqueController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: ZeroTorque.cpp:20