ROS2SystemComponent.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <signal.h>
  9. #include "ROS2SystemComponent.h"
  10. #include <ROS2/Clock/ROS2ClockRequestBus.h>
  11. #include <ROS2/Communication/PublisherConfiguration.h>
  12. #include <ROS2/Communication/QoS.h>
  13. #include <ROS2/Communication/TopicConfiguration.h>
  14. #include <ROS2/ROS2TypeIds.h>
  15. #include <ROS2/Sensor/SensorConfiguration.h>
  16. #include <ROS2/Utilities/ROS2Conversions.h>
  17. #include <AzCore/RTTI/BehaviorContext.h>
  18. #include <AzCore/RTTI/RTTI.h>
  19. #include <AzCore/Serialization/EditContext.h>
  20. #include <AzCore/Serialization/EditContextConstants.inl>
  21. #include <AzCore/Serialization/SerializeContext.h>
  22. #include <AzCore/Settings/SettingsRegistry.h>
  23. #include <AzCore/Time/ITime.h>
  24. #include <AzCore/std/smart_ptr/make_shared.h>
  25. #include <AzCore/std/sort.h>
  26. #include <AzCore/std/string/regex.h>
  27. #include <AzCore/std/string/string_view.h>
  28. #include <AzFramework/API/ApplicationAPI.h>
  29. #include <rcl/validate_topic_name.h>
  30. #include <rmw/validate_namespace.h>
  31. namespace ROS2
  32. {
  33. namespace
  34. {
  35. constexpr AZStd::string_view ROS2NodeArgumentsSetReg = "/O3DE/ROS2/NodeArguments";
  36. }
  37. AZ_COMPONENT_IMPL(ROS2SystemComponent, "ROS2SystemComponent", ROS2SystemComponentTypeId);
  38. void ROS2SystemComponent::Reflect(AZ::ReflectContext* context)
  39. {
  40. // Reflect structs not strictly owned by any single component
  41. QoS::Reflect(context);
  42. TopicConfiguration::Reflect(context);
  43. PublisherConfiguration::Reflect(context);
  44. SensorConfiguration::Reflect(context);
  45. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  46. {
  47. serialize->Class<ROS2SystemComponent, AZ::Component>()->Version(0);
  48. if (AZ::EditContext* ec = serialize->GetEditContext())
  49. {
  50. ec->Class<ROS2SystemComponent>(
  51. "ROS 2 System Component",
  52. "This component is responsible for creating ROS 2 node and executor, provides ROS 2 interfaces "
  53. "and publishes transforms.")
  54. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  55. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("System"))
  56. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  57. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  58. }
  59. }
  60. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  61. {
  62. behaviorContext->EBus<TFInterfaceBus>("TFInterfaceBus")
  63. ->Attribute(AZ::Script::Attributes::Category, "ROS2")
  64. ->Event(
  65. "GetLatestTransform", &TFInterfaceRequests::GetLatestTransform, { { { "Source Frame", "" }, { "Target Frame", "" } } })
  66. ->Event(
  67. "PublishTransform",
  68. &TFInterfaceRequests::PublishTransform,
  69. { { { "Source Frame", "" }, { "Target Frame", "" }, { "Transform", "" }, { "Is Dynamic", "" } } });
  70. }
  71. }
  72. void ROS2SystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  73. {
  74. provided.push_back(AZ_CRC_CE("ROS2Service"));
  75. }
  76. void ROS2SystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  77. {
  78. incompatible.push_back(AZ_CRC_CE("ROS2Service"));
  79. }
  80. void ROS2SystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  81. {
  82. required.push_back(AZ_CRC("AssetDatabaseService", 0x3abf5601));
  83. required.push_back(AZ_CRC_CE("ROS2ClockService"));
  84. }
  85. void ROS2SystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  86. {
  87. }
  88. ROS2SystemComponent::ROS2SystemComponent()
  89. {
  90. if (ROS2Interface::Get() == nullptr)
  91. {
  92. ROS2Interface::Register(this);
  93. }
  94. if (TFInterface::Get() == nullptr)
  95. {
  96. TFInterface::Register(this);
  97. }
  98. }
  99. ROS2SystemComponent::~ROS2SystemComponent()
  100. {
  101. if (TFInterface::Get() == this)
  102. {
  103. TFInterface::Unregister(this);
  104. }
  105. if (ROS2Interface::Get() == this)
  106. {
  107. ROS2Interface::Unregister(this);
  108. }
  109. rclcpp::shutdown();
  110. }
  111. void ROS2SystemComponent::Init()
  112. {
  113. rclcpp::init(0, 0);
  114. // handle signals, e.g. via `Ctrl+C` hotkey or `kill` command
  115. auto handler = [](int sig)
  116. {
  117. rclcpp::shutdown(); // shutdown rclcpp
  118. std::raise(sig); // shutdown o3de
  119. };
  120. signal(SIGINT, handler);
  121. signal(SIGTERM, handler);
  122. }
  123. void ROS2SystemComponent::Activate()
  124. {
  125. std::vector<std::string> nodeArgs;
  126. if (auto* settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
  127. {
  128. AZStd::string nodeArgsStr;
  129. settingsRegistry->Get(nodeArgsStr, ROS2NodeArgumentsSetReg);
  130. std::istringstream iss(nodeArgsStr.c_str());
  131. std::string arg;
  132. while (iss >> arg)
  133. {
  134. nodeArgs.push_back(arg);
  135. }
  136. }
  137. rclcpp::NodeOptions nodeOptions;
  138. nodeOptions.arguments(nodeArgs);
  139. m_ros2Node = std::make_shared<rclcpp::Node>("o3de_ros2_node", nodeOptions);
  140. m_executor = AZStd::make_shared<rclcpp::executors::SingleThreadedExecutor>();
  141. m_executor->add_node(m_ros2Node);
  142. m_staticTFBroadcaster = AZStd::make_unique<tf2_ros::StaticTransformBroadcaster>(m_ros2Node);
  143. m_dynamicTFBroadcaster = AZStd::make_unique<tf2_ros::TransformBroadcaster>(m_ros2Node);
  144. // setup tf2 buffer and listener
  145. m_tfBuffer = AZStd::make_shared<tf2_ros::Buffer>(m_ros2Node->get_clock());
  146. m_tfListener = AZStd::make_shared<tf2_ros::TransformListener>(*m_tfBuffer);
  147. AZ::TickBus::Handler::BusConnect();
  148. TFInterfaceBus::Handler::BusConnect();
  149. m_nodeChangedEvent.Signal(m_ros2Node);
  150. ROS2NamesRequestBus::Handler::BusConnect();
  151. }
  152. void ROS2SystemComponent::Deactivate()
  153. {
  154. ROS2NamesRequestBus::Handler::BusDisconnect();
  155. TFInterfaceBus::Handler::BusDisconnect();
  156. AZ::TickBus::Handler::BusDisconnect();
  157. m_dynamicTFBroadcaster.reset();
  158. m_staticTFBroadcaster.reset();
  159. if (m_executor)
  160. {
  161. if (m_ros2Node)
  162. {
  163. m_executor->remove_node(m_ros2Node);
  164. }
  165. m_executor.reset();
  166. }
  167. m_ros2Node.reset();
  168. m_nodeChangedEvent.Signal(m_ros2Node);
  169. }
  170. std::shared_ptr<rclcpp::Node> ROS2SystemComponent::GetNode() const
  171. {
  172. return m_ros2Node;
  173. }
  174. void ROS2SystemComponent::ConnectOnNodeChanged(NodeChangedEvent::Handler& handler)
  175. {
  176. handler.Connect(m_nodeChangedEvent);
  177. }
  178. void ROS2SystemComponent::BroadcastTransform(const geometry_msgs::msg::TransformStamped& t, bool isDynamic)
  179. {
  180. if (isDynamic)
  181. {
  182. m_frameTransforms.push_back(t);
  183. }
  184. else
  185. {
  186. m_staticTFBroadcaster->sendTransform(t);
  187. }
  188. }
  189. void ROS2SystemComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  190. {
  191. if (rclcpp::ok())
  192. {
  193. m_dynamicTFBroadcaster->sendTransform(m_frameTransforms);
  194. m_frameTransforms.clear();
  195. m_executor->spin_some();
  196. }
  197. }
  198. AZStd::string ROS2SystemComponent::GetNamespacedName(const AZStd::string& ns, const AZStd::string& name)
  199. {
  200. if (ns.empty())
  201. {
  202. return name;
  203. }
  204. return AZStd::string::format("%s/%s", ns.c_str(), name.c_str());
  205. }
  206. AZStd::string ROS2SystemComponent::RosifyName(const AZStd::string& input)
  207. {
  208. AZStd::string rosified = input;
  209. if (input.empty())
  210. {
  211. return rosified;
  212. }
  213. const char underscore = '_';
  214. if (input[0] == underscore)
  215. {
  216. AZ_Warning(
  217. "RosifyName",
  218. false,
  219. "'%s' name starts with an underscore, which makes topic/namespace/parameter hidden by default. Is this intended?",
  220. input.c_str());
  221. }
  222. const AZStd::string stringToReplaceViolations(1, underscore);
  223. const AZStd::regex ros2Disallowedlist("[^0-9|a-z|A-Z|_]");
  224. rosified = AZStd::regex_replace(rosified, ros2Disallowedlist, stringToReplaceViolations);
  225. if (AZStd::isdigit(rosified[0]) || (input[0] != underscore && rosified[0] == underscore))
  226. { // Prepend "o3de_" if it would otherwise start with a number (which would violate ros2 name requirements)
  227. // Also, starting with '_' is not desired unless explicit. Topics/namespaces/parameters starting with "_" are hidden by default.
  228. const AZStd::string prependToNumberStart = "o3de_";
  229. rosified = prependToNumberStart + rosified;
  230. }
  231. if (input != rosified)
  232. {
  233. AZ_TracePrintf(
  234. "RosifyName",
  235. "Name '%s' has been changed to '%s' to conform with ros2 naming restrictions\n",
  236. input.c_str(),
  237. rosified.c_str());
  238. }
  239. return rosified;
  240. }
  241. AZ::Outcome<void, AZStd::string> ROS2SystemComponent::ValidateNamespace(const AZStd::string& ros2Namespace)
  242. {
  243. auto ros2GlobalizedNamespace = ros2Namespace;
  244. const char namespacePrefix = '/';
  245. if (!ros2Namespace.starts_with(namespacePrefix))
  246. { // Prepend "/" if not included, this is done automatically by rclcpp so "/"-less namespaces are ok.
  247. ros2GlobalizedNamespace = namespacePrefix + ros2Namespace;
  248. }
  249. int validationResult = 0;
  250. auto ret = rmw_validate_namespace(ros2GlobalizedNamespace.c_str(), &validationResult, NULL);
  251. if (ret != RMW_RET_OK)
  252. {
  253. AZ_Error("ValidateNamespace", false, "Call to rmw validation for namespace failed");
  254. return AZ::Failure(AZStd::string("Unable to validate namespace due to rmw error"));
  255. }
  256. if (validationResult != RMW_NAMESPACE_VALID)
  257. {
  258. return AZ::Failure(AZStd::string(rmw_namespace_validation_result_string(validationResult)));
  259. }
  260. return AZ::Success();
  261. }
  262. AZ::Outcome<void, AZStd::string> ROS2SystemComponent::ValidateNamespaceField(void* newValue, const AZ::Uuid& valueType)
  263. {
  264. if (azrtti_typeid<AZStd::string>() != valueType)
  265. {
  266. return AZ::Failure(AZStd::string("Unexpected field type: the only valid input is a character string"));
  267. }
  268. const AZStd::string& ros2Namespace(*reinterpret_cast<const AZStd::string*>(newValue));
  269. return ValidateNamespace(ros2Namespace);
  270. }
  271. AZ::Outcome<void, AZStd::string> ROS2SystemComponent::ValidateTopic(const AZStd::string& topic)
  272. {
  273. int validationResult = 0;
  274. [[maybe_unused]] size_t invalidIndex;
  275. if (rcl_validate_topic_name(topic.c_str(), &validationResult, &invalidIndex) != RCL_RET_OK)
  276. {
  277. AZ_Error("ValidateTopic", false, "Call to rcl validation for topic failed");
  278. return AZ::Failure(AZStd::string("Unable to validate topic due to rcl error"));
  279. }
  280. if (RCL_TOPIC_NAME_VALID != validationResult)
  281. {
  282. return AZ::Failure(AZStd::string(rcl_topic_name_validation_result_string(validationResult)));
  283. }
  284. return AZ::Success();
  285. }
  286. AZ::Outcome<void, AZStd::string> ROS2SystemComponent::ValidateTopicField(void* newValue, const AZ::Uuid& valueType)
  287. {
  288. if (azrtti_typeid<AZStd::string>() != valueType)
  289. {
  290. return AZ::Failure(AZStd::string("Unexpected field type: the only valid input is a character string"));
  291. }
  292. const AZStd::string& topic(*reinterpret_cast<const AZStd::string*>(newValue));
  293. return ValidateTopic(topic);
  294. }
  295. AZ::Outcome<AZ::Transform, AZStd::string> ROS2SystemComponent::GetTransform(
  296. const AZStd::string& source, const AZStd::string& target, const builtin_interfaces::msg::Time& time)
  297. {
  298. AZ_Error("ROS2SystemComponent", m_tfBuffer, "This component was not activated, tf is not available");
  299. if (!m_tfBuffer)
  300. {
  301. return AZ::Failure("Component was not activated, TFInterface is not available.");
  302. }
  303. AZ_Assert(m_tfBuffer, "ROS2 TF buffer is not initialized.");
  304. try
  305. {
  306. const auto transform = m_tfBuffer->lookupTransform(source.c_str(), target.c_str(), time);
  307. const auto q = ROS2Conversions::FromROS2Quaternion(transform.transform.rotation);
  308. const auto t = ROS2Conversions::FromROS2Vector3(transform.transform.translation);
  309. return AZ::Transform::CreateFromQuaternionAndTranslation(q, t);
  310. } catch (const tf2::TransformException& ex)
  311. {
  312. return AZ::Failure(
  313. AZStd::string::format("Failed to get transform from %s to %s: %s", source.c_str(), target.c_str(), ex.what()));
  314. }
  315. }
  316. AZ::Transform ROS2SystemComponent::GetLatestTransform(const AZStd::string& source, const AZStd::string& target)
  317. {
  318. const auto result = GetTransform(source, target, builtin_interfaces::msg::Time());
  319. if (result.IsSuccess())
  320. {
  321. return AZ::Transform(result.GetValue());
  322. }
  323. AZ_Warning(
  324. "ROS2SystemComponent",
  325. false,
  326. "Failed to get latest transform from %s to %s: %s",
  327. source.c_str(),
  328. target.c_str(),
  329. result.GetError().c_str());
  330. return AZ::Transform::CreateIdentity();
  331. }
  332. void ROS2SystemComponent::PublishTransform(
  333. const AZStd::string& source, const AZStd::string& target, const AZ::Transform& transform, bool isDynamic)
  334. {
  335. geometry_msgs::msg::TransformStamped t;
  336. t.header.stamp = ROS2ClockInterface::Get()->GetROSTimestamp();
  337. t.header.frame_id = source.c_str();
  338. t.child_frame_id = target.c_str();
  339. t.transform.rotation = ROS2Conversions::ToROS2Quaternion(transform.GetRotation());
  340. t.transform.translation = ROS2Conversions::ToROS2Vector3(transform.GetTranslation());
  341. BroadcastTransform(t, isDynamic);
  342. }
  343. } // namespace ROS2