Skip to content

Commit 39aae01

Browse files
committed
Support for Logitech G920 whel, physics tuning for car
1 parent 0293441 commit 39aae01

File tree

13 files changed

+136
-46
lines changed

13 files changed

+136
-46
lines changed
Binary file not shown.

Unreal/Plugins/AirSim/AirSim.uplugin

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"FileVersion" : 3,
3-
"Version" : "1.1.5",
4-
"VersionName": "1.1.5",
3+
"Version" : "1.1.7",
4+
"VersionName": "1.1.7",
55
"FriendlyName": "AirSim",
66
"Description": "AirSim - Autonomous Aerial Vehicles Simulator Plugin",
77
"Category" : "Science",

Unreal/Plugins/AirSim/Source/Car/CarPawn.cpp

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ void ACarPawn::NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other,
178178
HitNormal, NormalImpulse, Hit);
179179
}
180180

181-
void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_server_address, bool engine_sound)
181+
void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_server_address, bool engine_sound, int remote_control_id)
182182
{
183+
remote_control_id_ = remote_control_id;
184+
183185
if (engine_sound)
184186
EngineSoundComponent->Activate();
185187
else
@@ -209,17 +211,25 @@ void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_se
209211

210212
startApiServer(enable_rpc, api_server_address);
211213

214+
//TODO: should do reset() here?
215+
keyboard_controls_ = joystick_controls_ = CarPawnApi::CarControls();
216+
212217
//joystick
213-
joystick_.getJoyStickState(0, joystick_state_);
214-
if (joystick_state_.is_initialized)
215-
UAirBlueprintLib::LogMessageString("RC Controller on USB: ", joystick_state_.pid_vid, LogDebugLevel::Informational);
216-
else
217-
UAirBlueprintLib::LogMessageString("RC Controller on USB not detected: ",
218-
std::to_string(joystick_state_.connection_error_code), LogDebugLevel::Informational);
218+
if (remote_control_id_ >= 0) {
219+
joystick_.getJoyStickState(remote_control_id_, joystick_state_);
220+
if (joystick_state_.is_initialized)
221+
UAirBlueprintLib::LogMessageString("RC Controller on USB: ", joystick_state_.pid_vid == "" ?
222+
"(Detected)" : joystick_state_.pid_vid, LogDebugLevel::Informational);
223+
else
224+
UAirBlueprintLib::LogMessageString("RC Controller on USB not detected: ",
225+
std::to_string(joystick_state_.connection_error_code), LogDebugLevel::Informational);
226+
}
227+
219228
}
220229

