recognizer_node.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021, Rein Appeldoorn
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #pragma once
19 
22 #include <ros/node_handle.h>
23 #include <string>
24 
25 #include "./ros_util.h"
26 #include "./util.h"
27 
28 namespace picovoice_driver
29 {
30 constexpr double EXECUTE_PERIOD = 0.01;
31 
32 template <typename RecognizerDataType, typename RecognizerType, typename ActionType>
34 {
35 public:
42  RecognizerNode(const std::string& name, const std::string& action_name,
43  const typename RecognizerDataType::Parameters& parameters)
44  : action_server_(action_name, boost::bind(&RecognizerNode::executeCallback, this, _1), false)
45  , parameters_(parameters)
46  {
47  ros::NodeHandle local_nh("~");
48  auto record_directory = local_nh.param("record_directory", defaultRecordDirectory(name));
49  auto record_timeout = local_nh.param("record_timeout", 300.);
50  if (!local_nh.getParam("access_key", parameters_.access_key_))
51  {
52  throw std::runtime_error("~access_key not specified");
53  }
54  recognizer_.initialize(record_directory, record_timeout);
55 
56  dynamic_reconfigure_server_.registerVariable<double>("sensitivity", &parameters_.sensitivity_,
57  "Recognizer sensitivity", 0., 1.);
59 
61  ROS_INFO("RecognizerNode(name=%s, action_name=%s, record_directory=%s, record_timeout=%.2f) initialized with "
62  "parameters %s",
63  name.c_str(), action_name.c_str(), record_directory.c_str(), record_timeout,
64  toString(parameters_).c_str());
65  }
66 
67 private:
68  typename RecognizerDataType::Parameters parameters_;
70 
71  RecognizerType recognizer_;
72 
73  virtual void updateParameters(const typename ActionType::_action_goal_type::_goal_type& goal,
74  typename RecognizerDataType::Parameters& parameters) = 0;
75  virtual void updateResult(const typename RecognizerDataType::Result& result,
76  typename ActionType::_action_result_type::_result_type& action_result) = 0;
77 
79  void executeCallback(const typename ActionType::_action_goal_type::_goal_type::ConstPtr& goal)
80  {
81  typename ActionType::_action_result_type::_result_type action_result;
82 
83  try
84  {
86  }
87  catch (const std::exception& e)
88  {
89  std::string msg = "Invalid goal: " + std::string(e.what());
90  ROS_ERROR("%s", msg.c_str());
91  action_server_.setAborted(action_result, msg);
92  return;
93  }
94 
95  try
96  {
97  recognizer_.configure(parameters_);
98  ROS_INFO("Configured recognizer: %s", toString(parameters_).c_str());
99  }
100  catch (const std::exception& e)
101  {
102  std::string msg = "Could not configure recognizer: " + std::string(e.what());
103  ROS_ERROR("%s", msg.c_str());
104  action_server_.setAborted(action_result, msg);
105  return;
106  }
107 
108  ROS_INFO("Recognizing ..");
109  recognizer_.recognize();
110  try
111  {
112  while (ros::ok() && recognizer_.isRecognizing())
113  {
114  if (action_server_.isPreemptRequested() && !recognizer_.isPreempting())
115  {
116  ROS_WARN("Preempt requested");
117  recognizer_.preempt();
118  }
120  }
121  }
122  catch (const std::exception& e)
123  {
124  std::string msg = "Recognize error: " + std::string(e.what());
125  ROS_ERROR("%s", msg.c_str());
126  action_server_.setAborted(action_result, msg);
127  return;
128  }
129  ROS_INFO("Recognizing done");
130 
131  auto result = recognizer_.getResult();
132  ROS_INFO("Result: %s", toString(result).c_str());
133 
134  updateResult(result, action_result);
136  {
137  action_server_.setPreempted(action_result);
138  }
139  else
140  {
141  action_server_.setSucceeded(action_result);
142  }
143  }
144 };
145 } // namespace picovoice_driver
picovoice_driver::RecognizerNode::executeCallback
void executeCallback(const typename ActionType::_action_goal_type::_goal_type::ConstPtr &goal)
Definition: recognizer_node.h:79
ddynamic_reconfigure.h
picovoice_driver::RecognizerNode::dynamic_reconfigure_server_
ddynamic_reconfigure::DDynamicReconfigure dynamic_reconfigure_server_
Definition: recognizer_node.h:69
node_handle.h
picovoice_driver::RecognizerNode
Definition: recognizer_node.h:33
actionlib::SimpleActionServer::start
void start()
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
picovoice_driver::RecognizerNode::recognizer_
RecognizerType recognizer_
Definition: recognizer_node.h:71
picovoice_driver::RecognizerNode::updateResult
virtual void updateResult(const typename RecognizerDataType::Result &result, typename ActionType::_action_result_type::_result_type &action_result)=0
picovoice_driver::RecognizerNode::action_server_
actionlib::SimpleActionServer< ActionType > action_server_
Definition: recognizer_node.h:78
simple_action_server.h
boost
actionlib::SimpleActionServer::isPreemptRequested
bool isPreemptRequested()
actionlib::SimpleActionServer::setSucceeded
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
ros::ok
ROSCPP_DECL bool ok()
python.setup.name
name
Definition: porcupine/binding/python/setup.py:69
picovoice_driver::defaultRecordDirectory
std::string defaultRecordDirectory(const std::string &name)
defaultRecordDirectory Get the default record dir
Definition: ros_util.cpp:32
picovoice_driver::RecognizerNode::updateParameters
virtual void updateParameters(const typename ActionType::_action_goal_type::_goal_type &goal, typename RecognizerDataType::Parameters &parameters)=0
picovoice_driver
Definition: porcupine_node.cpp:24
picovoice_driver::RecognizerNode::RecognizerNode
RecognizerNode(const std::string &name, const std::string &action_name, const typename RecognizerDataType::Parameters &parameters)
RecognizerNode Recognizer node that exposes an action interface for a recognizer.
Definition: recognizer_node.h:42
ROS_WARN
#define ROS_WARN(...)
picovoice_driver::RecognizerNode::parameters_
RecognizerDataType::Parameters parameters_
Definition: recognizer_node.h:68
actionlib::SimpleActionServer::setPreempted
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
actionlib::SimpleActionServer::setAborted
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
ddynamic_reconfigure::DDynamicReconfigure
actionlib::SimpleActionServer< ActionType >
ddynamic_reconfigure::DDynamicReconfigure::publishServicesTopics
virtual void publishServicesTopics()
ROS_ERROR
#define ROS_ERROR(...)
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
ros::Duration::sleep
bool sleep() const
ROS_INFO
#define ROS_INFO(...)
picovoice_driver::toString
std::string toString(const T &v)
Definition: util.h:28
ros::Duration
util.h
ros_util.h
ros::NodeHandle
picovoice_driver::EXECUTE_PERIOD
constexpr double EXECUTE_PERIOD
Definition: recognizer_node.h:30
ddynamic_reconfigure::DDynamicReconfigure::registerVariable
void registerVariable(const std::string &name, T *variable, const boost::function< void(T value)> &callback, const std::string &description="", T min=getMin< T >(), T max=getMax< T >(), const std::string &group="Default")


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50