RapyutaSimulationPlugins
RRRobotROS2Interface.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 
16 
17 // UE
18 
19 #include "CoreMinimal.h"
20 
21 
22 
23 // rclUE
24 
25 #include "ROS2NodeComponent.h"
26 
27 #include "ROS2ServiceClient.h"
28 
29 #include "Tools/ROS2Spawnable.h"
30 
32 
34 
35 
36 
37 // RapyutaSimulationPlugins
38 
39 #include "Core/RRUObjectUtils.h"
40 
42 
44 
45 
46 
47 #include "RRRobotROS2Interface.generated.h"
48 
49 
50 
51 class ARRBaseRobot;
52 
80 class RAPYUTASIMULATIONPLUGINS_API URRRobotROS2Interface : public URRBaseROS2Interface
81 
82 {
83 
84 
85 
86 public:
87 
89 
90 
95  TObjectPtr<ARRBaseRobot> Robot = nullptr;
96 
97 
98 
111  virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
112 
113 
114 
125  virtual void Initialize(AActor* Owner) override;
126 
127 
128 
137  virtual void InitInterfaces() override;
138 
139 
140 
151  virtual void DeInitialize() override;
152 
153 
154 
165  virtual void InitROS2NodeParam(AActor* Owner) override;
166 
167 
168 
170 
171  //Mobile
172 
174 
175 
176 
178 
179 
184  TObjectPtr<URRBaseOdomComponent> OdomComponent = nullptr;
185 
186 
187 
188 
193  bool bPublishOdom = true;
194 
195 
196 
197 
202  bool bPublishOdomTf = false;
203 
204 
205 
206 
211  float OdomPublicationFrequencyHz = 30;
212 
213 
214 
216 
217 
222  FString CmdVelTopicName = TEXT("cmd_vel");
223 
224 
225 
227 
228  //Joint
229 
231 
232 
233 
251  virtual void JointCmdCallback(const UROS2GenericMsg* Msg);
252 
253 
254 
256 
257 
262  FString JointCmdTopicName = TEXT("ue_joint_commands");
263 
264 
265 
281  void UpdateJointState(UROS2GenericMsg* InMessage);
282 
283 
284 
286 
287 
292  TObjectPtr<UROS2Publisher> JointStatePublisher = nullptr;
293 
294 
295 
297 
298 
303  float JointStatePublicationFrequencyHz = 30.f;
304 
305 
306 
308 
309 
314  FString JointStateTopicName = TEXT("ue_joint_states");
315 
316 
317 
318 
323  bool bWarnAboutMissingLink = true;
324 
325 
326 
327 
332  TObjectPtr<URRROS2TFsPublisher> JointsTFPublisher = nullptr;
333 
334 
335 
336 
341  bool bPublishJointTf = false;
342 
343 
344 
345 
350  float JointTfPublicationFrequencyHz = 1.f;
351 
352 
353 
355 
356 
361  TObjectPtr<URRROS2OdomPublisher> OdomPublisher = nullptr;
362 
363 
364 
373  virtual bool InitPublishers() override;
374 
375 
376 
385  virtual bool InitSubscriptions() override;
386 
387 
388 
402  virtual void MovementCallback(const UROS2GenericMsg* Msg);
403 
404 
405 
406 protected:
407 
408  template<typename TROS2Message,
409 
410  typename TROS2MessageData,
411 
412  typename TRobot,
413 
414  typename TRobotMemFuncType = void (TRobot::*)(const TROS2MessageData&)>
415 
416  FORCEINLINE void OnMessageReceived(const TROS2Message* InMsg, const TRobotMemFuncType& InMemFunc)
417 
418  {
419 
420  const auto* msg = Cast<TROS2Message>(InMsg);
421 
422  if (IsValid(msg))
423 
424  {
425 
426  TROS2MessageData msgData;
427 
428  msg->GetMsg(msgData);
429 
430 
431 
432  // (Note) In this callback, which could be invoked from a ROS working thread,
433 
434  // thus any direct referencing to its member in this GameThread lambda needs to be verified.
435 
436  AsyncTask(ENamedThreads::GameThread,
437 
438  [this, InMemFunc, msgData]
439 
440  {
441 
442  auto* robot = Cast<TRobot>(Robot);
443 
444  if (IsValid(Cast<UObject>(robot)))
445 
446  {
447 
448  ::Invoke(InMemFunc, robot, msgData);
449 
450  }
451 
452  });
453 
454  }
455 
456  }
457 
458 
459 
460  template<typename TROS2Message, typename TROS2MessageData, typename TRobot>
461 
462  FORCEINLINE void OnMessageReceivedFunc(const TROS2Message* InMsg, const TFunction<void(const TROS2MessageData&)>& InFunc)
463 
464  {
465 
466  if (IsValid(InMsg))
467 
468  {
469 
470  TROS2MessageData msgData;
471 
472  InMsg->GetMsg(msgData);
473 
474 
475 
476  // (Note) In this callback, which could be invoked from a ROS working thread,
477 
478  // thus any direct referencing to its member in this GameThread lambda needs to be verified.
479 
480  AsyncTask(ENamedThreads::GameThread,
481 
482  [this, InFunc, msgData]
483 
484  {
485 
486  auto* robot = Cast<TRobot>(Robot);
487 
488  if (IsValid(Cast<UObject>(robot)))
489 
490  {
491 
492  InFunc(msgData);
493 
494  }
495 
496  });
497 
498  }
499 
500  }
501 
502 
503 
504  template<typename TService, typename TServiceRequest>
505 
506  void MakeServiceRequest(const FString& InServiceName, const TServiceRequest& InRequest)
507 
508  {
509 
510  // Create and update request
511 
512  if (auto* client = ServiceClients.FindRef(InServiceName))
513 
514  {
515 
516  TService* service = CastChecked<TService>(client->Service);
517 
518  client->SendRequest(service, InRequest);
519 
520 
521 
522  UE_LOG_WITH_INFO(LogTemp, Warning, TEXT("%s [%s] Request made"), *InServiceName, *GetName());
523 
524  }
525 
526  else
527 
528  {
529 
530  UE_LOG_WITH_INFO(LogTemp, Error, TEXT("[MakeServiceRequest] [%s] srv client not found"), *InServiceName);
531 
532  }
533 
534  }
535 
536 };
537 
538 
539 
555 class RAPYUTASIMULATIONPLUGINS_API URRRobotROS2InterfaceComponent : public URRBaseROS2InterfaceComponent
556 
557 {
558 
559 public:
560 
562 
563  {
564 
565  ROS2InterfaceClass = URRRobotROS2Interface::StaticClass();
566 
567  };
568 
569 };
570 
URRBaseROS2Interface::DeInitialize
virtual void DeInitialize()
DeInitialize robot's ROS 2 interface by stopping publisher.
URRBaseROS2InterfaceComponent
Wrapper class of URRBaseROS2Interfaceas component.
Definition: RRBaseROS2Interface.h:514
URRBaseROS2Interface
Base Robot ROS 2 interface class.
Definition: RRBaseROS2Interface.h:80
URRBaseROS2Interface::InitInterfaces
virtual void InitInterfaces()
Init publishers, subscribers, service clients, service servers, action clients, and action servers.
RRROS2OdomPublisher.h
Odometry Topic and TF publisher of ARRBaseRobot.
ROS2Spawnable.h
BaseComponents which is used when spawning Actor from ROS 2 service in ASimulationState.
URRBaseROS2Interface::InitSubscriptions
virtual bool InitSubscriptions()
Initialize subscriptions for cmd_vel & joint_states topics.
RRUObjectUtils.h
UObject general utils.
RRBaseROS2Interface.h
Base Robot ROS2Interface class. Other robot ROS2Interface class should inherit from this class.
URRRobotROS2Interface
Base Robot ROS 2 interface class.
Definition: RRRobotROS2Interface.h:80
URRBaseROS2Interface::InitPublishers
virtual bool InitPublishers()
Initialize non sensor basic publishers such as odometry.
URRBaseROS2Interface::InitROS2NodeParam
virtual void InitROS2NodeParam(AActor *Owner)
Spawn ROS2 Node, and initialize it if ROS2 Node = nullptr.
URRRobotROS2Interface::MakeServiceRequest
void MakeServiceRequest(const FString &InServiceName, const TServiceRequest &InRequest)
Definition: RRRobotROS2Interface.h:506
URRRobotROS2InterfaceComponent::URRRobotROS2InterfaceComponent
URRRobotROS2InterfaceComponent()
Definition: RRRobotROS2Interface.h:561
URRBaseROS2Interface::GetLifetimeReplicatedProps
void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > &OutLifetimeProps) const override
Returns the properties used for network replication, this needs to be overridden by all actor classes...
URRRobotROS2InterfaceComponent
Wrapper class of URRRobotROS2Interfaceas component.
Definition: RRRobotROS2Interface.h:555
RRBaseOdomComponent.h
Base Odom Component which provides actor pose changes.
URRRobotROS2Interface::OnMessageReceivedFunc
FORCEINLINE void OnMessageReceivedFunc(const TROS2Message *InMsg, const TFunction< void(const TROS2MessageData &)> &InFunc)
Definition: RRRobotROS2Interface.h:462
URRBaseROS2Interface::Initialize
virtual void Initialize(AActor *Owner)
Initialize robot's ROS 2 interface by calling InitRobotROS2Node, InitPublishers, InitSubscriptions an...
URRRobotROS2Interface::OnMessageReceived
FORCEINLINE void OnMessageReceived(const TROS2Message *InMsg, const TRobotMemFuncType &InMemFunc)
Definition: RRRobotROS2Interface.h:416
RRROS2TFPublisher.h
TF publisher class.
ARRBaseRobot
Base Robot class. Other robot class should inherit from this class.
Definition: RRBaseRobot.h:161