RapyutaSimulationPlugins
RRBaseRobot.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 
16 
17 // UE
18 
19 #include "Components/StaticMeshComponent.h"
20 
21 #include "Components/WidgetComponent.h"
22 
23 #include "CoreMinimal.h"
24 
25 
26 
27 // rclUE
28 
29 #include "ROS2NodeComponent.h"
30 
31 
32 
33 // RapyutaSimulationPlugins
34 
35 #include "Core/RRBaseActor.h"
36 
37 #include "Core/RRObjectCommon.h"
38 
40 
42 
44 
45 #include "Tools/ROS2Spawnable.h"
46 
48 
49 
50 
51 #include "RRBaseRobot.generated.h"
52 
53 
54 
55 #define RR_VERIFY_DYNAMIC_ROBOT(InRobot) \
56 
57  if (InRobot && (false == InRobot->IsDynamicRuntimeRobot())) \
58 
59  { \
60 
61  UE_LOG_WITH_INFO(LogTemp, \
62 
63  Error, \
64 
65  TEXT("[ClassName:%s] is required to be NOT prefixed with [BP_] or [SKEL_BP] if this is purely a" \
66 
67  "cpp-based robot class!"), \
68 
69  *InRobot->GetClass()->GetName()); \
70 
71  }
72 
73 
74 
75 #define RR_VERIFY_STATIC_BP_ROBOT(InRobot) \
76 
77  if (InRobot && (false == InRobot->IsStaticBPRobot())) \
78 
79  { \
80 
81  UE_LOG_WITH_INFO(LogTemp, \
82 
83  Error, \
84 
85  TEXT("[ClassName:%s] is required to be prefixed with [BP_] if this is a blueprint robot class!"), \
86 
87  *InRobot->GetClass()->GetName()); \
88 
89  }
90 
91 
92 
93 #define RR_VERIFY_DYNAMIC_OR_STATIC_BP_ROBOT(InRobot) \
94 
95  { \
96 
97  }
98 
99 
100 
101 class ARRNetworkGameState;
102 
104 
106 
107 class URRUserWidget;
108 
109 
110 
124 enum class ERRNetworkAuthorityType : uint8
125 
126 {
127 
128  SERVER ,
129 
130  CLIENT ,
131 
132 };
133 
134 
135 
136 DECLARE_MULTICAST_DELEGATE_OneParam(FOnRobotCreationDone, bool /* bCreationResult */);
137 
138 
139 
161 class RAPYUTASIMULATIONPLUGINS_API ARRBaseRobot : public ARRBaseActor
162 
163 {
164 
165 
166 
167 public:
168 
177  ARRBaseRobot();
178 
179 
180 
191  ARRBaseRobot(const FObjectInitializer& ObjectInitializer);
192 
193 
194 
201  virtual void BeginPlay() override;
202 
203 
204 
215  virtual void Tick(float DeltaSeconds) override;
216 
217 
218 
227  void SetupDefault();
228 
229 
230 
231 
236  TObjectPtr<USceneComponent> DefaultRoot = nullptr;
237 
238 
239 
261  virtual void SetRootOffset(const FTransform& InRootOffset);
262 
263 
264 
273  FORCEINLINE bool IsDynamicRuntimeRobot() const
274 
275  {
276 
277  return (false == IsStaticBPRobot());
278 
279  }
280 
281 
282 
293  FORCEINLINE bool IsStaticBPRobot() const
294 
295  {
296 
297  // NOTE: (GetClass()->ClassGeneratedBy != nullptr) is also useful but WITH_EDITORONLY_DATA
298 
299  const FString className = GetClass()->GetName();
300 
301  return className.StartsWith(TEXT("BP")) // In-Editor
302 
303  || className.StartsWith(TEXT("SKEL_BP")); // In-Package auto prefixed [SKEL_]
304 
305  }
306 
307 
308 
319  FString GetDynamicResourceName(const ERRResourceDataType InDataType) const;
320 
321 
322 
333  FString GetDynamicResourceAssetPath(const ERRResourceDataType InDataType) const;
334 
335 
336 
338 
339  FOnRobotCreationDone OnRobotCreationDone;
340 
341 
342 
344 
345 
350  TSubclassOf<URRRobotROS2Interface> ROS2InterfaceClass = nullptr;
351 
352 
353 
367  TObjectPtr<URRRobotROS2Interface> ROS2Interface = nullptr;
368 
369 
370 
382  virtual void OnRep_ROS2Interface();
383 
384 
385 
397  bool bStartStopROS2Interface = false;
398 
399 
400 
412  virtual void OnRep_bStartStopROS2Interface();
413 
414 
415 
426  bool IsAuthorizedInThisClient();
427 
428 
429 
431 
433 
435 
436 
441  TObjectPtr<UROS2Spawnable> ROSSpawnParameters = nullptr;
442 
443 
444 
458  TObjectPtr<ARRBaseRobot> ServerRobot = nullptr;
459 
460 
461 
481  void CreateROS2Interface();
482 
483 
484 
502  void InitROS2Interface();
503 
504 
505 
506 
511  bool InitROS2InterfaceImpl();
512 
513 
514 
515 
520  FTimerHandle ROS2InitTimer;
521 
522 
523 
539  void DeInitROS2Interface();
540 
541 
542 
557 
558 
559 
566  void SetRobotName(const FString& InRobotName)
567 
568  {
569 
570  RobotUniqueName = InRobotName;
571 
572  EntityUniqueName = InRobotName;
573 
574  }
575 
576 
577 
579 
581 
583 
584 
589  FString RobotModelName;
590 
591 
592 
593 
598  virtual void SetRobotModelName(const FString InName)
599 
600  {
601 
602  RobotModelName = InName;
603 
604  EntityModelName = InName;
605 
606  if (ActorInfo.IsValid())
607 
608  {
609 
610  ActorInfo->EntityModelName = InName;
611 
612  }
613 
614  }
615 
616 
617 
618 
623  virtual FString GetRobotModelName()
624 
625  {
626 
627  return RobotModelName;
628 
629  }
630 
631 
632 
639  FORCEINLINE bool IsBuiltInRobotModel() const
640 
641  {
642 
643  return RobotModelName.StartsWith(TEXT("UE"), ESearchCase::IgnoreCase);
644 
645  }
646 
647 
648 
650 
651 
656  uint64 RobotID = 0;
657 
658 
659 
666  uint64 GetRobotID() const
667 
668  {
669 
670  return RobotID;
671 
672  }
673 
674 
675 
682  void SetRobotID(uint64 InRobotID)
683 
684  {
685 
686  RobotID = InRobotID;
687 
688  }
689 
690 
691 
705  TMap<FString, UStaticMeshComponent*> Links;
706 
707 
708 
732  virtual bool AddLink(const FString& InLinkName, UStaticMeshComponent* InMesh);
733 
734 
735 
737 
738 
743  TObjectPtr<UMeshComponent> BaseMeshComp = nullptr;
744 
745 
746 
764  void SetBaseMeshComp(UMeshComponent* InBaseMeshComp, bool bInMakeAsRoot = true, bool bInDestroyDefaultRoot = true);
765 
766 
767 
781  TMap<FString, URRJointComponent*> Joints;
782 
783 
784 
814  virtual bool AddJoint(const FString& InParentLinkName,
815 
816  const FString& InChildLinkName,
817 
818  const FString& InJointName,
819 
820  URRJointComponent* InJoint);
821 
822 
823 
835  bool bInitializeJoints = true;
836 
837 
838 
839 
844  virtual void StartJointsInitialization();
845 
846 
847 
848 
853  virtual void CheckJointsInitialization();
854 
855 
856 
874  virtual void SetChildComponentsCollisionEnabled(const bool IsEnable);
875 
876 
877 
898  virtual bool InitSensors(UROS2NodeComponent* InROS2Node);
899 
900 
901 
914  virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
915 
916 
917 
926  virtual bool ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags) override;
927 
928 
929 
938  // UFUNCTION(BlueprintCallable)
939 
940  virtual void SetJointState(const TMap<FString, TArray<float>>& InJointState, const ERRJointControlType InJointControlType);
941 
942 
943 
958 
959 
960 
961  // MOVEMENT --
962 
963  //
964 
966 
967 
972  FVector TargetLinearVel = FVector::ZeroVector;
973 
974 
975 
977 
978 
983  FVector TargetAngularVel = FVector::ZeroVector;
984 
985 
986 
998  virtual void StopMovement();
999 
1000 
1001 
1003 
1005 
1006 
1011  TObjectPtr<UMovementComponent> MovementComponent = nullptr;
1012 
1013 
1014 
1016 
1018 
1020 
1021  // UPROPERTY(EditAnywhere, BlueprintReadWrite, Instanced, Replicated)
1022 
1023 
1028  TObjectPtr<URobotVehicleMovementComponent> RobotVehicleMoveComponent;
1029 
1030 
1031 
1033 
1035 
1036 
1041  TSubclassOf<UMovementComponent> VehicleMoveComponentClass;
1042 
1043 
1044 
1045 
1050  virtual void SetMoveComponent(UMovementComponent* InMoveComponent);
1051 
1052 
1053 
1073  virtual void SetLinearVel(const FVector& InLinearVel);
1074 
1075 
1076 
1094  virtual void SetAngularVel(const FVector& InAngularVel);
1095 
1096 
1097 
1119  virtual void SyncServerLinearMovement(float InClientTimeStamp,
1120 
1121  const FTransform& InClientRobotTransform,
1122 
1123  const FVector& InLinearVel);
1124 
1125 
1126 
1148  virtual void SyncServerAngularMovement(float InClientTimeStamp,
1149 
1150  const FRotator& InClientRobotRotation,
1151 
1152  const FVector& InAngularVel);
1153 
1154 
1155 
1171  virtual void SetLocalLinearVel(const FVector& InLinearVel);
1172 
1173 
1174 
1190  virtual void SetLocalAngularVel(const FVector& InAngularVel);
1191 
1192 
1193 
1195 
1196 
1201  FTransform RootOffset = FTransform::Identity;
1202 
1203 
1204 
1206 
1207 
1212  bool bInitRobotVehicleMoveComponent = true;
1213 
1214 
1215 
1231  void BPConfigureMovementComponent();
1232 
1233 
1234 
1236 
1237 
1242  bool bMobileRobot = true;
1243 
1244 
1245 
1247 
1248 
1253  bool bJointControl = true;
1254 
1255 
1256 
1257  // UI WIDGET --
1258 
1260 
1261 
1266  uint8 bUIWidgetEnabled : 1;
1267 
1268 
1269 
1271 
1272 
1277  TObjectPtr<URRUIWidgetComponent> UIWidgetComp = nullptr;
1278 
1279 
1280 
1282 
1283 
1288  FTransform UIWidgetOffset = FTransform(FVector(0.f, 0.f, 100.f));
1289 
1290 
1291 
1293 
1294 
1299  TSubclassOf<UUserWidget> UIUserWidgetClass;
1300 
1301 
1302 
1309  bool CheckUIUserWidget() const;
1310 
1311 
1312 
1313 protected:
1314 
1321  virtual void PreInitializeComponents() override;
1322 
1323 
1324 
1335  virtual void PostInitializeComponents() override;
1336 
1337 
1338 
1353  virtual bool InitMoveComponent();
1354 
1355 
1356 
1372  void BPPreInitializeComponents();
1373 
1374 
1375 
1391  void BPPostInitializeComponents();
1392 
1393 
1394 
1395 public:
1396 
1526  virtual bool InitPropertiesFromJSON();
1527 
1528 
1529 
1545  void BPInitPropertiesFromJSON();
1546 
1547 
1548 
1557  virtual void ConfigureMovementComponent();
1558 
1559 
1560 
1567  virtual void InitUIWidget();
1568 
1569 
1570 
1582  bool bInitializingJoints = false;
1583 
1584 
1585 
1603  TMap<UPrimitiveComponent*, FName> OriginalCollisionProfiles;
1604 
1605 
1606 
1607 
1613 
1614  {
1615 
1616  bool res = InitPropertiesFromJSON();
1617 
1618  BPInitPropertiesFromJSON();
1619 
1620  return res;
1621 
1622  };
1623 
1624 };
1625 
DECLARE_MULTICAST_DELEGATE_OneParam
DECLARE_MULTICAST_DELEGATE_OneParam(FOnRobotCreationDone, bool)
ARRBaseRobot::Links
TMap< FString, UStaticMeshComponent * > Links
Definition: RRBaseRobot.h:705
ARRBaseRobot::RobotVehicleMoveComponent
TObjectPtr< URobotVehicleMovementComponent > RobotVehicleMoveComponent
Movecomponent casted to URobotVehicleMovementComponent for utility.
Definition: RRBaseRobot.h:1028
ERRJointControlType
ERRJointControlType
Definition: RRJointComponent.h:50
ARRBaseRobot::IsStaticBPRobot
FORCEINLINE bool IsStaticBPRobot() const
Static BP robot: implemented in BP, possibly inheriting from #ARBaseRobot or its children classes,...
Definition: RRBaseRobot.h:293
ARRBaseRobot::RobotModelName
FString RobotModelName
Robot Model Name (loaded from URDF/SDF)
Definition: RRBaseRobot.h:589
ARRBaseRobot::GetRobotID
uint64 GetRobotID() const
Get robot ID.
Definition: RRBaseRobot.h:666
ARRBaseRobot::SetRobotName
void SetRobotName(const FString &InRobotName)
Set robot unique name.
Definition: RRBaseRobot.h:566
ARRBaseRobot::GetRobotModelName
virtual FString GetRobotModelName()
Definition: RRBaseRobot.h:623
ARRBaseRobot::SetRobotModelName
virtual void SetRobotModelName(const FString InName)
Definition: RRBaseRobot.h:598
ARRBaseRobot::VehicleMoveComponentClass
TSubclassOf< UMovementComponent > VehicleMoveComponentClass
Class of the main robot movement component, configurable in child class.
Definition: RRBaseRobot.h:1041
ARRBaseRobot::InitPropertiesFromJSONAll
virtual bool InitPropertiesFromJSONAll()
Definition: RRBaseRobot.h:1612
ARRBaseRobot::UIUserWidgetClass
TSubclassOf< UUserWidget > UIUserWidgetClass
Widget class.
Definition: RRBaseRobot.h:1299
RRJointComponent.h
ARRBaseActor
Base actor class for all Rapyuta Sim actors:
Definition: RRBaseActor.h:70
ROS2Spawnable.h
BaseComponents which is used when spawning Actor from ROS 2 service in ASimulationState.
URRRobotROS2Interface
Base Robot ROS 2 interface class.
Definition: RRRobotROS2Interface.h:80
URRUserWidget
Base user widget, inheriting from UUserWidget
Definition: RRUserWidget.h:52
ARRNetworkPlayerController
Network Player controller provides functionality for client-server. Major functionalites are.
Definition: RRNetworkPlayerController.h:66
ARRBaseRobot::ROS2InitTimer
FTimerHandle ROS2InitTimer
Definition: RRBaseRobot.h:520
URRJointComponent
Base Joints class. Other joint class should inherit from this class.
Definition: RRJointComponent.h:81
ARRBaseRobot::OriginalCollisionProfiles
TMap< UPrimitiveComponent *, FName > OriginalCollisionProfiles
Children UPrimitives components orignal collision profiles.
Definition: RRBaseRobot.h:1603
ARRBaseRobot::Joints
TMap< FString, URRJointComponent * > Joints
Definition: RRBaseRobot.h:781
ARRBaseActor::GetLifetimeReplicatedProps
virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > &OutLifetimeProps) const override
Returns the properties used for network replication, this needs to be overridden by all actor classes...
ERRNetworkAuthorityType::CLIENT
@ CLIENT
UMETA(DisplayName = "Client", ToolTip = "robot moves in client first and use rpc to apply movement to...
RRUIWidgetComponent.h
RRROS2BaseSensorComponent.h
Base ROS 2 Sensor Component class. Other sensors class should inherit from this class.
ARRNetworkGameState
Network Game State for client-server.
Definition: RRNetworkGameState.h:40
ERRNetworkAuthorityType
ERRNetworkAuthorityType
Which server or client has robot movement authority.
Definition: RRBaseRobot.h:124
RobotVehicleMovementComponent.h
Base Robot vehicle movement class which is used as part of ARobotVehicle.
ARRBaseRobot::IsBuiltInRobotModel
FORCEINLINE bool IsBuiltInRobotModel() const
Is static built robot model by the Editor.
Definition: RRBaseRobot.h:639
ERRResourceDataType
ERRResourceDataType
Sim resource(Uassets) data types.
Definition: RRObjectCommon.h:174
ARRBaseActor::PreInitializeComponents
virtual void PreInitializeComponents() override
Set #GameMode #GameState #GameSingleton #PlayerController.
ARRBaseRobot::SetRobotID
void SetRobotID(uint64 InRobotID)
Set robot ID.
Definition: RRBaseRobot.h:682
RRObjectCommon.h
Common objects.
ARRBaseRobot::IsDynamicRuntimeRobot
FORCEINLINE bool IsDynamicRuntimeRobot() const
DynamicRuntime robot: Implemented purely in cpp, built & loaded up at runtime from raw CAD + metadata...
Definition: RRBaseRobot.h:273
ARRBaseRobot::OnRobotCreationDone
FOnRobotCreationDone OnRobotCreationDone
Robot creation done delegate.
Definition: RRBaseRobot.h:339
ERRNetworkAuthorityType::SERVER
@ SERVER
UMETA(DisplayName = "Server", ToolTip = "robot moves in server first and movement replicates to clien...
RRBaseActor.h
Base actor class for all Rapyuta Sim actors.
ARRBaseRobot::bUIWidgetEnabled
uint8 bUIWidgetEnabled
Enable widget or not.
Definition: RRBaseRobot.h:1266
ARRBaseRobot::RobotUniqueName
FString RobotUniqueName
Definition: RRBaseRobot.h:556
ARRBaseRobot
Base Robot class. Other robot class should inherit from this class.
Definition: RRBaseRobot.h:161