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 #include "ros/ros.h"
00031 #include <stdio.h>
00032 #include <math.h>
00033 #include <stdint.h>
00034
00035 #include "std_msgs/Float64.h"
00036 #include "corobot_msgs/MoveArm.h"
00037 #include "corobot_msgs/PosMsg.h"
00038 #include <phidget21.h>
00039
00040 #include <corobot_diagnostics/diagnostics.h>
00041 #include <diagnostic_updater/diagnostic_updater.h>
00042 #include <diagnostic_updater/publisher.h>
00043
00044
00045 typedef enum
00046 {
00047 armBaseRotation,
00048 armShoulder,
00049 armElbow,
00050 armWrist,
00051 armGripper,
00052 leftWheel,
00053 rightWheel
00054 } stepper_type;
00055
00056
00057
00058 typedef struct
00059 {
00060 stepper_type type;
00061 int serial;
00062 CPhidgetStepperHandle handle;
00063 } stepper;
00064
00065
00066 stepper* steppers = NULL;
00067
00068
00069 int number_stepper = 0;
00070
00071
00072 int stepperError = 0;
00073
00074
00075 ros::Publisher positionPublisher;
00076
00077
00078 ros::Publisher stepperPositionPublisher;
00079 int64_t leftPosition = 0;
00080 int64_t rightPosition = 0;
00081
00082
00086 void setStepperPosition(const corobot_msgs::MoveArm &msg)
00087 {
00088 int err;
00089 for(int i; i<number_stepper; i++)
00090 {
00091
00092
00093 if(msg.index == steppers[i].type)
00094 {
00095 err = CPhidgetStepper_setTargetPosition(steppers[i].handle, 0, msg.position);
00096 if (err != 0)
00097 {
00098 stepperError = 2;
00099 }
00100 else
00101 {
00102 stepperError = 0;
00103 }
00104 }
00105 }
00106 }
00107
00108
00109 void publishPosition()
00110 {
00111 corobot_msgs::PosMsg msg;
00112 msg.px = leftPosition;
00113 msg.py = rightPosition;
00114
00115 positionPublisher.publish(msg);
00116 }
00117
00118
00119 int positionCallback(CPhidgetStepperHandle phid, void *userPtr, int index, __int64 position)
00120 {
00121 stepper * stepperMoved = (stepper*) userPtr;
00122
00123
00124 if (stepperMoved->type == leftWheel)
00125 {
00126 leftPosition = position;
00127 publishPosition();
00128 }
00129 else if (stepperMoved->type == rightWheel)
00130 {
00131 rightPosition = position;
00132 publishPosition();
00133 }
00134 else
00135 {
00136 corobot_msgs::MoveArm msg;
00137 msg.index = stepperMoved->type;
00138 msg.position = position;
00139
00140
00141 stepperPositionPublisher.publish(msg);
00142 }
00143 }
00144
00145
00149 void init_servos_db(XmlRpc::XmlRpcValue stepper_list, ros::NodeHandle n)
00150 {
00151
00152
00153
00154 if(stepper_list.hasMember("leftWheel") && (int)stepper_list["leftWheel"]["serial"] != -1)
00155 {
00156
00157 steppers[number_stepper].type = leftWheel;
00158
00159 steppers[number_stepper].serial = (int) stepper_list["leftWheel"]["serial"];
00160 steppers[number_stepper].handle = 0;
00161
00162 number_stepper++;
00163 }
00164 if(stepper_list.hasMember("rightWheel") && (int)stepper_list["leftWheel"]["serial"] != -1)
00165 {
00166 steppers[number_stepper].type = rightWheel;
00167 steppers[number_stepper].serial = (int) stepper_list["rightWheel"]["serial"];
00168 steppers[number_stepper].handle = 0;
00169
00170 number_stepper++;
00171 }
00172 if(stepper_list.hasMember("armBaseRotation") && (int)stepper_list["leftWheel"]["serial"] != -1)
00173 {
00174 steppers[number_stepper].type = armBaseRotation;
00175 steppers[number_stepper].serial = (int) stepper_list["armBaseRotation"]["serial"];
00176 steppers[number_stepper].handle = 0;
00177
00178 number_stepper++;
00179 }
00180 if(stepper_list.hasMember("armShoulder") && (int)stepper_list["leftWheel"]["serial"] != -1)
00181 {
00182 steppers[number_stepper].type = armShoulder;
00183 steppers[number_stepper].serial = (int) stepper_list["armShoulder"]["serial"];
00184 steppers[number_stepper].handle = 0;
00185
00186 number_stepper++;
00187 }
00188 if(stepper_list.hasMember("armElbow") && (int)stepper_list["leftWheel"]["serial"] != -1)
00189 {
00190 steppers[number_stepper].type = armElbow;
00191 steppers[number_stepper].serial = (int) stepper_list["armElbow"]["serial"];
00192 steppers[number_stepper].handle = 0;
00193
00194 number_stepper++;
00195 }
00196 if(stepper_list.hasMember("armWrist") && (int)stepper_list["leftWheel"]["serial"] != -1)
00197 {
00198 steppers[number_stepper].type = armWrist;
00199 steppers[number_stepper].serial = (int) stepper_list["armWrist"]["serial"];
00200 steppers[number_stepper].handle = 0;
00201
00202 number_stepper++;
00203 }
00204 if(stepper_list.hasMember("armGripper") && (int)stepper_list["leftWheel"]["serial"] != -1)
00205 {
00206 steppers[number_stepper].type = armGripper;
00207 steppers[number_stepper].serial = (int) stepper_list["armGripper"]["serial"];
00208 steppers[number_stepper].handle = 0;
00209
00210 number_stepper++;
00211 }
00212 }
00213
00214
00215 void init_stepper_boards()
00216 {
00217 int err;
00218 for(int i; i<number_stepper; i++)
00219 {
00220
00221 err = CPhidgetStepper_create (&(steppers[i].handle));
00222 if (err != 0)
00223 {
00224 ROS_ERROR("error create the Phidget stepper controller device of serial number %d", steppers[i].serial);
00225 stepperError = 1;
00226 }
00227
00228 err = CPhidget_open((CPhidgetHandle)steppers[i].handle, steppers[i].serial);
00229 if (err != 0)
00230 {
00231 ROS_ERROR("error opening the Phidget stepper controller device of serial number %d", steppers[i].serial);
00232 stepperError = 1;
00233 }
00234
00235 CPhidgetStepper_set_OnPositionChange_Handler(steppers[i].handle, &positionCallback, (void*) &(steppers[i]));
00236
00237 if((err = CPhidget_waitForAttachment((CPhidgetHandle)steppers[i].handle, 10000)))
00238 {
00239 ROS_ERROR("Problem waiting for attachment of Phidget stepper controller of serial number %d", steppers[i].serial);
00240 stepperError = 1;
00241 }
00242
00243 if(err = CPhidgetStepper_setEngaged(steppers[i].handle, 0, 1))
00244 {
00245 ROS_ERROR("Problem engaging Phidget stepper controller of serial number %d", steppers[i].serial);
00246 stepperError = 1;
00247 }
00248 }
00249 }
00250
00251
00252 void stepper_diagnostic (diagnostic_updater::DiagnosticStatusWrapper &stat)
00253 {
00254 if (stepperError == 0)
00255 {
00256 stat.summaryf(diagnostic_msgs::DiagnosticStatus::OK, "initialized");
00257 }
00258 else if (stepperError == 1)
00259 {
00260 stat.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "Phidget Stepper cannot be initialized");
00261 stat.addf("Recommendation", PHIDGET_STEPPER_ERROR_CONNECTION);
00262 }
00263 else if (stepperError == 2)
00264 {
00265 stat.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "The Phidget Stepper motor cannot be moved");
00266 stat.addf("Recommendation",ERROR_MOVING_ARM);
00267 }
00268 }
00269
00270
00271 int main(int argc, char **argv)
00272 {
00273 ros::init(argc, argv, "phidget_stepper");
00274 ros::NodeHandle n;
00275 ros::NodeHandle n_private("~");
00276 diagnostic_updater::Updater updater;
00277
00278
00279 XmlRpc::XmlRpcValue stepper_list;
00280 n_private.param("stepper_list", stepper_list, stepper_list);
00281
00282
00283
00284 steppers = (stepper*) malloc(stepper_list.size() * sizeof(stepper));
00285 init_servos_db(stepper_list, n_private);
00286
00287
00288 updater.setHardwareIDf("Phidget");
00289
00290 updater.add("Stepper", stepper_diagnostic);
00291
00292
00293 init_stepper_boards();
00294
00295
00296
00297 ros::Subscriber stepperPos= n.subscribe("stepperPosition", 100, setStepperPosition);
00298
00299
00300 positionPublisher= n.advertise<corobot_msgs::PosMsg>("position_data", 100);
00301 stepperPositionPublisher = n.advertise<corobot_msgs::MoveArm>("stepper_position",100);
00302 ros::Rate rate(50);
00303
00304 while(ros::ok())
00305 {
00306 updater.update();
00307 ros::spinOnce();
00308 rate.sleep();
00309 }
00310
00311 free(steppers);
00312 return 0;
00313 }