Skip to content

Commit f327504

Browse files
committed
Potential fix for #631
1 parent 71edaea commit f327504

File tree

6 files changed

+49
-11
lines changed

6 files changed

+49
-11
lines changed

AirLib/include/vehicles/multirotor/controllers/RealMultirotorConnector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class RealMultirotorConnector : public VehicleConnectorBase
1818

1919
virtual void updateRenderedState(float dt) override
2020
{
21+
unused(dt);
2122
}
2223

2324
virtual void updateRendering(float dt) override

PythonClient/PythonClient.pyproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<SchemaVersion>2.0</SchemaVersion>
66
<ProjectGuid>e2049e20-b6dd-474e-8bca-1c8dc54725aa</ProjectGuid>
77
<ProjectHome>.</ProjectHome>
8-
<StartupFile>car_collision.py</StartupFile>
8+
<StartupFile>hello_car.py</StartupFile>
99
<SearchPath>
1010
</SearchPath>
1111
<WorkingDirectory>.</WorkingDirectory>

Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ T* UAirBlueprintLib::FindActor(const UObject* context, FString name)
107107
return nullptr;
108108
}
109109

110+
bool UAirBlueprintLib::IsInGameThread()
111+
{
112+
return ::IsInGameThread();
113+
}
110114

111115
void UAirBlueprintLib::RunCommandOnGameThread(TFunction<void()> InFunction, bool wait, const TStatId InStatId)
112116
{

Unreal/Plugins/AirSim/Source/AirBlueprintLib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
5353
static int GetMeshStencilID(const std::string& mesh_name);
5454
static void InitializeMeshStencilIDs();
5555

56+
static bool IsInGameThread();
57+
5658
template<class T>
5759
static std::string GetMeshName(T* mesh);
5860
static std::string GetMeshName(ALandscapeProxy* mesh);

Unreal/Plugins/AirSim/Source/Multirotor/MultiRotorConnector.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ MultiRotorConnector::MultiRotorConnector(VehiclePawnWrapper* vehicle_pawn_wrappe
5454

5555
last_pose_ = pending_pose_ = last_debug_pose_ = Pose::nanPose();
5656
pending_pose_status_ = PendingPoseStatus::NonePending;
57+
reset_pending_ = false;
5758

5859
std::string message;
5960
if (!vehicle_.getController()->isAvailable(message)) {
@@ -138,6 +139,12 @@ void MultiRotorConnector::updateRenderedState(float dt)
138139
{
139140
//Utils::log("------Render tick-------");
140141

142+
//if reset is pending then do it first, no need to do other things until next tick
143+
if (reset_pending_) {
144+
reset_task_();
145+
return;
146+
}
147+
141148
//move collision info from rendering engine to vehicle
142149
const CollisionInfo& collision_info = vehicle_pawn_wrapper_->getCollisionInfo();
143150
vehicle_.setCollisionInfo(collision_info);
@@ -187,6 +194,12 @@ void MultiRotorConnector::updateRenderedState(float dt)
187194

188195
void MultiRotorConnector::updateRendering(float dt)
189196
{
197+
//if we did reset then don't worry about synchrnozing states for this tick
198+
if (reset_pending_) {
199+
reset_pending_ = false;
200+
return;
201+
}
202+
190203
try {
191204
controller_->reportTelemetry(dt);
192205
}
@@ -296,16 +309,27 @@ bool MultiRotorConnector::isApiServerStarted()
296309
//*** Start: UpdatableState implementation ***//
297310
void MultiRotorConnector::reset()
298311
{
299-
UAirBlueprintLib::RunCommandOnGameThread([this]() {
300-
VehicleConnectorBase::reset();
312+
if (UAirBlueprintLib::IsInGameThread())
313+
resetPrivate();
314+
else {
315+
//schedule the task which we will execute in tick event when World object is locked
316+
reset_task_ = std::packaged_task<void()>([this]() { resetPrivate(); });
317+
std::future<void> reset_result = reset_task_.get_future();
318+
reset_pending_ = true;
319+
reset_result.wait();
320+
}
321+
}
322+
323+
void MultiRotorConnector::resetPrivate()
324+
{
325+
VehicleConnectorBase::reset();
301326

302-
//TODO: should this be done in MultiRotor.hpp
303-
//controller_->reset();
327+
//TODO: should this be done in MultiRotor.hpp
328+
//controller_->reset();
304329

305-
rc_data_ = RCData();
306-
vehicle_pawn_wrapper_->reset(); //we do flier resetPose so that flier is placed back without collisions
307-
vehicle_.reset();
308-
}, true);
330+
rc_data_ = RCData();
331+
vehicle_pawn_wrapper_->reset(); //we do flier resetPose so that flier is placed back without collisions
332+
vehicle_.reset();
309333
}
310334

311335
void MultiRotorConnector::update()

Unreal/Plugins/AirSim/Source/Multirotor/MultiRotorConnector.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <chrono>
1616
#include "api/ControlServerBase.hpp"
1717
#include "SimJoyStick/SimJoyStick.h"
18+
#include <future>
19+
1820

1921
class MultiRotorConnector : public msr::airlib::VehicleConnectorBase
2022
{
@@ -65,8 +67,8 @@ class MultiRotorConnector : public msr::airlib::VehicleConnectorBase
6567

6668
private:
6769
void detectUsbRc();
68-
static float joyStickToRC(int16_t val);
69-
const msr::airlib::RCData& getRCData();
70+
const msr::airlib::RCData& getRCData();
71+
void resetPrivate();
7072

7173
private:
7274
MultiRotor vehicle_;
@@ -104,6 +106,11 @@ class MultiRotorConnector : public msr::airlib::VehicleConnectorBase
104106
NonePending, RenderStatePending, RenderPending
105107
} pending_pose_status_;
106108
Pose pending_pose_; //force new pose through API
109+
110+
//reset must happen while World is locked so its async task initiated from API thread
111+
bool reset_pending_;
112+
std::packaged_task<void()> reset_task_;
113+
107114
Pose last_pose_; //for trace lines showing vehicle path
108115
Pose last_debug_pose_; //for purposes such as comparing recorded trajectory
109116
};

0 commit comments

Comments
 (0)