Choreonoid  1.8
SimulatorItem.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_BODY_PLUGIN_SIMULATOR_ITEM_H
7 #define CNOID_BODY_PLUGIN_SIMULATOR_ITEM_H
8 
9 #include "CollisionSeq.h"
10 #include <cnoid/Item>
11 #include <cnoid/EigenTypes>
12 #include <vector>
13 #include <memory>
14 #include "exportdecl.h"
15 
16 namespace cnoid {
17 
18 class Body;
19 class Link;
20 class Device;
21 class CollisionDetector;
22 class WorldItem;
23 class BodyItem;
24 class ControllerItem;
25 class SimulatorItem;
26 class SimulatedMotionEngineManager;
27 class CloneMap;
28 
29 class CNOID_EXPORT SimulationBody : public Referenced
30 {
31 public:
32  SimulationBody(Body* body);
33  virtual ~SimulationBody();
34 
35  BodyItem* bodyItem() const;
36  Body* body() const;
37 
38  int numControllers() const;
39  ControllerItem* controller(int index = 0) const;
40 
44  void cloneShapesOnce();
45 
46  virtual bool initialize(SimulatorItem* simulatorItem, BodyItem* bodyItem);
47 
48  const std::string& recordItemPrefix() const;
49  virtual void initializeRecordBuffers();
50  virtual void initializeRecordItems();
51 
55  bool isActive() const;
56  void setActive(bool on);
57 
62  void notifyUnrecordedDeviceStateChange(Device* device);
63 
67  virtual void bufferRecords();
68  virtual void flushRecords();
69 
70  class Impl;
71 
72 private:
73  Impl* impl;
74 
75  friend class SimulatorItem;
76 };
77 
79 
80 
81 class CNOID_EXPORT SimulatorItem : public Item
82 {
83 public:
84  static void initializeClass(ExtensionManager* ext);
85 
86  static SimulatorItem* findActiveSimulatorItemFor(Item* item);
87 
88  virtual ~SimulatorItem();
89 
90  WorldItem* worldItem();
91  virtual double worldTimeStep();
92  void setTimeStep(double step);
93 
94  bool startSimulation(bool doReset = true);
95  void stopSimulation(bool isForced = false);
96  void pauseSimulation();
97  void restartSimulation();
98  bool isRunning() const;
99  bool isPausing() const;
100  bool isActive() const;
101 
103  int currentFrame() const;
104 
106  double currentTime() const;
107 
109  int simulationFrame() const;
110 
112  double simulationTime() const;
113 
114  SignalProxy<void()> sigSimulationStarted();
115  SignalProxy<void()> sigSimulationPaused();
116  SignalProxy<void()> sigSimulationResumed();
117  SignalProxy<void(bool isForced)> sigSimulationFinished();
118 
124 
125  // Deprecated
126  REC_FULL = FullRecording,
127  REC_TAIL = TailRecording,
128  REC_NONE = NoRecording,
129  N_RECORDING_MODES = NumRecordingModes
130  };
131 
132  void setRecordingMode(int selection);
133  int recordingMode() const;
134 
140 
141  // Deprecated. Use setActiveControlTimeRangeMode.
143 
144  // Deprecated
145  TR_UNLIMITED = UnlimitedTime,
146  TR_ACTIVE_CONTROL = ActiveControlTime,
147  TR_SPECIFIED = SpecifiedTime,
148  TR_TIMEBAR = TimeBarTime,
149  N_TIME_RANGE_MODES = NumTimeRangeModes
150  };
151 
152  void setTimeRangeMode(int selection);
153 
154  void setTimeLength(double length);
155 
156  [[deprecated("Use setTimeLength")]]
157  void setSpecifiedRecordingTimeLength(double length){
158  setTimeLength(length);
159  }
160 
161  void setActiveControlTimeRangeMode(bool on);
162  bool isActiveControlTimeRangeMode() const;
163 
164  void setRealtimeSyncMode(bool on);
165 
166  void setDeviceStateOutputEnabled(bool on);
167 
168  bool isRecordingEnabled() const;
169  bool isDeviceStateOutputEnabled() const;
170 
171  bool isAllLinkPositionOutputMode();
172  virtual void setAllLinkPositionOutputMode(bool on);
173 
174  const std::string& controllerOptionString() const;
175 
179  const std::vector<SimulationBody*>& simulationBodies();
180 
181  SimulationBody* findSimulationBody(BodyItem* bodyItem);
182  SimulationBody* findSimulationBody(const std::string& name);
183 
187  int addPreDynamicsFunction(std::function<void()> func);
188  int addMidDynamicsFunction(std::function<void()> func);
189  int addPostDynamicsFunction(std::function<void()> func);
190 
191  void removePreDynamicsFunction(int id);
192  void removeMidDynamicsFunction(int id);
193  void removePostDynamicsFunction(int id);
194 
195  //void addRecordFunction(std::function<void()> func);
196 
197  CloneMap& cloneMap();
198 
202  SignalProxy<void(const std::vector<SimulationBodyPtr>& simulationBodies)>
203  sigSimulationBodyListUpdated();
204 
208  virtual Vector3 getGravity() const;
209 
214  virtual void setExternalForce(BodyItem* bodyItem, Link* link, const Vector3& point, const Vector3& f, double time = 0.0);
215  virtual void clearExternalForces();
216 
220  virtual void setVirtualElasticString(
221  BodyItem* bodyItem, Link* link, const Vector3& attachmentPoint, const Vector3& endPoint);
222  virtual void clearVirtualElasticStrings();
223 
224  virtual void setForcedPosition(BodyItem* bodyItem, const Isometry3& T);
225  virtual bool isForcedPositionActiveFor(BodyItem* bodyItem) const;
226  virtual void clearForcedPositions();
227 
228  class Impl;
229 
230 protected:
231  SimulatorItem();
232  SimulatorItem(const std::string& name);
233  SimulatorItem(const SimulatorItem& org);
234 
235  virtual void onTreePathChanged() override;
236  virtual void onDisconnectedFromRoot() override;
237 
238  virtual void clearSimulation();
239 
245  virtual SimulationBody* createSimulationBody(Body* orgBody, CloneMap& cloneMap);
246 
248  virtual SimulationBody* createSimulationBody(Body* orgBody);
249 
250  CollisionDetector* getOrCreateCollisionDetector();
251 
253  CollisionDetector* collisionDetector();
254 
259  virtual bool initializeSimulation(const std::vector<SimulationBody*>& simBodies) = 0;
260 
266  virtual bool completeInitializationOfSimulation();
267 
268  virtual void initializeSimulationThread();
269 
270  virtual void finalizeSimulationThread();
271 
276  virtual bool stepSimulation(const std::vector<SimulationBody*>& activeSimBodies) = 0;
277 
281  virtual void finalizeSimulation();
282 
283  virtual std::shared_ptr<CollisionLinkPairList> getCollisions();
284 
285  virtual void doPutProperties(PutPropertyFunction& putProperty) override;
286  virtual bool store(Archive& archive) override;
287  virtual bool restore(const Archive& archive) override;
288 
289 private:
290  Impl* impl;
291 
292  friend class SimulationBody;
293  friend class SimulatedMotionEngineManager;
294 };
295 
297 
298 }
299 
300 #endif
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
cnoid::ExtensionManager
Definition: ExtensionManager.h:23
cnoid::PutPropertyFunction
Definition: PutPropertyFunction.h:51
cnoid::Isometry3
Eigen::Isometry3d Isometry3
Definition: EigenTypes.h:73
cnoid::SimulatorItem::SpecifiedTime
@ SpecifiedTime
Definition: SimulatorItem.h:137
cnoid::Archive
Definition: Archive.h:22
cnoid::ControllerItem
Definition: ControllerItem.h:20
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::BodyItem
Definition: BodyItem.h:28
cnoid::SimulatorItem::TimeBarTime
@ TimeBarTime
Definition: SimulatorItem.h:138
cnoid::SimulationBody
Definition: SimulatorItem.h:29
cnoid::SimulatorItem::setSpecifiedRecordingTimeLength
void setSpecifiedRecordingTimeLength(double length)
Definition: SimulatorItem.h:157
cnoid::SimulationBodyPtr
ref_ptr< SimulationBody > SimulationBodyPtr
Definition: SimulatorItem.h:78
cnoid::WorldItem
Definition: WorldItem.h:21
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::CloneMap
Definition: CloneMap.h:13
cnoid::SimulatorItemPtr
ref_ptr< SimulatorItem > SimulatorItemPtr
Definition: SimulatorItem.h:296
cnoid::SimulatorItem::TimeRangeMode
TimeRangeMode
Definition: SimulatorItem.h:135
cnoid::CollisionDetector
Definition: CollisionDetector.h:20
cnoid::SimulatorItem::UnlimitedTime
@ UnlimitedTime
Definition: SimulatorItem.h:136
cnoid::Referenced
Definition: Referenced.h:54
cnoid::SimulatorItem::RecordingMode
RecordingMode
Definition: SimulatorItem.h:119
cnoid::SimulatorItem::NoRecording
@ NoRecording
Definition: SimulatorItem.h:122
cnoid::Body
Definition: Body.h:28
CollisionSeq.h
cnoid::Device
Definition: Device.h:53
cnoid::SimulatorItem::TailRecording
@ TailRecording
Definition: SimulatorItem.h:121
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::SimulatorItem::FullRecording
@ FullRecording
Definition: SimulatorItem.h:120
cnoid::SimulatorItem::NumTimeRangeModes
@ NumTimeRangeModes
Definition: SimulatorItem.h:139
cnoid::SimulatorItem::NumRecordingModes
@ NumRecordingModes
Definition: SimulatorItem.h:123
cnoid::SimulatorItem
Definition: SimulatorItem.h:81
cnoid::SimulatorItem::ActiveControlTime
@ ActiveControlTime
Definition: SimulatorItem.h:142