PlatformLocalTrajectoryController.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 
5 #include <Ice/Current.h>
6 #include <IceUtil/Time.h>
7 
8 #include <VirtualRobot/VirtualRobot.h>
9 
14 #include <ArmarXCore/interface/core/ManagedIceObjectDefinitions.h>
15 #include <ArmarXCore/interface/observers/ObserverInterface.h>
16 #include <ArmarXCore/interface/observers/VariantBase.h>
17 #include <ArmarXCore/interface/serialization/Eigen/Eigen_fdi.h>
19 
25 #include <RobotAPI/interface/units/RobotUnit/NJointController.h>
26 #include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
27 
29 #include <armarx/navigation/platform_controller/aron/PlatformLocalTrajectoryControllerConfig.aron.generated.h>
31 
33 {
34  const NJointControllerRegistration<Controller> Registration(
36 
38  const NJointControllerConfigPtr& config,
40  {
41  ARMARX_IMPORTANT << "Creating "
42  << common::ControllerTypeNames.to_name(
44  // config
45  ConfigPtrT cfg = ConfigPtrT::dynamicCast(config);
47  // ARMARX_CHECK_EXPRESSION(!cfg->nodeSetName.empty());
48 
49  ARMARX_CHECK_EXPRESSION(robotUnit);
50 
51  const auto robot = useSynchronizedRtRobot();
52 
53  platformTarget = useControlTarget(robotUnit->getRobotPlatformName(),
54  ControlModes::HolonomicPlatformVelocity)
56  ARMARX_CHECK_EXPRESSION(platformTarget)
57  << "The actuator " << robotUnit->getRobotPlatformName() << " has no control mode "
58  << ControlModes::HolonomicPlatformVelocity;
59 
60  const auto configData = ::armarx::fromAron<arondto::Config, Config>(cfg->config);
61  const auto trajectoryFollowingControllerParams = configData.params;
62 
64  configBuffer_updateConfigToAdditionalTask.reinitAllBuffers(configData);
65  configBuffer_updateConfigToOnPublish.reinitAllBuffers(configData);
66 
67  trajectoryFollowingController.emplace(trajectoryFollowingControllerParams);
68 
69  ARMARX_INFO << "Init done.";
70  }
71 
72  std::string
73  Controller::getClassName(const Ice::Current& iceCurrent) const
74  {
77  }
78 
79  void
80  Controller::rtRun(const IceUtil::Time& sensorValuesTimestamp,
81  const IceUtil::Time& timeSinceLastIteration)
82  {
84 
85  // update control devices
86  platformTarget->velocityX = rtGetControlStruct().linear.x();
87  platformTarget->velocityY = rtGetControlStruct().linear.y();
88  platformTarget->velocityRotation = rtGetControlStruct().angular;
89 
90  // read data (for non-rt)
91  robotStateBuffer_rtToAdditionalTask.getWriteBuffer().global_T_robot.matrix() =
92  rtGetRobot()->getGlobalPose();
93  robotStateBuffer_rtToAdditionalTask.commitWrite();
94  }
95 
96  void
98  const Ice::Current& iceCurrent)
99  {
100  ARMARX_IMPORTANT << "Controller::updateConfig";
101 
102  // TODO maybe update pid controller
103 
104  const auto updateConfig = ::armarx::fromAron<arondto::Config, Config>(dto);
105 
106  configBuffer_updateConfigToAdditionalTask.getWriteBuffer() = updateConfig;
107  configBuffer_updateConfigToAdditionalTask.commitWrite();
108 
109  configBuffer_updateConfigToOnPublish.getWriteBuffer() = updateConfig;
110  configBuffer_updateConfigToOnPublish.commitWrite();
111 
112  ARMARX_INFO << "Trajectory with " << updateConfig.targets.trajectory.points().size();
113 
114  ARMARX_IMPORTANT << "done Controller::updateConfig";
115  }
116 
117  void
119  {
120  ARMARX_CHECK(trajectoryFollowingController.has_value());
121 
122  const auto& configBuffer =
123  configBuffer_updateConfigToAdditionalTask.getUpToDateReadBuffer();
124 
125  // if trajectory is empty, set velocity to 0
126  if (configBuffer.targets.trajectory.points().empty())
127  {
128  ARMARX_INFO << deactivateSpam(1) << "Trajectory is empty!";
129 
130  filteredTwist.reset();
131 
132  getWriterControlStruct().reset();
134  return;
135  }
136 
137  // update controller
138  const auto result = trajectoryFollowingController->control(
139  configBuffer.targets.trajectory,
140  robotStateBuffer_rtToAdditionalTask.getUpToDateReadBuffer().global_T_robot);
141 
142  // low-pass filter
143  {
144  const float alpha = configBuffer.params.alpha;
145  filteredTwist.linear =
146  alpha * filteredTwist.linear + (1. - alpha) * result.twist.linear.head<2>();
147  filteredTwist.angular =
148  alpha * filteredTwist.angular + (1. - alpha) * result.twist.angular.z();
149  }
150 
151  // store result
152  getWriterControlStruct() = filteredTwist;
154 
155  // store results (onPublish)
156  targetBuffer_additionalTaskToOnPublish.getWriteBuffer() = filteredTwist;
157  targetBuffer_additionalTaskToOnPublish.commitWrite();
158  }
159 
160  void
162  const DebugDrawerInterfacePrx& debugDrawer,
163  const DebugObserverInterfacePrx& debugObservers)
164  {
165  StringVariantBaseMap datafields;
166 
167  const auto& targets = targetBuffer_additionalTaskToOnPublish.getUpToDateReadBuffer();
168 
169  datafields["vx"] = new Variant(targets.linear.x());
170  datafields["vy"] = new Variant(targets.linear.y());
171  datafields["vyaw"] = new Variant(targets.angular);
172  datafields["trajectory_points"] =
173  new Variant(configBuffer_updateConfigToOnPublish.getUpToDateReadBuffer()
174  .targets.trajectory.points()
175  .size());
176 
177  debugObservers->setDebugChannel(
179  datafields);
180  }
181 
182  void
184  {
185  runTask("PlatformTrajectoryControllerAdditionalTask",
186  [&]
187  {
188  CycleUtil c(10);
189  getObjectScheduler()->waitForObjectStateMinimum(eManagedIceObjectStarted);
190  ARMARX_IMPORTANT << "Create a new thread alone PlatformTrajectory controller";
191  while (getState() == eManagedIceObjectStarted)
192  {
193  if (isControllerActive() and rtReady.load())
194  {
195  ARMARX_VERBOSE << "additional task";
196  additionalTask();
197  }
198  c.waitForCycleDuration();
199  }
200  });
201 
202  ARMARX_INFO << "PlatformTrajectoryVelocityController::onInitNJointController";
203  }
204 
205  void
207  {
208  // additionalTask() is not executed. Thus, we can access lastTwist without mutex.
209  filteredTwist.reset();
210 
211  robotStateBuffer_rtToAdditionalTask.getWriteBuffer().global_T_robot.matrix() =
212  rtGetRobot()->getGlobalPose();
213  robotStateBuffer_rtToAdditionalTask.commitWrite();
214 
215  rtReady.store(true);
216  }
217 
218  void
220  {
221  rtReady.store(false);
222  }
223 
224  Controller::~Controller() = default;
225 
227  Controller::getConfig(const ::Ice::Current&)
228  {
229  ARMARX_ERROR << "NYI";
230  return nullptr;
231  }
232 } // 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:223
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::TripleBuffer::getWriteBuffer
T & getWriteBuffer()
Definition: TripleBuffer.h:92
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:110
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
NJointControllerBase.h
armarx::navigation::platform_controller::platform_local_trajectory::Controller::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: PlatformLocalTrajectoryController.cpp:219
RobotUnit.h
armarx::TripleBuffer::commitWrite
void commitWrite()
Definition: TripleBuffer.h:163
armarx::navigation::platform_controller::platform_local_trajectory::Twist2D::linear
Eigen::Vector2f linear
Definition: PlatformLocalTrajectoryController.h:73
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:769
PlatformLocalTrajectoryController.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::NJointControllerBase::ConfigPtrT
NJointControllerConfigPtr ConfigPtrT
Definition: NJointControllerBase.h:587
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
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:183
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::NJointControllerBase::rtGetRobot
const VirtualRobot::RobotPtr & rtGetRobot()
TODO make protected and use attorneys.
Definition: NJointControllerBase.h:846
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:182
armarx::NJointControllerBase::isControllerActive
bool isControllerActive(const Ice::Current &=Ice::emptyCurrent) const final override
Definition: NJointControllerBase.h:778
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:754
ControlModes.h
armarx::NJointControllerWithTripleBuffer< Target >::rtUpdateControlStruct
bool rtUpdateControlStruct()
Definition: NJointControllerWithTripleBuffer.h:38
armarx::navigation::platform_controller::platform_local_trajectory::Controller::additionalTask
void additionalTask()
Definition: PlatformLocalTrajectoryController.cpp:118
armarx::navigation::platform_controller::platform_local_trajectory::Controller::onPublish
void onPublish(const SensorAndControl &sac, const DebugDrawerInterfacePrx &debugDrawer, const DebugObserverInterfacePrx &debugObservers) override
Definition: PlatformLocalTrajectoryController.cpp:161
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:97
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:73
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
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:77
CycleUtil.h
ExpressionException.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:206
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:80
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:181
armarx::navigation::platform_controller::platform_local_trajectory::Controller::~Controller
~Controller() override
IceUtil::Handle< class RobotUnit >
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::navigation::platform_controller::platform_local_trajectory::Twist2D::angular
float angular
Definition: PlatformLocalTrajectoryController.h:74
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:99
Logging.h
armarx::navigation::platform_controller::platform_local_trajectory::Controller::getConfig
::armarx::aron::data::dto::DictPtr getConfig(const ::Ice::Current &=::Ice::emptyCurrent) override
Definition: PlatformLocalTrajectoryController.cpp:227
armarx::TripleBuffer::getUpToDateReadBuffer
const T & getUpToDateReadBuffer() const
Definition: TripleBuffer.h:116
Variant.h
armarx::TripleBuffer::reinitAllBuffers
std::enable_if< std::is_copy_constructible< U >::value >::type reinitAllBuffers(const T &init)
Definition: TripleBuffer.h:171
NJointControllerRegistry.h
armarx::ManagedIceObject::getObjectScheduler
ArmarXObjectSchedulerPtr getObjectScheduler() const
Definition: ManagedIceObject.cpp:776
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::navigation::platform_controller::platform_local_trajectory::Controller::Controller
Controller(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: PlatformLocalTrajectoryController.cpp:37
armarx::navigation::platform_controller::platform_local_trajectory
Definition: aron_conversions.cpp:53