driver_helpers.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 #include "driver_helpers.hpp"
19 
20 namespace naoqi
21 {
22 namespace helpers
23 {
24 namespace driver
25 {
26 
29 static naoqi_bridge_msgs::RobotInfo& getRobotInfoLocal( const qi::SessionPtr& session)
30 {
31  static naoqi_bridge_msgs::RobotInfo info;
32  static qi::Url robot_url;
33 
34  if (robot_url == session->url())
35  {
36  return info;
37  }
38 
39  robot_url = session->url();
40 
41  // Get the robot type
42  std::cout << "Receiving information about robot model" << std::endl;
43  qi::AnyObject p_memory = session->service("ALMemory");
44  std::string robot = p_memory.call<std::string>("getData", "RobotConfig/Body/Type" );
45  std::string version = p_memory.call<std::string>("getData", "RobotConfig/Body/BaseVersion" );
46  std::transform(robot.begin(), robot.end(), robot.begin(), ::tolower);
47 
48  if (std::string(robot) == "nao")
49  {
51  std::cout << BOLDYELLOW << "Robot detected: "
52  << BOLDCYAN << "NAO " << version
53  << RESETCOLOR << std::endl;
54  }
55  if (std::string(robot) == "pepper" || std::string(robot) == "juliette" )
56  {
58  std::cout << BOLDYELLOW << "Robot detected: "
59  << BOLDCYAN << "Pepper " << version
60  << RESETCOLOR << std::endl;
61  }
62  if (std::string(robot) == "romeo" )
63  {
65  std::cout << BOLDYELLOW << "Robot detected: "
66  << BOLDCYAN << "Romeo " << version
67  << RESETCOLOR << std::endl;
68  }
69 
70  // Get the data from RobotConfig
71  qi::AnyObject p_motion = session->service("ALMotion");
72  std::vector<std::vector<qi::AnyValue> > config = p_motion.call<std::vector<std::vector<qi::AnyValue> > >("getRobotConfig");
73 
74  // TODO, fill with the proper string matches from http://doc.aldebaran.com/2-1/naoqi/motion/tools-general-api.html#ALMotionProxy::getRobotConfig
75 
76  for (size_t i=0; i<config[0].size();++i)
77  {
78  if (config[0][i].as<std::string>() == "Model Type")
79  {
80  try{
81  info.model = config[1][i].as<std::string>();
82  }
83  catch(const std::exception& e)
84  {
85  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
86  }
87  }
88 
89  if (config[0][i].as<std::string>() == "Head Version")
90  {
91  try{
92  info.head_version = config[1][i].as<std::string>();
93  }
94  catch(const std::exception& e)
95  {
96  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
97  }
98  }
99 
100  if (config[0][i].as<std::string>() == "Body Version")
101  {
102  try{
103  info.body_version = config[1][i].as<std::string>();
104  }
105  catch(const std::exception& e)
106  {
107  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
108  }
109  }
110 
111  if (config[0][i].as<std::string>() == "Arm Version")
112  {
113  try{
114  info.arm_version = config[1][i].as<std::string>();
115  }
116  catch(const std::exception& e)
117  {
118  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
119  }
120  }
121 
122  if (config[0][i].as<std::string>() == "Laser")
123  {
124  try{
125  info.has_laser = config[1][i].as<bool>();
126  }
127  catch(const std::exception& e)
128  {
129  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
130  }
131  }
132 
133  if (config[0][i].as<std::string>() == "Extended Arms")
134  {
135  try{
136  info.has_extended_arms = config[1][i].as<bool>();
137  }
138  catch(const std::exception& e)
139  {
140  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
141  }
142  }
143 
144  if (config[0][i].as<std::string>() == "Number of Legs")
145  {
146  try{
147  info.number_of_legs = config[1][i].as<int>();
148  }
149  catch(const std::exception& e)
150  {
151  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
152  }
153  }
154 
155  if (config[0][i].as<std::string>() == "Number of Arms")
156  {
157  try{
158  info.number_of_arms = config[1][i].as<int>();
159  }
160  catch(const std::exception& e)
161  {
162  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
163  }
164  }
165 
166  if (config[0][i].as<std::string>() == "Number of Hands")
167  {
168  try{
169  info.number_of_hands = config[1][i].as<int>();
170  }
171  catch(const std::exception& e)
172  {
173  std::cout << "Error in robot config variable " << (config[0][i]).as<std::string>() << std::endl;
174  }
175  }
176 
177  }
178  return info;
179 }
180 
181 const robot::Robot& getRobot( const qi::SessionPtr& session )
182 {
183  static robot::Robot robot = robot::UNIDENTIFIED;
184 
185  if ( getRobotInfo(session).type == naoqi_bridge_msgs::RobotInfo::NAO )
186  {
187  robot = robot::NAO;
188  }
189  if ( getRobotInfo(session).type == naoqi_bridge_msgs::RobotInfo::PEPPER )
190  {
191  robot = robot::PEPPER;
192  }
193  if ( getRobotInfo(session).type == naoqi_bridge_msgs::RobotInfo::ROMEO )
194  {
195  robot = robot::ROMEO;
196  }
197 
198  return robot;
199 }
200 
201 const naoqi_bridge_msgs::RobotInfo& getRobotInfo( const qi::SessionPtr& session )
202 {
203  static naoqi_bridge_msgs::RobotInfo robot_info = getRobotInfoLocal(session);
204  return robot_info;
205 }
206 
209 bool& setLanguage( const qi::SessionPtr& session, naoqi_bridge_msgs::SetStringRequest req)
210 {
211  static bool success;
212  std::cout << "Receiving service call of setting speech language" << std::endl;
213  try{
214  qi::AnyObject p_text_to_speech = session->service("ALTextToSpeech");
215  p_text_to_speech.call<void>("setLanguage", req.data);
216  success = true;
217  return success;
218  }
219  catch(const std::exception& e){
220  success = false;
221  return success;
222  }
223 }
224 
227 std::string& getLanguage( const qi::SessionPtr& session )
228 {
229  static std::string language;
230  std::cout << "Receiving service call of getting speech language" << std::endl;
231  qi::AnyObject p_text_to_speech = session->service("ALTextToSpeech");
232  language = p_text_to_speech.call<std::string>("getLanguage");
233  return language;
234 }
235 
239 bool isDepthStereo(const qi::SessionPtr &session) {
240  std::vector<std::string> sensor_names;
241 
242  try {
243  qi::AnyObject p_motion = session->service("ALMotion");
244  sensor_names = p_motion.call<std::vector<std::string> >("getSensorNames");
245 
246  if (std::find(sensor_names.begin(),
247  sensor_names.end(),
248  "CameraStereo") != sensor_names.end()) {
249  return true;
250  }
251 
252  else {
253  return false;
254  }
255 
256  } catch (const std::exception &e) {
257  std::cerr << e.what() << std::endl;
258  return false;
259  }
260 }
261 
262 } // driver
263 } // helpers
264 } // naoqi
const naoqi_bridge_msgs::RobotInfo & getRobotInfo(const qi::SessionPtr &session)
#define RESETCOLOR
Definition: tools.hpp:22
#define BOLDCYAN
Definition: tools.hpp:28
static naoqi_bridge_msgs::RobotInfo & getRobotInfoLocal(const qi::SessionPtr &session)
bool isDepthStereo(const qi::SessionPtr &session)
std::string & getLanguage(const qi::SessionPtr &session)
#define BOLDYELLOW
Definition: tools.hpp:27
bool & setLanguage(const qi::SessionPtr &session, naoqi_bridge_msgs::SetStringRequest req)
const robot::Robot & getRobot(const qi::SessionPtr &session)


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 15 2020 03:24:26