Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temperature Rewrite #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/data/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kNumBrakeAndSuspensionTemperature

static constexpr size_t kNumAmbientTemp = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kNumAmbientTemperature


TemperatureData temperature;
AmbientPressureData ambient_pressure;

std::array<TemperatureData, kNumBrakeTemp> brake_temperature_array;
std::array<TemperatureData, kNumBrakeSuspensionTemp> brake_suspension_temperature_array;
std::array<TemperatureData, kNumAmbientTemp> ambient_temperature_array;

DataPoint<std::array<ImuData, kNumImus>> imu;
Expand Down
2 changes: 1 addition & 1 deletion src/debugging/observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void Observer::addFakeImuManagerTask(std::shared_ptr<sensors::FakeTrajectory> fa

void Observer::addTemperatureTask(const uint8_t pin)
{
auto temperature = std::make_shared<sensors::Temperature>(pin);
auto temperature = std::make_shared<sensors::AmbientTemperature>(pin);
std::stringstream name;
name << "temperature-" << static_cast<uint32_t>(pin);
Task temperature_task;
Expand Down
71 changes: 50 additions & 21 deletions src/sensors/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(ambient_temperature_pins->size()),
static_cast<uint32_t>(data::Sensors::kNumAmbientTemp));
sys_.stop();
return;
}
for (size_t i = 0; i < data::Sensors::kNumAmbientTemp; ++i) {
ambient_temperatures_.at(i) = std::make_unique<Temperature>(ambient_temperature_pins->at(i));
ambient_temperatures_.at(i)
= std::make_unique<AmbientTemperature>(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<uint32_t>(brakes_and_suspension_temperature_pins->size()),
static_cast<uint32_t>(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<BrakesAndSuspensionTemperature>(
brakes_and_suspension_temperature_pins->at(i));
}
}

Expand Down Expand Up @@ -142,23 +164,25 @@ 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);
}
}
}

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);
Expand Down Expand Up @@ -272,8 +296,8 @@ std::optional<std::vector<uint8_t>> Main::ambientTemperaturePinsFromFile(utils::
return ambient_temperature_pins;
}

std::optional<std::vector<uint8_t>> Main::brakeTemperaturePinsFromFile(utils::Logger &log,
const std::string &path)
std::optional<std::vector<uint8_t>> Main::brakeSuspensionTemperaturePinsFromFile(
utils::Logger &log, const std::string &path)
{
std::ifstream input_stream(path);
if (!input_stream.is_open()) {
Expand All @@ -292,21 +316,25 @@ std::optional<std::vector<uint8_t>> 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<uint8_t> 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<uint8_t>(pin));
Expand Down Expand Up @@ -409,6 +437,7 @@ void Main::run()

while (sys_.isRunning()) {
checkAmbientTemperature();
checkBrakeAndSuspensionTemperature();
checkAmbientPressure();
checkBrakePressure();
Thread::sleep(200);
Expand Down
12 changes: 5 additions & 7 deletions src/sensors/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class Main : public utils::concurrent::Thread {
const std::string &path);
static std::optional<std::vector<uint8_t>> ambientTemperaturePinsFromFile(
utils::Logger &log, const std::string &path);
static std::optional<std::vector<uint8_t>> brakeTemperaturePinsFromFile(utils::Logger &log,
const std::string &path);
static std::optional<std::vector<uint8_t>> brakeSuspensionTemperaturePinsFromFile(
utils::Logger &log, const std::string &path);
static std::optional<AmbientPressurePins> ambientPressurePinsFromFile(utils::Logger &log,
const std::string &path);

Expand All @@ -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,
Expand All @@ -81,9 +81,7 @@ class Main : public utils::concurrent::Thread {
std::unique_ptr<BmsManager> battery_manager_;

std::array<std::unique_ptr<ITemperature>, data::Sensors::kNumAmbientTemp> ambient_temperatures_;
std::array<std::unique_ptr<ITemperature>, data::Sensors::kNumBrakeTemp> brake_temperatures_;

data::TemperatureData temperature_data_;
std::array<std::unique_ptr<ITemperature>, data::Sensors::kNumBrakeSuspensionTemp> brakes_and_suspension_temperatures_;

std::unique_ptr<IAmbientPressure> ambient_pressure_;
data::AmbientPressureData pressure_data_;
Expand Down
48 changes: 41 additions & 7 deletions src/sensors/temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

log_.debug("raw value: %d", raw_value);
Expand All @@ -27,15 +27,49 @@ 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<double>(raw_value) / 4095;
temp = (temp * 175) - 50;
return static_cast<int8_t>(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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

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<double>(raw_value) / 4095;
temp = (temp * 175) - 50;
return static_cast<int8_t>(temp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a convoluted way of doing this. Is there a datasheet somewhere?
Why not the following?

return std::round(static_cast<double>(raw_value) / 23.4  - 50);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may have to recast the result to make the compiler happy. Actually, we may want to assert that the result is in bounds, e.g. by checking std::numeric_limits<int8_t>::max() or INT8_MAX.

}

uint8_t BrakesAndSuspensionTemperature::getData() const
{
return temperature_data_.temperature;
}
Expand Down
50 changes: 45 additions & 5 deletions src/sensors/temperature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace hyped::sensors {

using AmbientTemperaturePins = std::array<uint8_t, data::Sensors::kNumAmbientTemp>;
using BrakeTemperaturePins = std::array<uint8_t, data::Sensors::kNumBrakeTemp>;
using BrakeTemperaturePins = std::array<uint8_t, data::Sensors::kNumBrakeSuspensionTemp>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BrakeAndSuspension


class ITemperature {
public:
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const


utils::Logger log_;
utils::io::Adc pin_;

/**
* @brief int from data structs
*/
data::TemperatureData temperature_data_;
};

class BrakesAndSuspensionTemperature : public ITemperature {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should go into a separate file.

public:
/**
* @brief Construct a new Brakes and Suspension Temperature object
*
* @param pin for specific ADC pin
*/
BrakesAndSuspensionTemperature(const uint8_t pin);
~BrakesAndSuspensionTemperature();

/**
* @brief
Expand Down