Skip to content

Commit

Permalink
restore car*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 3649c67 commit 94ec7a0
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 192 deletions.
241 changes: 122 additions & 119 deletions AirLib/include/vehicles/car/api/CarApiBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,140 +12,143 @@
#include "sensors/SensorCollection.hpp"
#include "sensors/SensorFactory.hpp"

namespace msr {
namespace airlib {

class CarApiBase : public VehicleApiBase {
public:
struct CarControls {
float throttle = 0; /* 1 to -1 */
float steering = 0; /* 1 to -1 */
float brake = 0; /* 1 to -1 */
bool handbrake = false;
bool is_manual_gear = false;
int manual_gear = 0;
bool gear_immediate = true;

CarControls()
{
}
CarControls(float throttle_val, float steering_val, float brake_val, bool handbrake_val,
bool is_manual_gear_val, int manual_gear_val, bool gear_immediate_val)
: throttle(throttle_val), steering(steering_val), brake(brake_val), handbrake(handbrake_val),
is_manual_gear(is_manual_gear_val), manual_gear(manual_gear_val), gear_immediate(gear_immediate_val)
{
}
void set_throttle(float throttle_val, bool forward)
{
if (forward) {
is_manual_gear = false;
manual_gear = 0;
throttle = std::abs(throttle_val);
}
else {
is_manual_gear = false;
manual_gear = -1;
throttle = -std::abs(throttle_val);
}
}
};

struct CarState {
float speed;
int gear;
float rpm;
float maxrpm;
bool handbrake;
Kinematics::State kinematics_estimated;
uint64_t timestamp;

CarState(float speed_val, int gear_val, float rpm_val, float maxrpm_val, bool handbrake_val,
const Kinematics::State& kinematics_estimated_val, uint64_t timestamp_val)
: speed(speed_val), gear(gear_val), rpm(rpm_val), maxrpm(maxrpm_val), handbrake(handbrake_val),
kinematics_estimated(kinematics_estimated_val), timestamp(timestamp_val)
{
}
};

public:

// TODO: Temporary constructor for the Unity implementation which does not use the new Sensor Configuration Settings implementation.
//CarApiBase() {}

CarApiBase(const AirSimSettings::VehicleSetting* vehicle_setting,
std::shared_ptr<SensorFactory> sensor_factory,
const Kinematics::State& state, const Environment& environment)
{
initialize(vehicle_setting, sensor_factory, state, environment);
namespace msr { namespace airlib {

class CarApiBase : public VehicleApiBase {
public:
struct CarControls {
float throttle = 0; /* 1 to -1 */
float steering = 0; /* 1 to -1 */
float brake = 0; /* 1 to -1 */
bool handbrake = false;
bool is_manual_gear = false;
int manual_gear = 0;
bool gear_immediate = true;

CarControls()
{
}
CarControls(float throttle_val, float steering_val, float brake_val, bool handbrake_val,
bool is_manual_gear_val, int manual_gear_val, bool gear_immediate_val)
: throttle(throttle_val), steering(steering_val), brake(brake_val), handbrake(handbrake_val),
is_manual_gear(is_manual_gear_val), manual_gear(manual_gear_val), gear_immediate(gear_immediate_val)
{
}
void set_throttle(float throttle_val, bool forward)
{
if (forward) {
is_manual_gear = false;
manual_gear = 0;
throttle = std::abs(throttle_val);
}

virtual void update() override
{
VehicleApiBase::update();

getSensors().update();
else {
is_manual_gear = false;
manual_gear = -1;
throttle = - std::abs(throttle_val);
}
}
};

struct CarState {
float speed;
int gear;
float rpm;
float maxrpm;
bool handbrake;
Kinematics::State kinematics_estimated;
uint64_t timestamp;

CarState()
{
}

CarState(float speed_val, int gear_val, float rpm_val, float maxrpm_val, bool handbrake_val,
const Kinematics::State& kinematics_estimated_val, uint64_t timestamp_val)
: speed(speed_val), gear(gear_val), rpm(rpm_val), maxrpm(maxrpm_val), handbrake(handbrake_val),
kinematics_estimated(kinematics_estimated_val), timestamp(timestamp_val)
{
}
};

public:

// TODO: Temporary constructor for the Unity implementation which does not use the new Sensor Configuration Settings implementation.
//CarApiBase() {}

CarApiBase(const AirSimSettings::VehicleSetting* vehicle_setting,
std::shared_ptr<SensorFactory> sensor_factory,
const Kinematics::State& state, const Environment& environment)
{
initialize(vehicle_setting, sensor_factory, state, environment);
}

void reportState(StateReporter& reporter) override
{
getSensors().reportState(reporter);
}
virtual void update() override
{
VehicleApiBase::update();

// sensor helpers
virtual const SensorCollection& getSensors() const override
{
return sensors_;
}

SensorCollection& getSensors()
{
return sensors_;
}
getSensors().update();
}

void initialize(const AirSimSettings::VehicleSetting* vehicle_setting,
std::shared_ptr<SensorFactory> sensor_factory,
const Kinematics::State& state, const Environment& environment)
{
sensor_factory_ = sensor_factory;
void reportState(StateReporter& reporter) override
{
getSensors().reportState(reporter);
}

sensor_storage_.clear();
sensors_.clear();
// sensor helpers
virtual const SensorCollection& getSensors() const override
{
return sensors_;
}

addSensorsFromSettings(vehicle_setting);
SensorCollection& getSensors()
{
return sensors_;
}

getSensors().initialize(&state, &environment);
}
void initialize(const AirSimSettings::VehicleSetting* vehicle_setting,
std::shared_ptr<SensorFactory> sensor_factory,
const Kinematics::State& state, const Environment& environment)
{
sensor_factory_ = sensor_factory;

void addSensorsFromSettings(const AirSimSettings::VehicleSetting* vehicle_setting)
{
// use sensors from vehicle settings; if empty list, use default sensors.
// note that the vehicle settings completely override the default sensor "list";
// there is no piecemeal add/remove/update per sensor.
const std::map<std::string, std::unique_ptr<AirSimSettings::SensorSetting>>& sensor_settings
= vehicle_setting->sensors.size() > 0 ? vehicle_setting->sensors : AirSimSettings::AirSimSettings::singleton().sensor_defaults;
sensor_storage_.clear();
sensors_.clear();

addSensorsFromSettings(vehicle_setting);

sensor_factory_->createSensorsFromSettings(sensor_settings, sensors_, sensor_storage_);
}
getSensors().initialize(&state, &environment);
}

virtual void setCarControls(const CarControls& controls) = 0;
virtual CarState getCarState() const = 0;
virtual const CarApiBase::CarControls& getCarControls() const = 0;
void addSensorsFromSettings(const AirSimSettings::VehicleSetting* vehicle_setting)
{
// use sensors from vehicle settings; if empty list, use default sensors.
// note that the vehicle settings completely override the default sensor "list";
// there is no piecemeal add/remove/update per sensor.
const std::map<std::string, std::unique_ptr<AirSimSettings::SensorSetting>>& sensor_settings
= vehicle_setting->sensors.size() > 0 ? vehicle_setting->sensors : AirSimSettings::AirSimSettings::singleton().sensor_defaults;

virtual ~CarApiBase() = default;
sensor_factory_->createSensorsFromSettings(sensor_settings, sensors_, sensor_storage_);
}

std::shared_ptr<const SensorFactory> sensor_factory_;
SensorCollection sensors_; //maintains sensor type indexed collection of sensors
vector<unique_ptr<SensorBase>> sensor_storage_; //RAII for created sensors
virtual void setCarControls(const CarControls& controls) = 0;
virtual void updateCarState(const CarState& state) = 0;
virtual const CarState& getCarState() const = 0;
virtual const CarControls& getCarControls() const = 0;

protected:
virtual void resetImplementation() override
{
//reset sensors last after their ground truth has been reset
getSensors().reset();
}
};
virtual ~CarApiBase() = default;

std::shared_ptr<const SensorFactory> sensor_factory_;
SensorCollection sensors_; //maintains sensor type indexed collection of sensors
vector<unique_ptr<SensorBase>> sensor_storage_; //RAII for created sensors

protected:
virtual void resetImplementation() override
{
//reset sensors last after their ground truth has been reset
getSensors().reset();
}
} //namespace
};


}} //namespace
#endif
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;
Loading

0 comments on commit 94ec7a0

Please sign in to comment.