221230
void ACarPawn::reset(bool disable_api_control)
222231
{
232+
keyboard_controls_ = joystick_controls_ = CarPawnApi::CarControls();
223233
api_->reset();
224234

225235
if (disable_api_control)
@@ -385,22 +395,7 @@ void ACarPawn::Tick(float Delta)
385395
{
386396
Super::Tick(Delta);
387397

388-
const msr::airlib::CarApiBase::CarControls* current_controls = &keyboard_controls_;
389-
if (!api_->isApiControlEnabled()) {
390-
UAirBlueprintLib::LogMessageString("Control Mode: ", "Keyboard", LogDebugLevel::Informational);
391-
api_->setCarControls(keyboard_controls_);
392-
}
393-
else {
394-
UAirBlueprintLib::LogMessageString("Control Mode: ", "API", LogDebugLevel::Informational);
395-
current_controls = & api_->getCarControls();
396-
}
397-
UAirBlueprintLib::LogMessageString("Accel: ", std::to_string(current_controls->throttle), LogDebugLevel::Informational);
398-
UAirBlueprintLib::LogMessageString("Break: ", std::to_string(current_controls->brake), LogDebugLevel::Informational);
399-
UAirBlueprintLib::LogMessageString("Steering: ", std::to_string(current_controls->steering), LogDebugLevel::Informational);
400-
UAirBlueprintLib::LogMessageString("Handbreak: ", std::to_string(current_controls->handbrake), LogDebugLevel::Informational);
401-
UAirBlueprintLib::LogMessageString("Target Gear: ", std::to_string(current_controls->manual_gear), LogDebugLevel::Informational);
402-
403-
398+
updateCarControls();
404399

405400
updateKinematics(Delta);
406401

@@ -420,6 +415,57 @@ void ACarPawn::Tick(float Delta)
420415
getVehiclePawnWrapper()->setLogLine(getLogString());
421416
}
422417

418+
void ACarPawn::updateCarControls()
419+
{
420+
const msr::airlib::CarApiBase::CarControls* current_controls = nullptr;
421+
if (remote_control_id_ >= 0 && joystick_state_.is_initialized) {
422+
joystick_.getJoyStickState(0, joystick_state_);
423+
424+
if ((joystick_state_.buttons & 4) | (joystick_state_.buttons & 1024)) { //X button or Start button
425+
reset();
426+
return;
427+
}
428+
429+
joystick_controls_.steering = joystick_state_.left_y * 1.25;
430+
joystick_controls_.throttle = (-joystick_state_.right_x + 1) / 2;
431+
joystick_controls_.brake = -joystick_state_.right_z + 1;
432+
433+
//Two steel levers behind wheel
434+
joystick_controls_.handbrake = (joystick_state_.buttons & 32) | (joystick_state_.buttons & 64) ? 1 : 0;
435+
436+
if ((joystick_state_.buttons & 256) | (joystick_state_.buttons & 2)) { //RSB button or B button
437+
joystick_controls_.manual_gear = -1;
438+
joystick_controls_.is_manual_gear = true;
439+
joystick_controls_.gear_immediate = true;
440+
}
441+
else if ((joystick_state_.buttons & 512) | (joystick_state_.buttons & 1)) { //LSB button or A button
442+
joystick_controls_.manual_gear = 0;
443+
joystick_controls_.is_manual_gear = false;
444+
joystick_controls_.gear_immediate = true;
445+
}
446+
447+
UAirBlueprintLib::LogMessageString("Control Mode: ", "Wheel/Joystick", LogDebugLevel::Informational);
448+
current_controls = &joystick_controls_;
449+
}
450+
else {
451+
UAirBlueprintLib::LogMessageString("Control Mode: ", "Keyboard", LogDebugLevel::Informational);
452+
current_controls = &keyboard_controls_;
453+
}
454+
455+
if (!api_->isApiControlEnabled()) {
456+
api_->setCarControls(* current_controls);
457+
}
458+
else {
459+
UAirBlueprintLib::LogMessageString("Control Mode: ", "API", LogDebugLevel::Informational);
460+
current_controls = & api_->getCarControls();
461+
}
462+
UAirBlueprintLib::LogMessageString("Accel: ", std::to_string(current_controls->throttle), LogDebugLevel::Informational);
463+
UAirBlueprintLib::LogMessageString("Break: ", std::to_string(current_controls->brake), LogDebugLevel::Informational);
464+
UAirBlueprintLib::LogMessageString("Steering: ", std::to_string(current_controls->steering), LogDebugLevel::Informational);
465+
UAirBlueprintLib::LogMessageString("Handbreak: ", std::to_string(current_controls->handbrake), LogDebugLevel::Informational);
466+
UAirBlueprintLib::LogMessageString("Target Gear: ", std::to_string(current_controls->manual_gear), LogDebugLevel::Informational);
467+
}
468+
423469
void ACarPawn::BeginPlay()
424470
{
425471
Super::BeginPlay();
@@ -450,6 +496,8 @@ void ACarPawn::UpdateHUDStrings()
450496

451497
UAirBlueprintLib::LogMessage(TEXT("Speed: "), SpeedDisplayString.ToString(), LogDebugLevel::Informational);
452498
UAirBlueprintLib::LogMessage(TEXT("Gear: "), GearDisplayString.ToString(), LogDebugLevel::Informational);
499+
UAirBlueprintLib::LogMessage(TEXT("RPM: "), FText::AsNumber(GetVehicleMovement()->GetEngineRotationSpeed()).ToString(), LogDebugLevel::Informational);
500+
453501

454502
}
455503

Unreal/Plugins/AirSim/Source/Car/CarPawn.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ACarPawn : public AWheeledVehicle
8888
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
8989

9090
VehiclePawnWrapper* getVehiclePawnWrapper();
91-
void initializeForBeginPlay(bool enable_rpc, const std::string& api_server_address, bool engine_sound);
91+
void initializeForBeginPlay(bool enable_rpc, const std::string& api_server_address, bool engine_sound, int remoteControlID);
9292

9393
virtual void NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation,
9494
FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) override;
@@ -131,6 +131,8 @@ class ACarPawn : public AWheeledVehicle
131131
void stopApiServer();
132132
bool isApiServerStarted();
133133
void updateKinematics(float delta);
134+
void updateCarControls();
135+
134136
std::string getLogString();
135137

