Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "ros/ros.h"
00032 #include <stdio.h>
00033 #include <math.h>
00034
00035 #include "std_msgs/Float64.h"
00036 #include "corobot_msgs/MoveArm.h"
00037 #include "corobot_msgs/ServoPosition.h"
00038 #include "corobot_msgs/ServoType.h"
00039
00040
00049
00050 typedef enum
00051 {
00052 base_rotation,
00053 shoulder,
00054 elbow,
00055 wrist_flex,
00056 wrist_rotation,
00057 gripper
00058 } servo_type;
00059
00060
00061 typedef enum
00062 {
00063 arbotix,
00064 ssc32,
00065 phidget
00066 } hardware_controller;
00067
00068
00069 typedef struct
00070 {
00071 servo_type type;
00072 int id;
00073 int min_angle;
00074 int max_angle;
00075 } servo;
00076
00077
00078 ros::Publisher position_pub, type_pub;
00079
00080
00081 double centerOffset = 0.0;
00082
00083
00084 servo * servos = NULL;
00085
00086
00087 int number_servo = 0;
00088
00089
00090 hardware_controller controller;
00091
00092
00093 ros::Publisher * arbotix_publisher = NULL;
00094
00095
00099 void setServoPosition(const corobot_msgs::MoveArm &msg)
00100 {
00101 corobot_msgs::ServoPosition msg_sending;
00102
00103 for(int i=0; i<number_servo; i++)
00104 {
00105 msg_sending.index = -1;
00106
00107
00108 if(msg.index == msg.BASE_ROTATION && servos[i].type == base_rotation)
00109 msg_sending.index = servos[i].id;
00110 if(msg.index == msg.SHOULDER && servos[i].type == shoulder)
00111 msg_sending.index = servos[i].id;
00112 if(msg.index == msg.ELBOW && servos[i].type == elbow)
00113 msg_sending.index = servos[i].id;
00114 if(msg.index == msg.WRIST_FLEX && servos[i].type == wrist_flex)
00115 msg_sending.index = servos[i].id;
00116 if(msg.index == msg.WRIST_ROTATION && servos[i].type == wrist_rotation)
00117 msg_sending.index = servos[i].id;
00118 if(msg.index == msg.GRIPPER && servos[i].type == gripper)
00119 msg_sending.index = servos[i].id;
00120
00121
00122 if(msg_sending.index != -1)
00123 {
00124 msg_sending.position = msg.position + centerOffset;
00125
00126
00127 if(msg_sending.position < servos[i].min_angle)
00128 msg_sending.position = servos[i].min_angle;
00129 else if(msg_sending.position > servos[i].max_angle)
00130 msg_sending.position = servos[i].max_angle;
00131
00132
00133 if(controller == arbotix)
00134 {
00135 std_msgs::Float64 msg_arbotix;
00136
00137 msg_arbotix.data = (msg_sending.position / 180) * M_PI;
00138 arbotix_publisher[i].publish(msg_arbotix);
00139 }
00140 else
00141 position_pub.publish(msg_sending);
00142 }
00143 }
00144 }
00145
00149 void init_servos_db(XmlRpc::XmlRpcValue dynamixels, ros::NodeHandle n)
00150 {
00151 if(dynamixels.hasMember("base"))
00152 {
00153 servos[number_servo].type = base_rotation;
00154 servos[number_servo].id = (int) dynamixels["base"]["id"];
00155 servos[number_servo].min_angle = (int) dynamixels["base"]["min_angle"];
00156 servos[number_servo].max_angle = (int) dynamixels["base"]["max_angle"];
00157
00158
00159 if(controller == arbotix)
00160 arbotix_publisher[number_servo] = n.advertise<std_msgs::Float64>("/base/command", 100);
00161
00162 number_servo++;
00163 }
00164 if(dynamixels.hasMember("shoulder"))
00165 {
00166 servos[number_servo].type = shoulder;
00167 servos[number_servo].id = (int) dynamixels["shoulder"]["id"];
00168 servos[number_servo].min_angle = (int) dynamixels["shoulder"]["min_angle"];
00169 servos[number_servo].max_angle = (int) dynamixels["shoulder"]["max_angle"];
00170
00171
00172 if(controller == arbotix)
00173 arbotix_publisher[number_servo] = n.advertise<std_msgs::Float64>("/shoulder/command", 100);
00174
00175 number_servo++;
00176 }
00177 if(dynamixels.hasMember("shoulder2"))
00178 {
00179 servos[number_servo].type = shoulder;
00180 servos[number_servo].id = (int) dynamixels["shoulder2"]["id"];
00181 servos[number_servo].min_angle = (int) dynamixels["shoulder2"]["min_angle"];
00182 servos[number_servo].max_angle = (int) dynamixels["shoulder2"]["max_angle"];
00183
00184
00185 if(controller == arbotix)
00186 arbotix_publisher[number_servo] = n.advertise<std_msgs::Float64>("/shoulder2/command", 100);
00187
00188 number_servo++;
00189 }
00190 if(dynamixels.hasMember("elbow"))
00191 {
00192 servos[number_servo].type = elbow;
00193 servos[number_servo].id = (int) dynamixels["elbow"]["id"];
00194 servos[number_servo].min_angle = (int) dynamixels["elbow"]["min_angle"];
00195 servos[number_servo].max_angle = (int) dynamixels["elbow"]["max_angle"];
00196
00197
00198 if(controller == arbotix)
00199 arbotix_publisher[number_servo] = n.advertise<std_msgs::Float64>("/elbow/command", 100);
00200
00201 number_servo++;
00202 }
00203 if(dynamixels.hasMember("elbow2"))
00204 {
00205 servos[number_servo].type = elbow;
00206 servos[number_servo].id = (int) dynamixels["elbow2"]["id"];
00207 servos[number_servo].min_angle = (int) dynamixels["elbow2"]["min_angle"];
00208 servos[number_servo].max_angle = (int) dynamixels["elbow2"]["max_angle"];
00209
00210
00211 if(controller == arbotix)
00212 arbotix_publisher[number_servo] = n.advertise<std_msgs::Float64>("/elbow2/command", 100);
00213
00214 number_servo++;
00215 }
00216 if(dynamixels.hasMember("wrist_flex"))
00217 {
00218 servos[number_servo].type = wrist_flex;
00219 servos[number_servo].id = (int) dynamixels["wrist_flex"]["id"];
00220 servos[number_servo].min_angle = (int) dynamixels["wrist_flex"]["min_angle"];
00221 servos[number_servo].max_angle = (int) dynamixels["wrist_flex"]["max_angle"];
00222
00223
00224 if(controller == arbotix)
00225 arbotix_publisher[number_servo] = n.advertise<std_msgs::Float64>("/wrist_flex/command", 100);
00226
00227 number_servo++;
00228 }
00229 if(dynamixels.hasMember("wrist_rotation"))
00230 {
00231 servos[number_servo].type = wrist_rotation;
00232 servos[number_servo].id = (int) dynamixels["wrist_rotation"]["id"];
00233 servos[number_servo].min_angle = (int) dynamixels["wrist_rotation"]["min_angle"];
00234 servos[number_servo].max_angle = (int) dynamixels["wrist_rotation"]["max_angle"];
00235
00236
00237 if(controller == arbotix)
00238 arbotix_publisher[number_servo] = n.advertise<std_msgs::Float64>("/wrist_rotation/command", 100);
00239
00240 number_servo++;
00241 }
00242 if(dynamixels.hasMember("gripper"))
00243 {
00244 servos[number_servo].type = gripper;
00245 servos[number_servo].id = (int) dynamixels["gripper"]["id"];
00246 servos[number_servo].min_angle = -180;
00247 servos[number_servo].max_angle = 180;
00248
00249
00250 if(controller == arbotix)
00251 arbotix_publisher[number_servo] = n.advertise<std_msgs::Float64>("/gripper/command", 100);
00252
00253 number_servo++;
00254 }
00255 }
00256
00257 int main(int argc, char **argv)
00258 {
00259 ros::init(argc, argv, "corobot_arm");
00260 ros::NodeHandle n;
00261 ros::NodeHandle n_private("~");
00262
00263
00264 ros::Subscriber armPos= n.subscribe("armPosition", 100, setServoPosition);
00265
00266
00267
00268 n_private.param("offset", centerOffset, 0.0);
00269
00270
00271
00272 XmlRpc::XmlRpcValue dynamixels;
00273 XmlRpc::XmlRpcValue controller_type;
00274 n_private.param("dynamixels", dynamixels, dynamixels);
00275 n_private.param("controller_type", controller_type, controller_type);
00276
00277
00278 if (strcmp(static_cast<std::string>(controller_type).c_str(), "arbotix") == 0)
00279 {
00280 controller = arbotix;
00281
00282 arbotix_publisher = new ros::Publisher[dynamixels.size()];
00283 }
00284 else if (strcmp(static_cast<std::string>(controller_type).c_str(), "ssc32") == 0)
00285 {
00286 controller = ssc32;
00287 }
00288 else if (strcmp(static_cast<std::string>(controller_type).c_str(), "phidget") == 0)
00289 {
00290 controller = phidget;
00291 }
00292
00293 servos = (servo*) malloc(dynamixels.size() * sizeof(servo));
00294 init_servos_db(dynamixels, n_private);
00295
00296
00297
00298 if(controller == phidget)
00299 {
00300
00301 position_pub = n.advertise<corobot_msgs::ServoPosition>("setPositionServo", 100);
00302
00303
00304 type_pub = n.advertise<corobot_msgs::ServoType>("phidgetServo_setType", 100);
00305
00306 corobot_msgs::ServoType typeMsg;
00307 }
00308 else if (controller == ssc32)
00309 {
00310
00311 position_pub = n.advertise<corobot_msgs::ServoPosition>("setPositionServo", 100);
00312 }
00313
00314
00315 if(controller == phidget || controller == ssc32)
00316 {
00317
00318 corobot_msgs::ServoPosition msg;
00319
00320 ros::spinOnce();
00321
00322 ros::Rate loop_rate(0.5);
00323 loop_rate.sleep();
00324
00325
00326 for (int i=0; i<number_servo; i++)
00327 {
00328 msg.index = servos[i].id;
00329 if(servos[i].type == base_rotation)
00330 msg.position = 90;
00331 else if (servos[i].type == wrist_rotation)
00332 msg.position = 90;
00333 else if (servos[i].type == wrist_flex)
00334 msg.position = 90;
00335 else
00336 msg.position = 0;
00337 position_pub.publish(msg);
00338 }
00339 }
00340
00341 ros::spin();
00342
00343 free(servos);
00344 delete[] arbotix_publisher;
00345 return 0;
00346 }