Skip to content

Commit 8209317

Browse files
committed
Added option to publish joint state from LUA
Signed-off-by: Michał Pełka <[email protected]>
1 parent d2b1681 commit 8209317

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

Gems/ROS2ScriptIntegration/Code/Include/ROS2ScriptIntegration/ROS2ScriptPublisherBus.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ namespace ROS2ScriptIntegration
4040
const AZStd::string& topicName, const AZStd::string& frame, const AZ::Transform& transform) = 0;
4141
virtual void PublishAckermannDriveMsg(
4242
const AZStd::string& topicName, float steeringAngle, float steeringVelocity, float speed, float acceleration, float jerk) = 0;
43-
43+
virtual void PublishJointStateMsg(
44+
const AZStd::string& topicName,
45+
const AZStd::vector<AZStd::string>& names,
46+
const AZStd::vector<float>& positions,
47+
const AZStd::vector<float>& velocities,
48+
const AZStd::vector<float>& efforts) = 0;
4449
static void Reflect(AZ::ReflectContext* context);
4550
};
4651

Gems/ROS2ScriptIntegration/Code/Source/Clients/PublisherSystemComponent.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <AzCore/RTTI/BehaviorContext.h>
99
#include <AzCore/Serialization/SerializeContext.h>
1010

11+
#include <sensor_msgs/msg/joint_state.hpp>
1112
#include <ackermann_msgs/msg/ackermann_drive.hpp>
1213
#include <geometry_msgs/msg/point32.hpp>
1314
#include <geometry_msgs/msg/pose_stamped.hpp>
@@ -211,6 +212,27 @@ namespace ROS2ScriptIntegration
211212
PublishMessage(topicName, message);
212213
}
213214

215+
void PublisherSystemComponent::PublishJointStateMsg(
216+
const AZStd::string& topicName,
217+
const AZStd::vector<AZStd::string>& names,
218+
const AZStd::vector<float>& positions,
219+
const AZStd::vector<float>& velocities,
220+
const AZStd::vector<float>& efforts)
221+
{
222+
sensor_msgs::msg::JointState message;
223+
message.header.stamp = ROS2::ROS2ClockInterface::Get()->GetROSTimestamp();
224+
message.name.resize(names.size());
225+
for (size_t i = 0; i < names.size(); ++i)
226+
{
227+
message.name[i] = std::string(names[i].c_str());
228+
}
229+
message.position = std::vector<double>(positions.begin(), positions.end());
230+
message.velocity = std::vector<double>(velocities.begin(), velocities.end());
231+
message.effort = std::vector<double>(efforts.begin(), efforts.end());
232+
PublishMessage(topicName, message);
233+
}
234+
235+
214236
template<typename MessageType>
215237
std::shared_ptr<rclcpp::Publisher<MessageType>> PublisherSystemComponent::GetOrCreatePublisher(const AZStd::string& topicName)
216238
{

Gems/ROS2ScriptIntegration/Code/Source/Clients/PublisherSystemComponent.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ namespace ROS2ScriptIntegration
6666
const AZStd::string& topicName, float steeringAngle, float steeringVelocity, float speed, float acceleration, float jerk)
6767
override;
6868

69+
void PublishJointStateMsg(
70+
const AZStd::string& topicName,
71+
const AZStd::vector<AZStd::string>& names,
72+
const AZStd::vector<float>& positions,
73+
const AZStd::vector<float>& velocities,
74+
const AZStd::vector<float>& efforts) override;
75+
6976
// AZ::Component overrides ...
7077
void Init() override;
7178

Gems/ROS2ScriptIntegration/Code/Source/Clients/ROS2ScriptPublisherBus.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ namespace ROS2ScriptIntegration
5353
{ "SteeringVelocity", "" },
5454
{ "Speed", "" },
5555
{ "Acceleration", "" },
56-
{ "Jerk", "" } } });
56+
{ "Jerk", "" } } })
57+
->Event (
58+
"PublishJointStateMsg",
59+
&PublisherRequestBus::Events::PublishJointStateMsg);
5760
}
5861
}
5962
} // namespace ROS2ScriptIntegration

Gems/ROS2ScriptIntegration/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Currently, the supported message types are limited to the following:
99
- a subset of geometry messages
1010
- a subset of standard messages
1111
- joystick message
12+
- joint state message
1213

1314
## Hello world publisher
1415

@@ -80,3 +81,47 @@ function myscript:OnStdMsgString(message)
8081
Debug.Log("I heard : " .. message)
8182
end
8283
```
84+
85+
### Using messages with arrays (e.g. sensor_msgs/JointState)
86+
87+
- you need to declare `vector_*` types to create C++ array equivalents
88+
- you need to use `push_back` method to add elements to the array
89+
- those arrays can be passed to the publisher method
90+
Example:
91+
92+
```lua
93+
local LogJoints =
94+
{
95+
Properties = {}
96+
}
97+
98+
function LogJoints:OnActivate()
99+
self.tickBusHandler = TickBus.Connect(self)
100+
end
101+
102+
function LogJoints:OnDeactivate()
103+
self.tickBusHandler:Disconnect()
104+
end
105+
106+
function LogJoints:OnTick(deltaTime, timePoint)
107+
names = vector_basic_string_char_char_traits_char()
108+
names:push_back("egoback_left_wheel_joint")
109+
names:push_back("egoback_right_wheel_joint")
110+
111+
joints = vector_float()
112+
joints:push_back(0)
113+
joints:push_back(0)
114+
115+
velocities = vector_float()
116+
velocities:push_back(0)
117+
velocities:push_back(0)
118+
119+
efforts = vector_float()
120+
efforts:push_back(0)
121+
efforts:push_back(0)
122+
123+
PublisherRequestBus.Broadcast.PublishJointStateMsg("joint_states",names,joints,velocities,efforts)
124+
end
125+
126+
return LogJoints
127+
```

0 commit comments

Comments
 (0)