converters/sonar.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
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 /*
19 * LOCAL includes
20 */
21 #include "sonar.hpp"
22 #include "../tools/from_any_value.hpp"
23 
24 /*
25 * BOOST includes
26 */
27 #include <boost/foreach.hpp>
28 #define for_each BOOST_FOREACH
29 
30 namespace naoqi
31 {
32 namespace converter
33 {
34 
35 SonarConverter::SonarConverter( const std::string& name, const float& frequency, const qi::SessionPtr& session )
36  : BaseConverter( name, frequency, session ),
37  p_memory_(session->service("ALMemory").value()),
38  is_subscribed_(false)
39 {
40  // Only create a sonar proxy if NAOqi < 2.9
42  {
43  p_sonar_ = session->service("ALSonar").value();
44  }
45 
46  std::vector<std::string> keys;
47  if (robot_ == robot::PEPPER) {
48  keys.push_back("Device/SubDeviceList/Platform/Front/Sonar/Sensor/Value");
49  keys.push_back("Device/SubDeviceList/Platform/Back/Sonar/Sensor/Value");
50  frames_.push_back("SonarFront_frame");
51  frames_.push_back("SonarBack_frame");
52  //topics_.push_back(topic + "/Front_sensor");
53  //topics_.push_back(topic + "/Back_sensor");
54  } else if (robot_ == robot::NAO) {
55  keys.push_back("Device/SubDeviceList/US/Left/Sensor/Value");
56  keys.push_back("Device/SubDeviceList/US/Right/Sensor/Value");
57  frames_.push_back("LSonar_frame");
58  frames_.push_back("RSonar_frame");
59  //topics_.push_back(topic + "/Left_sensor");
60  //topics_.push_back(topic + "/Right_sensor");
61  }
62 
63  // Prepare the messages
64  msgs_.resize(frames_.size());
65  for(size_t i = 0; i < msgs_.size(); ++i)
66  {
67  msgs_[i].header.frame_id = frames_[i];
68  msgs_[i].min_range = 0.25;
69  msgs_[i].max_range = 2.55;
70  msgs_[i].field_of_view = 0.523598776;
71  msgs_[i].radiation_type = sensor_msgs::Range::ULTRASOUND;
72  }
73 
74  keys_.resize(keys.size());
75  size_t i = 0;
76  for(std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it, ++i)
77  keys_[i] = *it;
78 }
79 
81 {
83  {
84  p_sonar_.call<void>("unsubscribe", "ROS");
85  is_subscribed_ = false;
86  }
87 }
88 
90 {
91  callbacks_[action] = cb;
92 }
93 
94 void SonarConverter::callAll( const std::vector<message_actions::MessageAction>& actions )
95 {
97  {
98  p_sonar_.call<void>("subscribe", "ROS");
99  is_subscribed_ = true;
100  }
101 
102  std::vector<float> values;
103  try {
104  qi::AnyValue anyvalues = p_memory_.call<qi::AnyValue>("getListData", keys_);
106  } catch (const std::exception& e) {
107  std::cerr << "Exception caught in SonarConverter: " << e.what() << std::endl;
108  return;
109  }
110  ros::Time now = ros::Time::now();
111  for(size_t i = 0; i < msgs_.size(); ++i)
112  {
113  msgs_[i].header.stamp = now;
114  msgs_[i].range = float(values[i]);
115  }
116 
117  for_each( message_actions::MessageAction action, actions )
118  {
119  callbacks_[action]( msgs_ );
120  }
121 }
122 
124 {
126  {
127  p_sonar_.call<void>("unsubscribe", "ROS");
128  is_subscribed_ = false;
129  }
130 }
131 
132 } // publisher
133 } //naoqi
naoqi::converter::SonarConverter::reset
void reset()
Definition: converters/sonar.cpp:123
naoqi::converter::SonarConverter::Callback_t
boost::function< void(std::vector< sensor_msgs::Range > &)> Callback_t
Definition: converters/sonar.hpp:40
converter
naoqi::converter::SonarConverter::callAll
void callAll(const std::vector< message_actions::MessageAction > &actions)
Definition: converters/sonar.cpp:94
session
session
naoqi::message_actions::MessageAction
MessageAction
Definition: message_actions.h:9
naoqi::converter::SonarConverter::is_subscribed_
bool is_subscribed_
Definition: converters/sonar.hpp:63
naoqi::converter::SonarConverter::msgs_
std::vector< sensor_msgs::Range > msgs_
Definition: converters/sonar.hpp:70
naoqi::tools::fromAnyValueToFloatVector
std::vector< float > fromAnyValueToFloatVector(qi::AnyValue &value, std::vector< float > &result)
Definition: from_any_value.cpp:185
naoqi
Definition: converter.hpp:29
naoqi::converter::BaseConverter< SonarConverter >::naoqi_version_
const robot::NaoqiVersion & naoqi_version_
Definition: converter_base.hpp:73
naoqi::converter::SonarConverter::callbacks_
std::map< message_actions::MessageAction, Callback_t > callbacks_
Definition: converters/sonar.hpp:56
for_each
#define for_each
Definition: converters/sonar.cpp:28
naoqi::converter::SonarConverter::keys_
std::vector< std::string > keys_
Definition: converters/sonar.hpp:66
naoqi::converter::SonarConverter::p_sonar_
qi::AnyObject p_sonar_
Definition: converters/sonar.hpp:59
naoqi::helpers::driver::isNaoqiVersionLesser
bool isNaoqiVersionLesser(const robot::NaoqiVersion &naoqi_version, const int &major, const int &minor, const int &patch, const int &build)
Function that returns true if the provided naoqi_version is (strictly) lesser than the specified one ...
Definition: driver_helpers.cpp:408
naoqi::converter::SonarConverter::frames_
std::vector< std::string > frames_
Definition: converters/sonar.hpp:68
name
name
naoqi::converter::BaseConverter
Definition: converter_base.hpp:40
naoqi::robot::PEPPER
@ PEPPER
Definition: tools.hpp:41
ros::Time
values
std::vector< double > values
naoqi::robot::NAO
@ NAO
Definition: tools.hpp:40
naoqi::converter::SonarConverter::registerCallback
void registerCallback(message_actions::MessageAction action, Callback_t cb)
Definition: converters/sonar.cpp:89
naoqi::converter::SonarConverter::SonarConverter
SonarConverter(const std::string &name, const float &frequency, const qi::SessionPtr &session)
Definition: converters/sonar.cpp:35
naoqi::converter::SonarConverter::p_memory_
qi::AnyObject p_memory_
Definition: converters/sonar.hpp:61
naoqi::converter::SonarConverter::~SonarConverter
~SonarConverter()
Definition: converters/sonar.cpp:80
sonar.hpp
ros::Time::now
static Time now()
naoqi::converter::BaseConverter< SonarConverter >::robot_
const robot::Robot & robot_
Definition: converter_base.hpp:71


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 3 2024 03:50:06