Skip to content

Commit

Permalink
refactor and move common methods to pawn wrapper for multi-vehicle su…
Browse files Browse the repository at this point in the history
…pport
  • Loading branch information
sytelus committed Jan 19, 2018
1 parent 0c9df48 commit 282b34c
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 39 deletions.
10 changes: 4 additions & 6 deletions Unreal/Plugins/AirSim/Source/Car/CarPawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,8 @@ void ACarPawn::NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other,
HitNormal, NormalImpulse, Hit);
}

void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_server_address, bool engine_sound, int remote_control_id)
void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_server_address, bool engine_sound)
{
remote_control_id_ = remote_control_id;

if (engine_sound)
EngineSoundComponent->Activate();
else
Expand Down Expand Up @@ -215,8 +213,8 @@ void ACarPawn::initializeForBeginPlay(bool enable_rpc, const std::string& api_se
keyboard_controls_ = joystick_controls_ = CarPawnApi::CarControls();

//joystick
if (remote_control_id_ >= 0) {
joystick_.getJoyStickState(remote_control_id_, joystick_state_);
if (wrapper_->getRemoteControlID() >= 0) {
joystick_.getJoyStickState(wrapper_->getRemoteControlID(), joystick_state_);
if (joystick_state_.is_initialized)
UAirBlueprintLib::LogMessageString("RC Controller on USB: ", joystick_state_.pid_vid == "" ?
"(Detected)" : joystick_state_.pid_vid, LogDebugLevel::Informational);
Expand Down Expand Up @@ -418,7 +416,7 @@ void ACarPawn::Tick(float Delta)
void ACarPawn::updateCarControls()
{
const msr::airlib::CarApiBase::CarControls* current_controls = nullptr;
if (remote_control_id_ >= 0 && joystick_state_.is_initialized) {
if (wrapper_->getRemoteControlID() >= 0 && joystick_state_.is_initialized) {
joystick_.getJoyStickState(0, joystick_state_);

if ((joystick_state_.buttons & 4) | (joystick_state_.buttons & 1024)) { //X button or Start button
Expand Down
4 changes: 1 addition & 3 deletions Unreal/Plugins/AirSim/Source/Car/CarPawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ACarPawn : public AWheeledVehicle
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

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

virtual void NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation,
FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) override;
Expand Down Expand Up @@ -161,8 +161,6 @@ class ACarPawn : public AWheeledVehicle
CarPawnApi::CarControls keyboard_controls_;
CarPawnApi::CarControls joystick_controls_;

int remote_control_id_ = -1;

SimJoyStick joystick_;
SimJoyStick::State joystick_state_;
};
11 changes: 3 additions & 8 deletions Unreal/Plugins/AirSim/Source/Car/SimModeCar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void ASimModeCar::setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles)
//chose first pawn as FPV if none is designated as FPV
VehiclePawnWrapper* wrapper = vehicle_pawn->getVehiclePawnWrapper();
vehicle_pawn->initializeForBeginPlay(getSettings().enable_rpc,
getSettings().api_server_address, getSettings().engine_sound, getRemoteControlID(*wrapper));
getSettings().api_server_address, getSettings().engine_sound);

if (getSettings().enable_collision_passthrough)
wrapper->getConfig().enable_passthrough_on_collisions = true;
Expand All @@ -125,19 +125,14 @@ void ASimModeCar::setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles)
CameraDirector->initializeForBeginPlay(getInitialViewMode(), fpv_vehicle_pawn_wrapper_, external_camera);
}

std::string ASimModeCar::getVehicleName(const VehiclePawnWrapper& pawn)
{
return pawn.getConfig().vehicle_config_name == "" ? getSettings().default_vehicle_config
: pawn.getConfig().vehicle_config_name;
}

int ASimModeCar::getRemoteControlID(const VehiclePawnWrapper& pawn)
{
typedef msr::airlib::AirSimSettings AirSimSettings;

//find out which RC we should use
AirSimSettings::VehicleSettings vehicle_settings =
AirSimSettings::singleton().getVehicleSettings(getVehicleName(pawn));
AirSimSettings::singleton().getVehicleSettings(fpv_vehicle_pawn_wrapper_->getVehicleConfigName());

msr::airlib::Settings settings;
vehicle_settings.getRawSettings(settings);
Expand Down Expand Up @@ -189,7 +184,7 @@ void ASimModeCar::updateReport()
for (VehiclePtr vehicle : vehicles_) {
VehiclePawnWrapper* wrapper = vehicle->getVehiclePawnWrapper();
msr::airlib::StateReporter& reporter = * report_wrapper_.getReporter();
std::string vehicle_name = getVehicleName(*wrapper);
std::string vehicle_name = fpv_vehicle_pawn_wrapper_->getVehicleConfigName();

reporter.writeHeading(std::string("Vehicle: ").append(
vehicle_name == "" ? "(default)" : vehicle_name));
Expand Down
1 change: 0 additions & 1 deletion Unreal/Plugins/AirSim/Source/Car/SimModeCar.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class AIRSIM_API ASimModeCar : public ASimModeBase
void setupVehiclesAndCamera(std::vector<VehiclePtr>& vehicles);
void updateReport();
int getRemoteControlID(const VehiclePawnWrapper& pawn);
std::string getVehicleName(const VehiclePawnWrapper& pawn);


private:
Expand Down
19 changes: 6 additions & 13 deletions Unreal/Plugins/AirSim/Source/Multirotor/SimModeWorldMultiRotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void ASimModeWorldMultiRotor::BeginPlay()
Super::BeginPlay();

//create control server
for (std::shared_ptr<VehicleConnectorBase>& vehicle_connector_ : fpv_vehicle_connector_) {
for (const std::shared_ptr<VehicleConnectorBase>& vehicle_connector_ : fpv_vehicle_connectors_) {
try {
vehicle_connector_->startApiServer();
}
Expand All @@ -39,17 +39,15 @@ void ASimModeWorldMultiRotor::EndPlay(const EEndPlayReason::Type EndPlayReason)
//stop physics thread before we dismental
stopAsyncUpdator();

for (std::shared_ptr<VehicleConnectorBase>& vehicle_connector_ : fpv_vehicle_connector_)
for (const std::shared_ptr<VehicleConnectorBase>& vehicle_connector_ : fpv_vehicle_connectors_)
vehicle_connector_->stopApiServer();

//for (AActor* actor : spawned_actors_) {
// actor->Destroy();
//}
spawned_actors_.Empty();
if (CameraDirector != nullptr) {
fpv_vehicle_connector_.Empty();
CameraDirector = nullptr;
}
//fpv_vehicle_connectors_.Empty();
CameraDirector = nullptr;

Super::EndPlay(EndPlayReason);
}
Expand Down Expand Up @@ -113,8 +111,6 @@ void ASimModeWorldMultiRotor::setupVehiclesAndCamera(std::vector<VehiclePtr>& ve
pawns.Add(spawned_pawn);
}

unsigned int vehicle_id = 0;

//set up vehicle pawns
for (AActor* pawn : pawns)
{
Expand All @@ -134,8 +130,7 @@ void ASimModeWorldMultiRotor::setupVehiclesAndCamera(std::vector<VehiclePtr>& ve
VehiclePtr vehicle = createVehicle(wrapper);
if (vehicle != nullptr) {
vehicles.push_back(vehicle);
fpv_vehicle_connector_.Add(vehicle);
vehicle_id++;
fpv_vehicle_connectors_.Add(vehicle);
}
//else we don't have vehicle for this pawn
}
Expand Down Expand Up @@ -188,9 +183,7 @@ void ASimModeWorldMultiRotor::createVehicles(std::vector<VehiclePtr>& vehicles)

ASimModeWorldBase::VehiclePtr ASimModeWorldMultiRotor::createVehicle(VehiclePawnWrapper* wrapper)
{
auto vehicle_params = MultiRotorParamsFactory::createConfig(
wrapper->getConfig().vehicle_config_name == "" ? getSettings().default_vehicle_config
: wrapper->getConfig().vehicle_config_name);
auto vehicle_params = MultiRotorParamsFactory::createConfig(wrapper->getVehicleConfigName());

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ class AIRSIM_API ASimModeWorldMultiRotor : public ASimModeWorldBase
TArray<AActor*> spawned_actors_;

VehiclePawnWrapper* fpv_vehicle_pawn_wrapper_;
TArray <std::shared_ptr<VehicleConnectorBase> > fpv_vehicle_connector_;
TArray <std::shared_ptr<VehicleConnectorBase> > fpv_vehicle_connectors_;
};
16 changes: 9 additions & 7 deletions Unreal/Plugins/AirSim/Source/PIPCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ APIPCamera::APIPCamera()
else
UAirBlueprintLib::LogMessageString("Cannot create noise material for the PIPCamera",
"", LogDebugLevel::Failure);

noise_materials_.AddZeroed(imageTypeCount() + 1);
}

void APIPCamera::PostInitializeComponents()
Expand Down Expand Up @@ -52,6 +50,8 @@ void APIPCamera::BeginPlay()
{
Super::BeginPlay();

noise_materials_.AddZeroed(imageTypeCount() + 1);

//by default all image types are disabled
camera_type_enabled_.assign(imageTypeCount(), false);

Expand All @@ -65,12 +65,14 @@ void APIPCamera::BeginPlay()

void APIPCamera::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
for (unsigned int image_type = 0; image_type < imageTypeCount(); ++image_type) {
if (noise_materials_[image_type + 1])
captures_[image_type]->PostProcessSettings.RemoveBlendable(noise_materials_[image_type + 1]);
if (noise_materials_.Num()) {
for (unsigned int image_type = 0; image_type < imageTypeCount(); ++image_type) {
if (noise_materials_[image_type + 1])
captures_[image_type]->PostProcessSettings.RemoveBlendable(noise_materials_[image_type + 1]);
}
if (noise_materials_[0])
camera_->PostProcessSettings.RemoveBlendable(noise_materials_[0]);
}
if (noise_materials_[0])
camera_->PostProcessSettings.RemoveBlendable(noise_materials_[0]);

noise_material_static_ = nullptr;
noise_materials_.Reset();
Expand Down
22 changes: 22 additions & 0 deletions Unreal/Plugins/AirSim/Source/VehiclePawnWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ void VehiclePawnWrapper::setKinematics(const msr::airlib::Kinematics::State* kin
kinematics_ = kinematics;
}

std::string VehiclePawnWrapper::getVehicleConfigName() const
{
return getConfig().vehicle_config_name == "" ? msr::airlib::AirSimSettings::singleton().default_vehicle_config
: getConfig().vehicle_config_name;
}

int VehiclePawnWrapper::getRemoteControlID() const
{
typedef msr::airlib::AirSimSettings AirSimSettings;

//find out which RC we should use
AirSimSettings::VehicleSettings vehicle_settings =
AirSimSettings::singleton().getVehicleSettings(getVehicleConfigName());

msr::airlib::Settings settings;
vehicle_settings.getRawSettings(settings);

msr::airlib::Settings rc_settings;
settings.getChild("RC", rc_settings);
return rc_settings.getInt("RemoteControlID", -1);
}

void VehiclePawnWrapper::initialize(APawn* pawn, const std::vector<APIPCamera*>& cameras, const WrapperConfig& config)
{
pawn_ = pawn;
Expand Down
2 changes: 2 additions & 0 deletions Unreal/Plugins/AirSim/Source/VehiclePawnWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ class VehiclePawnWrapper

static VehiclePawnWrapper::Pose toPose(const FVector& u_position, const FQuat& u_quat);
msr::airlib::Pose getActorPose(std::string actor_name);
std::string getVehicleConfigName() const;

int getRemoteControlID() const;

protected:
UPROPERTY(VisibleAnywhere)
Expand Down

0 comments on commit 282b34c

Please sign in to comment.