Skip to content

Commit 9105f30

Browse files
michalpelkajhanca-robotecai
authored andcommitted
Cherry pick from: Mp/publish joint state (#152)
* Added option to publish joint state from LUA Signed-off-by: Michał Pełka <michal.pelka@robotec.ai> * Make clang format happy Signed-off-by: Michał Pełka <michal.pelka@robotec.ai> * Code review Signed-off-by: Jan Hanca <jan.hanca@robotec.ai> --------- Signed-off-by: Michał Pełka <michal.pelka@robotec.ai> Signed-off-by: Jan Hanca <jan.hanca@robotec.ai> Co-authored-by: Jan Hanca <jan.hanca@robotec.ai> (cherry picked from commit e850be3) Signed-off-by: Piotr Jaroszek <piotr.jaroszek@robotec.ai>
1 parent 741cd27 commit 9105f30

6 files changed

Lines changed: 89 additions & 3 deletions

File tree

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <geometry_msgs/msg/transform.hpp>
1313
#include <geometry_msgs/msg/twist.hpp>
1414
#include <geometry_msgs/msg/vector3.hpp>
15+
#include <sensor_msgs/msg/joint_state.hpp>
1516
#include <sensor_msgs/msg/joy.hpp>
1617
#include <std_msgs/msg/bool.hpp>
1718
#include <std_msgs/msg/empty.hpp>
@@ -208,6 +209,26 @@ namespace ROS2ScriptIntegration
208209
PublishMessage(topicName, message);
209210
}
210211

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

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

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

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

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ namespace ROS2ScriptIntegration
5353
{ "SteeringVelocity", "" },
5454
{ "Speed", "" },
5555
{ "Acceleration", "" },
56-
{ "Jerk", "" } } });
56+
{ "Jerk", "" } } })
57+
->Event(
58+
"PublishJointStateMsg",
59+
&PublisherRequestBus::Events::PublishJointStateMsg,
60+
{ { { "Topic", "" },
61+
{ "JointNames", "" },
62+
{ "JointPositions", "" },
63+
{ "JointVelocities", "" },
64+
{ "JointEfforts", "" } } });
5765
}
5866
}
5967
} // 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+
```

Gems/ROS2ScriptIntegration/gem.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"gem_name": "ROS2ScriptIntegration",
3-
"version": "1.0.0",
3+
"version": "2.1.0",
44
"display_name": "ROS2ScriptIntegration",
55
"license": "Apache-2.0",
66
"license_url": "https://opensource.org/licenses/Apache-2.0",

0 commit comments

Comments
 (0)