JointsPositionsEditorComponent.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  4. * of this distribution.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0 OR MIT
  7. *
  8. */
  9. #include "JointsPositionsEditorComponent.h"
  10. #include "JointPositionsSubscriptionHandler.h"
  11. #include "JointUtils.h"
  12. #include "JointsPositionsComponent.h"
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  16. #include <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
  17. #include <ROS2/Frame/ROS2FrameEditorComponentBus.h>
  18. #include <ROS2Controllers/Manipulation/JointsManipulationRequests.h>
  19. namespace ROS2Controllers
  20. {
  21. JointsPositionsEditorComponent::JointsPositionsEditorComponent()
  22. {
  23. m_topicConfiguration.m_type = "std_msgs::msg::Float64MultiArray";
  24. m_topicConfiguration.m_topic = "position_controller/commands";
  25. }
  26. void JointsPositionsEditorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  27. {
  28. required.push_back(AZ_CRC_CE("ROS2Frame"));
  29. required.push_back(AZ_CRC_CE("JointsControllerService"));
  30. }
  31. void JointsPositionsEditorComponent::BuildGameEntity(AZ::Entity* gameEntity)
  32. {
  33. gameEntity->CreateComponent<JointsPositionsComponent>(m_topicConfiguration, m_jointNames);
  34. }
  35. void JointsPositionsEditorComponent::Reflect(AZ::ReflectContext* context)
  36. {
  37. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serialize->Class<JointsPositionsEditorComponent, AZ::Component>()
  40. ->Version(0)
  41. ->Field("topicConfiguration", &JointsPositionsEditorComponent::m_topicConfiguration)
  42. ->Field("jointNames", &JointsPositionsEditorComponent::m_jointNames);
  43. if (AZ::EditContext* ec = serialize->GetEditContext())
  44. {
  45. ec->Class<JointsPositionsEditorComponent>("JointsPositionsEditorComponent", "Component controlling a finger gripper.")
  46. ->ClassElement(AZ::Edit::ClassElements::EditorData, "JointsPositionsEditorComponent")
  47. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  48. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  49. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/JointsManipulationEditorComponent.svg")
  50. ->Attribute(
  51. AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/JointsManipulationEditorComponent.svg")
  52. ->DataElement(
  53. AZ::Edit::UIHandlers::Default,
  54. &JointsPositionsEditorComponent::m_topicConfiguration,
  55. "Topic configuration",
  56. "Topic configuration of Joint Positions Component")
  57. ->UIElement(AZ::Edit::UIHandlers::Button, "Find all joints", "Find all joints")
  58. ->Attribute(AZ::Edit::Attributes::ButtonText, "Find all joints")
  59. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &JointsPositionsEditorComponent::FindAllJoints)
  60. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
  61. ->DataElement(
  62. AZ::Edit::UIHandlers::Default,
  63. &JointsPositionsEditorComponent::m_jointNames,
  64. "Joint names",
  65. "Names of all the joints (in order)");
  66. }
  67. }
  68. }
  69. AZ::Crc32 JointsPositionsEditorComponent::FindAllJoints()
  70. {
  71. AzToolsFramework::ScopedUndoBatch undo("FindAllJoints");
  72. m_jointNames.clear();
  73. AZStd::set<AZ::EntityId> childrenEntityIds;
  74. ROS2::ROS2FrameEditorComponentBus::EventResult(
  75. childrenEntityIds, GetEntityId(), &ROS2::ROS2FrameEditorComponentBus::Events::GetFrameDescendants);
  76. for (const auto& entityId : childrenEntityIds)
  77. {
  78. AZ::Entity* childEntity = nullptr;
  79. AZ::ComponentApplicationBus::BroadcastResult(childEntity, &AZ::ComponentApplicationBus::Events::FindEntity, entityId);
  80. AZ_Assert(childEntity, "Entity not found!");
  81. AZ::Name jointName;
  82. ROS2::ROS2FrameComponentBus::EventResult(
  83. jointName, childEntity->GetId(), &ROS2::ROS2FrameComponentBus::Events::GetNamespacedJointName);
  84. const AZStd::string jointNameStr = jointName.GetCStr();
  85. const bool hasNonFixedJoints = JointUtils::HasNonFixedJoints(childEntity);
  86. if (!jointNameStr.empty() && hasNonFixedJoints)
  87. {
  88. m_jointNames.emplace_back(jointNameStr);
  89. }
  90. }
  91. undo.MarkEntityDirty(GetEntity()->GetId());
  92. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  93. }
  94. } // namespace ROS2Controllers