Skip to content

Commit

Permalink
restore carapibase.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
madratman committed May 2, 2020
1 parent 3649c67 commit 0f645ce
Showing 1 changed file with 122 additions and 119 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

0 comments on commit 0f645ce

Please sign in to comment.