Skip to content

Commit

Permalink
restore carpawn*api.*cpp/h files
Browse files Browse the repository at this point in the history
  • Loading branch information
madratman committed May 2, 2020
1 parent 0f645ce commit 85a3ecf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 73 deletions.
58 changes: 14 additions & 44 deletions Unreal/Plugins/AirSim/Source/Vehicles/Car/CarPawnApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@

#include "PhysXVehicleManager.h"

CarPawnApi::CarPawnApi(ACarPawn* pawn, const msr::airlib::Kinematics::State* pawn_kinematics, const msr::airlib::GeoPoint& home_geopoint,
const msr::airlib::AirSimSettings::VehicleSetting* vehicle_setting, std::shared_ptr<msr::airlib::SensorFactory> sensor_factory,
const msr::airlib::Kinematics::State& state, const msr::airlib::Environment& environment)
: msr::airlib::CarApiBase(vehicle_setting, sensor_factory, state, environment),
pawn_(pawn), pawn_kinematics_(pawn_kinematics), home_geopoint_(home_geopoint)
CarPawnApi::CarPawnApi(ACarPawn* pawn, const msr::airlib::Kinematics::State* pawn_kinematics,
msr::airlib::CarApiBase* vehicle_api)
: pawn_(pawn), pawn_kinematics_(pawn_kinematics), vehicle_api_(vehicle_api)
{
movement_ = pawn->GetVehicleMovement();
}

bool CarPawnApi::armDisarm(bool arm)
{
//TODO: implement arming for car
unused(arm);
return true;
}

void CarPawnApi::setCarControls(const CarApiBase::CarControls& controls)
void CarPawnApi::updateMovement(const msr::airlib::CarApiBase::CarControls& controls)
{
last_controls_ = controls;

Expand All @@ -35,14 +26,9 @@ void CarPawnApi::setCarControls(const CarApiBase::CarControls& controls)
movement_->SetUseAutoGears(!controls.is_manual_gear);
}

const msr::airlib::CarApiBase::CarControls& CarPawnApi::getCarControls() const
{
return last_controls_;
}

msr::airlib::CarApiBase::CarState CarPawnApi::getCarState() const
{
CarApiBase::CarState state(
msr::airlib::CarApiBase::CarState state(
movement_->GetForwardSpeed() / 100, //cm/s -> m/s
movement_->GetCurrentGear(),
movement_->GetEngineRotationSpeed(),
Expand All @@ -54,11 +40,11 @@ msr::airlib::CarApiBase::CarState CarPawnApi::getCarState() const
return state;
}

void CarPawnApi::resetImplementation()
void CarPawnApi::reset()
{
msr::airlib::CarApiBase::resetImplementation();
vehicle_api_->reset();

last_controls_ = CarControls();
last_controls_ = msr::airlib::CarApiBase::CarControls();
auto phys_comps = UAirBlueprintLib::getPhysicsComponents(pawn_);
UAirBlueprintLib::RunCommandOnGameThread([this, &phys_comps]() {
for (auto* phys_comp : phys_comps) {
Expand All @@ -69,7 +55,8 @@ void CarPawnApi::resetImplementation()
movement_->ResetMoveState();
movement_->SetActive(false);
movement_->SetActive(true, true);
setCarControls(CarControls());
vehicle_api_->setCarControls(msr::airlib::CarApiBase::CarControls());
updateMovement(msr::airlib::CarApiBase::CarControls());

auto pv = movement_->PVehicle;
if (pv) {
Expand All @@ -79,35 +66,18 @@ void CarPawnApi::resetImplementation()
if (pvd) {
pvd->mDriveDynData.setToRestState();
}
}, true);
}, true);

UAirBlueprintLib::RunCommandOnGameThread([this, &phys_comps]() {
for (auto* phys_comp : phys_comps)
phys_comp->SetSimulatePhysics(true);
}, true);
}, true);
}

void CarPawnApi::update()
{
msr::airlib::CarApiBase::update();
}

msr::airlib::GeoPoint CarPawnApi::getHomeGeoPoint() const
{
return home_geopoint_;
}

void CarPawnApi::enableApiControl(bool is_enabled)
{
if (api_control_enabled_ != is_enabled) {
last_controls_ = CarControls();
api_control_enabled_ = is_enabled;
}
}

bool CarPawnApi::isApiControlEnabled() const
{
return api_control_enabled_;
vehicle_api_->updateCarState(getCarState());
vehicle_api_->update();
}

CarPawnApi::~CarPawnApi() = default;
30 changes: 10 additions & 20 deletions Unreal/Plugins/AirSim/Source/Vehicles/Car/CarPawnApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,26 @@
#include "CarPawn.h"


