RcsPySim
A robot control and simulation library
ViewerComponent.cpp
Go to the documentation of this file.
1 #include "ViewerComponent.h"
2 
3 #include <RcsViewer.h>
4 #include <GraphNode.h>
5 #include <FTSensorNode.h>
6 #include <HUD.h>
7 #include <Rcs_typedef.h>
8 #include <Rcs_macros.h>
9 #include <Rcs_timer.h>
10 
11 
12 namespace Rcs
13 {
14 
15 class LockedFTS : public Rcs::FTSensorNode
16 {
17 public:
18 
19  LockedFTS(const RcsSensor* fts, pthread_mutex_t* mtx) : FTSensorNode(fts), graphMtx(mtx)
20  {
21  }
22 
24  {
25  pthread_mutex_lock(graphMtx);
26  FTSensorNode::frameCallback();
27  pthread_mutex_unlock(graphMtx);
28 
29  return false;
30  }
31 
32  pthread_mutex_t* graphMtx;
33 };
34 
35 
36 class ComponentViewer : public Rcs::Viewer
37 {
38 public:
39 
40  ComponentViewer(const RcsGraph* desired, const RcsGraph* current) :
41  Rcs::Viewer(), gDes(NULL), gCurr(NULL), hud(NULL)
42  {
43  pthread_mutex_init(&this->graphMtx, NULL);
44 
45  if (desired != NULL) {
46  this->gDes = RcsGraph_clone(desired);
47 
48  this->gnDes = new Rcs::GraphNode(this->gDes);
49  gnDes->setGhostMode(true, "RED");
50  add(gnDes.get());
51  }
52 
53  if (current != NULL) {
54  this->gCurr = RcsGraph_clone(current);
55  this->gnCurr = new Rcs::GraphNode(this->gCurr);
56  add(this->gnCurr);
57 
58  RCSGRAPH_TRAVERSE_SENSORS(this->gCurr) {
59  if (SENSOR->type == RCSSENSOR_LOAD_CELL) {
60  osg::ref_ptr<LockedFTS> ftn = new LockedFTS(SENSOR, &this->graphMtx);
61  add(ftn.get());
62  }
63  }
64  }
65 
66  this->hud = new Rcs::HUD();
67  add(hud.get());
68  }
69 
71  {
72  // Graph destruction checks for NULL pointers
73  RcsGraph_destroy(this->gDes);
74  RcsGraph_destroy(this->gCurr);
75 
76  pthread_mutex_destroy(&this->graphMtx);
77  }
78 
79  void frame()
80  {
81 
82  if (isInitialized() == false) {
83  init();
84  }
85 
86  pthread_mutex_lock(&this->graphMtx);
87 
88  if (this->gDes != NULL) {
89  RcsGraph_setState(this->gDes, NULL, NULL);
90  }
91 
92  if (this->gCurr != NULL) {
93  RcsGraph_setState(this->gCurr, NULL, NULL);
94  }
95 
96  pthread_mutex_unlock(&this->graphMtx);
97 
98  double dtFrame = Timer_getSystemTime();
99 
100  // Publish all queued events before the frame() call
101  userEventMtx.lock();
102  for (size_t i = 0; i < userEventStack.size(); ++i) {
103  getOsgViewer()->getEventQueue()->userEvent(userEventStack[i].get());
104  }
105  userEventStack.clear();
106  userEventMtx.unlock();
107 
108  lock();
109  viewer->frame();
110  unlock();
111 
112  dtFrame = Timer_getSystemTime() - dtFrame;
113  this->fps = 0.9*this->fps + 0.1*(1.0/dtFrame);
114  }
115 
116  void setText(const std::string& text)
117  {
118  lock();
119  hud->setText(text);
120  unlock();
121  }
122 
123  RcsGraph* gDes;
124  RcsGraph* gCurr;
125  osg::ref_ptr<HUD> hud;
126  osg::ref_ptr<Rcs::GraphNode> gnDes;
127  osg::ref_ptr<Rcs::GraphNode> gnCurr;
128  pthread_mutex_t graphMtx;
129 };
130 }
131 
132 /*******************************************************************************
133  *
134  ******************************************************************************/
136  RcsGraph* graphCurr,
137  bool syncWithEventLoop_) :
138  Rcs::SensorComponent(), desiredGraph(NULL), currentGraph(graphCurr),
139  viewer(NULL), kc(NULL), syncWithEventLoop(syncWithEventLoop_)
140 {
141  init();
142 }
143 
144 /*******************************************************************************
145  *
146  ******************************************************************************/
148  RcsGraph* graphDes, RcsGraph* graphCurr,
149  bool syncWithEventLoop_) :
150  Rcs::SensorComponent(), desiredGraph(graphDes), currentGraph(graphCurr),
151  viewer(NULL), kc(NULL), syncWithEventLoop(syncWithEventLoop_)
152 {
153  init();
154 }
155 
156 /*******************************************************************************
157  *
158  ******************************************************************************/
160 {
162  getKeyCatcher();
163 
164 }
165 
166 /*******************************************************************************
167  *
168  ******************************************************************************/
170 {
171  delete this->viewer;
172 }
173 
174 /*******************************************************************************
175  *
176  ******************************************************************************/
178 {
179 }
180 
181 /*******************************************************************************
182  *
183  ******************************************************************************/
185 {
186  pthread_mutex_lock(&viewer->graphMtx);
187 
188  if (desiredGraph != NULL) {
189  MatNd_copy(viewer->gDes->q, desiredGraph->q);
190  }
191 
192  if (currentGraph != NULL) {
193  MatNd_copy(viewer->gCurr->q, currentGraph->q);
194 
195  RcsSensor* src = currentGraph->sensor;
196  RcsSensor* dst = viewer->gCurr->sensor;
197 
198  while (src != NULL) {
199  if (src->type == RCSSENSOR_LOAD_CELL) {
200  MatNd_copy(dst->rawData, src->rawData);
201  }
202  src = src->next;
203  dst = dst->next;
204  }
205 
206  }
207 
208  pthread_mutex_unlock(&viewer->graphMtx);
209 
210  if (this->syncWithEventLoop == true) {
211  viewer->frame();
212  }
213 }
214 
215 /*******************************************************************************
216  *
217  ******************************************************************************/
218 const char* Rcs::ViewerComponent::getName() const
219 {
220  return "ViewerComponent";
221 }
222 
223 /*******************************************************************************
224  *
225  ******************************************************************************/
227 {
228  return 1.0/viewer->updateFrequency();
229 }
230 
231 /*******************************************************************************
232  *
233  ******************************************************************************/
234 void Rcs::ViewerComponent::setText(const std::string& text)
235 {
236  viewer->setText(text);
237 }
238 
239 /*******************************************************************************
240  *
241  ******************************************************************************/
243 {
244  if (this->kc == NULL) {
245  this->kc = new Rcs::KeyCatcher();
246  viewer->add(kc);
247  }
248 
249  return this->kc;
250 }
251 
252 /*******************************************************************************
253  *
254  ******************************************************************************/
256 {
257  return this->viewer;
258 }
259 
260 /*******************************************************************************
261  *
262  ******************************************************************************/
264 {
265  return viewer->gnDes->getBodyNode(name);
266 }
267 
268 /*******************************************************************************
269  *
270  ******************************************************************************/
272 {
273  return viewer->gnCurr->getBodyNode(name);
274 }
275 
276 /*******************************************************************************
277  *
278  ******************************************************************************/
280 {
281  viewer->lock();
282 }
283 
284 /*******************************************************************************
285  *
286  ******************************************************************************/
288 {
289  viewer->unlock();
290 }
291 
293 {
294  if (this->syncWithEventLoop == false) {
295  viewer->runInThread();
296  }
297  // do nothing
298  return false;
299 }
300 
302 {
303  // do nothing
304  return false;
305 }
osg::ref_ptr< Rcs::GraphNode > gnDes
pthread_mutex_t graphMtx
const RcsGraph * desiredGraph
const char * getName() const
BodyNode * getBodyNodePtrFromDesiredGraph(const char *name)
KeyCatcher * getKeyCatcher()
LockedFTS(const RcsSensor *fts, pthread_mutex_t *mtx)
const RcsGraph * currentGraph
ViewerComponent(RcsGraph *currentGraph, bool syncWithEventLoop=false)
osg::ref_ptr< HUD > hud
void setText(const std::string &text)
osg::ref_ptr< Rcs::GraphNode > gnCurr
pthread_mutex_t * graphMtx
void updateGraph(RcsGraph *graph)
double getCallbackUpdatePeriod() const
void setText(const std::string &text)
ComponentViewer(const RcsGraph *desired, const RcsGraph *current)
BodyNode * getBodyNodePtrFromCurrentGraph(const char *name)
ComponentViewer * viewer