Skip to content

Commit

Permalink
Use generic findSensorByName function internally in VehicleApiBase
Browse files Browse the repository at this point in the history
  • Loading branch information
rajat2004 committed Jun 30, 2020
1 parent fd13cae commit 73f6924
Showing 1 changed file with 16 additions and 73 deletions.
89 changes: 16 additions & 73 deletions AirLib/include/api/VehicleApiBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class VehicleApiBase : public UpdatableObject {
// Lidar APIs
virtual LidarData getLidarData(const std::string& lidar_name) const
{
auto *lidar = findLidarByName(lidar_name);
auto *lidar = static_cast<const LidarBase*>(findSensorByName(lidar_name, SensorBase::SensorType::Lidar));
if (lidar == nullptr)
throw VehicleControllerException(Utils::stringf("No lidar with name %s exist on vehicle", lidar_name.c_str()));

Expand All @@ -122,20 +122,7 @@ class VehicleApiBase : public UpdatableObject {
// IMU API
virtual ImuBase::Output getImuData(const std::string& imu_name) const
{
const ImuBase* imu = nullptr;

// Find imu with the given name (for empty input name, return the first one found)
// Not efficient but should suffice given small number of imus
uint count_imus = getSensors().size(SensorBase::SensorType::Imu);
for (uint i = 0; i < count_imus; i++)
{
const ImuBase* current_imu = static_cast<const ImuBase*>(getSensors().getByType(SensorBase::SensorType::Imu, i));
if (current_imu != nullptr && (current_imu->getName() == imu_name || imu_name == ""))
{
imu = current_imu;
break;
}
}
auto *imu = static_cast<const ImuBase*>(findSensorByName(imu_name, SensorBase::SensorType::Imu));
if (imu == nullptr)
throw VehicleControllerException(Utils::stringf("No IMU with name %s exist on vehicle", imu_name.c_str()));

Expand All @@ -145,18 +132,7 @@ class VehicleApiBase : public UpdatableObject {
// Barometer API
virtual BarometerBase::Output getBarometerData(const std::string& barometer_name) const
{
const BarometerBase* barometer = nullptr;

uint count_barometers = getSensors().size(SensorBase::SensorType::Barometer);
for (uint i = 0; i < count_barometers; i++)
{
const BarometerBase* current_barometer = static_cast<const BarometerBase*>(getSensors().getByType(SensorBase::SensorType::Barometer, i));
if (current_barometer != nullptr && (current_barometer->getName() == barometer_name || barometer_name == ""))
{
barometer = current_barometer;
break;
}
}
auto *barometer = static_cast<const BarometerBase*>(findSensorByName(barometer_name, SensorBase::SensorType::Barometer));
if (barometer == nullptr)
throw VehicleControllerException(Utils::stringf("No barometer with name %s exist on vehicle", barometer_name.c_str()));

Expand All @@ -166,18 +142,7 @@ class VehicleApiBase : public UpdatableObject {
// Magnetometer API
virtual MagnetometerBase::Output getMagnetometerData(const std::string& magnetometer_name) const
{
const MagnetometerBase* magnetometer = nullptr;

uint count_magnetometers = getSensors().size(SensorBase::SensorType::Magnetometer);
for (uint i = 0; i < count_magnetometers; i++)
{
const MagnetometerBase* current_magnetometer = static_cast<const MagnetometerBase*>(getSensors().getByType(SensorBase::SensorType::Magnetometer, i));
if (current_magnetometer != nullptr && (current_magnetometer->getName() == magnetometer_name || magnetometer_name == ""))
{
magnetometer = current_magnetometer;
break;
}
}
auto *magnetometer = static_cast<const MagnetometerBase*>(findSensorByName(magnetometer_name, SensorBase::SensorType::Magnetometer));
if (magnetometer == nullptr)
throw VehicleControllerException(Utils::stringf("No magnetometer with name %s exist on vehicle", magnetometer_name.c_str()));

Expand All @@ -187,18 +152,7 @@ class VehicleApiBase : public UpdatableObject {
// Gps API
virtual GpsBase::Output getGpsData(const std::string& gps_name) const
{
const GpsBase* gps = nullptr;

uint count_gps = getSensors().size(SensorBase::SensorType::Gps);
for (uint i = 0; i < count_gps; i++)
{
const GpsBase* current_gps = static_cast<const GpsBase*>(getSensors().getByType(SensorBase::SensorType::Gps, i));
if (current_gps != nullptr && (current_gps->getName() == gps_name || gps_name == ""))
{
gps = current_gps;
break;
}
}
auto *gps = static_cast<const GpsBase*>(findSensorByName(gps_name, SensorBase::SensorType::Gps));
if (gps == nullptr)
throw VehicleControllerException(Utils::stringf("No gps with name %s exist on vehicle", gps_name.c_str()));

Expand All @@ -208,18 +162,7 @@ class VehicleApiBase : public UpdatableObject {
// Distance Sensor API
virtual DistanceBase::Output getDistanceSensorData(const std::string& distance_sensor_name) const
{
const DistanceBase* distance_sensor = nullptr;

uint count_distance_sensors = getSensors().size(SensorBase::SensorType::Distance);
for (uint i = 0; i < count_distance_sensors; i++)
{
const DistanceBase* current_distance_sensor = static_cast<const DistanceBase*>(getSensors().getByType(SensorBase::SensorType::Distance, i));
if (current_distance_sensor != nullptr && (current_distance_sensor->getName() == distance_sensor_name || distance_sensor_name == ""))
{
distance_sensor = current_distance_sensor;
break;
}
}
auto *distance_sensor = static_cast<const DistanceBase*>(findSensorByName(distance_sensor_name, SensorBase::SensorType::Distance));
if (distance_sensor == nullptr)
throw VehicleControllerException(Utils::stringf("No distance sensor with name %s exist on vehicle", distance_sensor_name.c_str()));

Expand Down Expand Up @@ -251,24 +194,24 @@ class VehicleApiBase : public UpdatableObject {
};

private:
const LidarBase* findLidarByName(const std::string& lidar_name) const
const SensorBase* findSensorByName(const std::string& sensor_name, const SensorBase::SensorType type) const
{
const LidarBase* lidar = nullptr;
const SensorBase* sensor = nullptr;

// Find lidar with the given name (for empty input name, return the first one found)
// Not efficient but should suffice given small number of lidars
uint count_lidars = getSensors().size(SensorBase::SensorType::Lidar);
for (uint i = 0; i < count_lidars; i++)
// Find sensor with the given name (for empty input name, return the first one found)
// Not efficient but should suffice given small number of sensors
uint count_sensors = getSensors().size(type);
for (uint i = 0; i < count_sensors; i++)
{
const LidarBase* current_lidar = static_cast<const LidarBase*>(getSensors().getByType(SensorBase::SensorType::Lidar, i));
if (current_lidar != nullptr && (current_lidar->getName() == lidar_name || lidar_name == ""))
const SensorBase* current_sensor = getSensors().getByType(type, i);
if (current_sensor != nullptr && (current_sensor->getName() == sensor_name || sensor_name == ""))
{
lidar = current_lidar;
sensor = current_sensor;
break;
}
}

return lidar;
return sensor;
}
};

Expand Down

0 comments on commit 73f6924

Please sign in to comment.