diff --git a/AirLib/include/api/VehicleApiBase.hpp b/AirLib/include/api/VehicleApiBase.hpp index d1288176f5..a307baeaf3 100644 --- a/AirLib/include/api/VehicleApiBase.hpp +++ b/AirLib/include/api/VehicleApiBase.hpp @@ -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(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())); @@ -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(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(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())); @@ -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(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(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())); @@ -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(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(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())); @@ -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(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(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())); @@ -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(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(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())); @@ -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(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; } };