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_);
105  tools::fromAnyValueToFloatVector(anyvalues, values);
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 
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
std::vector< double > values
#define for_each
SonarConverter(const std::string &name, const float &frequency, const qi::SessionPtr &session)
std::vector< std::string > frames_
void registerCallback(message_actions::MessageAction action, Callback_t cb)
boost::function< void(std::vector< sensor_msgs::Range > &)> Callback_t
void callAll(const std::vector< message_actions::MessageAction > &actions)
action
std::vector< std::string > keys_
std::vector< float > fromAnyValueToFloatVector(qi::AnyValue &value, std::vector< float > &result)
static Time now()
std::map< message_actions::MessageAction, Callback_t > callbacks_
std::vector< sensor_msgs::Range > msgs_
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 ...


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Jul 1 2023 02:53:24