Simulable.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 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 <Box2D/Dynamics/b2Body.h>
13 #include <mrpt/math/TPoint2D.h>
14 #include <mrpt/math/TPose3D.h>
15 #include <mrpt/poses/CPose2D.h>
16 #include <mvsim/basic_types.h>
17 
18 #include <shared_mutex>
19 
20 namespace mvsim
21 {
22 class Client;
23 
24 class Simulable
25 {
26  public:
27  using Ptr = std::shared_ptr<Simulable>;
28 
31  virtual void simul_pre_timestep(const TSimulContext& context);
32 
38  virtual void simul_post_timestep(const TSimulContext& context);
39 
40  virtual void poses_mutex_lock() = 0;
41  virtual void poses_mutex_unlock() = 0;
42 
46  virtual void apply_force(
47  const mrpt::math::TVector2D& force,
48  const mrpt::math::TPoint2D& applyPoint = mrpt::math::TPoint2D(0, 0));
49 
52 
56  {
57  m_q_mtx.lock_shared();
58  mrpt::math::TPose3D ret = m_q;
59  m_q_mtx.unlock_shared();
60  return ret;
61  }
62 
64  {
65  m_q_mtx.lock_shared();
66  mrpt::math::TTwist2D ret = m_dq;
67  m_q_mtx.unlock_shared();
68  return ret;
69  }
70 
73  void setPose(const mrpt::math::TPose3D& p) const
74  {
75  m_q_mtx.lock();
76  const_cast<mrpt::math::TPose3D&>(m_q) = p;
77  m_q_mtx.unlock();
78  }
79 
80  void setTwist(const mrpt::math::TTwist2D& dq) const
81  {
82  m_q_mtx.lock();
83  const_cast<mrpt::math::TTwist2D&>(m_dq) = dq;
84  m_q_mtx.unlock();
85  }
86 
89 
92  const mrpt::math::TTwist2D& getVelocity() const { return m_dq; }
93 
95  const std::string& getName() const { return m_name; }
96 
98  void setName(const std::string& s) { m_name = s; }
99 
101  bool isInCollision() const { return m_isInCollision; }
102 
106  bool hadCollision() const { return m_hadCollisionFlag; }
107 
109  void resetCollisionFlag() { m_hadCollisionFlag = false; }
110 
111  virtual void registerOnServer(mvsim::Client& c);
112 
113  const b2Body* b2d_body() const { return m_b2d_body; }
114  b2Body* b2d_body() { return m_b2d_body; }
115 
116  protected:
118  std::string m_name;
119 
125  b2Body* m_b2d_body = nullptr;
126 
127  bool parseSimulable(const rapidxml::xml_node<char>* node);
128 
129  void internalHandlePublish(const TSimulContext& context);
130 
131  private:
133  mutable std::shared_mutex m_q_mtx;
134 
136  mrpt::math::TPose3D m_q = mrpt::math::TPose3D::Identity();
137 
139  mrpt::math::TTwist2D m_dq{0, 0, 0};
140 
142  bool m_isInCollision = false;
143 
146  bool m_hadCollisionFlag = false;
147 
149  std::string publishPoseTopic_;
150  double publishPosePeriod_ = 100e-3;
151  double publishPoseLastTime_ = 0;
152 };
153 } // namespace mvsim
void setTwist(const mrpt::math::TTwist2D &dq) const
Definition: Simulable.h:80
bool isInCollision() const
Definition: Simulable.h:101
void setPose(const mrpt::math::TPose3D &p) const
Definition: Simulable.h:73
std::shared_ptr< Simulable > Ptr
Definition: Simulable.h:27
b2Body * b2d_body()
Definition: Simulable.h:114
const GLfloat * c
mrpt::poses::CPose2D getCPose2D() const
Alternative to getPose()
Definition: Simulable.cpp:93
virtual void apply_force(const mrpt::math::TVector2D &force, const mrpt::math::TPoint2D &applyPoint=mrpt::math::TPoint2D(0, 0))
Definition: Simulable.cpp:78
mrpt::math::TPose3D getPose() const
Definition: Simulable.h:55
GLdouble s
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
const std::string & getName() const
Definition: Simulable.h:95
mrpt::math::TTwist2D getVelocityLocal() const
Definition: Simulable.cpp:84
GLfloat GLfloat p
void setName(const std::string &s)
Definition: Simulable.h:98
virtual void registerOnServer(mvsim::Client &c)
Definition: Simulable.cpp:150
virtual void simul_post_timestep(const TSimulContext &context)
Definition: Simulable.cpp:38
virtual void simul_pre_timestep(const TSimulContext &context)
Definition: Simulable.cpp:25
virtual void poses_mutex_unlock()=0
const mrpt::math::TTwist2D & getVelocity() const
Definition: Simulable.h:92
void resetCollisionFlag()
Definition: Simulable.h:109
virtual void poses_mutex_lock()=0
bool hadCollision() const
Definition: Simulable.h:106
std::string m_name
Definition: Simulable.h:118
const b2Body * b2d_body() const
Definition: Simulable.h:113
mrpt::math::TTwist2D getTwist() const
Definition: Simulable.h:63


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51