real_arm.cpp
Go to the documentation of this file.
1 
26 #include "sr_hand/hand/real_arm.h"
27 // our robot code
28 #include <robot/robot.h>
29 #include <robot/hand.h>
30 #include <robot/hand_protocol.h>
31 
32 #include <string>
33 #include <vector>
34 #include <time.h>
35 #include <ros/ros.h>
36 
37 namespace shadowrobot
38 {
41  {
42  /* We need to attach the program to the robot, or fail if we cannot. */
43  if (robot_init() < 0)
44  {
45  ROS_FATAL("Robot interface broken\n");
46  ROS_BREAK();
47  }
48 
49  /* We need to attach the program to the hand as well, or fail if we cannot. */
50  if (hand_init() < 0)
51  {
52  ROS_FATAL("Arm interface broken\n");
53  ROS_BREAK();
54  }
55 
56  srand(time(NULL));
57  initializeMap();
58  }
59 
61  {
62  }
63 
65  {
66  joints_map_mutex.lock();
67  JointData tmpData;
68 
69  tmpData.position = 0.0;
70  tmpData.target = 0.0;
71  tmpData.temperature = 0.0;
72  tmpData.current = 0.0;
73  tmpData.force = 0.0;
74  tmpData.flags = "";
75 
76  for (unsigned int i = START_OF_ARM; i < NUM_HAND_JOINTS; ++i)
77  {
78  std::string name = hand_joints[i].joint_name;
79  tmpData.jointIndex = i;
80 
81  joints_map[name] = tmpData;
82 
83  ROS_INFO("NAME[%d]: %s ", i, name.c_str());
84  }
85 
86  joints_map_mutex.unlock();
87  }
88 
89  int16_t RealArm::sendupdate(std::string joint_name, double target)
90  {
91  joints_map_mutex.lock();
92 
93  // search the sensor in the hash_map
94  JointsMap::iterator iter = joints_map.find(joint_name);
95 
96  if (iter != joints_map.end())
97  {
98  JointData tmpData = joints_map.find(joint_name)->second;
99  int index_arm_joints = tmpData.jointIndex;
100 
101  // trim to the correct range
102  if (target < hand_joints[index_arm_joints].min_angle)
103  {
104  target = hand_joints[index_arm_joints].min_angle;
105  }
106  if (target > hand_joints[index_arm_joints].max_angle)
107  {
108  target = hand_joints[index_arm_joints].max_angle;
109  }
110 
111  // here we update the actual hand angles
112  robot_update_sensor(&hand_joints[index_arm_joints].joint_target, target);
113  joints_map_mutex.unlock();
114  return 0;
115  }
116 
117  ROS_DEBUG("Joint %s not found", joint_name.c_str());
118 
119  joints_map_mutex.unlock();
120  return -1;
121  }
122 
123  JointData RealArm::getJointData(std::string joint_name)
124  {
125  joints_map_mutex.lock();
126  JointsMap::iterator iter = joints_map.find(joint_name);
127 
128  // joint not found
129  if (iter == joints_map.end())
130  {
131  ROS_ERROR("Joint %s not found.", joint_name.c_str());
132  JointData noData;
133  noData.position = 0.0;
134  noData.target = 0.0;
135  noData.temperature = 0.0;
136  noData.current = 0.0;
137  noData.force = 0.0;
138  noData.flags = "";
139  noData.jointIndex = 0;
140 
141  joints_map_mutex.unlock();
142  return noData;
143  }
144 
145  // joint found
146  JointData tmpData = iter->second;
147  int index = tmpData.jointIndex;
148 
149  tmpData.position = robot_read_sensor(&hand_joints[index].position);
150  tmpData.target = robot_read_sensor(&hand_joints[index].joint_target);
151 
152  joints_map[joint_name] = tmpData;
153 
154  joints_map_mutex.unlock();
155  return tmpData;
156  }
157 
159  {
160  // update the map for each joints
161  for (JointsMap::const_iterator it = joints_map.begin(); it != joints_map.end(); ++it)
162  {
163  getJointData(it->first);
164  }
165 
167 
168  // return the map
169  return tmp;
170  }
171 
172  int16_t RealArm::setContrl(std::string contrlr_name, JointControllerData ctrlr_data)
173  {
174  ROS_WARN("The setContrl method is not yet implemented");
175  return 0;
176  }
177 
178  JointControllerData RealArm::getContrl(std::string contrlr_name)
179  {
180  ROS_WARN("The getContrl method is not yet implemented");
181  JointControllerData no_result;
182  return no_result;
183  }
184 
185  int16_t RealArm::setConfig(std::vector <std::string> myConfig)
186  {
187  ROS_WARN("The set config function is not implemented in the virtual arm.");
188  return 0;
189  }
190 
191  void RealArm::getConfig(std::string joint_name)
192  {
193  ROS_WARN("The get config function is not implemented in the virtual arm.");
194  }
195 
196  std::vector <DiagnosticData> RealArm::getDiagnostics()
197  {
198  joints_map_mutex.lock();
199  std::vector <DiagnosticData> returnVect;
200 
201  // @todo: read diagnostic data from the robot
202 
203  for (JointsMap::const_iterator it = joints_map.begin(); it != joints_map.end(); ++it)
204  {
205  DiagnosticData tmpDiag;
206  tmpDiag.joint_name = it->first;
207  tmpDiag.level = 0;
208  tmpDiag.flags = "";
209  tmpDiag.target_sensor_num = 0;
210  tmpDiag.target = it->second.target;
211  tmpDiag.position_sensor_num = 0;
212  tmpDiag.position = it->second.position;
213 
214  returnVect.push_back(tmpDiag);
215  }
216 
217  joints_map_mutex.unlock();
218  return returnVect;
219  }
220 
221 } // namespace shadowrobot
#define ROS_FATAL(...)
std::string flags
a string containing flags: FORCE, TEMPERATURE, ... indicating the different cutouts / warning sent by...
std::string joint_name
the name of the joint
virtual int16_t setContrl(std::string contrlr_name, JointControllerData ctrlr_data)
Definition: real_arm.cpp:172
std::map< std::string, JointData > JointsMap
int target_sensor_num
the channel number of the target sensor
int position_sensor_num
the channel number of the position sensor
#define ROS_WARN(...)
virtual JointData getJointData(std::string joint_name)
Definition: real_arm.cpp:123
virtual ~RealArm()
Definition: real_arm.cpp:60
virtual JointsMap getAllJointsData()
Definition: real_arm.cpp:158
#define ROS_INFO(...)
JointsMap joints_map
A mapping between the joint names and the information regarding those joints.
virtual int16_t setConfig(std::vector< std::string > myConfig)
Definition: real_arm.cpp:185
This is a parent class for the different types of Shadowhand we can have. It makes it possible to swa...
virtual std::vector< DiagnosticData > getDiagnostics()
Definition: real_arm.cpp:196
virtual void getConfig(std::string joint_name)
Definition: real_arm.cpp:191
int16_t level
the level of alert: 0 = OK, 1 = WARNING, 2 = ERROR
The real arm is a ROS interface to Shadow Robot muscle arm.
double target
the actual value of the target
#define ROS_BREAK()
#define ROS_ERROR(...)
virtual int16_t sendupdate(std::string joint_name, double target)
Definition: real_arm.cpp:89
double position
the actual value of the position
virtual JointControllerData getContrl(std::string ctrlr_name)
Definition: real_arm.cpp:178
#define ROS_DEBUG(...)


sr_hand
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:53