MVSim
Lightweight simulator for 2.5D vehicles and robots
ControllerBase.h
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2026 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mvsim/basic_types.h>
13 
14 #include <optional>
15 
16 namespace mvsim
17 {
23 {
24  public:
25  struct TeleopInput
26  {
27  int keycode = 0;
28  std::optional<TJoyStickEvent> js;
29 
30  TeleopInput() = default;
31  };
32 
33  struct TeleopOutput
34  {
35  std::string append_gui_lines;
36  };
37 
38  virtual void teleop_interface(
39  [[maybe_unused]] const TeleopInput& in, [[maybe_unused]] TeleopOutput& out)
40  {
41  /*default: do nothing*/
42  }
43 
51  virtual bool setTwistCommand([[maybe_unused]] const mrpt::math::TTwist2D& t)
52  {
53  return false; /* default: no */
54  }
55 
67  [[maybe_unused]] std::vector<mrpt::math::TPoint2D>& pts,
68  [[maybe_unused]] double& height) const
69  {
70  return false; /* default: no trajectory data */
71  }
72 };
73 
75 template <class VEH_DYNAMICS>
77 {
78  public:
79  using Ptr = std::shared_ptr<ControllerBaseTempl<VEH_DYNAMICS>>;
80 
81  ControllerBaseTempl(VEH_DYNAMICS& veh) : veh_(veh) {}
82  virtual ~ControllerBaseTempl() {}
84  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override
85  {
86  using namespace std::string_literals;
87 
88  /*default: handle logging events*/
89  static bool isRecording = false;
90  switch (in.keycode)
91  {
92  case 'l':
93  case 'L':
94  {
95  isRecording = !isRecording;
96  setLogRecording(isRecording);
97  }
98  break;
99  case 'c':
100  case 'C':
101  {
102  clearLogs();
103  }
104  break;
105 
106  case 'n':
107  case 'N':
108  {
109  newLogSession();
110  }
111  break;
112 
113  default:
114  break;
115  }
116 
117  out.append_gui_lines += "\nToggle logging [L]. Clear logs[C]. New session [N].\n";
118  out.append_gui_lines += "Now logging: "s + (isRecording ? "YES\n"s : "NO\n"s);
119  }
120 
123  virtual void control_step(
124  const typename VEH_DYNAMICS::TControllerInput& ci,
125  typename VEH_DYNAMICS::TControllerOutput& co) = 0;
126 
127  virtual void on_post_step([[maybe_unused]] const TSimulContext& context) {}
128 
130  virtual void load_config( //
131  [[maybe_unused]] const rapidxml::xml_node<char>& node)
132  { /*default: do nothing*/
133  }
134 
135  virtual void setLogRecording(bool recording) { veh_.setRecording(recording); }
136  virtual void clearLogs() { veh_.clearLogs(); }
137  virtual void newLogSession() { veh_.newLogSession(); }
138 
139  protected:
140  VEH_DYNAMICS& veh_;
141 };
142 } // namespace mvsim
Definition: ControllerBase.h:23
virtual bool setTwistCommand([[maybe_unused]] const mrpt::math::TTwist2D &t)
Definition: ControllerBase.h:51
virtual bool getTrajectoryPlotPoints([[maybe_unused]] std::vector< mrpt::math::TPoint2D > &pts, [[maybe_unused]] double &height) const
Definition: ControllerBase.h:66
Definition: ControllerBase.h:77
virtual void control_step(const typename VEH_DYNAMICS::TControllerInput &ci, typename VEH_DYNAMICS::TControllerOutput &co)=0
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
Definition: ControllerBase.h:84
virtual void load_config([[maybe_unused]] const rapidxml::xml_node< char > &node)
Definition: ControllerBase.h:130
Definition: ControllerBase.h:26
Definition: ControllerBase.h:34
Definition: basic_types.h:58