PlatformGlobalTrajectoryController.cpp
Go to the documentation of this file.
2 
7 
10 
13 #include <armarx/navigation/platform_controller/aron/PlatformGlobalTrajectoryControllerConfig.aron.generated.h>
15 
17 {
18  const NJointControllerRegistration<Controller> Registration(
20 
21  Controller::Controller(const RobotUnitPtr& robotUnit,
22  const NJointControllerConfigPtr& config,
24  {
25  ARMARX_IMPORTANT << "Creating "
26  << common::ControllerTypeNames.to_name(
28  // config
29  ConfigPtrT cfg = ConfigPtrT::dynamicCast(config);
31  // ARMARX_CHECK_EXPRESSION(!cfg->nodeSetName.empty());
32 
33  ARMARX_CHECK_EXPRESSION(robotUnit);
34 
35  const auto robot = useSynchronizedRtRobot();
36 
37  // Control target
38  {
39  const std::string controlTargetName = robotUnit->getRobotPlatformName();
40 
41  ARMARX_INFO << "Using control target " << controlTargetName;
42  auto* ct = useControlTarget(controlTargetName,
43  ControlModes::HolonomicPlatformVelocity);
44  ARMARX_CHECK_NOT_NULL(ct) << "Cannot use control target `" << controlTargetName << "`";
45 
46  platformTarget = ct->asA<ControlTargetHolonomicPlatformVelocity>();
47 
48  ARMARX_CHECK_EXPRESSION(platformTarget)
49  << "The actuator " << controlTargetName << " has no control mode "
50  << ControlModes::HolonomicPlatformVelocity;
51  }
52 
53 
54  const auto configData = ::armarx::fromAron<arondto::Config, Config>(cfg->config);
55  const auto trajectoryFollowingControllerParams = configData.params;
56 
58  configBuffer_updateConfigToAdditionalTask.reinitAllBuffers(configData);
59  configBuffer_updateConfigToOnPublish.reinitAllBuffers(configData);
60 
61  trajectoryFollowingController.emplace(trajectoryFollowingControllerParams);
62 
63  ARMARX_INFO << "Init done.";
64  }
65 
66  std::string
67  Controller::getClassName(const Ice::Current& iceCurrent) const
68  {
71  }
72 
73  void
74  Controller::rtRun(const IceUtil::Time& sensorValuesTimestamp,
75  const IceUtil::Time& timeSinceLastIteration)
76  {
78 
79  // update control devices
80  platformTarget->velocityX = rtGetControlStruct().linear.x();
81  platformTarget->velocityY = rtGetControlStruct().linear.y();
82  platformTarget->velocityRotation = rtGetControlStruct().angular;
83 
84  // read data (for non-rt)
85  robotStateBuffer_rtToAdditionalTask.getWriteBuffer().global_T_robot.matrix() =
86  rtGetRobot()->getGlobalPose();
87  robotStateBuffer_rtToAdditionalTask.commitWrite();
88  }
89 
90  void
92  const Ice::Current& iceCurrent)
93  {
94  ARMARX_IMPORTANT << "Controller::updateConfig";
95 
96  // TODO maybe update pid controller
97 
98  const auto updateConfig = ::armarx::fromAron<arondto::Config, Config>(dto);
99 
100  configBuffer_updateConfigToAdditionalTask.getWriteBuffer() = updateConfig;
101  configBuffer_updateConfigToAdditionalTask.commitWrite();
102 
103  configBuffer_updateConfigToOnPublish.getWriteBuffer() = updateConfig;
104  configBuffer_updateConfigToOnPublish.commitWrite();
105 
106  ARMARX_INFO << "Trajectory with " << updateConfig.targets.trajectory.points().size();
107 
108  ARMARX_IMPORTANT << "done Controller::updateConfig";
109  }
110 
111  void
113  {
114  ARMARX_TRACE;
115  ARMARX_CHECK(trajectoryFollowingController.has_value());
116 
117  const auto& configBuffer =
118  configBuffer_updateConfigToAdditionalTask.getUpToDateReadBuffer();
119 
120 
121  // if trajectory is empty, set velocity to 0
122  if (configBuffer.targets.trajectory.points().empty())
123  {
124  ARMARX_INFO << deactivateSpam(1) << "Trajectory is empty!";
125 
126  filteredTwist.reset();
127 
128  getWriterControlStruct().reset();
130  return;
131  }
132 
133  // update controller
134  ARMARX_TRACE;
135 
137  trajectoryFollowingController->control(
138  configBuffer.targets.trajectory,
139  robotStateBuffer_rtToAdditionalTask.getUpToDateReadBuffer().global_T_robot);
140 
141  // low-pass filter
142  {
143  const float alpha = configBuffer.params.alpha;
144  filteredTwist.linear =
145  alpha * filteredTwist.linear + (1. - alpha) * result.twist.linear.head<2>();
146  filteredTwist.angular =
147  alpha * filteredTwist.angular + (1. - alpha) * result.twist.angular.z();
148  }
149 
150  // store result
151  getWriterControlStruct() = filteredTwist;
152  ARMARX_TRACE;
154 
155  // store results (onPublish)
156  targetBuffer_additionalTaskToOnPublish.getWriteBuffer().target = filteredTwist;
157  targetBuffer_additionalTaskToOnPublish.getWriteBuffer().dropPointVelocity =
158  result.dropPoint.velocity;
159  targetBuffer_additionalTaskToOnPublish.getWriteBuffer().currentOrientation =
160  result.currentOrientation;
161  targetBuffer_additionalTaskToOnPublish.getWriteBuffer().desiredOrientation =
162  result.desiredOrientation;
163  targetBuffer_additionalTaskToOnPublish.getWriteBuffer().orientationError =
164  result.orientationError;
165  targetBuffer_additionalTaskToOnPublish.getWriteBuffer().positionError =
166  result.positionError;
167  targetBuffer_additionalTaskToOnPublish.getWriteBuffer().isFinalSegment =
168  result.isFinalSegment;
169  targetBuffer_additionalTaskToOnPublish.commitWrite();
170  }
171 
172  void
174  const DebugDrawerInterfacePrx& /*debugDrawer*/,
175  const DebugObserverInterfacePrx& debugObservers)
176  {
177  StringVariantBaseMap datafields;
178 
179  const auto& debugStuff = targetBuffer_additionalTaskToOnPublish.getUpToDateReadBuffer();
180 
181 
182  datafields["vx"] = new Variant(debugStuff.target.linear.x());
183  datafields["vy"] = new Variant(debugStuff.target.linear.y());
184  datafields["vyaw"] = new Variant(debugStuff.target.angular);
185  datafields["trajectory_points"] =
186  new Variant(configBuffer_updateConfigToOnPublish.getUpToDateReadBuffer()
187  .targets.trajectory.points()
188  .size());
189 
190  datafields["drop_point_velocity"] = new Variant(debugStuff.dropPointVelocity);
191 
192  datafields["orientationError"] = new Variant(debugStuff.orientationError);
193  datafields["desiredOrientation"] = new Variant(debugStuff.desiredOrientation);
194  datafields["currentOrientation"] = new Variant(debugStuff.currentOrientation);
195  datafields["isFinalSegment"] = new Variant(debugStuff.isFinalSegment);
196 
197  datafields["positionError"] = new Variant(debugStuff.positionError);
198 
199  debugObservers->setDebugChannel(
201  datafields);
202  }
203 
204  void
206  {
207  ARMARX_TRACE;
208  ARMARX_INFO << "PlatformGlobalTrajectoryController::onInitNJointController";
209 
210  runTask("PlatformGlobalTrajectoryControllerAdditionalTask",
211  [&]
212  {
213  CycleUtil c(10);
214  getObjectScheduler()->waitForObjectStateMinimum(eManagedIceObjectStarted);
215  ARMARX_IMPORTANT << "Create a new thread alone PlatformGlobalTrajectoryController controller";
216  while (getState() == eManagedIceObjectStarted)
217  {
218  if (isControllerActive() and rtReady.load())
219  {
220  ARMARX_VERBOSE << "additional task";
221  additionalTask();
222  }
223  c.waitForCycleDuration();
224  }
225  });
226 
227  ARMARX_INFO << "PlatformGlobalTrajectoryController::onInitNJointController done.";
228 
229 
230  }
231 
232  void
234  {
235  // additionalTask() is not executed. Thus, we can access lastTwist without mutex.
236  filteredTwist.reset();
237 
238  robotStateBuffer_rtToAdditionalTask.getWriteBuffer().global_T_robot.matrix() =
239  rtGetRobot()->getGlobalPose();
240  robotStateBuffer_rtToAdditionalTask.commitWrite();
241 
242  rtReady.store(true);
243  }
244 
245  void
247  {
248  rtReady.store(false);
249  }
250 
251  Controller::~Controller() = default;
252 
253 } // namespace armarx::navigation::platform_controller::platform_global_trajectory
armarx::NJointControllerWithTripleBuffer< Target >::reinitTripleBuffer
void reinitTripleBuffer(const Target &initial)
Definition: NJointControllerWithTripleBuffer.h:68
armarx::ControlTargetHolonomicPlatformVelocity
Brief description of class ControlTargetHolonomicPlatformVelocity.
Definition: ControlTargetHolonomicPlatformVelocity.h:38
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::navigation::traj_ctrl::global::TrajectoryControllerResult::currentOrientation
float currentOrientation
Definition: TrajectoryController.h:49
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::navigation::traj_ctrl::global::TrajectoryControllerResult
Definition: TrajectoryController.h:44
armarx::navigation::platform_controller::platform_global_trajectory::Twist2D::reset
void reset()
Definition: PlatformGlobalTrajectoryController.h:65
armarx::TripleBuffer::getWriteBuffer
T & getWriteBuffer()
Definition: TripleBuffer.h:90
armarx::NJointControllerBase::useSynchronizedRtRobot
const VirtualRobot::RobotPtr & useSynchronizedRtRobot(bool updateCollisionModel=false)
Requests a VirtualRobot for use in rtRun *.
Definition: NJointController.cpp:293
armarx::NJointControllerWithTripleBuffer< Target >::rtGetControlStruct
const Target & rtGetControlStruct() const
Definition: NJointControllerWithTripleBuffer.h:32
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::navigation::traj_ctrl::global::TrajectoryControllerResult::desiredOrientation
float desiredOrientation
Definition: TrajectoryController.h:50
armarx::TripleBuffer::commitWrite
void commitWrite()
Definition: TripleBuffer.h:146
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::navigation::core::GlobalTrajectoryPoint::velocity
float velocity
Definition: Trajectory.h:38
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
armarx::CycleUtil
This util class helps with keeping a cycle time during a control cycle.
Definition: CycleUtil.h:40
armarx::ManagedIceObject::getState
int getState() const
Retrieve current state of the ManagedIceObject.
Definition: ManagedIceObject.cpp:725
armarx::navigation::common::ControllerType::PlatformGlobalTrajectory
@ PlatformGlobalTrajectory
armarx::navigation::platform_controller::platform_global_trajectory::Controller::updateConfig
void updateConfig(const ::armarx::aron::data::dto::DictPtr &dto, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: PlatformGlobalTrajectoryController.cpp:91
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::NJointControllerBase::ConfigPtrT
NJointControllerConfigPtr ConfigPtrT
Definition: NJointControllerBase.h:586
armarx::navigation::platform_controller::platform_global_trajectory::Controller::onInitNJointController
void onInitNJointController() override
Definition: PlatformGlobalTrajectoryController.cpp:205
armarx::navigation::traj_ctrl::global::TrajectoryControllerResult::dropPoint
core::GlobalTrajectoryPoint dropPoint
Definition: TrajectoryController.h:47
armarx::NJointControllerWithTripleBuffer< Target >::getWriterControlStruct
Target & getWriterControlStruct()
Definition: NJointControllerWithTripleBuffer.h:54
armarx::ControlTargetHolonomicPlatformVelocity::velocityRotation
float velocityRotation
Definition: ControlTargetHolonomicPlatformVelocity.h:43
type.h
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::navigation::platform_controller::platform_global_trajectory::Controller::additionalTask
void additionalTask()
Definition: PlatformGlobalTrajectoryController.cpp:112
armarx::NJointControllerBase::rtGetRobot
const VirtualRobot::RobotPtr & rtGetRobot()
TODO make protected and use attorneys.
Definition: NJointControllerBase.h:845
armarx::navigation::traj_ctrl::global::TrajectoryControllerResult::isFinalSegment
bool isFinalSegment
Definition: TrajectoryController.h:48
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:177
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::navigation::platform_controller::platform_global_trajectory::Controller::Controller
Controller(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: PlatformGlobalTrajectoryController.cpp:21
armarx::navigation::traj_ctrl::global::TrajectoryControllerResult::orientationError
float orientationError
Definition: TrajectoryController.h:51
armarx::navigation::platform_controller::platform_global_trajectory::Controller::getClassName
std::string getClassName(const Ice::Current &iceCurrent=Ice::emptyCurrent) const override
Definition: PlatformGlobalTrajectoryController.cpp:67
armarx::NJointControllerBase::isControllerActive
bool isControllerActive(const Ice::Current &=Ice::emptyCurrent) const final override
Definition: NJointControllerBase.h:777
armarx::navigation::platform_controller::platform_global_trajectory::Controller::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: PlatformGlobalTrajectoryController.cpp:246
armarx::NJointControllerBase::runTask
void runTask(const std::string &taskName, Task &&task)
Executes a given task in a separate thread from the Application ThreadPool.
Definition: NJointControllerBase.h:753
armarx::navigation::platform_controller::platform_global_trajectory::Twist2D::angular
float angular
Definition: PlatformGlobalTrajectoryController.h:62
armarx::NJointControllerWithTripleBuffer< Target >::rtUpdateControlStruct
bool rtUpdateControlStruct()
Definition: NJointControllerWithTripleBuffer.h:38
armarx::navigation::platform_controller::platform_global_trajectory::Registration
const NJointControllerRegistration< Controller > Registration(common::ControllerTypeNames.to_name(common::ControllerType::PlatformGlobalTrajectory))
aron_conversions.h
armarx::NJointControllerWithTripleBuffer< Target >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
ArmarXObjectScheduler.h
controller_types.h
armarx::ControlTargetHolonomicPlatformVelocity::velocityX
float velocityX
Definition: ControlTargetHolonomicPlatformVelocity.h:41
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::ControlTargetHolonomicPlatformVelocity::velocityY
float velocityY
Definition: ControlTargetHolonomicPlatformVelocity.h:42
CycleUtil.h
ExpressionException.h
armarx::navigation::core::Twist::linear
LinearVelocity linear
Definition: basic_types.h:55
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
ControlTargetHolonomicPlatformVelocity.h
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
PlatformGlobalTrajectoryController.h
armarx::navigation::common::ControllerTypeNames
const simox::meta::EnumNames< ControllerType > ControllerTypeNames
Definition: controller_types.h:17
armarx::navigation::platform_controller::platform_global_trajectory::Controller::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: PlatformGlobalTrajectoryController.cpp:74
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::navigation::platform_controller::platform_global_trajectory::Controller::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: PlatformGlobalTrajectoryController.cpp:233
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::navigation::core::Twist::angular
AngularVelocity angular
Definition: basic_types.h:56
armarx::navigation::traj_ctrl::global::TrajectoryControllerResult::twist
core::Twist twist
Definition: TrajectoryController.h:46
armarx::navigation::platform_controller::platform_global_trajectory::Twist2D::linear
Eigen::Vector2f linear
Definition: PlatformGlobalTrajectoryController.h:61
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
armarx::navigation::traj_ctrl::global::TrajectoryControllerResult::positionError
float positionError
Definition: TrajectoryController.h:52
Logging.h
armarx::navigation::platform_controller::platform_global_trajectory::Controller::~Controller
~Controller() override
armarx::TripleBuffer::getUpToDateReadBuffer
const T & getUpToDateReadBuffer() const
Definition: TripleBuffer.h:108
armarx::navigation::platform_controller::platform_global_trajectory
Definition: aron_conversions.cpp:38
armarx::TripleBuffer::reinitAllBuffers
std::enable_if< std::is_copy_constructible< U >::value >::type reinitAllBuffers(const T &init)
Definition: TripleBuffer.h:153
NJointControllerRegistry.h
armarx::ManagedIceObject::getObjectScheduler
ArmarXObjectSchedulerPtr getObjectScheduler() const
Definition: ManagedIceObject.cpp:731
armarx::navigation::platform_controller::platform_global_trajectory::Controller::onPublish
void onPublish(const SensorAndControl &sac, const DebugDrawerInterfacePrx &debugDrawer, const DebugObserverInterfacePrx &debugObservers) override
Definition: PlatformGlobalTrajectoryController.cpp:173
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18