-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRRRobotROS2Interface.cpp
More file actions
337 lines (298 loc) · 11.8 KB
/
Copy pathRRRobotROS2Interface.cpp
File metadata and controls
337 lines (298 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2020-2022 Rapyuta Robotics Co., Ltd.
#include "Robots/RRRobotROS2Interface.h"
// UE
#include "Net/UnrealNetwork.h"
// rclUE
#include "Msgs/ROS2JointState.h"
#include "Msgs/ROS2Twist.h"
#include "ROS2ActionClient.h"
#include "ROS2ActionServer.h"
#include "ROS2Publisher.h"
#include "ROS2ServiceClient.h"
#include "ROS2ServiceServer.h"
#include "ROS2Subscriber.h"
// RapyutaSimulationPlugins
#include "Core/RRConversionUtils.h"
#include "Core/RRGeneralUtils.h"
#include "Robots/RRBaseRobot.h"
void URRRobotROS2Interface::Initialize(AActor* Owner)
{
Robot = Cast<ARRBaseRobot>(Owner);
if (nullptr == Robot)
{
UE_LOG_WITH_INFO_NAMED(LogRapyutaCore, Warning, TEXT("Owner should be child class of RRBaseRobot."));
return;
}
Robot->ROS2Interface = this;
ROSSpawnParameters = Robot->ROSSpawnParameters;
Super::Initialize(Owner);
}
void URRRobotROS2Interface::InitInterfaces()
{
if (!Robot)
{
return;
}
// OdomPublisher (with TF)
if (bPublishOdom && Robot->bMobileRobot)
{
if (nullptr == OdomComponent)
{
OdomComponent =
URRUObjectUtils::CreateChildComponent<URRBaseOdomComponent>(Robot, *FString::Printf(TEXT("%sOdom"), *GetName()));
OdomComponent->bPublishOdomTf = bPublishOdomTf;
OdomComponent->PublicationFrequencyHz = OdomPublicationFrequencyHz;
OdomComponent->QoS = OdomQoS;
OdomComponent->RootOffset = Robot->RootOffset;
}
}
// Initialize Robot's sensors (lidar, etc.)
// NOTE: This inits both static sensors added by BP robot & possiblly also dynamic ones added in the overriding child InitSensors()
verify(Robot->InitSensors(RobotROS2Node));
Super::InitInterfaces();
}
void URRRobotROS2Interface::DeInitialize()
{
Super::DeInitialize();
if (nullptr != Robot)
{
Robot->ROS2Interface = nullptr;
Robot = nullptr;
}
}
void URRRobotROS2Interface::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(URRRobotROS2Interface, Robot);
DOREPLIFETIME(URRRobotROS2Interface, OdomComponent);
DOREPLIFETIME(URRRobotROS2Interface, bPublishOdom);
DOREPLIFETIME(URRRobotROS2Interface, bPublishOdomTf);
DOREPLIFETIME(URRRobotROS2Interface, OdomPublicationFrequencyHz);
DOREPLIFETIME(URRRobotROS2Interface, CmdVelTopicName);
DOREPLIFETIME(URRRobotROS2Interface, JointCmdTopicName);
DOREPLIFETIME(URRRobotROS2Interface, JointStateTopicName);
DOREPLIFETIME(URRRobotROS2Interface, bWarnAboutMissingLink);
}
void URRRobotROS2Interface::InitROS2NodeParam(AActor* Owner)
{
Super::InitROS2NodeParam(Owner);
if (!ROSSpawnParameters && Robot)
{
RobotROS2Node->Namespace = Robot->RobotUniqueName;
if (bUseActorNameAsNamespace)
{
#if WITH_EDITOR
RobotROS2Node->Namespace = UKismetSystemLibrary::GetDisplayName(Owner);
#else
RobotROS2Node->Namespace = Owner->GetName();
#endif
}
}
}
bool URRRobotROS2Interface::InitPublishers()
{
if (!Super::InitPublishers())
{
return false;
}
// JointState publisher
if (Robot && Robot->bJointControl)
{
ROS2_CREATE_LOOP_PUBLISHER_WITH_QOS(RobotROS2Node,
this,
JointStateTopicName,
UROS2Publisher::StaticClass(),
UROS2JointStateMsg::StaticClass(),
JointStatePublicationFrequencyHz,
&URRRobotROS2Interface::UpdateJointState,
UROS2QoS::Default,
JointStatePublisher);
}
if (Robot && bPublishJointTf)
{
JointsTFPublisher = CastChecked<URRROS2TFsPublisher>(RobotROS2Node->CreateLoopPublisherWithClass(
TEXT("tf"), URRROS2TFsPublisher::StaticClass(), JointTfPublicationFrequencyHz));
for (const auto& joint : Robot->Joints)
{
if (joint.Value == nullptr)
{
continue;
}
const FString* parentLinkName = Robot->Links.FindKey(joint.Value->ParentLink);
const FString* childLinkName = Robot->Links.FindKey(joint.Value->ChildLink);
if (!parentLinkName->IsEmpty() && !childLinkName->IsEmpty())
{
URRROS2JointTFComponent::AddJoint(joint.Value, *parentLinkName, *childLinkName, JointsTFPublisher);
}
}
}
return true;
}
bool URRRobotROS2Interface::InitSubscriptions()
{
if (!Super::InitSubscriptions())
{
return false;
}
if (Robot && Robot->bMobileRobot)
{
// Subscription with callback to enqueue vehicle spawn info.
RR_ROBOT_ROS2_SUBSCRIBE_TO_TOPIC(CmdVelTopicName, UROS2TwistMsg::StaticClass(), &URRRobotROS2Interface::MovementCallback);
}
if (Robot && Robot->bJointControl)
{
// Subscription with callback to enqueue vehicle spawn info.
RR_ROBOT_ROS2_SUBSCRIBE_TO_TOPIC(
JointCmdTopicName, UROS2JointStateMsg::StaticClass(), &URRRobotROS2Interface::JointCmdCallback);
}
return true;
}
void URRRobotROS2Interface::MovementCallback(const UROS2GenericMsg* Msg)
{
const UROS2TwistMsg* twistMsg = Cast<UROS2TwistMsg>(Msg);
if (IsValid(twistMsg))
{
// TODO refactoring will be needed to put units and system of reference conversions in a consistent location
// probably should not stay in msg though
FROSTwist twist;
twistMsg->GetMsg(twist);
const FVector linear = URRConversionUtils::VectorROSToUE(twist.Linear);
const FVector angular = URRConversionUtils::RotationROSToUEVector(twist.Angular, true);
// (Note) In this callback, which could be invoked from a ROS working thread,
// thus any direct referencing to its member in this GameThread lambda needs to be verified.
AsyncTask(ENamedThreads::GameThread,
[this, linear, angular]
{
if (IsValid(Robot))
{
Robot->SetLinearVel(linear);
Robot->SetAngularVel(angular);
}
});
}
}
void URRRobotROS2Interface::JointCmdCallback(const UROS2GenericMsg* Msg)
{
const UROS2JointStateMsg* jointStateMsg = Cast<UROS2JointStateMsg>(Msg);
if (IsValid(jointStateMsg))
{
// TODO refactoring will be needed to put units and system of reference conversions in a consistent location
// probably should not stay in msg though
FROSJointState jointState;
jointStateMsg->GetMsg(jointState);
// Check Joint type. should be different function?
ERRJointControlType jointControlType;
if (jointState.Name.Num() == jointState.Position.Num())
{
jointControlType = ERRJointControlType::POSITION;
}
else if (jointState.Name.Num() == jointState.Velocity.Num())
{
jointControlType = ERRJointControlType::VELOCITY;
}
else if (jointState.Name.Num() == jointState.Effort.Num())
{
jointControlType = ERRJointControlType::EFFORT;
UE_LOG_WITH_INFO_NAMED(LogRapyutaCore, Warning, TEXT("Effort control is not supported."));
return;
}
else
{
UE_LOG_WITH_INFO_NAMED(
LogRapyutaCore, Warning, TEXT("[position, velocity or effort array must be same size of name array"));
return;
}
// Calculate input, ROS to UE conversion.
TMap<FString, TArray<float>> joints;
for (auto i = 0; i < jointState.Name.Num(); ++i)
{
if (!Robot->Joints.Contains(jointState.Name[i]))
{
if (bWarnAboutMissingLink)
{
UE_LOG_WITH_INFO_NAMED(LogRapyutaCore, Warning, TEXT("robot do not have joint named %s."), *jointState.Name[i]);
}
continue;
}
TArray<float> input;
if (ERRJointControlType::POSITION == jointControlType)
{
input.Add(jointState.Position[i]);
}
else if (ERRJointControlType::VELOCITY == jointControlType)
{
input.Add(jointState.Velocity[i]);
}
else
{
UE_LOG_WITH_INFO(
LogRapyutaCore, Warning, TEXT("position, velocity or effort array must be same size of name array"));
continue;
}
// ROS To UE conversion
if (Robot->Joints[jointState.Name[i]]->LinearDOF == 1)
{
input[0] = URRConversionUtils::DistanceROSToUE(input[0]);
}
else if (Robot->Joints[jointState.Name[i]]->RotationalDOF == 1)
{
input[0] = FMath::RadiansToDegrees(input[0]);
}
else
{
UE_LOG_WITH_INFO(LogRapyutaCore,
Warning,
TEXT("[%s] Supports only single DOF joint. %s has %d "
"linear DOF and %d rotational DOF"),
*jointState.Name[i],
Robot->Joints[jointState.Name[i]]->LinearDOF,
Robot->Joints[jointState.Name[i]]->RotationalDOF);
}
joints.Emplace(jointState.Name[i], input);
}
// (Note) In this callback, which could be invoked from a ROS working thread,
// thus any direct referencing to its member in this GameThread lambda needs to be verified.
AsyncTask(ENamedThreads::GameThread,
[this, joints, jointControlType]
{
if (!IsValid(Robot))
{
UE_LOG_WITH_INFO_NAMED(
LogRapyutaCore, Warning, TEXT("Robot is nullptr. RobotROS2Interface::Robot must not be nullptr."));
return;
}
Robot->SetJointState(joints, jointControlType);
});
}
}
void URRRobotROS2Interface::UpdateJointState(UROS2GenericMsg* InMessage)
{
if (nullptr == Robot)
{
UE_LOG_WITH_INFO(LogRapyutaCore, Warning, TEXT("Robot is not set."));
return;
}
FROSJointState msg;
msg.Header.Stamp = URRConversionUtils::FloatToROSStamp(UGameplayStatics::GetTimeSeconds(Robot->GetWorld()));
for (const auto& joint : Robot->Joints)
{
if (nullptr == joint.Value)
{
continue;
}
msg.Name.Emplace(joint.Key);
// UE to ROS conversion
if (joint.Value->LinearDOF == 1)
{
msg.Position.Emplace(URRConversionUtils::DistanceUEToROS(joint.Value->Position[0]));
msg.Velocity.Emplace(URRConversionUtils::DistanceUEToROS(joint.Value->LinearVelocity[0]));
}
else if (joint.Value->RotationalDOF == 1)
{
msg.Position.Emplace(FMath::DegreesToRadians(joint.Value->Orientation.Euler()[0]));
msg.Velocity.Emplace(FMath::DegreesToRadians(joint.Value->AngularVelocity[0]));
}
msg.Effort.Emplace(0); //effort is not supported yet.
}
CastChecked<UROS2JointStateMsg>(InMessage)->SetMsg(msg);
}