ControllerBase.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 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 namespace mvsim
15 {
20 {
21  public:
22  struct TeleopInput
23  {
24  int keycode;
25  TeleopInput() : keycode(0) {}
26  };
27  struct TeleopOutput
28  {
29  std::string append_gui_lines;
30  };
31 
32  virtual void teleop_interface(
33  [[maybe_unused]] const TeleopInput& in,
34  [[maybe_unused]] TeleopOutput& out)
35  {
36  /*default: do nothing*/
37  }
38 
46  virtual bool setTwistCommand([[maybe_unused]] const mrpt::math::TTwist2D& t)
47  {
48  return false; /* default: no */
49  }
50 };
51 
53 template <class VEH_DYNAMICS>
55 {
56  public:
57  using Ptr = std::shared_ptr<ControllerBaseTempl<VEH_DYNAMICS>>;
58 
59  ControllerBaseTempl(VEH_DYNAMICS& veh) : veh_(veh) {}
60  virtual ~ControllerBaseTempl() {}
62  virtual void teleop_interface(
63  const TeleopInput& in, TeleopOutput& out) override
64  {
65  /*default: handle logging events*/
66  static bool isRecording = false;
67  switch (in.keycode)
68  {
69  case 'l':
70  case 'L':
71  {
72  isRecording = !isRecording;
73  setLogRecording(isRecording);
74  }
75  break;
76  case 'c':
77  case 'C':
78  {
79  clearLogs();
80  }
81  break;
82 
83  case 'n':
84  case 'N':
85  {
86  newLogSession();
87  }
88  break;
89 
90  default:
91  break;
92  }
93 
94  out.append_gui_lines +=
95  std::string(
96  "Toggle logging [L]. Clear logs[C]. New log session [N]. "
97  "Now ") +
98  std::string(isRecording ? "logging" : "not logging") +
99  std::string("\n");
100  }
101 
104  virtual void control_step(
105  const typename VEH_DYNAMICS::TControllerInput& ci,
106  typename VEH_DYNAMICS::TControllerOutput& co) = 0;
107 
108  virtual void on_post_step([[maybe_unused]] const TSimulContext& context) {}
109 
111  virtual void load_config( //
112  [[maybe_unused]] const rapidxml::xml_node<char>& node)
113  { /*default: do nothing*/
114  }
115 
116  virtual void setLogRecording(bool recording)
117  {
118  veh_.setRecording(recording);
119  }
120  virtual void clearLogs() { veh_.clearLogs(); }
121  virtual void newLogSession() { veh_.newLogSession(); }
122 
123  protected:
124  VEH_DYNAMICS& veh_;
125 };
126 } // namespace mvsim
virtual bool setTwistCommand([[maybe_unused]] const mrpt::math::TTwist2D &t)
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out) override
virtual void load_config([[maybe_unused]] const rapidxml::xml_node< char > &node)
std::shared_ptr< ControllerBaseTempl< VEH_DYNAMICS > > Ptr
virtual void setLogRecording(bool recording)
ControllerBaseTempl(VEH_DYNAMICS &veh)
virtual void on_post_step([[maybe_unused]] const TSimulContext &context)
virtual void teleop_interface([[maybe_unused]] const TeleopInput &in, [[maybe_unused]] TeleopOutput &out)


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19