Skip to content
Snippets Groups Projects
SpiderFurnace.hpp 2.6 KiB
Newer Older
fxk8y's avatar
fxk8y committed
#pragma once

// C++ system level
#include <vector>
// #include <cstring>     // memset, strncmp
// #include <cstdlib>     // TODO: is this for memcpy?
// #include <functional>

// ESP32 specific
#include "esp_log.h"
fxk8y's avatar
fxk8y committed
#include "driver/ledc.h"
fxk8y's avatar
fxk8y committed

// project specific
#include <SpiderLib.hpp>
#include "Service.hpp"
#include "../Hardware/MAX31856.hpp"

// qthing stuff
#include "SiliconTorch/FxCyanRGB8.hpp"
// #include <qthing>

// misc
#include <nlohmann/json.hpp>


namespace SiliconTorch {

  namespace Service {

    namespace SpiderFurnace {

fxk8y's avatar
fxk8y committed
      constexpr u8  PWM_BITS               =    12;
      constexpr u32 PWM_FREQ               = 10000;     // Hz
      constexpr u8  TICK_FREQ              =    10;     // Hz
      constexpr u8  SlidingWindowLength    =     8;
      constexpr f32 MaxTempChangePerSecond =  1000;     // K  // TODO: formally set to 100 but triggers too often?!
      constexpr f32 MAX_TEMP               =  1320.0f;  // °C
      constexpr f32 MAX_PWM                =     0.5f;  // p%
fxk8y's avatar
fxk8y committed

fxk8y's avatar
fxk8y committed
      // TODO: Make configurable"
      constexpr u8 IO_CS                   =    26;
      constexpr u8 IO_MISO                 =    17;
      constexpr u8 IO_MOSI                 =    16;
      constexpr u8 IO_SCLK                 =    27;
      constexpr u8 IO_HEATER               =    25;
fxk8y's avatar
fxk8y committed
      // enum FurnaceState {
      //   RUNNING,
      //   PAUSED,
      //   STOPPED
      // };




      // TODO: ???
      // Compile-time config
      constexpr bool CFG_EN_PIDTask      = false;  // !!
      constexpr bool CFG_EN_MQTT_PIDvals = true;
fxk8y's avatar
fxk8y committed
      class SpiderFurnace : public ServiceManager::Service, public SpiderLib::HasMQTT {
fxk8y's avatar
fxk8y committed

        public:

          void init();

          void start();

          virtual nlohmann::json getConfigJSON() const;



          bool hasFault() const;

          f32 temperature() const;

fxk8y's avatar
fxk8y committed
          void setTargetTemperature(f32 t);
fxk8y's avatar
fxk8y committed



          // TODO: should we block copy/assignment by default…?
          SpiderFurnace() {};
          SpiderFurnace(const SpiderFurnace&) = delete;
          SpiderFurnace& operator=(SpiderFurnace const&) = delete;

        private:

fxk8y's avatar
fxk8y committed
          f32 kP = 0.0f;
          f32 kI = 0.0f;
          f32 kD = 0.0f;

          f32 targetTemperature = 0.0f;

          // FurnaceState state = RUNNING;

          ledc_channel_t pwmCh;
fxk8y's avatar
fxk8y committed

          MAX31856::SR faultReg;

          u64 tc0age = 0;  // ms
          MAX31856::MAX31856* tc0;
fxk8y's avatar
fxk8y committed
          SlidingWindow<f32>* tc0dat = new SlidingWindow<f32>(SlidingWindowLength);
fxk8y's avatar
fxk8y committed

          SpiderLib::Util::LambdaTask* pidTask = NULL;

fxk8y's avatar
fxk8y committed
          f32 pwmVal = 0.0f;
fxk8y's avatar
fxk8y committed
          void setPWM(f32 value);