Skip to content

Commit

Permalink
fixup (if you like it): move Pt, pt100 and Data<Pt> into max31865 struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rleh authored and hshose committed Apr 11, 2023
1 parent 2e335fc commit 422e765
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 72 deletions.
2 changes: 1 addition & 1 deletion examples/stm32f469_discovery/max31865/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ThermocoupleThread : public modm::pt::Protothread
}

private:
using Max31865 = modm::Max31865<SpiMaster, Cs, modm::pt100>;
using Max31865 = modm::Max31865<SpiMaster, Cs, modm::max31865::pt100>;
Max31865::Data data;
Max31865 pt100;

Expand Down
128 changes: 63 additions & 65 deletions src/modm/driver/temperature/max31865.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,28 @@
namespace modm
{

struct Pt
{
float R0;
float Rref;
float alpha;
double a;
double b;
double c;
};

/// Standard values for IEC 751 (PT100) with 430 Ohm reference
constexpr Pt pt100{
.R0 = 100.f,
.Rref = 430.f,
.alpha = 3.85055e-3f,
.a = double{3.90830e-3},
.b = double{-5.77500e-7},
.c = double{-4.18301e-12},
};

/// @ingroup modm_driver_max31865
struct max31865
{
struct Pt
{
float R0;
float Rref;
float alpha;
double a;
double b;
double c;
};

/// Standard values for IEC 751 (PT100) with 430 Ohm reference
static constexpr Pt pt100{
.R0 = 100.f,
.Rref = 430.f,
.alpha = 3.85055e-3f,
.a = double{3.90830e-3},
.b = double{-5.77500e-7},
.c = double{-4.18301e-12},
};

enum class Fault : uint8_t
{
Expand Down Expand Up @@ -126,56 +125,55 @@ struct max31865
WriteLowFaultThresholdMsb = 0x85,
WriteLowFaultThresholdLsb = 0x86,
};
}; // struct max31865

template<Pt pt>
/// @ingroup modm_driver_max31865
struct modm_packed max31865Data
{
/// @return measure resistance in ohm
constexpr float
getResistance() const
template<Pt pt>
struct modm_packed Data
{
const uint16_t adccode = data >> 1;
return adccode / 32768.f * pt.Rref;
}

/// @return fast temperature in degrees celsius, about 0.3 degrees error between 0 and 100
/// degrees celsius
constexpr float
getTemperatureFast() const
{
return (getResistance() - pt.R0) / pt.alpha / 100.f;
}
/// @return measure resistance in ohm
constexpr float
getResistance() const
{
const uint16_t adccode = data >> 1;
return adccode / 32768.f * pt.Rref;
}

/// @return slow but accurate temperature in degrees celsius
constexpr double
getTemperaturePrecise() const
{
const double res = getResistance();
double T = getTemperatureFast();
const double c0 = T <= 0 ? pt.c : 0;

// Do some fixed number of newton steps for root finding
// on Callendar Van Dusen equation:
// R = R0*(1+a*T+b*T*T+c*(T-100)*T*T*T)
// Newton seems to need double precision to achieve 1.e-10 residual?!
for (int i = 0; i < 10; i++)
/// @return fast temperature in degrees celsius, about 0.3 degrees error between 0 and 100
/// degrees celsius
constexpr float
getTemperatureFast() const
{
const double R = double{pt.R0} * (1 + (pt.a * T) + (pt.b * T * T) +
(c0 * (T - 100) * T * T * T)) -
res;
const double Rdash =
double{pt.R0} * (pt.a + (2 * pt.b * T) + c0 * (((4 * T) - 300) * T * T));
T -= R / Rdash;
if (std::abs(R) <= 1.e-10) { break; }
return (getResistance() - pt.R0) / pt.alpha / 100.f;
}

return T;
}
/// @return slow but accurate temperature in degrees celsius
constexpr double
getTemperaturePrecise() const
{
const double res = getResistance();
double T = getTemperatureFast();
const double c0 = T <= 0 ? pt.c : 0;

// Do some fixed number of newton steps for root finding
// on Callendar Van Dusen equation:
// R = R0*(1+a*T+b*T*T+c*(T-100)*T*T*T)
// Newton seems to need double precision to achieve 1.e-10 residual?!
for (int i = 0; i < 10; i++)
{
const double R = double{pt.R0} * (1 + (pt.a * T) + (pt.b * T * T) +
(c0 * (T - 100) * T * T * T)) -
res;
const double Rdash =
double{pt.R0} * (pt.a + (2 * pt.b * T) + c0 * (((4 * T) - 300) * T * T));
T -= R / Rdash;
if (std::abs(R) <= 1.e-10) { break; }
}

return T;
}

uint16_t data;
};
uint16_t data;
};
}; // struct max31865

