Fawkes API  Fawkes Development Version
blocked_timing.cpp
1 
2 /***************************************************************************
3  * blocked_timing.h - Blocked timing aspect for Fawkes
4  *
5  * Created: Thu Jan 11 16:52:28 2007
6  * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <stdexcept>
25 #include <aspect/blocked_timing.h>
26 #include <core/exception.h>
27 #include <core/threading/thread.h>
28 
29 namespace fawkes {
30 
31 /** @class BlockedTimingAspect <aspect/blocked_timing.h>
32  * Thread aspect to use blocked timing.
33  * The Fawkes main application provides basic means to synchronize all
34  * running thread with respect to several given hooks (see WakeupHook).
35  * Threads of a woken up at a particular point in time. The hooks basically
36  * correspond to an extended sense - plan - act kind of loop.
37  * Your thread must run in Thread::OPMODE_WAITFORWAKEUP mode, otherwise it
38  * is not started. This is a requirement for having the BlockedTimingAspect.
39  *
40  * @see Thread::OpMode
41  * @ingroup Aspects
42  * @author Tim Niemueller
43  */
44 
45 // Side note: Overriding Thread::run() can make our requirement useless, but
46 // we believe in the best of the coder: laziness
47 
48 /** Constructor.
49  * This special constructor is needed to define the wakeup point.
50  * @param wakeup_hook hook when this thread should be woken up
51  */
52 BlockedTimingAspect::BlockedTimingAspect(WakeupHook wakeup_hook)
53 : SyncPointAspect(SyncPoint::WAIT_FOR_ALL,
54  blocked_timing_hook_to_start_syncpoint(wakeup_hook),
55  blocked_timing_hook_to_end_syncpoint(wakeup_hook))
56 {
57  add_aspect("BlockedTimingAspect");
58  wakeup_hook_ = wakeup_hook;
59  loop_listener_ = new BlockedTimingLoopListener();
60 }
61 
62 /** Virtual empty destructor. */
64 {
65  delete loop_listener_;
66 }
67 
68 /** Init BlockedTiming aspect.
69  * This intializes the aspect and adds the loop listener to the thread.
70  * @param thread thread which uses this aspect
71  */
72 void
74 {
75  thread->add_loop_listener(loop_listener_);
76  thread->wakeup();
77 }
78 
79 /** Finalize BlockedTiming aspect.
80  * This finalizes the aspect and removes the loop listener from the thread.
81  * @param thread thread which uses this aspect
82  */
83 void
85 {
86  thread->remove_loop_listener(loop_listener_);
87 }
88 
89 /** Get the wakeup hook.
90  * The wakeup hook defines when this thread should be woken up. This heavily
91  * depends on the used main thread.
92  * @return wakeup hook
93  */
96 {
97  return wakeup_hook_;
98 }
99 
100 /** Get string for wakeup hook.
101  * @param hook wakeup hook to get string for
102  * @return string representation of hook
103  */
104 const char *
106 {
107  switch (hook) {
108  case WAKEUP_HOOK_PRE_LOOP: return "WAKEUP_HOOK_PRE_LOOP";
109  case WAKEUP_HOOK_SENSOR_ACQUIRE: return "WAKEUP_HOOK_SENSOR_ACQUIRE";
110  case WAKEUP_HOOK_SENSOR_PREPARE: return "WAKEUP_HOOK_SENSOR_PREPARE";
111  case WAKEUP_HOOK_SENSOR_PROCESS: return "WAKEUP_HOOK_SENSOR_PROCESS";
112  case WAKEUP_HOOK_WORLDSTATE: return "WAKEUP_HOOK_WORLDSTATE";
113  case WAKEUP_HOOK_THINK: return "WAKEUP_HOOK_THINK";
114  case WAKEUP_HOOK_SKILL: return "WAKEUP_HOOK_SKILL";
115  case WAKEUP_HOOK_ACT: return "WAKEUP_HOOK_ACT";
116  case WAKEUP_HOOK_ACT_EXEC: return "WAKEUP_HOOK_ACT_EXEC";
117  case WAKEUP_HOOK_POST_LOOP: return "WAKEUP_HOOK_POST_LOOP";
118  default: throw Exception("Unknown blocked timing wakeup hook");
119  }
120 }
121 
122 const std::map<const BlockedTimingAspect::WakeupHook, const std::string>
123  BlockedTimingAspect::hook_to_syncpoint = {{WAKEUP_HOOK_PRE_LOOP, "/preloop"},
124  {WAKEUP_HOOK_SENSOR_ACQUIRE, "/sensors/acquire"},
125  {WAKEUP_HOOK_SENSOR_PREPARE, "/sensors/prepare"},
126  {WAKEUP_HOOK_SENSOR_PROCESS, "/sensors/process"},
127  {WAKEUP_HOOK_WORLDSTATE, "/worldstate"},
128  {WAKEUP_HOOK_THINK, "/agent"},
129  {WAKEUP_HOOK_SKILL, "/skill"},
130  {WAKEUP_HOOK_ACT, "/act/main"},
131  {WAKEUP_HOOK_ACT_EXEC, "/act/exec"},
132  {WAKEUP_HOOK_POST_LOOP, "/postloop"}};
133 
134 /** Get the syncpoint identifier corresponding to the end of a wakeup hook.
135  * This is the syncpoint emitted at the end of a hook.
136  * @param hook wakeup hook to get the syncpoint identifier for
137  * @return the identifier of the corresponding syncpoint
138  */
139 std::string
141 {
142  try {
143  return std::string(hook_to_syncpoint.at(hook)) + "/end";
144  } catch (const std::out_of_range &e) {
145  throw Exception("Unknown blocked timing wakeup hook. Error: %s", e.what());
146  }
147 }
148 
149 /** Get the syncpoint identifier corresponding to the start of a wakeup hook.
150  * This is the syncpoint waited for at the start of a hook.
151  * @param hook wakeup hook to get the syncpoint identifier for
152  * @return the identifier of the corresponding syncpoint
153  */
154 std::string
156 {
157  try {
158  return std::string(hook_to_syncpoint.at(hook)) + "/start";
159  } catch (const std::out_of_range &e) {
160  throw Exception("Unknown blocked timing wakeup hook. Error: %s", e.what());
161  }
162 }
163 
164 /** The post loop function of the BlockedTimingAspect
165  * This function is called right after the loop of the thread with the aspect.
166  * @param thread thread this loop listener belongs to
167  */
168 void
170 {
171  thread->wakeup();
172 }
173 
174 } // end namespace fawkes
fawkes::BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE
world state thread
Definition: blocked_timing.h:69
fawkes::BlockedTimingAspect::init_BlockedTimingAspect
void init_BlockedTimingAspect(Thread *thread)
Init BlockedTiming aspect.
Definition: blocked_timing.cpp:77
fawkes::BlockedTimingAspect::blocked_timing_hook_to_start_syncpoint
static std::string blocked_timing_hook_to_start_syncpoint(WakeupHook hook)
Get the syncpoint identifier corresponding to the start of a wakeup hook.
Definition: blocked_timing.cpp:159
fawkes::BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PREPARE
sensor data preparation thread, convert acquired data to usable format
Definition: blocked_timing.h:66
fawkes::BlockedTimingAspect::finalize_BlockedTimingAspect
void finalize_BlockedTimingAspect(Thread *thread)
Finalize BlockedTiming aspect.
Definition: blocked_timing.cpp:88
fawkes::Thread::wakeup
void wakeup()
Wake up thread.
Definition: thread.cpp:999
fawkes::BlockedTimingAspect::hook_to_syncpoint
static const std::map< const WakeupHook, const std::string > hook_to_syncpoint
Translation from WakeupHooks to SyncPoints.
Definition: blocked_timing.h:93
fawkes::BlockedTimingAspect::WAKEUP_HOOK_THINK
think thread (agent)
Definition: blocked_timing.h:70
fawkes::Thread::add_loop_listener
void add_loop_listener(ThreadLoopListener *loop_listener)
Add loop listener.
Definition: thread.cpp:1185
fawkes::BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS
sensor data processing thread
Definition: blocked_timing.h:68
fawkes::BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC
act execution thread
Definition: blocked_timing.h:73
fawkes
fawkes::BlockedTimingLoopListener
Definition: blocked_timing.h:48
fawkes::BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP
before each loop
Definition: blocked_timing.h:63
fawkes::BlockedTimingAspect::WAKEUP_HOOK_ACT
act thread (motor module etc.)
Definition: blocked_timing.h:72
fawkes::BlockedTimingAspect::blockedTimingAspectHook
WakeupHook blockedTimingAspectHook() const
Get the wakeup hook.
Definition: blocked_timing.cpp:99
fawkes::BlockedTimingAspect::WakeupHook
WakeupHook
Type to define at which hook the thread is woken up.
Definition: blocked_timing.h:62
fawkes::BlockedTimingLoopListener::post_loop
void post_loop(Thread *thread)
The post loop function of the BlockedTimingAspect This function is called right after the loop of the...
Definition: blocked_timing.cpp:173
fawkes::BlockedTimingAspect::BlockedTimingAspect
BlockedTimingAspect(WakeupHook wakeup_hook)
Constructor.
Definition: blocked_timing.cpp:56
fawkes::BlockedTimingAspect::blocked_timing_hook_to_end_syncpoint
static std::string blocked_timing_hook_to_end_syncpoint(WakeupHook hook)
Get the syncpoint identifier corresponding to the end of a wakeup hook.
Definition: blocked_timing.cpp:144
fawkes::Thread
Definition: thread.h:44
fawkes::BlockedTimingAspect::blocked_timing_hook_to_string
static const char * blocked_timing_hook_to_string(WakeupHook hook)
Get string for wakeup hook.
Definition: blocked_timing.cpp:109
fawkes::BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP
run after loop
Definition: blocked_timing.h:74
fawkes::BlockedTimingAspect::WAKEUP_HOOK_SKILL
skill thread (skill module)
Definition: blocked_timing.h:71
fawkes::Thread::remove_loop_listener
void remove_loop_listener(ThreadLoopListener *loop_listener)
Remove loop listener.
Definition: thread.cpp:1194
fawkes::BlockedTimingAspect::WAKEUP_HOOK_SENSOR_ACQUIRE
sensor acquisition thread, acquire data from sensor
Definition: blocked_timing.h:64
fawkes::BlockedTimingAspect::~BlockedTimingAspect
virtual ~BlockedTimingAspect()
Virtual empty destructor.
Definition: blocked_timing.cpp:67
fawkes::Exception
Definition: exception.h:39