SimulatorViewerApp.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2013-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXSimulation::application::SimulatorViewer
19  * @author Nikolaus Vahrenkamp ( vahrenkamp at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "SimulatorViewerApp.h"
26 
29 
31 
32 //SoQT
33 #include <Inventor/Qt/SoQt.h>
34 
35 using namespace armarx;
36 
37 // Redraw with 30Hz
38 #define MAIN_LOOP_SLEEP_MS 33
39 
40 // print warnings if render time is low (ms)
41 #define MAX_RENDER_TIME_WARNING_MS 33
42 
44 {
45 }
46 
48 {
49  ARMARX_VERBOSE_S << "destructor";
50  //SoQt::done();
51  qApplication->quit();
52 }
53 
54 void
56  Ice::PropertiesPtr properties)
57 {
58  // init SoDB and Qt, SoQt::init also creates an application object
59  VirtualRobot::init("SimulatorViewerApp");
60 
61  mutex.reset(new CoinViewer::RecursiveMutex());
62 
63 
64  // create the debugdrawer / entityDrawer component
65  if (getProperty<bool>("UseDebugDrawer").getValue())
66  {
67  std::stringstream ddName;
68  ddName << getName() << "_EntityDrawer";
69  debugDrawer = Component::create<memoryx::EntityDrawerComponent>(properties, ddName.str());
70  debugDrawer->setMutex(mutex);
71  registry->addObject(debugDrawer);
72  }
73 
74  std::stringstream svName;
75  svName << getName() << "_PhysicsWorldVisualization";
76  simVisu = Component::create<ArmarXPhysicsWorldVisualization>(properties, svName.str());
77  //simVisu.reset(new ArmarXPhysicsWorldVisualization(getArmarXManager()));
78  simVisu->setMutex(mutex);
79  registry->addObject(simVisu);
80 
81 
82  // create top-level widget
83  std::stringstream swName;
84  swName << getName() << "_SimulationWindow";
85  mainWindow = Component::create<ArmarXSimulatorWindow>(properties, swName.str());
86  mainWindow->setMutex(mutex);
87  registry->addObject(mainWindow);
88 
89  //mainWindow.reset(new ArmarXSimulatorWindow(registry, simVisu, debugDrawer));
90 }
91 
92 int
94 {
95 
96  mainWindow->initialize(simVisu, debugDrawer);
97  mainWindow->setCamPoseFromConfig();
98  qApplication = QApplication::instance();
99  qApplication->connect(
100  mainWindow.get(), SIGNAL(closeRequest()), this, SLOT(closeRequest_sent()));
101 
102  //int timeoutMs = 30000;
103  //if (!world->getObjectScheduler()->waitForObjectStateMinimum(eManagedIceObjectInitialized, timeoutMs))
104  // ARMARX_WARNING_S << "ManagedIceObject " << world->getName() << " was not initialized after "<< timeoutMs/1000 << " seconds" << std::endl;
105 
106  std::cout << "Started ArmarXSimulatorViewerApp" << std::endl;
107 
108  IceUtil::Time lastWarnTimeRender = IceUtil::Time::now();
109  /*
110  ARMARX_INFO_S << "Waiting for ManagedIceObject startup: " << physicsWorld->getName() << armarx::flush;
111  int timeoutMs = 30000;
112  ARMARX_INFO_S << "Waiting for ManagedIceObject startup: " << physicsWorld->getName() << ":ok" << armarx::flush;
113  */
114  // initialize visualization
115 
116  mainWindow->show();
117 
118 
119 #if 0
120  memoryx::EntityDrawerInterfacePrx prxDD = armarXManager->getIceManager()->getTopic<memoryx::EntityDrawerInterfacePrx>("DebugDrawerUpdates");
121  memoryx::PriorKnowledgeInterfacePrx priorKnowledgePrx = armarXManager->getIceManager()->getProxy<memoryx::PriorKnowledgeInterfacePrx>("PriorKnowledge");
122 
123  std::string objClassName = "vitaliscereal";
124  memoryx::PersistentObjectClassSegmentBasePrx classesSegmentPrx = priorKnowledgePrx->getObjectClassesSegment();
125  memoryx::EntityBasePtr classesEntity = classesSegmentPrx->getEntityByName(objClassName);
126  memoryx::ObjectClassPtr objectClass = memoryx::ObjectClassPtr::dynamicCast(classesEntity);
127 
128 
129  Eigen::Matrix4f p;
130  p.setIdentity();
131  p(2, 3) = 1000.0f;
132  PosePtr gp(new Pose(p));
133  prxDD->setObjectVisu("debug", "vitalis", objectClass, gp);
134 
135  int color = 0;
136 #endif
137  armarx::CycleUtil cycle(
138  IceUtil::Time::secondsDouble(1.0 / getProperty<float>("UpdateRate").getValue()));
139  // loop until ice quit or window closed
140  while (!armarXManager->isShutdown() && !exitApplication)
141  {
142  try
143  {
144 
145 #if 0
146  color = (color + 1) % 1000;
147  DrawColor col;
148  col.r = 1.0f;
149  col.g = 0.0f;
150  col.b = 0.0f;
151  col.a = float(color) / 1000.0f;
152 
153  prxDD->updateObjectColor("debug", "vitalis", col);
154 #endif
155  /*if (prxDD)
156  {
157  //ARMARX_INFO_S << "TESTING DEBUG DRAWER";
158  // just a test
159  Eigen::Matrix4f p;
160  p.setIdentity();
161  p(2,3) = 1000.0f;
162  PosePtr gp(new Pose(p));
163  prxDD->setPoseDebugLayerVisu("testPose",gp);
164 
165  armarx::Vector3Ptr p1(new armarx::Vector3());
166  p1->x = 0;
167  p1->y = 0;
168  p1->z = 0;
169  armarx::Vector3Ptr p2(new armarx::Vector3());
170  p2->x = 1000.0f;
171  p2->y = 1000.0f;
172  p2->z = 1000.0f;
173 
174  armarx::DrawColor c;
175  c.r = 1.0f;
176  c.g = 0;
177  c.b = 0;
178  c.a = 1;
179  prxDD->setLineDebugLayerVisu("test",p1,p2,10.0f,c);
180  }*/
181 
182  // PAINT WINDOW
183  IceUtil::Time startTime = IceUtil::Time::now();
184  {
185  {
186  auto startTime1 = IceUtil::Time::now();
187  auto l = mainWindow->getScopedLock();
188  IceUtil::Time duration1 = IceUtil::Time::now() - startTime1;
189  if (duration1.toMilliSecondsDouble() > 5)
190  {
192  << " mainWindow->getScopedLock() took long:"
193  << duration1.toMilliSecondsDouble();
194  }
195  simVisu->synchronizeVisualizationData();
196  }
197 
198  // ensure engine is locked while we draw the content (there might be requests from ice to update the models)
199  auto startTime1 = IceUtil::Time::now();
200  // allow max 20ms for event processing
201  qApplication->processEvents(QEventLoop::AllEvents);
202  IceUtil::Time duration1 = IceUtil::Time::now() - startTime1;
203  if (duration1.toMilliSecondsDouble() > 20)
204  {
206  << " qApplication->processEvents took long:"
207  << duration1.toMilliSecondsDouble();
208  }
209  }
210  IceUtil::Time duration = IceUtil::Time::now() - startTime;
211  mainWindow->setDrawTimeMeasured((float)duration.toMilliSecondsDouble());
213  << "*** END paint, drawTimeMS:" << duration.toMilliSecondsDouble();
214 
215  if (duration.toMilliSecondsDouble() > MAX_RENDER_TIME_WARNING_MS)
216  {
217  IceUtil::Time lastWarnDuration = IceUtil::Time::now() - lastWarnTimeRender;
218 
219  if (lastWarnDuration.toSecondsDouble() > 4.0)
220  {
221  lastWarnTimeRender = IceUtil::Time::now();
222  ARMARX_DEBUG << "*** Rendering slow !! Render time in ms:"
223  << duration.toMilliSecondsDouble();
224  }
225  }
226 
227  cycle.waitForCycleDuration();
228  }
229  catch (...)
230  {
232  }
233  }
234 
235  ARMARX_IMPORTANT_S << "Leaving main loop";
236 
237  //interruptCallback(0);// signal to the application it should terminate the ice connection
238  armarXManager->shutdown();
239 
240  armarXManager->waitForShutdown();
241 
242  mainWindow = NULL;
243  debugDrawer = NULL;
244  simVisu = NULL; //.reset();
245  qApplication->quit();
246 
247  return 0;
248 }
249 
250 void
252 {
253  ARMARX_IMPORTANT_S << "Interrupting ArmarXSimulatorViewerApp";
254  exitApplication = true;
255 }
256 
257 void
258 SimulatorViewerApp::closeRequest_sent()
259 {
260  ARMARX_INFO_S << "Stopping ArmarXSimulatorViewerApp";
261  exitApplication = true;
262 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
ARMARX_IMPORTANT_S
#define ARMARX_IMPORTANT_S
Definition: Logging.h:210
armarx::SimulatorViewerApp::exitApplication
bool exitApplication
Definition: SimulatorViewerApp.h:120
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::SimulatorViewerApp::mutex
CoinViewer::RecursiveMutexPtr mutex
Definition: SimulatorViewerApp.h:118
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::SimulatorViewerApp::simVisu
ArmarXPhysicsWorldVisualizationPtr simVisu
Definition: SimulatorViewerApp.h:115
Pose.h
armarx::CycleUtil
This util class helps with keeping a cycle time during a control cycle.
Definition: CycleUtil.h:40
armarx::SimulatorViewerApp::qApplication
QCoreApplication * qApplication
Definition: SimulatorViewerApp.h:111
armarx::SimulatorViewerApp::interruptCallback
void interruptCallback(int signal) override
interruptCallback Recieve interrupt callbacks to gracefully shut down simulator
Definition: SimulatorViewerApp.cpp:251
armarx::SimulatorViewerApp::exec
int exec(const ArmarXManagerPtr &armarXManager) override
Runs the Qt Event Loop.
Definition: SimulatorViewerApp.cpp:93
armarx::SimulatorViewerApp::debugDrawer
memoryx::EntityDrawerComponentPtr debugDrawer
Definition: SimulatorViewerApp.h:113
IceInternal::Handle<::Ice::Properties >
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::SimulatorViewerApp::setup
void setup(const ManagedIceObjectRegistryInterfacePtr &registry, Ice::PropertiesPtr properties) override
Definition: SimulatorViewerApp.cpp:55
armarx::SimulatorViewerApp::~SimulatorViewerApp
~SimulatorViewerApp() override
Definition: SimulatorViewerApp.cpp:47
armarx::SimulatorViewerApp::mainWindow
ArmarXSimulatorWindowPtr mainWindow
Definition: SimulatorViewerApp.h:112
ArmarXObjectScheduler.h
armarx::CycleUtil::waitForCycleDuration
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition: CycleUtil.cpp:53
armarx::Application
Baseclass for all ArmarX applications.
Definition: Application.h:193
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::Application::getName
std::string getName() const
Retrieve name of the application.
Definition: Application.cpp:633
CycleUtil.h
SimulatorViewerApp.h
float
#define float
Definition: 16_Level.h:22
IceUtil::Handle< ManagedIceObjectRegistryInterface >
armarx::CoinViewer::RecursiveMutex
std::recursive_mutex RecursiveMutex
Definition: CoinViewer.h:50
MAX_RENDER_TIME_WARNING_MS
#define MAX_RENDER_TIME_WARNING_MS
Definition: SimulatorViewerApp.cpp:41
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:207
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:157
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::SimulatorViewerApp::SimulatorViewerApp
SimulatorViewerApp()
Constructor.
Definition: SimulatorViewerApp.cpp:43