PlatformLocalTrajectoryController.cpp
Go to the documentation of this file.
2 
6 #include <ArmarXCore/interface/serialization/Eigen/Eigen_fdi.h>
7 
10 
13 #include <armarx/navigation/platform_controller/aron/PlatformLocalTrajectoryControllerConfig.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  platformTarget = useControlTarget(robotUnit->getRobotPlatformName(),
38  ControlModes::HolonomicPlatformVelocity)
40  ARMARX_CHECK_EXPRESSION(platformTarget)
41  << "The actuator " << robotUnit->getRobotPlatformName() << " has no control mode "
42  << ControlModes::HolonomicPlatformVelocity;
43 
44  const auto configData = ::armarx::fromAron<arondto::Config, Config>(cfg->config);
45  const auto trajectoryFollowingControllerParams = configData.params;
46 
48  configBuffer_updateConfigToAdditionalTask.reinitAllBuffers(configData);
49  configBuffer_updateConfigToOnPublish.reinitAllBuffers(configData);
50 
51  trajectoryFollowingController.emplace(trajectoryFollowingControllerParams);
52 
53  ARMARX_INFO << "Init done.";
54  }
55 
56  std::string
57  Controller::getClassName(const Ice::Current& iceCurrent) const
58  {
61  }
62 
63  void
64  Controller::rtRun(const IceUtil::Time& sensorValuesTimestamp,
65  const IceUtil::Time& timeSinceLastIteration)
66  {
68 
69  // update control devices
70  platformTarget->velocityX = rtGetControlStruct().linear.x();
71  platformTarget->velocityY = rtGetControlStruct().linear.y();
72  platformTarget->velocityRotation = rtGetControlStruct().angular;
73 
74  // read data (for non-rt)
75  robotStateBuffer_rtToAdditionalTask.getWriteBuffer().global_T_robot.matrix() =
76  rtGetRobot()->getGlobalPose();
77  robotStateBuffer_rtToAdditionalTask.commitWrite();
78  }
79 
80  void
82  const Ice::Current& iceCurrent)
83  {
84  ARMARX_IMPORTANT << "Controller::updateConfig";
85 
86  // TODO maybe update pid controller
87 
88  const auto updateConfig = ::armarx::fromAron<arondto::Config, Config>(dto);
89 
90  configBuffer_updateConfigToAdditionalTask.getWriteBuffer() = updateConfig;
91  configBuffer_updateConfigToAdditionalTask.commitWrite();
92 
93  configBuffer_updateConfigToOnPublish.getWriteBuffer() = updateConfig;
94  configBuffer_updateConfigToOnPublish.commitWrite();
95 
96  ARMARX_INFO << "Trajectory with " << updateConfig.targets.trajectory.points().size();
97 
98  ARMARX_IMPORTANT << "done Controller::updateConfig";
99  }
100 
101  void
103  {
104  ARMARX_CHECK(trajectoryFollowingController.has_value());
105 
106  const auto& configBuffer =
107  configBuffer_updateConfigToAdditionalTask.getUpToDateReadBuffer();
108 
109  // if trajectory is empty, set velocity to 0
110  if (configBuffer.targets.trajectory.points().empty())
111  {
112  ARMARX_INFO << deactivateSpam(1) << "Trajectory is empty!";
113 
114  filteredTwist.reset();
115 
116  getWriterControlStruct().reset();
118  return;
119  }
120 
121  // update controller
122  const auto result = trajectoryFollowingController->control(
123  configBuffer.targets.trajectory,
124  robotStateBuffer_rtToAdditionalTask.getUpToDateReadBuffer().global_T_robot);
125 
126  // low-pass filter
127  {
128  const float alpha = configBuffer.params.alpha;
129  filteredTwist.linear =
130  alpha * filteredTwist.linear + (1. - alpha) * result.twist.linear.head<2>();
131  filteredTwist.angular =
132  alpha * filteredTwist.angular + (1. - alpha) * result.twist.angular.z();
133  }
134 
135  // store result
136  getWriterControlStruct() = filteredTwist;
138 
139  // store results (onPublish)
140  targetBuffer_additionalTaskToOnPublish.getWriteBuffer() = filteredTwist;
141  targetBuffer_additionalTaskToOnPublish.commitWrite();
142  }
143 
144  void
146  const DebugDrawerInterfacePrx& debugDrawer,
147  const DebugObserverInterfacePrx& debugObservers)
148  {
149  StringVariantBaseMap datafields;
150 
151  const auto& targets = targetBuffer_additionalTaskToOnPublish.getUpToDateReadBuffer();
152 
153  datafields["vx"] = new Variant(targets.linear.x());
154  datafields["vy"] = new Variant(targets.linear.y());
155  datafields["vyaw"] = new Variant(targets.angular);
156  datafields["trajectory_points"] =
157  new Variant(configBuffer_updateConfigToOnPublish.getUpToDateReadBuffer()
158  .targets.trajectory.points()
159  .size());
160 
161  debugObservers->setDebugChannel(
163  datafields);
164  }
165 
166  void
168  {
169  runTask("PlatformTrajectoryControllerAdditionalTask",
170  [&]
171  {
172  CycleUtil c(10);
173  getObjectScheduler()->waitForObjectStateMinimum(eManagedIceObjectStarted);
174  ARMARX_IMPORTANT << "Create a new thread alone PlatformTrajectory controller";
175  while (getState() == eManagedIceObjectStarted)
176  {
177  if (isControllerActive() and rtReady.load())
178  {
179  ARMARX_VERBOSE << "additional task";
180  additionalTask();
181  }
182  c.waitForCycleDuration();
183  }
184  });
185 
186  ARMARX_INFO << "PlatformTrajectoryVelocityController::onInitNJointController";
187  }
188 
189  void
191  {
192  // additionalTask() is not executed. Thus, we can access lastTwist without mutex.
193  filteredTwist.reset();
194 
195  robotStateBuffer_rtToAdditionalTask.getWriteBuffer().global_T_robot.matrix() =
196  rtGetRobot()->getGlobalPose();
197  robotStateBuffer_rtToAdditionalTask.commitWrite();
198 
199  rtReady.store(true);
200  }
201 
202  void
204  {
205  rtReady.store(false);
206  }
207 
208  Controller::~Controller() = default;
209 
210 } // namespace armarx::navigation::platform_controller::platform_local_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_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
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::platform_controller::platform_local_trajectory::Controller::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: PlatformLocalTrajectoryController.cpp:203
armarx::TripleBuffer::commitWrite
void commitWrite()
Definition: TripleBuffer.h:146
armarx::navigation::platform_controller::platform_local_trajectory::Twist2D::linear
Eigen::Vector2f linear
Definition: PlatformLocalTrajectoryController.h:59
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::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
PlatformLocalTrajectoryController.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::NJointControllerBase::ConfigPtrT
NJointControllerConfigPtr ConfigPtrT
Definition: NJointControllerBase.h:586
armarx::navigation::common::ControllerType::PlatformLocalTrajectory
@ PlatformLocalTrajectory
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_local_trajectory::Controller::onInitNJointController
void onInitNJointController() override
Definition: PlatformLocalTrajectoryController.cpp:167
armarx::NJointControllerBase::rtGetRobot
const VirtualRobot::RobotPtr & rtGetRobot()
TODO make protected and use attorneys.
Definition: NJointControllerBase.h:845
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:177
armarx::NJointControllerBase::isControllerActive
bool isControllerActive(const Ice::Current &=Ice::emptyCurrent) const final override
Definition: NJointControllerBase.h:777
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::NJointControllerWithTripleBuffer< Target >::rtUpdateControlStruct
bool rtUpdateControlStruct()
Definition: NJointControllerWithTripleBuffer.h:38
armarx::navigation::platform_controller::platform_local_trajectory::Controller::additionalTask
void additionalTask()
Definition: PlatformLocalTrajectoryController.cpp:102
armarx::navigation::platform_controller::platform_local_trajectory::Controller::onPublish
void onPublish(const SensorAndControl &sac, const DebugDrawerInterfacePrx &debugDrawer, const DebugObserverInterfacePrx &debugObservers) override
Definition: PlatformLocalTrajectoryController.cpp:145
aron_conversions.h
armarx::NJointControllerWithTripleBuffer< Target >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
ArmarXObjectScheduler.h
armarx::navigation::platform_controller::platform_local_trajectory::Controller::updateConfig
void updateConfig(const ::armarx::aron::data::dto::DictPtr &dto, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: PlatformLocalTrajectoryController.cpp:81
controller_types.h
armarx::navigation::platform_controller::platform_local_trajectory::Controller::getClassName
std::string getClassName(const Ice::Current &iceCurrent=Ice::emptyCurrent) const override
Definition: PlatformLocalTrajectoryController.cpp:57
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
armarx::navigation::platform_controller::platform_local_trajectory::Twist2D::reset
void reset()
Definition: PlatformLocalTrajectoryController.h:63
CycleUtil.h
armarx::navigation::platform_controller::platform_local_trajectory::Controller::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: PlatformLocalTrajectoryController.cpp:190
armarx::navigation::platform_controller::platform_local_trajectory::Controller::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: PlatformLocalTrajectoryController.cpp:64
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
armarx::navigation::common::ControllerTypeNames
const simox::meta::EnumNames< ControllerType > ControllerTypeNames
Definition: controller_types.h:17
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::navigation::platform_controller::platform_local_trajectory::Controller::~Controller
~Controller() override
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::navigation::platform_controller::platform_local_trajectory::Twist2D::angular
float angular
Definition: PlatformLocalTrajectoryController.h:60
armarx::navigation::platform_controller::platform_local_trajectory::Registration
const NJointControllerRegistration< Controller > Registration(common::ControllerTypeNames.to_name(common::ControllerType::PlatformLocalTrajectory))
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
Logging.h
armarx::TripleBuffer::getUpToDateReadBuffer
const T & getUpToDateReadBuffer() const
Definition: TripleBuffer.h:108
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
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::navigation::platform_controller::platform_local_trajectory::Controller::Controller
Controller(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: PlatformLocalTrajectoryController.cpp:21
armarx::navigation::platform_controller::platform_local_trajectory
Definition: aron_conversions.cpp:56