From 90046b06de5405bd8f2a23a5a3e7b71257d8216b Mon Sep 17 00:00:00 2001 From: Kshitij Sharma Date: Thu, 5 May 2022 12:24:00 +0100 Subject: [PATCH] SNS: Temp Rewrite --- src/data/data.hpp | 12 +++---- src/debugging/observer.cpp | 2 +- src/sensors/main.cpp | 71 ++++++++++++++++++++++++++----------- src/sensors/main.hpp | 12 +++---- src/sensors/temperature.cpp | 48 +++++++++++++++++++++---- src/sensors/temperature.hpp | 50 +++++++++++++++++++++++--- 6 files changed, 148 insertions(+), 47 deletions(-) diff --git a/src/data/data.hpp b/src/data/data.hpp index 700365ad..34b7a18d 100644 --- a/src/data/data.hpp +++ b/src/data/data.hpp @@ -75,16 +75,16 @@ struct BrakePressureData : public SensorData { }; struct Sensors : public Module { - static constexpr size_t kNumImus = 4; - static constexpr size_t kNumEncoders = 4; - static constexpr size_t kNumBrakePressure = 2; - static constexpr size_t kNumBrakeTemp = 2; - static constexpr size_t kNumAmbientTemp = 4; + static constexpr size_t kNumImus = 4; + static constexpr size_t kNumEncoders = 4; + static constexpr size_t kNumBrakePressure = 2; + static constexpr size_t kNumBrakeSuspensionTemp = 4; + static constexpr size_t kNumAmbientTemp = 1; TemperatureData temperature; AmbientPressureData ambient_pressure; - std::array brake_temperature_array; + std::array brake_suspension_temperature_array; std::array ambient_temperature_array; DataPoint> imu; diff --git a/src/debugging/observer.cpp b/src/debugging/observer.cpp index a7b4c1a1..262b1824 100644 --- a/src/debugging/observer.cpp +++ b/src/debugging/observer.cpp @@ -256,7 +256,7 @@ void Observer::addFakeImuManagerTask(std::shared_ptr fa void Observer::addTemperatureTask(const uint8_t pin) { - auto temperature = std::make_shared(pin); + auto temperature = std::make_shared(pin); std::stringstream name; name << "temperature-" << static_cast(pin); Task temperature_task; diff --git a/src/sensors/main.cpp b/src/sensors/main.cpp index 99b94692..4c9d6c61 100644 --- a/src/sensors/main.cpp +++ b/src/sensors/main.cpp @@ -77,14 +77,36 @@ Main::Main() return; } if (ambient_temperature_pins->size() != data::Sensors::kNumAmbientTemp) { - log_.error("found %u temperature pins but %u were expected", + log_.error("found %u ambient temperature pins but %u were expected", static_cast(ambient_temperature_pins->size()), static_cast(data::Sensors::kNumAmbientTemp)); sys_.stop(); return; } for (size_t i = 0; i < data::Sensors::kNumAmbientTemp; ++i) { - ambient_temperatures_.at(i) = std::make_unique(ambient_temperature_pins->at(i)); + ambient_temperatures_.at(i) + = std::make_unique(ambient_temperature_pins->at(i)); + } + + const auto brakes_and_suspension_temperature_pins + = brakeSuspensionTemperaturePinsFromFile(log_, sys_.config_.temperature_config_path); + if (!brakes_and_suspension_temperature_pins) { + log_.error("failed to initialise brakes and suspension temperature sensor"); + sys_.stop(); + return; + } + + if (brakes_and_suspension_temperature_pins->size() != data::Sensors::kNumBrakeSuspensionTemp) { + log_.error("found %u brake/suspension temperature pins but %u were expected", + static_cast(brakes_and_suspension_temperature_pins->size()), + static_cast(data::Sensors::kNumBrakeSuspensionTemp)); + sys_.stop(); + return; + } + + for (size_t i = 0; i < data::Sensors::kNumBrakeSuspensionTemp; ++i) { + brakes_and_suspension_temperatures_.at(i) = std::make_unique( + brakes_and_suspension_temperature_pins->at(i)); } } @@ -142,7 +164,7 @@ void Main::checkAmbientTemperature() ambient_temperature_data.at(i).temperature = ambient_temperatures_[i]->getData(); if ((ambient_temperature_data.at(i).temperature > 75 || ambient_temperature_data.at(i).temperature < 1)) { - log_.info("PCB temperature is getting a wee high...sorry Cheng"); + log_.info("Ambient Temperature is higher than 75 degrees"); auto sensors_data = data_.getSensorsData(); sensors_data.module_status = data::ModuleStatus::kCriticalFailure; data_.setSensorsData(sensors_data); @@ -150,15 +172,17 @@ void Main::checkAmbientTemperature() } } -void Main::checkBrakeTemperature() +void Main::checkBrakeAndSuspensionTemperature() { - auto brakes_temperature_data = data_.getSensorsData().brake_temperature_array; - for (size_t i = 0; i < data::Sensors::kNumBrakeTemp; ++i) { - brake_temperatures_[i]->run(); // not a thread - brakes_temperature_data.at(i).temperature = brake_temperatures_[i]->getData(); - if ((brakes_temperature_data.at(i).temperature > 75 - || brakes_temperature_data.at(i).temperature < 0)) { - log_.info("PCB temperature is getting a wee high...sorry Cheng"); + auto brakes_and_suspension_temperature_data + = data_.getSensorsData().brake_suspension_temperature_array; + for (size_t i = 0; i < data::Sensors::kNumBrakeSuspensionTemp; ++i) { + brakes_and_suspension_temperatures_[i]->run(); // not a thread + brakes_and_suspension_temperature_data.at(i).temperature + = brakes_and_suspension_temperatures_[i]->getData(); + if ((brakes_and_suspension_temperature_data.at(i).temperature > 85 + || brakes_and_suspension_temperature_data.at(i).temperature < 0)) { + log_.info("A Brake or Suspension pcb is hotter than 85 degrees"); auto sensors_data = data_.getSensorsData(); sensors_data.module_status = data::ModuleStatus::kCriticalFailure; data_.setSensorsData(sensors_data); @@ -272,8 +296,8 @@ std::optional> Main::ambientTemperaturePinsFromFile(utils:: return ambient_temperature_pins; } -std::optional> Main::brakeTemperaturePinsFromFile(utils::Logger &log, - const std::string &path) +std::optional> Main::brakeSuspensionTemperaturePinsFromFile( + utils::Logger &log, const std::string &path) { std::ifstream input_stream(path); if (!input_stream.is_open()) { @@ -292,21 +316,25 @@ std::optional> Main::brakeTemperaturePinsFromFile(utils::Lo return std::nullopt; } auto config_object = document["sensors"].GetObject(); - if (!config_object.HasMember("temperature_pins")) { - log.error("Missing required field 'sensors.temperature_pins' in configuration file at %s", - path.c_str()); + if (!config_object.HasMember("brakes_suspension_temperature_pins")) { + log.error( + "Missing required field 'sensors.brakes_suspension_temperature_pins' in configuration file " + "at %s", + path.c_str()); return std::nullopt; } - const auto brake_temperature_pin_array = config_object["temperature_pins"].GetArray(); - if (brake_temperature_pin_array.Size() != data::Sensors::kNumAmbientTemp) { - log.error("Found %d brake temperature pins but %d were expected in configuration file at %s", - brake_temperature_pin_array.Size(), data::Sensors::kNumBrakeTemp, path.c_str()); + const auto brake_temperature_pin_array + = config_object["brakes_suspension_temperature_pins"].GetArray(); + if (brake_temperature_pin_array.Size() != data::Sensors::kNumBrakeSuspensionTemp) { + log.error( + "Found %d brake/suspension temperature pins but %d were expected in configuration file at %s", + brake_temperature_pin_array.Size(), data::Sensors::kNumBrakeSuspensionTemp, path.c_str()); } std::vector brake_temperature_pins; for (const auto &brake_temperature_pin : brake_temperature_pin_array) { const auto pin = brake_temperature_pin.GetUint(); if (pin > UINT8_MAX) { - log.error("brake temperature pin value %u is too large", pin); + log.error("brake/suspension temperature pin value %u is too large", pin); return std::nullopt; } brake_temperature_pins.push_back(static_cast(pin)); @@ -409,6 +437,7 @@ void Main::run() while (sys_.isRunning()) { checkAmbientTemperature(); + checkBrakeAndSuspensionTemperature(); checkAmbientPressure(); checkBrakePressure(); Thread::sleep(200); diff --git a/src/sensors/main.hpp b/src/sensors/main.hpp index b85dacfe..0d1b1662 100644 --- a/src/sensors/main.hpp +++ b/src/sensors/main.hpp @@ -28,8 +28,8 @@ class Main : public utils::concurrent::Thread { const std::string &path); static std::optional> ambientTemperaturePinsFromFile( utils::Logger &log, const std::string &path); - static std::optional> brakeTemperaturePinsFromFile(utils::Logger &log, - const std::string &path); + static std::optional> brakeSuspensionTemperaturePinsFromFile( + utils::Logger &log, const std::string &path); static std::optional ambientPressurePinsFromFile(utils::Logger &log, const std::string &path); @@ -52,10 +52,10 @@ class Main : public utils::concurrent::Thread { void checkAmbientTemperature(); /** - * @brief used to check the temperature of the brake temperature sensors + * @brief used to check the temperature of the brake and suspension temperature sensors * infrequently in main loop, unnecessary to constantly check temperature; */ - void checkBrakeTemperature(); + void checkBrakeAndSuspensionTemperature(); /** * @brief used to check the pressure every twenty times in the main loop, @@ -81,9 +81,7 @@ class Main : public utils::concurrent::Thread { std::unique_ptr battery_manager_; std::array, data::Sensors::kNumAmbientTemp> ambient_temperatures_; - std::array, data::Sensors::kNumBrakeTemp> brake_temperatures_; - - data::TemperatureData temperature_data_; + std::array, data::Sensors::kNumBrakeSuspensionTemp> brakes_and_suspension_temperatures_; std::unique_ptr ambient_pressure_; data::AmbientPressureData pressure_data_; diff --git a/src/sensors/temperature.cpp b/src/sensors/temperature.cpp index 10dfa0af..e6d3fbe9 100644 --- a/src/sensors/temperature.cpp +++ b/src/sensors/temperature.cpp @@ -6,19 +6,19 @@ namespace hyped::sensors { -Temperature::Temperature(const uint8_t pin) - : log_("TEMPERATURE", utils::System::getSystem().config_.log_level_sensors), +AmbientTemperature::AmbientTemperature(const uint8_t pin) + : log_("AMBIENT TEMPERATURE", utils::System::getSystem().config_.log_level_sensors), pin_(pin) { log_.info("started temperature for pin %u", pin); } -Temperature::~Temperature() +AmbientTemperature::~AmbientTemperature() { - log_.info("stopped temperature for pin"); + log_.info("stopped ambient temperature for pin"); } -void Temperature::run() +void AmbientTemperature::run() { uint16_t raw_value = pin_.read(); log_.debug("raw value: %d", raw_value); @@ -27,7 +27,7 @@ void Temperature::run() temperature_data_.operational = true; } -int8_t Temperature::scaleData(const uint8_t raw_value) +int8_t AmbientTemperature::scaleData(const uint8_t raw_value) { // convert to degrees C double temp = static_cast(raw_value) / 4095; @@ -35,7 +35,41 @@ int8_t Temperature::scaleData(const uint8_t raw_value) return static_cast(temp); } -uint8_t Temperature::getData() const +uint8_t AmbientTemperature::getData() const +{ + return temperature_data_.temperature; +} + +BrakesAndSuspensionTemperature::BrakesAndSuspensionTemperature(const uint8_t pin) + : log_("BRAKE-SUSP TEMPERATURE", utils::System::getSystem().config_.log_level_sensors), + pin_(pin) +{ + log_.info("started temperature for pin %u", pin); +} + +BrakesAndSuspensionTemperature::~BrakesAndSuspensionTemperature() +{ + log_.info("stopped brake/suspension temperature for pin"); +} + +void BrakesAndSuspensionTemperature::run() +{ + uint16_t raw_value = pin_.read(); + log_.debug("raw value: %d", raw_value); + temperature_data_.temperature = scaleData(raw_value); + log_.debug("scaled value: %d", temperature_data_.temperature); + temperature_data_.operational = true; +} + +int8_t BrakesAndSuspensionTemperature::scaleData(const uint8_t raw_value) +{ + // convert to degrees C + double temp = static_cast(raw_value) / 4095; + temp = (temp * 175) - 50; + return static_cast(temp); +} + +uint8_t BrakesAndSuspensionTemperature::getData() const { return temperature_data_.temperature; } diff --git a/src/sensors/temperature.hpp b/src/sensors/temperature.hpp index 4dabc7cd..d1745fbc 100644 --- a/src/sensors/temperature.hpp +++ b/src/sensors/temperature.hpp @@ -8,7 +8,7 @@ namespace hyped::sensors { using AmbientTemperaturePins = std::array; -using BrakeTemperaturePins = std::array; +using BrakeTemperaturePins = std::array; class ITemperature { public: @@ -29,15 +29,55 @@ class ITemperature { virtual uint8_t getData() const = 0; }; -class Temperature : public ITemperature { +class AmbientTemperature : public ITemperature { public: /** - * @brief Construct a new Temperature object + * @brief Construct a new Ambient Temperature object * * @param pin for specific ADC pin */ - Temperature(const uint8_t pin); - ~Temperature(); + AmbientTemperature(const uint8_t pin); + ~AmbientTemperature(); + + /** + * @brief + * + * @return int to set to data struct in sensors main + */ + uint8_t getData() const override; + + /** + * @brief one interation of checking sensors + */ + void run() override; + + private: + /** + * @brief scale raw digital data to output in degrees C + * + * @param raw_value input voltage + * @return int representation of temperature + */ + static int8_t scaleData(uint8_t raw_value); + + utils::Logger log_; + utils::io::Adc pin_; + + /** + * @brief int from data structs + */ + data::TemperatureData temperature_data_; +}; + +class BrakesAndSuspensionTemperature : public ITemperature { + public: + /** + * @brief Construct a new Brakes and Suspension Temperature object + * + * @param pin for specific ADC pin + */ + BrakesAndSuspensionTemperature(const uint8_t pin); + ~BrakesAndSuspensionTemperature(); /** * @brief