TAMSVIZ
Visualization and annotation tool for ROS
robot.h
1 // TAMSVIZ
2 // (c) 2020 Philipp Ruppel
3 
4 #pragma once
5 
6 #include "../core/document.h"
7 #include "../core/loader.h"
8 #include "../core/timeseries.h"
9 #include "../core/topic.h"
10 #include "../core/watcher.h"
11 
12 #include "../render/renderer.h"
13 #include "../render/resource.h"
14 
15 #include "frame.h"
16 #include "mesh.h"
17 
18 #include <moveit_msgs/DisplayRobotState.h>
19 #include <moveit_msgs/DisplayTrajectory.h>
20 #include <sensor_msgs/JointState.h>
21 
22 #include <condition_variable>
23 #include <mutex>
24 
25 class RobotModel;
26 class RobotState;
27 
29  DECLARE_STRUCT_PROPERTY(bool, recomputeNormals, false);
30  DECLARE_STRUCT_PROPERTY(bool, smoothNormals, false);
31 };
32 bool operator==(const RobotModelImportOptions &a,
33  const RobotModelImportOptions &b) {
34  return (a.recomputeNormals() == b.recomputeNormals()) &&
35  (a.smoothNormals() == b.smoothNormals());
36 }
37 bool operator<(const RobotModelImportOptions &a,
38  const RobotModelImportOptions &b) {
39  return std::make_tuple(a.recomputeNormals() == a.smoothNormals()) <
40  std::make_tuple(b.recomputeNormals() == b.smoothNormals());
41 }
42 bool operator!=(const RobotModelImportOptions &a,
43  const RobotModelImportOptions &b) {
44  return !(a == b);
45 }
46 STRUCT_BEGIN(RobotModelImportOptions);
47 STRUCT_PROPERTY(recomputeNormals);
48 STRUCT_PROPERTY(smoothNormals);
49 STRUCT_END();
50 
52 protected:
53  std::shared_ptr<Loader<RobotModel>> _robot_model_loader;
54  std::shared_ptr<RobotState> _robot_state;
55  Eigen::Isometry3d pose_temp = Eigen::Isometry3d::Identity();
56  EventFlag _invalidated{ResourceEvents::instance().reload};
57  Watcher _watcher;
58  std::shared_ptr<MaterialOverride> _material_override =
59  std::make_shared<MaterialOverride>();
60  RobotDisplayBase() {}
61 
62 public:
63  virtual void renderSync(const RenderSyncContext &context) override;
64  virtual void renderAsync(const RenderAsyncContext &context) override;
65  PROPERTY(std::shared_ptr<MaterialOverride>, materialOverride,
66  _material_override);
67  PROPERTY(std::string, description, "/robot_description");
68  PROPERTY(RobotModelImportOptions, importOptions);
69  PROPERTY(bool, doubleSided, false);
70 };
71 DECLARE_TYPE_C(RobotDisplayBase, MeshDisplayBase, Robot);
72 
74 
75 class RobotStateDisplayBase : public GenericFrameDisplay<RobotDisplayBase> {
76  std::shared_ptr<TimeSeriesSubscriber> _subscriber;
77  std::shared_ptr<RobotStateTimeSeriesListener> _listener;
78 
79 protected:
80  virtual void refreshTopic(const std::string &topic);
82 
83 public:
84  virtual void renderSync(const RenderSyncContext &context) override;
85 };
86 DECLARE_TYPE_C(RobotStateDisplayBase, RobotDisplayBase, Robot);
87 
89 
90 public:
91  PROPERTY(TopicProperty<sensor_msgs::JointState>, topic, "/joint_states");
92  virtual void refresh() override {
93  refreshTopic(topic().topic());
94  RobotStateDisplayBase::refresh();
95  }
96 };
97 DECLARE_TYPE_C(RobotStateDisplay, RobotStateDisplayBase, Robot);
98 
99 /*
100 class DisplayRobotStateDisplay : public RobotStateDisplayBase {
101 
102 public:
103  PROPERTY(TopicProperty<moveit_msgs::DisplayRobotState>, topic,
104  "/joint_states");
105  virtual void refresh() override {
106  refreshTopic(topic().topic());
107  RobotStateDisplayBase::refresh();
108  }
109 };
110 DECLARE_TYPE(DisplayRobotStateDisplay, RobotStateDisplayBase);
111 */
112 
113 class DisplayRobotStateDisplay : public GenericFrameDisplay<RobotDisplayBase> {
114  // std::shared_ptr<const moveit_msgs::DisplayRobotState>
115  // _display_robot_state_message;
116 
117 public:
119  "/display_robot_state");
120  virtual void renderSync(const RenderSyncContext &context) override;
121  virtual void renderAsync(const RenderAsyncContext &context) override;
122 };
123 DECLARE_TYPE_C(DisplayRobotStateDisplay, RobotDisplayBase, Robot);
124 
125 /*
126 class RobotTrajectoryDisplay : public GenericFrameDisplay<RobotDisplayBase> {
127  Watcher _trajectory_watcher;
128  size_t _frame_index = 0;
129 
130 public:
131  PROPERTY(TopicProperty<moveit_msgs::DisplayTrajectory>, topic,
132  "/move_group/display_planned_path");
133  virtual void renderSync(const RenderSyncContext &context) override;
134 };
135 DECLARE_TYPE(RobotTrajectoryDisplay, RobotDisplayBase);
136 */
137 
138 class RobotTrajectoryDisplay : public GenericFrameDisplay<RobotDisplayBase> {
139  Watcher _trajectory_watcher;
140  std::vector<std::pair<double, std::shared_ptr<RobotState>>> _trajectory;
141  std::shared_ptr<const moveit_msgs::DisplayTrajectory>
142  _display_trajectory_message;
143  int _max_steps = 10;
144  bool _show_all = false;
145  double _frame_time = 0;
146  bool _update_show_all = false;
147  std::thread _update_thread;
148  std::condition_variable _update_condition;
149  std::mutex _update_mutex;
150  std::vector<double> _update_times;
151  volatile size_t _frame_update = 0;
152  volatile bool _update_exit = false;
153  Watcher _update_parameter_watcher;
154  double _update_speed = 1;
155 
156 public:
158  "/move_group/display_planned_path");
159  PROPERTY(int, maxSteps, 10, min = 1);
160  PROPERTY(bool, showAllStates, false);
161  PROPERTY(double, speed, 1, min = 0);
162  // PROPERTY(double, stateDisplayTime, 0.1);
163  virtual void renderSync(const RenderSyncContext &context) override;
164  virtual void renderAsync(const RenderAsyncContext &context) override;
167 };
168 DECLARE_TYPE_C(RobotTrajectoryDisplay, RobotDisplayBase, Robot);
169 
170 class Transformer;
172  std::shared_ptr<Transformer> _transformer;
173 
174 public:
175  virtual void renderSync(const RenderSyncContext &context) override;
176  virtual void renderAsync(const RenderAsyncContext &context) override;
177 };
178 DECLARE_TYPE_C(RobotModelDisplay, RobotDisplayBase, Robot);
Definition: watcher.h:8