/**
* @tparam SpiMaster
Expand All @@ -184,13 +182,13 @@ struct modm_packed max31865Data
* @author Henrik Hose
* @ingroup modm_driver_max31865
*/
template<typename SpiMaster, typename Cs, Pt pt = pt100>
template<typename SpiMaster, typename Cs, max31865::Pt pt = max31865::pt100>
class Max31865 : public max31865,
public modm::SpiDevice<SpiMaster>,
protected modm::NestedResumable<3>
{
public:
using Data = max31865Data<pt>;
using Data = max31865::Data<pt>;

/**
* @param data pointer to buffer of the internal data of type Data
Expand Down
12 changes: 6 additions & 6 deletions src/modm/driver/temperature/max31865_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
namespace modm
{

template<typename SpiMaster, typename Cs, Pt pt>
template<typename SpiMaster, typename Cs, max31865::Pt pt>
Max31865<SpiMaster, Cs, pt>::Max31865(Data &data) : data(data)
{
this->attachConfigurationHandler([]() { SpiMaster::setDataMode(SpiMaster::DataMode::Mode3); });
Cs::setOutput(modm::Gpio::High);
}

template<typename SpiMaster, typename Cs, Pt pt>
template<typename SpiMaster, typename Cs, max31865::Pt pt>
modm::ResumableResult<void>
Max31865<SpiMaster, Cs, pt>::initialize()
{
Expand All @@ -38,7 +38,7 @@ Max31865<SpiMaster, Cs, pt>::initialize()
RF_END();
}

template<typename SpiMaster, typename Cs, Pt pt>
template<typename SpiMaster, typename Cs, max31865::Pt pt>
modm::ResumableResult<void>
Max31865<SpiMaster, Cs, pt>::readout()
{
Expand All @@ -61,7 +61,7 @@ Max31865<SpiMaster, Cs, pt>::readout()
RF_END();
}

template<typename SpiMaster, typename Cs, Pt pt>
template<typename SpiMaster, typename Cs, max31865::Pt pt>
modm::ResumableResult<uint8_t>
Max31865<SpiMaster, Cs, pt>::readSingleRegister(Register address)
{
Expand All @@ -78,7 +78,7 @@ Max31865<SpiMaster, Cs, pt>::readSingleRegister(Register address)
RF_END_RETURN(buffer[0]);
}

template<typename SpiMaster, typename Cs, Pt pt>
template<typename SpiMaster, typename Cs, max31865::Pt pt>
modm::ResumableResult<uint16_t>
Max31865<SpiMaster, Cs, pt>::readTwoRegisters(Register address)
{
Expand All @@ -95,7 +95,7 @@ Max31865<SpiMaster, Cs, pt>::readTwoRegisters(Register address)
RF_END_RETURN(static_cast<uint16_t>(buffer[0] << 8 | buffer[1]));
}

template<typename SpiMaster, typename Cs, Pt pt>
template<typename SpiMaster, typename Cs, max31865::Pt pt>
modm::ResumableResult<void>
Max31865<SpiMaster, Cs, pt>::writeSingleRegister(Register address, uint8_t data)
{
Expand Down

0 comments on commit 422e765

Please sign in to comment.