Skip to content

Commit 68efe60

Browse files
committed
update: more docs
1 parent 0b25971 commit 68efe60

2 files changed

Lines changed: 333 additions & 92 deletions

File tree

include/robotkernel/trigger_base.h

Lines changed: 140 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,27 @@
2424
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2525
*/
2626

27+
/**
28+
* @file trigger_base.h
29+
* @brief Base class for trigger callbacks in robotkernel.
30+
*
31+
* The trigger subsystem in robotkernel allows registration of callback
32+
* objects that are invoked each time a trigger event ("tick") occurs.
33+
* Classes that wish to receive these callbacks should derive from
34+
* `trigger_base` and implement the pure virtual function `tick()`.
35+
*
36+
* A trigger_base object also supports configuration from YAML via its
37+
* constructor, and can acquire/release itself from a trigger device
38+
* identified by name. :contentReference[oaicite:0]{index=0}
39+
*/
40+
2741
#ifndef ROBOTKERNEL__TRIGGER_BASE_H
2842
#define ROBOTKERNEL__TRIGGER_BASE_H
2943

30-
#include <string>
31-
#include <memory>
32-
#include <list>
3344
#include <functional>
34-
45+
#include <list>
46+
#include <memory>
47+
#include <string>
3548
#include <yaml-cpp/yaml.h>
3649