136138
/* Are we on a 'slippery' surface */
@@ -157,6 +159,9 @@ class ACarPawn : public AWheeledVehicle
157159
msr::airlib::Kinematics::State kinematics_;
158160

159161
CarPawnApi::CarControls keyboard_controls_;
162+
CarPawnApi::CarControls joystick_controls_;
163+
164+
int remote_control_id_ = -1;
160165

161166
SimJoyStick joystick_;
162167
SimJoyStick::State joystick_state_;

Unreal/Plugins/AirSim/Source/Car/CarPawnApi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ CarApiBase::CarState CarPawnApi::getCarState()
9797

9898
void CarPawnApi::reset()
9999
{
100+
last_controls_ = CarControls();
100101
UAirBlueprintLib::RunCommandOnGameThread([this]() {
101102
pawn_->reset();
103+
//movement_->ResetMoveState();
104+
//movement_->SetActive(false);
105+
//movement_->SetActive(true, true);
102106
setCarControls(CarControls());
107+
103108
}, true);
104109
}
105110

Unreal/Plugins/AirSim/Source/Car/SimModeCar.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "SimModeCar.h"
22
#include "ConstructorHelpers.h"
33
#include "AirBlueprintLib.h"
4+
#include "controllers/Settings.hpp"
45
#include "Car/CarPawn.h"
56

67
ASimModeCar::ASimModeCar()
@@ -111,13 +112,14 @@ void ASimModeCar::setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles)
111112
//initialize each vehicle pawn we found
112113
TVehiclePawn* vehicle_pawn = static_cast<TVehiclePawn*>(pawn);
113114
vehicles.push_back(vehicle_pawn);
114-
vehicle_pawn->initializeForBeginPlay(enable_rpc, api_server_address, engine_sound);
115115

116116
//chose first pawn as FPV if none is designated as FPV
117117
VehiclePawnWrapper* wrapper = vehicle_pawn->getVehiclePawnWrapper();
118+
vehicle_pawn->initializeForBeginPlay(enable_rpc, api_server_address, engine_sound, getRemoteControlID(*wrapper));
119+
118120
if (enable_collision_passthrough)
119-
wrapper->config.enable_passthrough_on_collisions = true;
120-
if (wrapper->config.is_fpv_vehicle || fpv_vehicle_pawn_wrapper_ == nullptr)
121+
wrapper->getConfig().enable_passthrough_on_collisions = true;
122+
if (wrapper->getConfig().is_fpv_vehicle || fpv_vehicle_pawn_wrapper_ == nullptr)
121123
fpv_vehicle_pawn_wrapper_ = wrapper;
122124
}
123125
}
@@ -126,6 +128,18 @@ void ASimModeCar::setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles)
126128
}
127129

128130

