Fawkes API Fawkes Development Version
gologpp_fawkes_backend.cpp
1/***************************************************************************
2 * gologpp_fawkes_backend.cpp - Fawkes backend for Golog++
3 *
4 * Created: Mon 26 Aug 2019 CEST 15:38
5 * Copyright 2019 Victor Mataré <matare@fh-aachen.de>
6 * Till Hofmann <hofmann@kbsg.rwth-aachen.de>
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include "gologpp_fawkes_backend.h"
23
24#include "message_action_executor.h"
25#include "print_action_executor.h"
26#include "remote_skiller_executor.h"
27#include "skiller_action_executor.h"
28#include "sleep_action_executor.h"
29
30#include <config/config.h>
31#include <golog++/model/activity.h>
32#include <golog++/model/transition.h>
33
34namespace fawkes {
35namespace gpp {
36
37using namespace gologpp;
38
39/** @class GologppFawkesBackend
40 * A Golog++ backend to get data from and send commands to Fawkes.
41 * The backend currently only provides access to the skiller for action
42 * execution.
43 */
44
45/** Constructor.
46 * @param config The configuration to read from
47 * @param cfg_prefix The spec-specific config prefix to use
48 * @param logger The logger to use for log messages
49 * @param blackboard The blackboard to use to access the skiller
50 */
52 std::string cfg_prefix,
53 Logger * logger,
54 BlackBoard * blackboard)
55: AspectProviderAspect(&dispatcher_inifin_), logger_(logger), blackboard_(blackboard)
56{
57 // Register RemoteSkillerActionExecutors before the local
58 // SkillerActionExecutor. This way, any action that cannot be executed on any
59 // remote will be tried locally.
60 for (const string &robot :
61 config->get_strings_or_defaults((cfg_prefix + "/agents/names").c_str(), {})) {
62 const std::string agent_prefix = cfg_prefix + "/agents/" + robot;
63 const std::string &hostname =
64 config->get_string_or_default((agent_prefix + "/host").c_str(), "localhost");
65 const unsigned short int &port =
66 config->get_uint_or_default((agent_prefix + "/port").c_str(), 1910);
67 action_dispatcher_.register_executor(std::make_shared<RemoteSkillerActionExecutor>(
68 logger, "robot", robot, hostname, port, config, cfg_prefix));
69 }
70 if (config->get_bool_or_default((cfg_prefix + "/use_local_skiller").c_str(), true)) {
71 action_dispatcher_.register_executor(
72 std::make_shared<SkillerActionExecutor>(logger, blackboard, config, cfg_prefix));
73 }
74 action_dispatcher_.register_executor(
75 std::make_shared<BBMessageActionExecutor>(logger, blackboard, config, cfg_prefix));
76 action_dispatcher_.register_executor(std::make_shared<SleepActionExecutor>(logger));
77 action_dispatcher_.register_executor(std::make_shared<PrintActionExecutor>(logger));
78}
79
80GologppFawkesBackend::~GologppFawkesBackend()
81{
82}
83
84/** Preempt the currently running activity.
85 * Determine the right executor and instruct the executor to stop the activity.
86 * @param a The activity to stop
87 */
88void
90{
91 auto executor = action_dispatcher_.get_executor(a);
92 executor->stop(a);
93}
94
95/** Get the current time from Fawkes.
96 * @return the current time
97 */
98gologpp::Clock::time_point
100{
101 return gologpp::Clock::time_point{
102 gologpp::Clock::duration{clock->now().in_sec() / gologpp::Clock::duration::period::den}};
103}
104
105/** Execute the given activity using a suitable executor.
106 * @param a The activity to start.
107 */
108void
109GologppFawkesBackend::execute_activity(shared_ptr<Activity> a)
110{
111 auto executor = action_dispatcher_.get_executor(a);
112 executor->start(a);
113}
114
115} // namespace gpp
116} // namespace fawkes
Thread aspect provide a new aspect.
The BlackBoard abstract class.
Definition: blackboard.h:46
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:42
Time now() const
Get the current time.
Definition: clock.cpp:242
Interface for configuration handling.
Definition: config.h:68
virtual std::string get_string_or_default(const char *path, const std::string &default_val)
Get value from configuration which is of type string, or the given default if the path does not exist...
Definition: config.cpp:736
virtual unsigned int get_uint_or_default(const char *path, const unsigned int &default_val)
Get value from configuration which is of type unsigned int, or the given default if the path does not...
Definition: config.cpp:706
virtual bool get_bool_or_default(const char *path, const bool &default_val)
Get value from configuration which is of type bool, or the given default if the path does not exist.
Definition: config.cpp:726
virtual std::vector< std::string > get_strings_or_defaults(const char *path, const std::vector< std::string > &default_val)
Get list of values from configuration which is of type string, or the given default if the path does ...
Definition: config.cpp:786
Interface for logging.
Definition: logger.h:42
double in_sec() const
Convet time to seconds.
Definition: time.cpp:219
std::shared_ptr< ActionExecutor > get_executor(std::shared_ptr< gologpp::Activity >)
Determine the executor for a given activity.
void register_executor(std::shared_ptr< ActionExecutor > executor)
Register a new executor.
virtual gologpp::Clock::time_point time() const noexcept override
Get the current time from Fawkes.
virtual void preempt_activity(std::shared_ptr< gologpp::Activity > a) override
Preempt the currently running activity.
GologppFawkesBackend(Configuration *config, std::string cfg_prefix, Logger *logger, BlackBoard *blackboard)
Constructor.
Fawkes library namespace.