Program Listing for File base_controller.hpp

Return to documentation for file (cosm/controller/base_controller.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <ticpp/ticpp.h>

#include <memory>
#include <string>
#include <typeindex>

#include "rcppsw/er/client.hpp"
#include "rcppsw/math/rng.hpp"
#include "rcppsw/types/discretize_ratio.hpp"
#include "rcppsw/types/timestep.hpp"
#include "rcppsw/types/type_uuid.hpp"

#include "cosm/cosm.hpp"
#include "cosm/pal/config/output_config.hpp"
#include "cosm/pal/pal.hpp"
#include "cosm/spatial/common/interference_tracker.hpp"
#include "cosm/spatial/metrics/interference_metrics.hpp"

/*******************************************************************************
 * Namespaces
 ******************************************************************************/
namespace cosm::fsm {
class supervisor_fsm;
} /* namespace cosm::fsm */

namespace cosm::kin {
class metrics_proxy;
} /* namespace cosm::kin */

namespace cosm::controller {

namespace fs = std::filesystem;

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
class base_controller : public rer::client<base_controller>,
                        public csmetrics::interference_metrics {
 public:
  base_controller(void) RCPPSW_COLD;
  ~base_controller(void) override RCPPSW_COLD;

  base_controller(const base_controller&) = delete;
  base_controller& operator=(const base_controller&) = delete;

  virtual void init(ticpp::Element&) RCPPSW_COLD = 0;

  virtual void reset(void) RCPPSW_COLD = 0;

  virtual void control_step(void) = 0;

  virtual rtypes::type_uuid entity_id(void) const = 0;

  virtual void sensing_update(const rtypes::timestep& tick,
                              const rtypes::discretize_ratio& ratio) = 0;

  virtual void sensing_update(const rtypes::timestep& tick) = 0;

  virtual void mdc_ts_update(void) const = 0;

  void ndc_uuid_push(void) const {
    ER_NDC_PUSH("[" + cpal::kRobotNamePrefix +
                rcppsw::to_string(entity_id().v()) + "]");
  }
  void ndc_uuid_pop(void) const { ER_NDC_POP(); }

  rmath::rng* rng(void) { return m_rng; }

  rmath::vector3d rpos3D(void) const;
  rmath::vector3z dpos3D(void) const;
  rmath::radians azimuth(void) const;
  rmath::radians zenith(void) const;
  rmath::vector2d rpos2D(void) const;
  rmath::vector2z dpos2D(void) const;

  cfsm::supervisor_fsm* supervisor(void) { return m_supervisor.get(); }
  const cfsm::supervisor_fsm* supervisor(void) const {
    return m_supervisor.get();
  }
  const cspatial::interference_tracker* inta_tracker(void) const {
    return m_inta_tracker.get();
  }
  const ckin::metrics_proxy* kin_proxy(void) const { return m_kin_proxy.get(); }

  const class subsystem::base_saa_subsystem* saa(void) const { return m_saa.get(); }

  class subsystem::base_saa_subsystem* saa(void) {
    return m_saa.get();
  }

 protected:
  void saa(std::unique_ptr<subsystem::base_saa_subsystem> saa);


  cspatial::interference_tracker* inta_tracker(void) {
    return m_inta_tracker.get();
  }

  virtual fs::path output_init(const cpconfig::output_config* config);

  virtual void rng_init(int seed, const std::string& category);

  void supervisor(std::unique_ptr<cfsm::supervisor_fsm> fsm);

  void inta_tracker(std::unique_ptr<cspatial::interference_tracker> inta);

  void kin_proxy(std::unique_ptr<ckin::metrics_proxy> prox);

 private:
  /* clang-format off */
  rmath::rng*                                     m_rng{nullptr};
  std::unique_ptr<cfsm::supervisor_fsm>           m_supervisor{nullptr};
  std::unique_ptr<cspatial::interference_tracker> m_inta_tracker{nullptr};
  std::unique_ptr<ckin::metrics_proxy>            m_kin_proxy{nullptr};
  std::unique_ptr<subsystem::base_saa_subsystem>  m_saa{nullptr};
  /* clang-format on */

 public:
  /* collision metrics */
  RCPPSW_WRAP_DECLDEF_OVERRIDE(exp_interference, *m_inta_tracker, const);
  RCPPSW_WRAP_DECLDEF_OVERRIDE(entered_interference, *m_inta_tracker, const);
  RCPPSW_WRAP_DECLDEF_OVERRIDE(exited_interference, *m_inta_tracker, const);
  RCPPSW_WRAP_DECLDEF_OVERRIDE(interference_duration, *m_inta_tracker, const);
  RCPPSW_WRAP_DECLDEF_OVERRIDE(interference_loc3D, *m_inta_tracker, const);
};

} /* namespace cosm::controller */