ApplePickerComponent.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #pragma once
  9. #include "AppleDetectionGroundTruth.h"
  10. #include "ApplePickingNotifications.h"
  11. #include "ApplePickingRequests.h"
  12. #include <AzCore/Component/Component.h>
  13. #include <AzCore/Component/TickBus.h>
  14. #include <rclcpp/rclcpp.hpp>
  15. #include <std_srvs/srv/trigger.hpp>
  16. #include <std_srvs/srv/empty.hpp>
  17. #include <std_msgs/msg/float32.hpp>
  18. #include <std_msgs/msg/string.hpp>
  19. namespace AppleKraken
  20. {
  21. using TriggerRequestPtr = std::shared_ptr<std_srvs::srv::Trigger::Request>;
  22. using TriggerResponsePtr = std::shared_ptr<std_srvs::srv::Trigger::Response>;
  23. //! Demo component handling orchestration of apple picking
  24. class ApplePickerComponent
  25. : public AZ::Component
  26. , public ApplePickingNotificationBus::Handler
  27. , public AZ::TickBus::Handler
  28. {
  29. public:
  30. AZ_COMPONENT(ApplePickerComponent, "{E9E83A4A-31A4-4E7A-AF88-7565AC8B9F27}", AZ::Component);
  31. ApplePickerComponent() = default;
  32. static void Reflect(AZ::ReflectContext* context);
  33. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  34. //! Detect and pick all apples in manipulator range.
  35. void StartAutomatedOperation();
  36. //! Report overall progress of gathering task.
  37. //! @returns how much of the task is complete (0: nothing, 1: all of it). The task is completed when all reachable apples are
  38. //! gathered (or had a failure) and the effector is in IDLE state.
  39. float ReportProgress();
  40. private:
  41. void Activate() override;
  42. void Deactivate() override;
  43. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  44. void EffectorReadyForPicking() override;
  45. void ApplePicked() override;
  46. void AppleRetrieved() override;
  47. void PickingFailed(const AZStd::string& reason) override;
  48. bool IsBusy() const;
  49. void PickNextApple();
  50. void QueryEnvironmentForAllApplesInBox(const AZ::Obb& globalBox);
  51. void ProcessTriggerServiceCall(const TriggerRequestPtr req, TriggerResponsePtr resp);
  52. void ProcessCancelServiceCall(const TriggerRequestPtr req, TriggerResponsePtr resp);
  53. AZStd::string m_triggerServiceTopic = "trigger_apple_gathering";
  54. AZStd::string m_cancelServiceTopic = "cancel_apple_gathering";
  55. AZStd::string m_orchestratorStatusTopic = "orchestration_status";
  56. AZStd::string m_doneServiceTopic = "done_apple_gathering";
  57. AZStd::string m_progressTopic = "progress_apple_gathering";
  58. AZ::EntityId m_effectorEntityId;
  59. AZ::EntityId m_fruitStorageEntityId;
  60. // TODO - actually use this entity for retrieval position
  61. AZ::EntityId m_retrievalPointEntityId; //!< used to sort apples by distance to retrieval chute
  62. AZ::EntityId m_entryAnimationEntityId; //!< used to animate apple going into chute
  63. rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr m_triggerService;
  64. rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr m_cancelService;
  65. rclcpp::Client<std_srvs::srv::Empty>::SharedPtr m_doneServiceClient;
  66. rclcpp::Subscription<std_msgs::msg::String>::SharedPtr m_orchestrationStatusSubscriber;
  67. rclcpp::Publisher<std_msgs::msg::Float32>::SharedPtr m_progressPublisher;
  68. size_t m_initialTasksSize = 0;
  69. AZStd::queue<PickAppleTask> m_currentAppleTasks;
  70. AZStd::unique_ptr<AppleDetectionGroundTruth> m_appleGroundTruthDetector;
  71. };
  72. } // namespace AppleKraken