2
* @file real_shadowhand.cpp
3
* @author Ugo Cupcic <ugo@shadowrobot.com>, Contact <contact@shadowrobot.com>
4
* @date Tue May 25 17:50:42 2010
12
#include <robot/robot.h>
13
#include <robot/hand.h>
14
#include <robot/hand_protocol.h>
18
#include "sr_hand/hand/real_shadowhand.h"
22
RealShadowhand::RealShadowhand()
25
/* We need to attach the program to the robot, or fail if we cannot. */
28
ROS_FATAL("Robot interface broken\n");
32
/* We need to attach the program to the hand as well, or fail if we cannot. */
35
ROS_FATAL("Hand interface broken\n");
43
RealShadowhand::~RealShadowhand()
47
void RealShadowhand::initializeMap()
51
tmpData.position = 0.0;
53
tmpData.temperature = 0.0;
54
tmpData.current = 0.0;
58
for(unsigned int i=0; i<NUM_HAND_JOINTS + 4; i++)
60
std::string name=hand_joints[i].joint_name;
61
tmpData.jointIndex = i;
63
joints_map[name] = tmpData;
65
ROS_INFO("NAME[%d]: %s ", i, name.c_str());
69
parameters_map["d"] = PARAM_d;
70
parameters_map["i"] = PARAM_i;
71
parameters_map["p"] = PARAM_p;
72
parameters_map["target"] = PARAM_target;
73
parameters_map["sensor"] = PARAM_sensor;
75
parameters_map["valve"] = PARAM_valve;
76
parameters_map["dead"] = PARAM_deadband;
77
parameters_map["deadband"] = PARAM_deadband;
78
parameters_map["imax"] = PARAM_imax;
79
parameters_map["offset"] = PARAM_output_offset;
80
parameters_map["shift"] = PARAM_shift;
81
parameters_map["max"] = PARAM_output_max;
83
//! the parameters for the motors
84
parameters_map["motor_maxforce"] = PARAM_motor_maxforce;
85
parameters_map["motor_safeforce"] = PARAM_motor_safeforce;
87
parameters_map["force_p"] = PARAM_force_p;
88
parameters_map["force_i"] = PARAM_force_i;
89
parameters_map["force_d"] = PARAM_force_d;
91
parameters_map["force_imax"] = PARAM_force_imax;
92
parameters_map["force_out_shift"] = PARAM_force_out_shift;
93
parameters_map["force_deadband"] = PARAM_force_deadband;
94
parameters_map["force_offset"] = PARAM_force_offset;
96
parameters_map["sensor_imax"] = PARAM_sensor_imax;
97
parameters_map["sensor_out_shift"] = PARAM_sensor_out_shift;
98
parameters_map["sensor_deadband"] = PARAM_sensor_deadband;
99
parameters_map["sensor_offset"] = PARAM_sensor_offset;
100
parameters_map["max_temp"] = PARAM_max_temperature;
101
parameters_map["max_temperature"] = PARAM_max_temperature;
102
parameters_map["max_current"] = PARAM_max_current;
105
short RealShadowhand::sendupdate(std::string joint_name, double target)
107
//search the sensor in the hash_map
108
JointsMap::iterator iter = joints_map.find(joint_name);
110
if(iter != joints_map.end())
112
JointData tmpData = joints_map.find(joint_name)->second;
113
int index_hand_joints = tmpData.jointIndex;
115
//trim to the correct range
116
if (target < hand_joints[index_hand_joints].min_angle)
117
target = hand_joints[index_hand_joints].min_angle;
118
if (target > hand_joints[index_hand_joints].max_angle)
119
target = hand_joints[index_hand_joints].max_angle;
121
//here we update the actual hand angles
122
robot_update_sensor(&hand_joints[index_hand_joints].joint_target, target);
127
ROS_ERROR("Can't open sensor [%s]", joint_name.c_str());
133
JointData RealShadowhand::getJointData(std::string joint_name)
135
JointsMap::iterator iter = joints_map.find(joint_name);
138
if(iter == joints_map.end())
140
ROS_ERROR("Joint %s not found.", joint_name.c_str());
142
noData.position = 0.0;
144
noData.temperature = 0.0;
145
noData.current = 0.0;
148
noData.jointIndex = 0;
153
JointData tmpData = iter->second;
154
int index = tmpData.jointIndex;
156
tmpData.position = robot_read_sensor(&hand_joints[index].position);
157
tmpData.target = robot_read_sensor(&hand_joints[index].joint_target);
160
if (hand_joints[index].a.smartmotor.has_sensors)
162
tmpData.temperature = robot_read_sensor( &hand_joints[index].a.smartmotor.temperature );
163
tmpData.current = robot_read_sensor( &hand_joints[index].a.smartmotor.current );
164
tmpData.force = robot_read_sensor( &hand_joints[index].a.smartmotor.torque );
168
joints_map[joint_name] = tmpData;
174
shadowhand::joints_data msg;
175
std::vector<shadowhand::joint> myVector;
177
sensor_msgs::JointState jointstate_msg;
179
//get the data from the hand
180
for (unsigned int i=0; i<NUM_HAND_JOINTS + 4; ++i)
182
shadowhand::joint joint;
184
std::string jointName = hand_joints[i].joint_name;
185
joint.joint_name = jointName;
187
// /!\ don't publish the joint0s to joint_states otherwise
188
// it won't match the model
189
bool isJoint0 = false;
190
std::string joint0 = "J0";
192
int foundAt = jointName.find(joint0);
197
// jointstate_msg.effort.push_back(0.0);
200
//publish 0 for joint1 effort
201
std::string joint1 = "FJ1";
202
foundAt = jointName.find(joint1);
204
jointstate_msg.effort.push_back(0.0);
209
joint.joint_position = robot_read_sensor(&hand_joints[i].position);
211
jointstate_msg.name.push_back(hand_joints[i].joint_name);
213
jointstate_msg.position.push_back( (robot_read_sensor(&hand_joints[i].position)*3.14159f / 180.0f));
215
//WARNING the velocity vector contains the targets... IT's a
217
//TODO: remove this hack
218
jointstate_msg.velocity.push_back((robot_read_sensor(&hand_joints[i].joint_target)*3.14159f / 180.0f));
220
joint.joint_target = robot_read_sensor(&hand_joints[i].joint_target);
223
if (hand_joints[i].a.smartmotor.has_sensors)
226
joint.joint_torque = robot_read_sensor( &hand_joints[i].a.smartmotor.torque );
228
jointstate_msg.effort.push_back(robot_read_sensor( &hand_joints[i].a.smartmotor.torque ));
230
//temperature & current
231
joint.joint_temperature = robot_read_sensor(&hand_joints[i].a.smartmotor.temperature);
232
joint.joint_current = robot_read_sensor(&hand_joints[i].a.smartmotor.current);
234
//check for error_flags
235
uint64_t uuid = robot_node_id(hand_joints[i].a.smartmotor.nodename);
236
struct hand_protocol_flags fl;
237
fl = hand_protocol_get_status_flags(uuid);
240
struct hand_protocol_flags_smart_motor f;
241
f = fl.u.smart_motor;
242
//concatenate the flags in a stringstream
243
std::stringstream ss;
245
bool at_least_one_error_flag = false;
248
at_least_one_error_flag = true;
250
ROS_ERROR( "[%s]: NFAULT", hand_joints[i].joint_name );
252
if( f.temperature_cutout )
254
at_least_one_error_flag = true;
257
if( f.current_throttle )
259
at_least_one_error_flag = true;
262
if( f.force_hard_limit )
264
at_least_one_error_flag = true;
267
if( hand_protocol_dead(uuid) )
269
at_least_one_error_flag = true;
273
//set the message flags
274
joint.error_flag = ss.str();
275
//if a flag is up, then print a warning as well
276
if( at_least_one_error_flag )
277
ROS_WARN( "[%s]: %s", hand_joints[i].joint_name, (ss.str()).c_str());
281
myVector.push_back(joint);
284
//set the standard message
285
msg.joints_list_length = NUM_HAND_JOINTS;
286
msg.joints_list = myVector;
288
//publish standard data (pos, target, current, temp, force, ...)
289
shadowhand_pub.publish(msg);
291
//publish JointState message
292
shadowhand_jointstate_pub.publish(jointstate_msg);
296
Shadowhand::JointsMap RealShadowhand::getAllJointsData()
298
//update the map for each joints
299
for(JointsMap::const_iterator it = joints_map.begin(); it != joints_map.end(); ++it)
300
getJointData(it->first);
306
short RealShadowhand::setContrl(std::string contrlr_name, JointControllerData ctrlr_data)
308
struct controller_config myConfig;
309
memset(&myConfig, 0, sizeof(myConfig));
311
//set the nodename from contrlr_name
312
myConfig.nodename = contrlr_name.c_str();
314
controller_read_from_hardware(&myConfig);
315
ROS_DEBUG("%s", controller_to_string(&myConfig));
317
for( unsigned int i = 0; i < ctrlr_data.data.size() ; ++i )
319
std::string name = ctrlr_data.data[i].name;
320
ParametersMap::iterator iter = parameters_map.find(name);
322
//parameter not found
323
if(iter == parameters_map.end())
325
ROS_ERROR("Parameter %s not found.", name.c_str());
330
controller_update_param( &myConfig, (controller_param)iter->second, ctrlr_data.data[i].value.c_str() );
333
int result_ctrlr = controller_write_to_hardware(&myConfig);
336
ROS_ERROR("Failed to update contrlr (%s)", myConfig.nodename );
343
JointControllerData RealShadowhand::getContrl(std::string contrlr_name)
345
struct controller_config myConfig;
346
memset(&myConfig, 0, sizeof(myConfig));
348
//set the nodename from contrlr_name
349
myConfig.nodename = contrlr_name.c_str();
351
controller_read_from_hardware(&myConfig);
353
JointControllerData tmp_data;
355
ROS_WARN("The get contrlr function is not implemented in the real shadowhand.");
361
short RealShadowhand::setConfig(std::vector<std::string> myConfig)
363
ROS_WARN("The set config function is not implemented in the real shadowhand.");
367
hand_protocol_config_t cfg;
368
hand_protocol_get_config(cfg);
370
//set the transmit rate value
371
int value = msg->rate_value;
372
cfg->u.palm.tx_freq[num]=value;
374
//send the config to the palm.
375
hand_protocol_set_config(cfg);
382
void RealShadowhand::getConfig(std::string joint_name)
384
ROS_WARN("The get config function is not yet implement in the real shadow hand.");
388
char* ShadowhandDiagnosticer::get_setpoint_name(uint16_t s_num)
390
// struct sensor temp;
391
static char name_buff[256];
393
snprintf(name_buff, 256, "%s.%d", "smart_motor_setpoints", s_num);
395
//TODO: get the sensor name
396
if (0==robot_channel_to_sensor("smart_motor_setpoints", s_num, &temp))
398
const char *name = robot_sensor_calibration_name(&temp);
404
std::vector<DiagnosticData> RealShadowhand::getDiagnostics()
406
std::vector<DiagnosticData> returnVector;
408
DiagnosticData tmpData;
410
//concatenate the flags in a stringstream
411
std::stringstream ss;
414
//get the data from the hand
415
for (unsigned int index=0; index<NUM_HAND_JOINTS + 4; ++index)
417
tmpData.joint_name = std::string( hand_joints[index].joint_name );
420
tmpData.position = robot_read_sensor(&hand_joints[index].position);
421
tmpData.target = robot_read_sensor(&hand_joints[index].joint_target);
424
if (hand_joints[index].a.smartmotor.has_sensors)
426
tmpData.temperature = robot_read_sensor( &hand_joints[index].a.smartmotor.temperature );
427
tmpData.current = robot_read_sensor( &hand_joints[index].a.smartmotor.current );
428
tmpData.force = robot_read_sensor( &hand_joints[index].a.smartmotor.torque );
430
//check for error_flags
431
uint64_t uuid = robot_node_id(hand_joints[index].a.smartmotor.nodename);
432
struct hand_protocol_flags fl;
433
fl = hand_protocol_get_status_flags(uuid);
436
struct hand_protocol_flags_smart_motor f;
437
f = fl.u.smart_motor;
439
//empty the stringstream
442
bool at_least_one_error_flag = false;
445
at_least_one_error_flag = true;
447
ROS_ERROR( "[%s]: NFAULT", hand_joints[index].joint_name );
449
if( f.temperature_cutout )
451
at_least_one_error_flag = true;
454
if( f.current_throttle )
456
at_least_one_error_flag = true;
459
if( f.force_hard_limit )
461
at_least_one_error_flag = true;
464
if( hand_protocol_dead(uuid) )
466
at_least_one_error_flag = true;
470
//set the message flags
471
tmpData.flags = ss.str();
472
//if a flag is up, then print a warning as well
473
if( at_least_one_error_flag )
475
ROS_WARN( "[%s]: %s", hand_joints[index].joint_name, (ss.str()).c_str());
481
returnVector.push_back(tmpData);