Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
rtt
extras
SimulationThread.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
tag: Peter Soetens Tue Dec 21 22:43:07 CET 2004 SimulationThread.cxx
3
4
SimulationThread.cxx - description
5
-------------------
6
begin : Tue December 21 2004
7
copyright : (C) 2004 Peter Soetens
8
email : peter.soetens@mech.kuleuven.ac.be
9
10
***************************************************************************
11
* This library is free software; you can redistribute it and/or *
12
* modify it under the terms of the GNU General Public *
13
* License as published by the Free Software Foundation; *
14
* version 2 of the License. *
15
* *
16
* As a special exception, you may use this file as part of a free *
17
* software library without restriction. Specifically, if other files *
18
* instantiate templates or use macros or inline functions from this *
19
* file, or you compile this file and link it with other files to *
20
* produce an executable, this file does not by itself cause the *
21
* resulting executable to be covered by the GNU General Public *
22
* License. This exception does not however invalidate any other *
23
* reasons why the executable file might be covered by the GNU General *
24
* Public License. *
25
* *
26
* This library is distributed in the hope that it will be useful, *
27
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
28
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29
* Lesser General Public License for more details. *
30
* *
31
* You should have received a copy of the GNU General Public *
32
* License along with this library; if not, write to the Free Software *
33
* Foundation, Inc., 59 Temple Place, *
34
* Suite 330, Boston, MA 02111-1307 USA *
35
* *
36
***************************************************************************/
37
38
39
40
#include "
SimulationThread.hpp
"
41
#include "../os/TimeService.hpp"
42
#include "
SimulationActivity.hpp
"
43
#include "../Logger.hpp"
44
#include "../os/threads.hpp"
45
#include "../os/MainThread.hpp"
46
47
#include "../os/StartStopManager.hpp"
48
namespace
RTT
{
49
using namespace
extras;
50
namespace
51
{
52
// Stop it before the application quits.
53
void
stopSIMThread()
54
{
55
SimulationThread::Release
();
56
}
57
58
os::CleanupFunction SIMCleanup( &stopSIMThread );
59
}
60
}
61
62
namespace
RTT
{
63
using namespace
extras;
64
using namespace
os;
65
66
// The static class variables
67
SimulationThreadPtr
SimulationThread::_instance
;
68
69
SimulationThreadPtr
SimulationThread::Instance
(
double
period)
70
{
71
if
( !_instance )
72
{
73
_instance.reset(
new
SimulationThread
(period) );
74
}
75
76
return
_instance;
77
}
78
79
bool
SimulationThread::Release
()
80
{
81
_instance.reset();
82
return
true
;
83
}
84
85
86
SimulationThread::SimulationThread
(
double
period)
87
:
TimerThread
( os::
LowestPriority
,
88
"SimulationThread"
,
89
period),
90
beat(
TimeService
::Instance() ), maxsteps_(0), sim_running(false)
91
{
92
Logger::In
in(
"SimulationThread"
);
93
this->
setScheduler
(
ORO_SCHED_OTHER
);
94
Logger::log
() <<
Logger::Info
<< this->
getName
() <<
" created with "
<< this->
getPeriod
() <<
"s periodicity"
;
95
Logger::log
() <<
Logger::Info
<<
" and priority "
<< this->
getPriority
() <<
Logger::endl
;
96
}
97
98
SimulationThread::~SimulationThread
()
99
{
100
this->
stop
();
101
}
102
103
bool
SimulationThread::isRunning
()
const
104
{
105
return
sim_running
||
Thread::isRunning
();
106
}
107
108
bool
SimulationThread::start
()
109
{
110
maxsteps_
= 0;
111
return
os::Thread::start
();
112
}
113
114
bool
SimulationThread::start
(
unsigned
int
maxsteps)
115
{
116
if
(maxsteps == 0)
117
return
false
;
118
maxsteps_
= maxsteps;
119
return
os::Thread::start
();
120
}
121
122
bool
SimulationThread::run
(
unsigned
int
ms)
123
{
124
if
( ms == 0 || this->
isRunning
() || this->
initialize
() ==
false
)
125
return
false
;
126
unsigned
int
cur = 0;
127
this->
sim_running
=
true
;
128
while
( cur != ms ) {
129
++cur;
130
TimerThread::step
();
131
beat
->
secondsChange
(this->
getPeriod
());
132
}
133
this->
sim_running
=
false
;
134
this->
finalize
();
135
return
true
;
136
}
137
os::ThreadInterface
*
SimulationThread::simthread
() {
138
return
os::MainThread::Instance
();
139
}
140
141
bool
SimulationThread::initialize
()
142
{
143
Logger::In
in(
"SimulationThread"
);
144
Logger::log
() <<
Logger::Info
<<
"SimulationThread takes over system time."
<<
Logger::nl
;
145
Logger::log
() <<
Logger::Info
<<
"System time will increase significantly faster."
<<
Logger::endl
;
146
147
// we will update the clock in step()
148
beat
->
enableSystemClock
(
false
);
149
150
cursteps
= 0;
151
// No TimerThread::initialize() to allow 'freeze'
152
return
true
;
153
}
154
155
void
SimulationThread::finalize
()
156
{
157
Logger::In
in(
"SimulationThread"
);
158
Logger::log
() <<
Logger::Info
<<
"SimulationThread releases system time."
<<
Logger::endl
;
159
// release systemclock again.
160
beat
->
enableSystemClock
(
true
);
161
162
// DO NOT CALL TimerThread::finalize(), since we want to be able to start/stop the
163
// SimulationThread and inspect the activities still running.
164
}
165
166
void
SimulationThread::step
()
167
{
168
++
cursteps
;
169
170
if
(
maxsteps_
== 0 ||
cursteps
<
maxsteps_
+ 1 ) {
171
TimerThread::step
();
172
beat
->
secondsChange
(this->
getPeriod
());
173
}
174
175
// call stop once :
176
if
(
cursteps
==
maxsteps_
) {
// if maxsteps == 0, will never call stop().
177
this->
stop
();
178
}
179
}
180
181
}
RTT::os::TimeService
Definition:
TimeService.hpp:53
RTT::extras::TimerThread
Definition:
TimerThread.hpp:69
RTT::os::MainThread::Instance
static ThreadInterface * Instance()
Definition:
MainThread.cpp:58
RTT::Logger::Info
Definition:
Logger.hpp:121
RTT::extras::SimulationThread::~SimulationThread
virtual ~SimulationThread()
Definition:
SimulationThread.cpp:98
SimulationActivity.hpp
RTT::extras::SimulationThread::SimulationThread
SimulationThread(double period)
Definition:
SimulationThread.cpp:86
RTT::extras::SimulationThread::run
virtual bool run(unsigned int maxsteps)
Definition:
SimulationThread.cpp:122
RTT::Logger::nl
static std::ostream & nl(std::ostream &__os)
Definition:
Logger.cpp:373
RTT::os::Thread::setScheduler
virtual bool setScheduler(int sched_type)
Definition:
Thread.cpp:476
RTT::extras::SimulationThread::step
void step()
Definition:
SimulationThread.cpp:166
RTT::extras::SimulationThread::Instance
static SimulationThreadPtr Instance(double period=0.001)
Definition:
SimulationThread.cpp:69
RTT::os::ThreadInterface
Definition:
ThreadInterface.hpp:56
RTT::extras::SimulationThread::beat
os::TimeService * beat
Definition:
SimulationThread.hpp:140
RTT::extras::SimulationThread::finalize
void finalize()
Definition:
SimulationThread.cpp:155
RTT::extras::SimulationThreadPtr
boost::shared_ptr< SimulationThread > SimulationThreadPtr
Definition:
SimulationThread.hpp:49
RTT::Logger::endl
static std::ostream & endl(std::ostream &__os)
Definition:
Logger.cpp:383
RTT::extras::SimulationThread::simthread
virtual os::ThreadInterface * simthread()
Definition:
SimulationThread.cpp:137
RTT::extras::SimulationThread::sim_running
bool sim_running
Definition:
SimulationThread.hpp:144
RTT::os::TimeService::enableSystemClock
void enableSystemClock(bool yes_no)
Definition:
TimeService.cpp:95
RTT::extras::SimulationThread::_instance
static SimulationThreadPtr _instance
Definition:
SimulationThread.hpp:135
SimulationThread.hpp
RTT::extras::SimulationThread
Definition:
SimulationThread.hpp:64
RTT::Logger::In
Definition:
Logger.hpp:159
RTT::Logger::log
static Logger & log()
Definition:
Logger.cpp:117
RTT::extras::SimulationThread::Release
static bool Release()
Definition:
SimulationThread.cpp:79
RTT::os::Thread::start
virtual bool start()
Definition:
Thread.cpp:345
RTT::os::LowestPriority
const int LowestPriority
Definition:
ecosthreads.cpp:44
RTT::os::Thread::stop
virtual bool stop()
Definition:
Thread.cpp:424
RTT
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition:
Activity.cpp:53
RTT::extras::SimulationThread::maxsteps_
unsigned int maxsteps_
Definition:
SimulationThread.hpp:142
RTT::extras::SimulationThread::initialize
bool initialize()
Definition:
SimulationThread.cpp:141
RTT::os::Thread::getPriority
virtual int getPriority() const
Definition:
Thread.cpp:593
RTT::os::Thread::getPeriod
virtual Seconds getPeriod() const
Definition:
Thread.cpp:598
RTT::extras::SimulationThread::isRunning
virtual bool isRunning() const
Definition:
SimulationThread.cpp:103
RTT::extras::TimerThread::step
virtual void step()
Definition:
TimerThread.cpp:150
RTT::extras::SimulationThread::cursteps
unsigned int cursteps
Definition:
SimulationThread.hpp:142
ORO_SCHED_OTHER
#define ORO_SCHED_OTHER
Definition:
ecos/fosi.h:62
RTT::os::TimeService::secondsChange
Seconds secondsChange(Seconds delta)
Definition:
TimeService.cpp:156
RTT::extras::SimulationThread::start
virtual bool start()
Definition:
SimulationThread.cpp:108
RTT::os::Thread::getName
virtual const char * getName() const
Definition:
Thread.cpp:645
RTT::os::Thread::isRunning
virtual bool isRunning() const
Definition:
Thread.cpp:466
rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:36