Skip to content

Commit cfe25f2

Browse files
committed
Added Physx stuff
Signed-off-by: Michał Pełka <[email protected]>
1 parent de956bb commit cfe25f2

File tree

3 files changed

+113
-6
lines changed

3 files changed

+113
-6
lines changed

Gems/SensorDebug/Code/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ly_add_target(
5454
Gem::ImGui
5555
Gem::ROS2.Static
5656
Gem::ROS2.API
57+
Gem::PhysX5.Static
5758
)
5859

5960
# Here add ${gem_name} target, it depends on the Private Object library and Public API interface

Gems/SensorDebug/Code/Source/Clients/SensorDebugSystemComponent.cpp

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
#include <AzCore/Serialization/SerializeContext.h>
77
#include <AzFramework/Components/ConsoleBus.h>
8+
9+
#include <AzCore/Time/ITime.h>
10+
#include <PhysX/Configuration/PhysXConfiguration.h>
11+
812
namespace SensorDebug
913
{
1014
AZ_COMPONENT_IMPL(SensorDebugSystemComponent, "SensorDebugSystemComponent", SensorDebugSystemComponentTypeId);
@@ -21,6 +25,7 @@ namespace SensorDebug
2125
{
2226
ImGui::ImGuiUpdateListenerBus::Handler::BusConnect();
2327
AZ::TickBus::Handler::BusConnect();
28+
GetPhysXConfig();
2429
}
2530

2631
void SensorDebugSystemComponent::Deactivate()
@@ -98,6 +103,29 @@ namespace SensorDebug
98103
AZ_Printf("TestComponent", "Found %d sensor entities", m_sensorEntities.size());
99104
}
100105

106+
AzPhysics::Scene* SensorDebugSystemComponent::GetScene()
107+
{
108+
if (m_scene)
109+
{
110+
return m_scene;
111+
}
112+
AzPhysics::SystemInterface* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
113+
AZ_Assert(physicsSystem, "No physics system.");
114+
115+
AzPhysics::SceneInterface* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
116+
AZ_Assert(sceneInterface, "No scene intreface.");
117+
118+
AzPhysics::SceneHandle defaultSceneHandle = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
119+
if (defaultSceneHandle == AzPhysics::InvalidSceneHandle)
120+
{
121+
return nullptr;
122+
}
123+
AzPhysics::Scene* scene = sceneInterface->GetScene(defaultSceneHandle);
124+
AZ_Assert(defaultSceneHandle != AzPhysics::InvalidSceneHandle, "Invalid default physics scene pointer.");
125+
m_scene = scene;
126+
return scene;
127+
}
128+
101129
void SensorDebugSystemComponent::FindSensorsWithGivenType(const char* typeId)
102130
{
103131
ClearSensors();
@@ -128,15 +156,77 @@ namespace SensorDebug
128156
AZ_Printf("TestComponent", "Found %d sensor entities", m_sensorEntities.size());
129157
}
130158

159+
void SensorDebugSystemComponent::Pause(bool isPaused)
160+
{
161+
GetScene()->SetEnabled(!isPaused);
162+
}
163+
164+
void SensorDebugSystemComponent::UpdatePhysXConfig()
165+
{
166+
AzPhysics::SystemInterface* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
167+
AZ_Assert(physicsSystem, "No physics system.");
168+
auto* physicsSystemConfigurationPtr = physicsSystem->GetConfiguration();
169+
auto* physicsSystemConfiguration = azdynamic_cast<const PhysX::PhysXSystemConfiguration*>(physicsSystemConfigurationPtr);
170+
AZ_Assert(physicsSystemConfiguration, "Invalid physics system configuration pointer, a new Physics system in O3DE????");
171+
physicsSystem->UpdateConfiguration(&ModifiedPhysXConfig, true);
172+
}
173+
174+
void SensorDebugSystemComponent::GetPhysXConfig()
175+
{
176+
AzPhysics::SystemInterface* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
177+
AZ_Assert(physicsSystem, "No physics system.");
178+
auto* physicsSystemConfigurationPtr = physicsSystem->GetConfiguration();
179+
auto* physicsSystemConfiguration = azdynamic_cast<const PhysX::PhysXSystemConfiguration*>(physicsSystemConfigurationPtr);
180+
AZ_Assert(physicsSystemConfiguration, "Invalid physics system configuration pointer, a new Physics system in O3DE????");
181+
ModifiedPhysXConfig = *physicsSystemConfiguration;
182+
}
183+
131184
void SensorDebugSystemComponent::OnImGuiUpdate()
132185
{
133186
ImGui::Begin("ROS2 SensorDebugger");
187+
ImGui::Separator();
188+
ImGui::Text("TimeyWimey Stuff");
189+
190+
auto ros2ts = ROS2::ROS2Interface::Get()->GetROSTimestamp();
191+
const float ros2tsSec = ros2ts.sec + ros2ts.nanosec / 1e9;
192+
auto ros2Node = ROS2::ROS2Interface::Get()->GetNode();
193+
194+
auto nodeTime = ros2Node->now();
195+
const float ros2nodetsSec = nodeTime.seconds() + nodeTime.nanoseconds() / 1e9;
196+
197+
auto timeSystem = AZ::Interface<AZ::ITime>::Get();
198+
const auto elapsedTime = timeSystem ? static_cast<double>(timeSystem->GetElapsedTimeUs()) / 1e6 : 0.0;
199+
200+
ImGui::Text("Current ROS 2 time (Gem) : %f", ros2tsSec);
201+
ImGui::Text("Current ROS 2 time (Node) : %f", ros2nodetsSec);
202+
ImGui::Text("Current O3DE time : %f", elapsedTime);
203+
204+
ImGui::Separator();
205+
ImGui::Text("PhysX");
206+
ImGui::InputFloat("Fixed timestamp", &ModifiedPhysXConfig.m_fixedTimestep, 0.0f, 0.0f, "%.6f");
207+
ImGui::InputFloat("Max timestamp", &ModifiedPhysXConfig.m_maxTimestep, 0.0f, 0.0f, "%.6f"git a);
208+
if (ImGui::Button("Update PhysX Config"))
209+
{
210+
UpdatePhysXConfig();
211+
}
212+
ImGui::SameLine();
213+
if (ImGui::Button("Pause"))
214+
{
215+
Pause(true);
216+
}
217+
ImGui::SameLine();
218+
if (ImGui::Button("Unpause"))
219+
{
220+
Pause(false);
221+
}
222+
ImGui::Separator();
223+
ImGui::Text("Atom");
134224

135225
ImGui::InputFloat("Application Max FPS", &m_maxFPS);
136226
ImGui::SameLine();
137227
if (ImGui::Button("Set sys_MaxFPS"))
138228
{
139-
//disable vsync
229+
// disable vsync
140230
AZStd::string commandVsync = AZStd::string::format("vsync_interval=0");
141231
AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, commandVsync.c_str());
142232
AZStd::string commandMaxFps = AZStd::string::format("sys_MaxFPS=%f", m_maxFPS);
@@ -228,22 +318,22 @@ namespace SensorDebug
228318
frequency, sensorEntity, &ROS2::SensorConfigurationRequest::GetEffectiveFrequency);
229319

