/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #include #include #include #include #include #include #include #include #include namespace ROS2 { void ROS2SensorComponent::Activate() { AZ::TickBus::Handler::BusConnect(); } void ROS2SensorComponent::Deactivate() { AZ::TickBus::Handler::BusDisconnect(); } void ROS2SensorComponent::Reflect(AZ::ReflectContext* context) { SensorConfiguration::Reflect(context); if (AZ::SerializeContext* serialize = azrtti_cast(context)) { serialize->Class()->Version(1)->Field( "SensorConfiguration", &ROS2SensorComponent::m_sensorConfiguration); if (AZ::EditContext* ec = serialize->GetEditContext()) { ec->Class("ROS2 Sensor", "Base component for sensors") ->DataElement( AZ::Edit::UIHandlers::Default, &ROS2SensorComponent::m_sensorConfiguration, "Sensor configuration", "Sensor configuration"); } } } AZStd::string ROS2SensorComponent::GetNamespace() const { auto* ros2Frame = Utils::GetGameOrEditorComponent(GetEntity()); return ros2Frame->GetNamespace(); }; AZStd::string ROS2SensorComponent::GetFrameID() const { auto* ros2Frame = Utils::GetGameOrEditorComponent(GetEntity()); return ros2Frame->GetFrameID(); } void ROS2SensorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required) { required.push_back(AZ_CRC_CE("ROS2Frame")); } void ROS2SensorComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time) { Visualise(); // each frame if (!m_sensorConfiguration.m_publishingEnabled) { return; } m_tickCountDown--; if (m_tickCountDown <= 0) { const AZStd::chrono::duration expectedLoopTime = ROS2Interface::Get()->GetSimulationClock().GetExpectedSimulationLoopTime(); const auto frequency = m_sensorConfiguration.m_frequency; const auto frameTime = frequency == 0.f ? 1.f : 1.f / frequency; const float numberOfFrames = frameTime / expectedLoopTime.count(); m_tickCountDown = AZStd::round(numberOfFrames); FrequencyTick(); } } } // namespace ROS2