Extending the framework
Every piece that touches data is a plugin, loaded by its class name from the parameters. New behaviour is added by writing a plugin in your own package; the framework itself does not change.
Which plugin to write
Base class |
It turns |
Examples in the framework |
|---|---|---|
|
a topic (or fixed values) into state entries |
|
|
state entries into the model’s input tensors |
|
|
state entries (or a topic) into one block of the observation |
|
|
input tensors into output tensors |
|
|
output tensors into command entries |
|
|
command entries into topics (node) |
|
|
command entries into command interface values (controller) |
|
|
state entries (and the kinematics) into a |
|
Both hosts run the SafetyCheck plugins (Safety behaviour).
The base classes are in policy_framework/interfaces/, one header per kind of plugin (for
example interfaces/preprocessor.hpp or interfaces/observation_term.hpp), each method
documented there. policy_framework/plugin_interfaces.hpp includes them all except the
observation terms.
The life of a plugin
Two phases, with different rules:
Configuring happens once, outside the control loop. Here a plugin reads its parameters, checks the model, registers every store entry it will read or write (keeping the returned index), creates subscriptions and allocates its buffers. Anything wrong is reported by throwing an exception: the host then fails to configure, with the message.
The control cycle runs at the control rate, in the controller inside the real-time loop. Here a plugin only uses the indices it kept. It must not allocate memory, wait on a lock, look anything up by name, or log on every call.
Context and parameters
A plugin receives a PluginContext: the node, its own name, the kinematics if enabled,
and the name of the safety entry (executors get only the node and their name; input sources
no kinematics). The kinematics is given to the policy stages and their
observation terms (the model of the state the step reads) and to the safety checks (the
host’s model), not to input sources or executors (Kinematics). Its parameters live
under its name, so two policies can use the same plugin class with different settings:
loco: # name of the policy = prefix of its stages' parameters
preprocessor: my_package/MyPreprocessor
my_parameter: 3.0
Example: an observation term from a topic
TopicTerm owns the subscription and hands the latest message to the control cycle
safely. A term only says how big it is and how to write its value. Its max_age parameter
makes the host fault when the topic stops (Safety behaviour):
#include "geometry_msgs/msg/point_stamped.hpp"
#include "pluginlib/class_list_macros.hpp"
#include "policy_framework/interfaces/observation_term.hpp"
using namespace policy_framework;
class TargetTerm : public TopicTerm<geometry_msgs::msg::PointStamped>
{
public:
TargetTerm() : TopicTerm("target") {} // default topic
double compute(const DataStore &, const TensorMap &, float * out) override
{
const auto in = latest(); // ready() is true once a message arrived
out[0] = static_cast<float>(in.msg->point.x);
out[1] = static_cast<float>(in.msg->point.y);
out[2] = static_cast<float>(in.msg->point.z);
// When the value was produced: the header stamp, else now.
const auto & h = in.msg->header.stamp;
return h.sec == 0 && h.nanosec == 0 ? now() : rclcpp::Time(h).seconds();
}
protected:
size_t configure_term(const PluginContext &, const ModelInfo &) override {return 3;}
};
PLUGINLIB_EXPORT_CLASS(TargetTerm, policy_framework::ObservationTerm)
Declare it in a plugins.xml and register that file against policy_framework:
pluginlib_export_plugin_description_file(policy_framework plugins.xml)
Then use it for the term of that name in a policy:
loco:
terms:
target:
type: my_package/TargetTerm
topic: /target
Example: a user command term
A term that feeds a user command, such as a velocity command or a motion reference, derives
from CommandTerm. It only says how to write the command and the neutral command; the base
does what keeps a command safe, so that no command term can forget it:
the neutral command before the first message, once the last one was received longer ago than
timeout(above zero), whileusable()refuses the message, and while theCommandGateis closed (safety_entryis set and its entry has not been received or is not above 0, or theNEUTRAL_COMMANDSreaction is active);the stamp: the header stamp when the message type has one and it is set, else the receive time, so that the input age of the command is how long ago it arrived (the neutral command is stamped now);
the user command flag, so that its stamp does not count for the observation stamp (Data flow).
#include "pluginlib/class_list_macros.hpp"
#include "policy_framework/interfaces/observation_term.hpp"
#include "std_msgs/msg/float64.hpp"
using namespace policy_framework;
class HeadingCommand : public CommandTerm<std_msgs::msg::Float64>
{
public:
HeadingCommand() : CommandTerm("heading", 0.5) {} // default topic, default timeout (s)
protected:
size_t configure_command(const PluginContext &, const ModelInfo &) override {return 1;}
void write_command(const std_msgs::msg::Float64 & msg, const DataStore &, float * out)
override
{
out[0] = static_cast<float>(msg.data);
}
void write_neutral(const DataStore &, float * out) override {out[0] = 0.0F;}
};
PLUGINLIB_EXPORT_CLASS(HeadingCommand, policy_framework::ObservationTerm)
On every cycle exactly one of write_command() and write_neutral() is called, so
write_neutral() runs on every cycle while the command is neutral: a term with state resets
it there. Both run in the control
cycle, like usable(), which sees the same message again on every cycle until a new one
arrives: cache what is costly to check, and throttle any log. ready() is true by default;
a term may override it to wait for something else than the command.
TwistCommandTerm is built this way.
Example: a term from the state and the kinematics
A term that reads sensors available as state interfaces should read them from the state store, so the controller gets them in real time:
class HandPositionTerm : public ObservationTerm
{
public:
size_t configure(const PluginContext & ctx, const ModelInfo &, DataStore &) override
{
if (!ctx.kinematics) {throw std::runtime_error("needs kinematics.enabled");}
kinematics_ = ctx.kinematics;
hand_ = kinematics_->frame("hand_link"); // name lookup: only while configuring
return 3;
}
bool ready(const DataStore &) override {return kinematics_->ready();}
double compute(const DataStore &, const TensorMap &, float * out) override
{
const Eigen::Vector3d p = kinematics_->transform(hand_).translation();
for (int i = 0; i < 3; ++i) {out[i] = static_cast<float>(p[i]);}
return kinematics_->stamp();
}
private:
std::shared_ptr<Kinematics> kinematics_;
size_t hand_ = 0;
};
Example: a safety check
A SafetyCheck registers what it reads, and returns a Verdict on every cycle. This one
fails on any leg actuator error, and asks for ERROR when both legs have one; with a single
leg it leaves the reaction to the configured on_fault:
#include <algorithm>
#include <cmath>
#include "pluginlib/class_list_macros.hpp"
#include "policy_framework/plugin_interfaces.hpp"
using namespace policy_framework;
class LegActuatorsCheck : public SafetyCheck
{
public:
void configure(const PluginContext & ctx, const DataStore &, DataStore & state) override
{
using Names = std::vector<std::string>;
for (const auto & j : get_parameter<Names>(ctx, "left", {})) {
left_.push_back(state.add(j + "/error_code")); // the controller claims it
}
for (const auto & j : get_parameter<Names>(ctx, "right", {})) {
right_.push_back(state.add(j + "/error_code"));
}
}
// Every reaction a Verdict may ask for, known once configured: checked at startup.
std::vector<FaultReaction> possible_reactions() const override
{
return {FaultReaction::ERROR};
}
Verdict check(const DataStore & state, double, std::string & reason) override
{
const bool left = any_error(state, left_);
const bool right = any_error(state, right_);
if (!left && !right) {return {};} // safe: nothing allocated
if (left && right) {
reason = "actuator errors on both legs";
return {false, FaultReaction::ERROR};
}
reason = left ? "an actuator error on the left leg" : "an actuator error on the right leg";
return {false, std::nullopt}; // the configured on_fault
}
private:
static bool any_error(const DataStore & state, const std::vector<size_t> & entries)
{
return std::any_of(
entries.begin(), entries.end(),
// An integer code stored as a double; written so that NaN is an error too.
[&](size_t i) {return state.received(i) && !(std::abs(state.get(i)) < 0.5);});
}
std::vector<size_t> left_, right_;
};
PLUGINLIB_EXPORT_CLASS(LegActuatorsCheck, policy_framework::SafetyCheck)
The Verdict carries the whole answer of a cycle, so a check keeps no state between
check() and its host besides its own filters and counters (forgotten in reset()). Its
configuration:
safety_checks: [legs]
legs:
type: my_package/LegActuatorsCheck
left: [leg_left_1_joint, leg_left_2_joint]
right: [leg_right_1_joint, leg_right_2_joint]
on_fault: neutral_commands # one leg: stop walking; both legs: ERROR from the Verdict
Hooks beyond the basic cycle
Most plugins only need configure() and one cycle method. Some other hooks, required or
optional, cover the situations described in the other pages:
Hook |
When it is called, and what for |
|---|---|
|
Required: when the latest data arrived (0 before any), checked against the source’s
|
|
When the latest message of a term with its own subscription arrived, checked against
its |
|
While configuring: whether the source writes an entry. The controller claims every entry no source provides as a state interface; the node fails to configure instead. |
|
After each step: one stamp per input. They make the observation stamp, the term
|
|
When the policy is activated for the first time, and for its first activation after the host is reactivated or its safety is reset after a latched stop: forget everything. |
|
Whenever the policy becomes active: restart per-activation state (a term’s is called by
the preprocessor that hosts it, e.g. |
|
Every step while another policy is active: keep a history current (Policies and switching). |
|
When the host starts, for the default (first) policy only: write what is known before the first step, such as gains. |
|
While configuring: the state entries the executor reads (e.g. the feedback of a PD law). They must come from state interfaces: configuration fails if an input source provides one. |
|
Every update from an activation, or a safety reset after a latched stop, until the
policy’s first commands: keep the robot where it is (Safety behaviour). Use only what
|
|
On deactivation and failure: the values that stop commanding. |
|
The values of a fault reaction ( |
|
Once configured: the reactions a |
|
When the host is reactivated, when its safety is reset ( |
|
False while |
Testing a plugin
Plugins are plain classes: they can be constructed and called directly in a unit test, with
a DataStore filled by hand, without a robot, a simulator or a controller manager. The
tests of both packages are written that way and are the best examples to start from.