230320
m_sensorFrequencyHistory[sensorEntity].push_back(frequency);
231-
auto &sensorFrequencyHistory = m_sensorFrequencyHistory[sensorEntity];
321+
auto& sensorFrequencyHistory = m_sensorFrequencyHistory[sensorEntity];
232322
if (sensorFrequencyHistory.size() > m_historySize)
233323
{
234324
sensorFrequencyHistory.erase(sensorFrequencyHistory.begin());
235325
}
236326
float freqencySum = 0.0f;
237327
for (const auto& freq : sensorFrequencyHistory)
238328
{
239-
freqencySum += freq;
329+
freqencySum += freq;
240330
}
241331
const float averageFrequency = freqencySum / static_cast<float>(sensorFrequencyHistory.size());
242332
// compute std deviation
243333
float variance = 0.0f;
244334
for (const auto& freq : sensorFrequencyHistory)
245335
{
246-
variance += (freq - averageFrequency) * (freq - averageFrequency);
336+
variance += (freq - averageFrequency) * (freq - averageFrequency);
247337
}
248338
float stdDeviation = sqrt(variance / static_cast<float>(sensorFrequencyHistory.size()));
249339
const AZStd::string histogramNameWithCookie = AZStd::string::format("Histogram%s", cookie.c_str());
@@ -254,7 +344,7 @@ namespace SensorDebug
254344
const AZStd::string resetButton = AZStd::string::format("reset stats%s", cookie.c_str());
255345
if (ImGui::Button(resetButton.c_str()))
256346
{
257-
sensorFrequencyHistory.clear();
347+
sensorFrequencyHistory.clear();
258348
}
259349
ImGui::Text(
260350
"%s : %s effective Freq: %.2f Hz [ std_dev = %.2f ]",

Gems/SensorDebug/Code/Source/Clients/SensorDebugSystemComponent.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
#include <AzCore/Component/Component.h>
55
#include <AzCore/Component/TickBus.h>
66

7+
#include <AzCore/Component/TickBus.h>
8+
#include <AzFramework/Physics/PhysicsSystem.h>
79
#include <ImGuiBus.h>
10+
#include <PhysX/Configuration/PhysXConfiguration.h>
811
#include <ROS2/Sensor/Events/TickBasedSource.h>
912
#include <ROS2/Sensor/ROS2SensorComponentBase.h>
1013
#include <ROS2/Sensor/SensorConfigurationRequestBus.h>
1114
#include <ROS2/Sensor/SensorHelper.h>
1215
#include <imgui/imgui.h>
13-
#include <AzCore/Component/TickBus.h>
16+
1417
namespace SensorDebug
1518
{
19+
// Hate to do this global, but we need to access this config after this component is deactivated and destroyed
20+
static PhysX::PhysXSystemConfiguration ModifiedPhysXConfig;
21+
1622
class SensorDebugSystemComponent
1723
: public AZ::Component
1824
, public ImGui::ImGuiUpdateListenerBus::Handler
@@ -40,6 +46,11 @@ namespace SensorDebug
4046
// AZ::TickBus::Handler interface implementation
4147
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
4248

49+
void UpdatePhysXConfig();
50+
void GetPhysXConfig();
51+
void Pause(bool isPaused);
52+
AzPhysics::Scene* GetScene();
53+
4354
void FindSensorsWithBusAPI();
4455
void FindSensorsWithComponentAPI();
4556
void FindSensorsWithGivenType(const char* typeId);
@@ -51,5 +62,10 @@ namespace SensorDebug
5162
AZStd::vector<float> m_appFrequencies;
5263
float m_maxFPS = 60.0f;
5364
int m_historySize = 1000;
65+
66+
67+
68+
69+
AzPhysics::Scene* m_scene = nullptr;
5470
};
5571
} // namespace SensorDebug

0 commit comments

Comments
 (0)