dcm.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 SoftBank Robotics Europe
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 // ROS Headers
19 #include <ros/ros.h>
20 
21 #include "naoqi_dcm_driver/dcm.hpp"
23 
24 DCM::DCM(const qi::SessionPtr& session,
25  const double &controller_freq):
26  controller_freq_(controller_freq)
27 {
28  try
29  {
30  dcm_proxy_ = session->service("DCM");
31  }
32  catch (const std::exception& e)
33  {
34  ROS_ERROR("DCM: Failed to connect to DCM Proxy!\n\tTrace: %s", e.what());
35  }
36 }
37 
38 bool DCM::init(const std::vector <std::string> &joints)
39 {
40  // DCM Motion Commands Initialization
42 
43  // Create an alias for Joints Actuators
44  if (!createPositionActuatorAlias(joints))
45  return false;
46 
47  // Create an alias for Joints Hardness
48  if (!createHardnessActuatorAlias(joints))
49  return false;
50 
51  return true;
52 }
53 
54 void DCM::createPositionActuatorCommand(const std::vector <std::string> &joints)
55 {
56  //get the number of active joints
57  int joints_nbr = joints.size();
58 
59  // Create the Motion Command
60  commands_.reserve(4);
61  commands_.resize(4);
62  commands_[0] = qi::AnyValue(qi::AnyReference::from("jointActuator"), false, false);
63  commands_[1] = qi::AnyValue(qi::AnyReference::from("ClearAll"), false, false);
64  commands_[2] = qi::AnyValue(qi::AnyReference::from("time-mixed"), false, false);
65 
66  // set keys
67  commands_values_.reserve(joints_nbr);
68  commands_values_.resize(joints_nbr);
69  for(int i=0; i<joints_nbr; ++i)
70  {
71  commands_values_[i].resize(1);
72  commands_values_[i][0].resize(3);
73  commands_values_[i][0][2] = qi::AnyValue(qi::AnyReference::from(0), false, false);
74  }
75 
76  commands_[3] = qi::AnyValue(qi::AnyReference::from(commands_values_), false, false);
77 }
78 
79 bool DCM::createPositionActuatorAlias(const std::vector <std::string> &joints)
80 {
81  // prepare the command
82  std::vector <qi::AnyValue> commandAlias;
83  commandAlias.reserve(2);
84  commandAlias.resize(2);
85  commandAlias[0] = qi::AnyValue(qi::AnyReference::from("jointActuator"), false, false);
86 
87  // set joints actuators keys
88  std::vector <qi::AnyValue> commandAlias_keys;
89  commandAlias_keys.resize(joints.size());
90  for(int i=0; i<joints.size(); ++i)
91  {
92  std::string key = "Device/SubDeviceList/" + joints.at(i) + "/Position/Actuator/Value";
93  commandAlias_keys[i] = qi::AnyValue(qi::AnyReference::from(key), false, false);
94  }
95  commandAlias[1] = qi::AnyValue(qi::AnyReference::from(commandAlias_keys), false, false);
96 
97  qi::AnyValue commandAlias_qi(qi::AnyReference::from(commandAlias), false, false);
98 
99  // Create alias
100  try
101  {
102  dcm_proxy_.call<void>("createAlias", commandAlias_qi);
103  }
104  catch(const std::exception& e)
105  {
106  ROS_ERROR("DCM: Could not create alias for jonts positions!\n\tTrace: %s", e.what());
107  return false;
108  }
109  return true;
110 }
111 
112 bool DCM::createHardnessActuatorAlias(const std::vector <std::string> &joints)
113 {
114  //prepare a command
115  std::vector <qi::AnyValue> commandAlias;
116  commandAlias.reserve(2);
117  commandAlias.resize(2);
118  commandAlias[0] = qi::AnyValue(qi::AnyReference::from("jointStiffness"), false, false);
119 
120  //set stiffness keys
121  std::vector <qi::AnyValue> commandAlias_keys;
122  for(int i=0; i<joints.size(); ++i)
123  {
124  if((joints.at(i) == "RHipYawPitch") //for mimic joints: Nao only
125  || (joints.at(i).find("Wheel") != std::string::npos))
126  continue;
127 
128  std::string key = "Device/SubDeviceList/" + joints.at(i) + "/Hardness/Actuator/Value";
129  commandAlias_keys.push_back(qi::AnyValue(qi::AnyReference::from(key), false, false));
130  }
131  commandAlias[1] = qi::AnyValue(qi::AnyReference::from(commandAlias_keys), false, false);
132 
133  qi::AnyValue commandAlias_qi(qi::AnyReference::from(commandAlias), false, false);
134 
135  // Create alias
136  try
137  {
138  dcm_proxy_.call<void>("createAlias", commandAlias_qi);
139  }
140  catch(const std::exception& e)
141  {
142  ROS_ERROR("DCM: Could not create alias for joints hardness!\n\tTrace: %s", e.what());
143  return false;
144  }
145  return true;
146 }
147 
148 bool DCM::DCMAliasTimedCommand(const std::string& alias,
149  const float& value,
150  const int& timeOffset,
151  const std::string& type_update)
152 {
153  //get DCM time at timeOffset
154  int time = getTime(timeOffset);
155 
156  // Create Alias timed-command
157  qi::AnyValue command_qi;
158  try
159  {
160  std::vector <qi::AnyValue> command;
161  command.resize(3);
162  command[0] = qi::AnyValue(qi::AnyReference::from(alias), false, false);
163  command[1] = qi::AnyValue(qi::AnyReference::from(type_update), false, false);
164 
165  std::vector <std::vector <qi::AnyValue> > command_keys;
166  command_keys.resize(1);
167  command_keys[0].resize(2);
168  command_keys[0][0] = qi::AnyValue(qi::AnyReference::from(value), false, false);
169  command_keys[0][1] = qi::AnyValue(qi::AnyReference::from(time), false, false);
170 
171  command[2] = qi::AnyValue(qi::AnyReference::from(command_keys), false, false);
172  command_qi = qi::AnyValue(qi::AnyReference::from(command), false, false);
173  }
174  catch(const std::exception& e)
175  {
176  ROS_ERROR("DCM: Failed to convert to qi::AnyValue \n\tTrace: %s", e.what());
177  }
178 
179  // Execute Alias timed-command
180  try
181  {
182  dcm_proxy_.call<void>("set", command_qi);
183  return false;
184  }
185  catch(const std::exception& e)
186  {
187  ROS_ERROR("DCM: Failed to execute DCM timed-command! \n\tTrace: %s", e.what());
188  }
189  return true;
190 }
191 
192 int DCM::getTime(const int &offset)
193 {
194  int res;
195  try
196  {
197  res = dcm_proxy_.call<int>("getTime", 0) + offset;
198  }
199  catch(const std::exception& e)
200  {
201  ROS_ERROR("DCM: Failed to get time! \n\tTrace: %s", e.what());
202  }
203  return res;
204 }
205 
206 void DCM::writeJoints(const std::vector <double> &joint_commands)
207 {
208  int time = getTime(static_cast<int>(5000.0/controller_freq_));
209 
210  // Create Alias timed-command
211  qi::AnyValue commands_qi;
212  try
213  {
214  std::vector<double>::const_iterator it_comm = joint_commands.begin();
215  for(int i=0; i<joint_commands.size(); ++i, ++it_comm)
216  {
217  commands_values_[i][0][0] = qi::AnyValue(qi::AnyReference::from(static_cast<float>(*it_comm)), false, false);
218  commands_values_[i][0][1] = qi::AnyValue(qi::AnyReference::from(time), false, false);
219  }
220 
221  commands_[3] = qi::AnyValue(qi::AnyReference::from(commands_values_), false, false);
222  commands_qi = qi::AnyValue(qi::AnyReference::from(commands_), false, false);
223  }
224  catch(const std::exception& e)
225  {
226  ROS_ERROR("DCM: Failed to convert to qi::AnyValue \n\tTrace: %s", e.what());
227  }
228 
229  // Execute Alias timed-command
230  try
231  {
232  dcm_proxy_.call<void>("setAlias", commands_qi);
233  }
234  catch(const std::exception& e)
235  {
236  ROS_ERROR("DCM: Failed to execute DCM timed-command! \n\tTrace: %s", e.what());
237  }
238 }
239 
240 bool DCM::setStiffness(const float &stiffness)
241 {
242  //set stiffness with 1sec timeOffset
243  return DCMAliasTimedCommand("jointStiffness", stiffness, 1000);
244 }
std::vector< std::vector< std::vector< qi::AnyValue > > > commands_values_
Definition: dcm.hpp:68
bool init(const std::vector< std::string > &joints)
initialize all Aliases
Definition: dcm.cpp:38
void writeJoints(const std::vector< double > &joint_commands)
update joints values
Definition: dcm.cpp:206
double controller_freq_
Definition: dcm.hpp:71
ROSLIB_DECL std::string command(const std::string &cmd)
DCM(const qi::SessionPtr &session, const double &controller_freq)
Definition: dcm.cpp:24
bool DCMAliasTimedCommand(const std::string &alias, const float &values, const int &timeOffset, const std::string &type_update="Merge")
DCM Wrapper Method.
Definition: dcm.cpp:148
std::vector< qi::AnyValue > commands_
Definition: dcm.hpp:65
qi::AnyObject dcm_proxy_
Definition: dcm.hpp:62
void createPositionActuatorCommand(const std::vector< std::string > &joints)
initialize of DCM Motion commands
Definition: dcm.cpp:54
bool setStiffness(const float &stiffness)
update joints stiffness
Definition: dcm.cpp:240
bool createHardnessActuatorAlias(const std::vector< std::string > &joints)
create Hardness Actuator Alias
Definition: dcm.cpp:112
bool createPositionActuatorAlias(const std::vector< std::string > &joints)
create Position Actuator Alias
Definition: dcm.cpp:79
#define ROS_ERROR(...)
int getTime(const int &offset)
get time from the DCM proxy
Definition: dcm.cpp:192


naoqi_dcm_driver
Author(s): Konstantinos Chatzilygeroudis , Mikael Arguedas , Karsten Knese , Natalia Lyubova
autogenerated on Thu Jul 25 2019 03:49:27