131+
int ASimModeCar::getRemoteControlID(const VehiclePawnWrapper& pawn)
132+
{
133+
//find out which RC we should use
134+
msr::airlib::Settings settings;
135+
msr::airlib::Settings::singleton().getChild(pawn.getConfig().vehicle_config_name == "" ? default_vehicle_config
136+
: pawn.getConfig().vehicle_config_name, settings);
137+
138+
msr::airlib::Settings rc_settings;
139+
settings.getChild("RC", rc_settings);
140+
return rc_settings.getInt("RemoteControlID", -1);
141+
}
142+
129143
void ASimModeCar::createVehicles(std::vector<VehiclePtr>& vehicles)
130144
{
131145
//find vehicles and cameras available in environment
@@ -168,7 +182,7 @@ void ASimModeCar::updateReport()
168182
for (VehiclePtr vehicle : vehicles_) {
169183
VehiclePawnWrapper* wrapper = vehicle->getVehiclePawnWrapper();
170184
msr::airlib::StateReporter& reporter = * report_wrapper_.getReporter();
171-
std::string vehicle_name = std::string(TCHAR_TO_UTF8(* wrapper->config.vehicle_config_name));
185+
std::string vehicle_name = wrapper->getConfig().vehicle_config_name;
172186

173187
reporter.writeHeading(std::string("Vehicle: ").append(
174188
vehicle_name == "" ? "(default)" : vehicle_name));

Unreal/Plugins/AirSim/Source/Car/SimModeCar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class AIRSIM_API ASimModeCar : public ASimModeBase
3131
private:
3232
void setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles);
3333
void updateReport();
34+
int getRemoteControlID(const VehiclePawnWrapper& pawn);
35+
3436

3537
private:
3638
UClass* external_camera_class_;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ void ASimModeWorldMultiRotor::setupVehiclesAndCamera(std::vector<VehiclePtr>& ve
125125
//chose first pawn as FPV if none is designated as FPV
126126
VehiclePawnWrapper* wrapper = vehicle_pawn->getVehiclePawnWrapper();
127127
if (enable_collision_passthrough)
128-
wrapper->config.enable_passthrough_on_collisions = true;
129-
if (wrapper->config.is_fpv_vehicle || fpv_vehicle_pawn_wrapper_ == nullptr)
128+
wrapper->getConfig().enable_passthrough_on_collisions = true;
129+
if (wrapper->getConfig().is_fpv_vehicle || fpv_vehicle_pawn_wrapper_ == nullptr)
130130
fpv_vehicle_pawn_wrapper_ = wrapper;
131131

132132
//now create the connector for each pawn
@@ -189,8 +189,8 @@ void ASimModeWorldMultiRotor::createVehicles(std::vector<VehiclePtr>& vehicles)
189189
ASimModeWorldBase::VehiclePtr ASimModeWorldMultiRotor::createVehicle(VehiclePawnWrapper* wrapper)
190190
{
191191
auto vehicle_params = MultiRotorParamsFactory::createConfig(
192-
wrapper->config.vehicle_config_name == "" ? default_vehicle_config
193-
: std::string(TCHAR_TO_UTF8(*wrapper->config.vehicle_config_name)));
192+
wrapper->getConfig().vehicle_config_name == "" ? default_vehicle_config
193+
: wrapper->getConfig().vehicle_config_name);
194194

195195
vehicle_params_.push_back(std::move(vehicle_params));
196196

Unreal/Plugins/AirSim/Source/SimMode/SimModeBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void ASimModeBase::readSettings()
9393
if (simmode_name == "Multirotor")
9494
default_vehicle_config = "SimpleFlight";
9595
else if (simmode_name == "Car")
96-
default_vehicle_config = "PhysXCar4x4";
96+
default_vehicle_config = "PhysXCar";
9797
else
9898
UAirBlueprintLib::LogMessageString("SimMode is not valid: ", simmode_name, LogDebugLevel::Failure);
9999
}

Unreal/Plugins/AirSim/Source/VehiclePawnWrapper.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ void VehiclePawnWrapper::setKinematics(const msr::airlib::Kinematics::State* kin
114114
kinematics_ = kinematics;
115115
}
116116

117-
void VehiclePawnWrapper::initialize(APawn* pawn, const std::vector<APIPCamera*>& cameras)
117+
void VehiclePawnWrapper::initialize(APawn* pawn, const std::vector<APIPCamera*>& cameras, const WrapperConfig& config)
118118
{
119119
pawn_ = pawn;
120120
cameras_ = cameras;
121+
config_ = config;
121122

122123
for (auto camera : cameras_) {
123124
camera_connectors_.push_back(std::unique_ptr<VehicleCameraConnector>(new VehicleCameraConnector(camera)));
@@ -153,6 +154,15 @@ void VehiclePawnWrapper::initialize(APawn* pawn, const std::vector<APIPCamera*>&
153154
setupCamerasFromSettings();
154155
}
155156

157+
VehiclePawnWrapper::WrapperConfig& VehiclePawnWrapper::getConfig()
158+
{
159+
return config_;
160+
}
161+
162+
const VehiclePawnWrapper::WrapperConfig& VehiclePawnWrapper::getConfig() const
163+
{
164+
return config_;
165+
}
156166

157167
APIPCamera* VehiclePawnWrapper::getCamera(int index)
158168
{
@@ -176,7 +186,10 @@ int VehiclePawnWrapper::getCameraCount()
176186
void VehiclePawnWrapper::reset()
177187
{
178188
state_ = initial_state_;
179-
189+
//if (pawn_->GetRootPrimitiveComponent()->IsAnySimulatingPhysics()) {
190+
// pawn_->GetRootPrimitiveComponent()->SetSimulatePhysics(false);
191+
// pawn_->GetRootPrimitiveComponent()->SetSimulatePhysics(true);
192+
//}
180193
pawn_->SetActorLocationAndRotation(state_.start_location, state_.start_rotation, false, nullptr, ETeleportType::TeleportPhysics);
181194

182195
//TODO: delete below

0 commit comments

Comments
 (0)