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 };
56 
58 template <class VEH_DYNAMICS>
60 {
61  public:
62  using Ptr = std::shared_ptr<ControllerBaseTempl<VEH_DYNAMICS>>;
63 
64  ControllerBaseTempl(VEH_DYNAMICS& veh) : veh_(veh) {}
65  virtual ~ControllerBaseTempl() {}
67  virtual void teleop_interface(const TeleopInput& in, TeleopOutput& out) override
68  {
69  using namespace std::string_literals;
70 
71  /*default: handle logging events*/
72  static bool isRecording = false;
73  switch (in.keycode)
74  {
75  case 'l':
76  case 'L':
77  {
78  isRecording = !isRecording;
79  setLogRecording(isRecording);
80  }
81  break;
82  case 'c':
83  case 'C':
84  {
85  clearLogs();
86  }
87  break;
88 
89  case 'n':
90  case 'N':
91  {
92  newLogSession();
93  }
94  break;
95 
96  default:
97  break;
98  }
99 
100  out.append_gui_lines += "\nToggle logging [L]. Clear logs[C]. New session [N].\n";
101  out.append_gui_lines += "Now logging: "s + (isRecording ? "YES\n"s : "NO\n"s);
102  }
103 
106  virtual void control_step(
107  const typename VEH_DYNAMICS::TControllerInput& ci,
108  typename VEH_DYNAMICS::TControllerOutput& co) = 0;
109 
110  virtual void on_post_step([[maybe_unused]] const TSimulContext& context) {}
111 
113  virtual void load_config( //
114  [[maybe_unused]] const rapidxml::xml_node<char>& node)
115  { /*default: do nothing*/
116  }
117 
118  virtual void setLogRecording(bool recording) { veh_.setRecording(recording); }
119  virtual void clearLogs() { veh_.clearLogs(); }
120  virtual void newLogSession() { veh_.newLogSession(); }
121 
122  protected:
123  VEH_DYNAMICS& veh_;
124 };
125 } // namespace mvsim
Definition: ControllerBase.h:23
virtual bool setTwistCommand([[maybe_unused]] const mrpt::math::TTwist2D &t)
Definition: ControllerBase.h:51
Definition: ControllerBase.h:60
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:67
virtual void load_config([[maybe_unused]] const rapidxml::xml_node< char > &node)
Definition: ControllerBase.h:113
Definition: ControllerBase.h:26
Definition: ControllerBase.h:34
Definition: basic_types.h:58