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

ADC Base Extension Simplification #258

Merged
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
114 changes: 61 additions & 53 deletions src/flight_system/extensions/adc_extension_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,34 @@ namespace extension {
*/
inline constexpr uint32_t kWatchdogMultiplier = 4;

template <uint32_t AdcResolution> class AdcExtensionBase : public Extension {
static_assert(AdcResolution >= 4 && AdcResolution <= 16);
// template <uint32_t AdcResolution>
class AdcExtensionBase : public Extension {
// static_assert(AdcResolution >= 4 && AdcResolution <= 16);

public:
AdcExtensionBase(ExtensionResources &resources,
cfg::ExtensionMetadata metadata)
: Extension(resources, metadata),
adc_watchdog_(metadata.update_interval * kWatchdogMultiplier) {
adc_config_.resolution = adc_resolution_;
if (!parseAdcConfig(metadata.extra_args, adc_config_)) {
error(DiagnosticId::EXTENSION_adcConfigFail);
}
// adc_config_.resolution = adc_resolution_;
// if (!parseAdcConfig(metadata.extra_args, adc_config_)) {
// error(DiagnosticId::EXTENSION_adcConfigFail);
// }
}

protected:
virtual void adcStartup(){};
virtual bool readAdc(uint32_t &value) = 0;
virtual bool readAdc() {
giraffe_assert(false);
return false;
};
virtual void adcShutdown(){};

void reportAdcData(uint32_t value, uint8_t channel) {
dataWithSecondaryIdentifier(data::DataId::ADC_rawCount, value,
std::to_string(channel));
}

private:
void startup() override {
adcStartup();
Expand All @@ -63,62 +72,61 @@ template <uint32_t AdcResolution> class AdcExtensionBase : public Extension {
raiseFault(DiagnosticId::EXT_FAULT_adcWatchdogTimeout);
return;
}
uint32_t adc_value;
if (!readAdc(adc_value)) {
if (!readAdc()) {
return;
}

switch (adc_config_.type) {
case AdcType::RAW_COUNT:
data(data::DataId::ADC_rawCount, adc_value);
break;
case AdcType::PERCENTAGE:
percentage(adc_value);
break;
case AdcType::VOLTAGE_DIVIDER:
voltageDivider(adc_value);
break;
case AdcType::VOLTAGE_REFERENCE:
voltageReference(adc_value);
break;
default:
giraffe_assert(false);
break;
}
// switch (adc_config_.type) {
// case AdcType::RAW_COUNT:
// data(data::DataId::ADC_rawCount, adc_value);
// break;
// case AdcType::PERCENTAGE:
// percentage(adc_value);
// break;
// case AdcType::VOLTAGE_DIVIDER:
// voltageDivider(adc_value);
// break;
// case AdcType::VOLTAGE_REFERENCE:
// voltageReference(adc_value);
// break;
// default:
// giraffe_assert(false);
// break;
// }

adc_watchdog_.reset();
}

void shutdown() override {
}

void percentage(uint32_t value) {
int max = adc_config_.max;
int min = adc_config_.min;
int range = max - min;
int scaled = value - min;
int percentage = (scaled * 100) / range;
data(data::DataId::ADC_percentage, percentage);
}

void voltageDivider(uint32_t value) {
uint32_t unscaled =
(value * adc_config_.voltage_reference) / (1 << adc_resolution_);
uint32_t millivolts =
(unscaled * (adc_config_.resistor_1 + adc_config_.resistor_2)) /
adc_config_.resistor_2;
data(data::DataId::ADC_voltage, millivolts);
}

void voltageReference(uint32_t value) {
uint32_t millivolts =
(value * adc_config_.voltage_reference) / (1 << adc_resolution_);
data(data::DataId::ADC_voltage, millivolts);
}

const uint32_t adc_resolution_ = AdcResolution;

AdcConfig adc_config_{};
// void percentage(uint32_t value) {
// int max = adc_config_.max;
// int min = adc_config_.min;
// int range = max - min;
// int scaled = value - min;
// int percentage = (scaled * 100) / range;
// data(data::DataId::ADC_percentage, percentage);
// }

// void voltageDivider(uint32_t value) {
// uint32_t unscaled =
// (value * adc_config_.voltage_reference) / (1 << adc_resolution_);
// uint32_t millivolts =
// (unscaled * (adc_config_.resistor_1 + adc_config_.resistor_2)) /
// adc_config_.resistor_2;
// data(data::DataId::ADC_voltage, millivolts);
// }

// void voltageReference(uint32_t value) {
// uint32_t millivolts =
// (value * adc_config_.voltage_reference) / (1 << adc_resolution_);
// data(data::DataId::ADC_voltage, millivolts);
// }

// const uint32_t adc_resolution_ = AdcResolution;

// AdcConfig adc_config_{};
bst::Timer adc_watchdog_;
};

Expand Down
11 changes: 11 additions & 0 deletions src/flight_system/extensions/extension_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ void Extension::data(data::ImuFrame frame) {
metadata_.name, frame);
}

void Extension::dataWithSecondaryIdentifier(
data::DataId identifier, uint32_t value,
const std::string &secondary_identifier) {
data::DataPacket packet;
packet.source = node::Identification::EXTENSION;
packet.identifier = identifier;
packet.secondary_identifier = metadata_.name + ":" + secondary_identifier;
packet.value = std::to_string(value);
interfaces_.streams.data.addPacket(packet);
}

void Extension::extSleep(uint32_t ms) {
const uint32_t kMaxSleepTimeMs = 1000;
if (ms > kMaxSleepTimeMs) {
Expand Down
4 changes: 3 additions & 1 deletion src/flight_system/extensions/extension_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ class Extension {

template <typename T>
void data(data::DataId identifier, T value, int precision = 2);
void date(data::DataId identifier, std::string string_data);
// void data(data::DataId identifier, std::string string_data);
void data(data::GpsFrame frame);
void data(data::ImuFrame frame);
void dataWithSecondaryIdentifier(data::DataId identifier, uint32_t value,
const std::string &secondary_identifier);
void error(DiagnosticId, const std::string &info = "");
void error(DiagnosticId log_id, int info);
void info(std::string info);
Expand Down
7 changes: 4 additions & 3 deletions src/flight_system/extensions/mcp3021.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ void Mcp3021::adcStartup() {

bool Mcp3021::i2cHandshake() {
uint32_t value;
if (!readAdc(value)) {
if (!readAdc()) {
return false;
}
return true;
}

bool Mcp3021::readAdc(uint32_t &value) {
bool Mcp3021::readAdc() {
constexpr uint32_t kMcp3021ReadSize = 2;
std::vector<uint8_t> read_buffer(kMcp3021ReadSize);
auto result = i2c_.readChunk(read_buffer, kMcp3021ReadSize);
Expand All @@ -62,10 +62,11 @@ bool Mcp3021::readAdc(uint32_t &value) {
return false;
}

uint32_t value = 0;
value = read_buffer.at(0) << 6;
value |= read_buffer.at(1) >> 2;

// debug("Read value: " << value);
reportAdcData(value, 0);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/flight_system/extensions/mcp3021.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

namespace extension {

class Mcp3021 : public AdcExtensionBase<10>, public I2cExtensionAdapter {
class Mcp3021 : public AdcExtensionBase, public I2cExtensionAdapter {
public:
Mcp3021(ExtensionResources &resources, cfg::ExtensionMetadata metadata);

private:
void adcStartup() override;
bool i2cHandshake() override;
bool readAdc(uint32_t &value) override;
bool readAdc() override;
};

} // namespace extension
Expand Down
Loading