3750
#include "robotkernel/helpers.h"
@@ -40,71 +53,161 @@ namespace robotkernel {
4053

4154
class trigger;
4255

43-
//! trigger_base
44-
/*
45-
* derive from this class if you want to get
46-
* triggered by a trigger_device
56+
/**
57+
* @class trigger_base
58+
* @brief Abstract base class for objects that are notified by a trigger.
59+
*
60+
* Derive from this class to implement a callback object that can be
61+
* registered with a `trigger` (or a trigger device) to receive periodic
62+
* notifications (`tick()` calls). The base class also holds configuration
63+
* fields such as `divisor`, execution mode flags, and worker thread hints.
64+
*
65+
* @note
66+
* - The `tick()` method must be overridden in derived classes.
67+
* - The object can be configured from a YAML node using the second ctor.
68+
* - Call `aquire()` to register with the trigger device and
69+
* `release()` to unregister. :contentReference[oaicite:1]{index=1}
4770
*/
48-
class trigger_base :
49-
public virtual shared_base
71+
class trigger_base : public virtual shared_base
5072
{
51-
5273
public:
53-
int divisor = 1; //!< trigger every ""divisor"" step
54-
int cnt = 0; //!< internal step counter
74+
/// Trigger divisor: number of ticks before invoking this callback.
75+
int divisor = 1;
76+
77+
/// Internal counter updated by trigger device.
78+
int cnt = 0;
79+
80+
/**
81+
* @brief If true, callback runs in the triggering thread.
82+
*
83+
* If false, execution may be handed off to a worker thread
84+
* according to `worker_prio` / `worker_affinity`. :contentReference[oaicite:2]{index=2}
85+
*/
5586
bool direct_mode = true;
87+
88+
/// Priority for worker thread execution.
5689
int worker_prio = 0;
90+
91+
/// CPU affinity mask for worker thread execution.
5792
int worker_affinity = 0xFFFFFFFF;
93+
94+
/// Optional device name from which this callback is sourced.
5895
std::string dev_name = "";
96+
97+
/// Optional pointer to the owning device instance.
5998
std::shared_ptr<robotkernel::trigger> dev = nullptr;
60-
61-
trigger_base(int divisor=1) :
62-
divisor(divisor)
63-
{};
64-
65-
trigger_base(const YAML::Node& node) {
99+
100+
public:
101+
/**
102+
* @brief Construct with an optional trigger divisor.
103+
*
104+
* @param divisor
105+
* Number of base trigger ticks between invocations. Default 1. :contentReference[oaicite:3]{index=3}
106+
*/
107+
trigger_base(int divisor = 1)
108+
: divisor(divisor)
109+
{
110+
}
111+
112+
/**
113+
* @brief Construct and configure from a YAML node.
114+
*
115+
* This constructor reads configuration fields such as:
116+
* - `divisor`
117+
* - `direct_mode`
118+
* - `worker_prio`
119+
* - `worker_affinity`
120+
* - `dev_name`
121+
*
122+
* @param node
123+
* YAML node containing configuration. :contentReference[oaicite:4]{index=4}
124+
*/
125+
trigger_base(const YAML::Node& node)
126+
{
66127
using robotkernel::helpers::get_as;
67-
68128
divisor = get_as<int>(node, "divisor", 1);
69129
direct_mode = get_as<bool>(node, "direct_mode", true);
70130
worker_prio = get_as<int>(node, "worker_prio", 0);
71131
worker_affinity = get_as<int>(node, "worker_affinity", 0xFFFFFFFF);
72132
dev_name = get_as<std::string>(node, "dev_name", "");
73133
}
74-
75-
~trigger_base() { release(); }
76134

77-
//! trigger function
135+
/**
136+
* @brief Virtual destructor ensures cleanup via `release()`. :contentReference[oaicite:5]{index=5}
137+
*/
138+
virtual ~trigger_base() { release(); }
139+
140+
/**
141+
* @brief Pure virtual callback invoked on trigger ticks.
142+
*
143+
* Derived classes must override this to implement the actual
144+
* behavior when a trigger event occurs. :contentReference[oaicite:6]{index=6}
145+
*/
78146
virtual void tick() = 0;
79-
80-
/*! @brief Get trigger device from robotkernel and register us
81-
* as trigger.
147+
148+
/**
149+
* @brief Register this callback with the trigger device.
150+
*
151+
* This method looks up the trigger by `dev_name` and adds this
152+
* callback to it. The owning device pointer (`dev`) may be set
153+
* as part of registration. :contentReference[oaicite:7]{index=7}
82154
*/
83155
void aquire(void);
84-
85-
/*! @brief Remove us as trigger devce.
156+
157+
/**
158+
* @brief Unregister this callback from the trigger device.
159+
*
160+
* Reverse operation to `aquire()`: remove this callback from
161+
* the trigger device and release references. :contentReference[oaicite:8]{index=8}
86162
*/
87163
void release(void);
88164
};
89165

166+
/**
167+
* @brief Shared pointer alias for a trigger callback. :contentReference[oaicite:9]{index=9}
168+
*/
90169
typedef std::shared_ptr<trigger_base> sp_trigger_base_t;
170+
171+
/**
172+
* @brief List of trigger callbacks. :contentReference[oaicite:10]{index=10}
173+
*/
91174
typedef std::list<sp_trigger_base_t> trigger_list_t;
92175

93-
class triggerable :
94-
public trigger_base
176+
/**
177+
* @class triggerable
178+
* @brief Helper type wrapping a simple lambda/function into trigger_base.
179+
*
180+
* This convenience class allows binding a `std::function<void()>` to
181+
* a trigger callback without needing to derive your own subclass.
182+
*/
183+
class triggerable : public trigger_base
95184
{
96185
private:
97-
std::function<void(void)> cb;
98-
186+
/// Underlying function to call on tick.
187+
std::function<void()> cb;
188+
99189
public:
100-
triggerable(const YAML::Node& node, std::function<void(void)> cb) :
101-
trigger_base(node), cb(cb) {}
102-
103-
//! trigger function
190+
/**
191+
* @brief Construct a triggerable from a YAML node and function. :contentReference[oaicite:11]{index=11}
192+
*
193+
* @param node
194+
* YAML configuration node.
195+
*
196+
* @param cb
197+
* Function to invoke on `tick()`.
198+
*/
199+
triggerable(const YAML::Node& node, std::function<void()> cb)
200+
: trigger_base(node), cb(cb)
201+
{
202+
}
203+
204+
/**
205+
* @brief Invoke the underlying function. :contentReference[oaicite:12]{index=12}
206+
*/
104207
virtual void tick() override { cb(); }
105208
};
106209

107-
}; // namespace robotkernel;
210+
} // namespace robotkernel
108211

109212
#endif // ROBOTKERNEL__TRIGGER_BASE_H
110213

0 commit comments

Comments
 (0)