Skip to content

Commit ee43564

Browse files
committed
Geometric simu uses ROS parameter /sot/dt
1 parent e57af6b commit ee43564

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

include/dynamic_graph_bridge/sot_loader.hh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <boost/program_options.hpp>
3535
#include <boost/foreach.hpp>
3636
#include <boost/format.hpp>
37+
#include <boost/thread/thread.hpp>
3738

3839
// ROS includes
3940
#include "ros/ros.h"
@@ -82,6 +83,9 @@ protected:
8283
std::string robot_desc_string_;
8384

8485

86+
/// The thread running dynamic graph
87+
boost::thread thread_;
88+
8589
// \brief Start control loop
8690
virtual void startControlLoop();
8791

@@ -92,7 +96,7 @@ protected:
9296

9397
public:
9498
SotLoader();
95-
~SotLoader() {};
99+
~SotLoader();
96100

97101
// \brief Create a thread for ROS and start the control loop.
98102
void initializeRosNode(int argc, char *argv[]);

src/geometric_simu.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ int main(int argc, char *argv[])
3232

3333
aSotLoader.initializeRosNode(argc,argv);
3434

35-
while(true){
36-
usleep(5000);
37-
}
38-
35+
ros::spin();
36+
return 0;
3937
}

src/sot_loader.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
// POSIX.1-2001
2727
#include <dlfcn.h>
2828

29-
#include <boost/thread/thread.hpp>
3029
#include <boost/thread/condition.hpp>
3130

3231
boost::condition_variable cond;
@@ -39,15 +38,30 @@ namespace po = boost::program_options;
3938

4039
void workThreadLoader(SotLoader *aSotLoader)
4140
{
41+
unsigned period = 1000; // micro seconds
42+
if (ros::param::has("/sot/dt")) {
43+
double periodd;
44+
ros::param::get("/sot/dt", periodd);
45+
period = unsigned(1e6 * periodd);
46+
}
47+
DataToLog dataToLog (5000);
48+
4249
while(aSotLoader->isDynamicGraphStopped())
4350
{
44-
usleep(5000);
51+
usleep(period);
4552
}
4653

54+
struct timeval start, stop;
4755
while(!aSotLoader->isDynamicGraphStopped())
4856
{
57+
gettimeofday(&start,0);
4958
aSotLoader->oneIteration();
50-
usleep(5000);
59+
gettimeofday(&stop,0);
60+
61+
unsigned long long dt = 1000000 * (stop.tv_sec - start.tv_sec) + (stop.tv_usec - start.tv_usec);
62+
if (period > dt) {
63+
usleep(period - (unsigned)dt);
64+
}
5165
}
5266
cond.notify_all();
5367
ros::waitForShutdown();
@@ -62,15 +76,22 @@ SotLoader::SotLoader():
6276
torques_ (),
6377
baseAtt_ (),
6478
accelerometer_ (3),
65-
gyrometer_ (3)
79+
gyrometer_ (3),
80+
thread_ ()
6681
{
6782
readSotVectorStateParam();
6883
initPublication();
6984
}
7085

86+
SotLoader::~SotLoader()
87+
{
88+
dynamic_graph_stopped_ = true;
89+
thread_.join();
90+
}
91+
7192
void SotLoader::startControlLoop()
7293
{
73-
boost::thread thr(workThreadLoader, this);
94+
thread_ = boost::thread (workThreadLoader, this);
7495
}
7596

7697
void SotLoader::initializeRosNode(int argc, char *argv[])

0 commit comments

Comments
 (0)