class CarPawnApi : public msr::airlib::CarApiBase {
class CarPawnApi {
public:
typedef msr::airlib::ImageCaptureBase ImageCaptureBase;

CarPawnApi(ACarPawn* pawn, const msr::airlib::Kinematics::State* pawn_kinematics, const msr::airlib::GeoPoint& home_geopoint,
const msr::airlib::AirSimSettings::VehicleSetting* vehicle_setting, std::shared_ptr<msr::airlib::SensorFactory> sensor_factory,
const msr::airlib::Kinematics::State& state, const msr::airlib::Environment& environment);
CarPawnApi(ACarPawn* pawn, const msr::airlib::Kinematics::State* pawn_kinematics,
msr::airlib::CarApiBase* vehicle_api);

virtual void setCarControls(const CarApiBase::CarControls& controls) override;
void updateMovement(const msr::airlib::CarApiBase::CarControls& controls);

virtual CarApiBase::CarState getCarState() const override;
msr::airlib::CarApiBase::CarState getCarState() const;

virtual void resetImplementation() override;
virtual void update() override;

virtual msr::airlib::GeoPoint getHomeGeoPoint() const override;

virtual void enableApiControl(bool is_enabled) override;
virtual bool isApiControlEnabled() const override;
virtual bool armDisarm(bool arm) override;

virtual const CarApiBase::CarControls& getCarControls() const override;
void reset();
void update();

virtual ~CarPawnApi();

private:
UWheeledVehicleMovementComponent* movement_;
bool api_control_enabled_ = false;
CarControls last_controls_;
msr::airlib::CarApiBase::CarControls last_controls_;
ACarPawn* pawn_;
const msr::airlib::Kinematics::State* pawn_kinematics_;
msr::airlib::GeoPoint home_geopoint_;
};
msr::airlib::CarApiBase* vehicle_api_;
};
21 changes: 12 additions & 9 deletions Unreal/Plugins/AirSim/Source/Vehicles/Car/CarPawnSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using namespace msr::airlib;

CarPawnSimApi::CarPawnSimApi(const Params& params,
const CarPawnApi::CarControls& keyboard_controls, UWheeledVehicleMovementComponent* movement)
const msr::airlib::CarApiBase::CarControls& keyboard_controls, UWheeledVehicleMovementComponent* movement)
: PawnSimApi(params), params_(params),
keyboard_controls_(keyboard_controls)
{
Expand All @@ -20,16 +20,17 @@ void CarPawnSimApi::initialize()
createVehicleApi(static_cast<ACarPawn*>(params_.pawn), params_.home_geopoint);

//TODO: should do reset() here?
joystick_controls_ = CarPawnApi::CarControls();
joystick_controls_ = msr::airlib::CarApiBase::CarControls();
}

void CarPawnSimApi::createVehicleApi(ACarPawn* pawn, const msr::airlib::GeoPoint& home_geopoint)
{
//create vehicle params
std::shared_ptr<UnrealSensorFactory> sensor_factory = std::make_shared<UnrealSensorFactory>(getPawn(), &getNedTransform());
vehicle_api_ = std::unique_ptr<CarApiBase>(new CarPawnApi(pawn, getGroundTruthKinematics(), home_geopoint,
getVehicleSetting(), sensor_factory,
(*getGroundTruthKinematics()), (*getGroundTruthEnvironment())));

vehicle_api_ = CarApiFactory::createApi(getVehicleSetting(), sensor_factory, (*getGroundTruthKinematics()),
(*getGroundTruthEnvironment()), home_geopoint);
pawn_api_ = std::unique_ptr<CarPawnApi>(new CarPawnApi(pawn, getGroundTruthKinematics(), vehicle_api_.get()));
}

std::string CarPawnSimApi::getRecordFileLine(bool is_header_line) const
Expand All @@ -41,7 +42,7 @@ std::string CarPawnSimApi::getRecordFileLine(bool is_header_line) const
}

const msr::airlib::Kinematics::State* kinematics = getGroundTruthKinematics();
const auto state = vehicle_api_->getCarState();
const auto state = pawn_api_->getCarState();

common_line
.append(std::to_string(current_controls_.throttle)).append("\t")
Expand All @@ -60,7 +61,7 @@ std::string CarPawnSimApi::getRecordFileLine(bool is_header_line) const
void CarPawnSimApi::updateRenderedState(float dt)
{
PawnSimApi::updateRenderedState(dt);

vehicle_api_->getStatusMessages(vehicle_api_messages_);

//TODO: do we need this for cars?
Expand Down Expand Up @@ -146,10 +147,12 @@ void CarPawnSimApi::updateCarControls()
if (!vehicle_api_->isApiControlEnabled()) {
//all car controls from anywhere must be routed through API component
vehicle_api_->setCarControls(current_controls_);
pawn_api_->updateMovement(current_controls_);
}
else {
UAirBlueprintLib::LogMessageString("Control Mode: ", "API", LogDebugLevel::Informational);
current_controls_ = vehicle_api_->getCarControls();
pawn_api_->updateMovement(current_controls_);
}
UAirBlueprintLib::LogMessageString("Accel: ", std::to_string(current_controls_.throttle), LogDebugLevel::Informational);
UAirBlueprintLib::LogMessageString("Break: ", std::to_string(current_controls_.brake), LogDebugLevel::Informational);
Expand All @@ -163,13 +166,13 @@ void CarPawnSimApi::resetImplementation()
{
PawnSimApi::resetImplementation();

vehicle_api_->reset();
pawn_api_->reset();
}

//physics tick
void CarPawnSimApi::update()
{
vehicle_api_->update();
pawn_api_->update();

PawnSimApi::update();
}
Expand Down

0 comments on commit 85a3ecf

Please sign in to comment.