Program Listing for File periodic_waveform.hpp

Return to documentation for file (rcppsw/control/periodic_waveform.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <cmath>

#include "rcppsw/control/base_waveform.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace rcppsw::control {

/*******************************************************************************
 * Classes
 ******************************************************************************/

class sine_waveform final : public base_waveform {
 public:
  explicit sine_waveform(const struct config::waveform_config* const config)
      : base_waveform(config) {}

  double value(double time) override {
    double t = frequency() * time;
    double v = std::sin(2 * M_PI * t + phase().v());
    return amplitude() * v + offset();
  }
};

class square_waveform final : public base_waveform {
 public:
  explicit square_waveform(const struct config::waveform_config* const config)
      : base_waveform(config) {}

  double value(double time) override {
    double t = frequency() * time;
    return amplitude() *
               std::copysign(1.0, std::sin(2 * M_PI * t + phase().v())) +
           offset();
  }
};

class sawtooth_waveform : public base_waveform {
 public:
  explicit sawtooth_waveform(const struct config::waveform_config* const config)
      : base_waveform(config) {}

  double value(double time) override {
    double t = frequency() * time;
    double v = 2.0 * (t - std::floor(t + 0.5) + phase().v());
    return amplitude() * v + offset();
  }
};

} /* namespace rcppsw::control */