00001 #include <iostream>
00002 #include <cstdio>
00003 #include <cstdlib>
00004 #include <string>
00005 #include <cassert>
00006 #include <cmath>
00007
00008 #include <HD/hd.h>
00009 #include <HDU/hduVector.h>
00010 #include <HDU/hduMatrix.h>
00011 #include <HDU/hduError.h>
00012
00013 #include <ros/ros.h>
00014 #include <std_msgs/Float64.h>
00015 #include <sensor_msgs/JointState.h>
00016 #include <vrep_common/JointSetStateData.h>
00017 #include <vrep_common/ForceSensorData.h>
00018 #include <vrep_common/ObjectGroupData.h>
00019 #include <vrep_common/simRosGetObjectHandle.h>
00020 #include <phantom_dual/conio.h>
00021
00022 using namespace std;
00023
00024 typedef struct
00025 {
00026 HDdouble angle[3];
00027 HDdouble torque[3];
00028
00029 } omni_state;
00030
00031 omni_state omni;
00032
00033 vrep_common::JointSetStateData joint_target;
00034 vrep_common::simRosGetObjectHandle object_handles;
00035
00036 ros::ServiceClient object_handles_srv;
00037 ros::Publisher command_joints_position_pub;
00038
00039 int right_joint1Handle;
00040 int right_joint2Handle;
00041 int right_joint3Handle;
00042 int right_joint4Handle;
00043 int right_joint5Handle;
00044 int right_joint6Handle;
00045
00046 int left_sensor1Handle;
00047 int left_sensor2Handle;
00048 int left_sensor3Handle;
00049 int left_sensor4Handle;
00050 int right_sensor1Handle;
00051
00052 HDSchedulerHandle gCallbackHandle = HD_INVALID_HANDLE;
00053
00054 HDdouble base_torque[3];
00055
00056
00057 HDCallbackCode HDCALLBACK ServoSchedulerCallback(void *pUserData)
00058 {
00059 HDErrorInfo error;
00060
00061 hdBeginFrame(hdGetCurrentDevice());
00062
00063 hdGetDoublev(HD_CURRENT_JOINT_ANGLES, omni.angle);
00064 hdSetDoublev(HD_CURRENT_JOINT_TORQUE, base_torque);
00065
00066 hdEndFrame(hdGetCurrentDevice());
00067
00068
00069
00070 if (HD_DEVICE_ERROR(error = hdGetError()))
00071 {
00072 hduPrintError(stderr, &error, "Error during main scheduler callback\n");
00073 if (hduIsSchedulerError(&error))
00074 return HD_CALLBACK_DONE;
00075 }
00076
00077 return HD_CALLBACK_CONTINUE;
00078 }
00079
00080
00081 void objectHandles()
00082 {
00083 object_handles.request.objectName = "right_joint1";
00084 if (object_handles_srv.call(object_handles))
00085 right_joint1Handle = object_handles.response.handle;
00086 else
00087 ROS_ERROR("Failed to call service get_joint_handles");
00088
00089 object_handles.request.objectName = "right_joint2";
00090 if (object_handles_srv.call(object_handles))
00091 right_joint2Handle = object_handles.response.handle;
00092 else
00093 ROS_ERROR("Failed to call service get_joint_handles");
00094
00095 object_handles.request.objectName = "right_joint3";
00096 if (object_handles_srv.call(object_handles))
00097 right_joint3Handle = object_handles.response.handle;
00098 else
00099 ROS_ERROR("Failed to call service get_joint_handles");
00100
00101 object_handles.request.objectName = "right_joint4";
00102 if (object_handles_srv.call(object_handles))
00103 right_joint4Handle = object_handles.response.handle;
00104 else
00105 ROS_ERROR("Failed to call service get_joint_handles");
00106
00107 object_handles.request.objectName = "right_joint5";
00108 if (object_handles_srv.call(object_handles))
00109 right_joint5Handle = object_handles.response.handle;
00110 else
00111 ROS_ERROR("Failed to call service get_joint_handles");
00112
00113 object_handles.request.objectName = "right_joint6";
00114 if (object_handles_srv.call(object_handles))
00115 right_joint6Handle = object_handles.response.handle;
00116 else
00117 ROS_ERROR("Failed to call service get_joint_handles");
00118
00119
00120 object_handles.request.objectName = "left_sensor1";
00121 if (object_handles_srv.call(object_handles))
00122 left_sensor1Handle = object_handles.response.handle;
00123 else
00124 ROS_ERROR("Failed to call service get_joint_handles");
00125
00126 object_handles.request.objectName = "left_sensor2";
00127 if (object_handles_srv.call(object_handles))
00128 left_sensor2Handle = object_handles.response.handle;
00129 else
00130 ROS_ERROR("Failed to call service get_joint_handles");
00131
00132 object_handles.request.objectName = "left_sensor3";
00133 if (object_handles_srv.call(object_handles))
00134 left_sensor3Handle = object_handles.response.handle;
00135 else
00136 ROS_ERROR("Failed to call service get_joint_handles");
00137
00138 object_handles.request.objectName = "left_sensor4";
00139 if (object_handles_srv.call(object_handles))
00140 left_sensor4Handle = object_handles.response.handle;
00141 else
00142 ROS_ERROR("Failed to call service get_joint_handles");
00143
00144 object_handles.request.objectName = "right_sensor1";
00145 if (object_handles_srv.call(object_handles))
00146 right_sensor1Handle = object_handles.response.handle;
00147 else
00148 ROS_ERROR("Failed to call service get_joint_handles");
00149
00150
00151
00152
00153
00154
00155
00156 }
00157
00158
00159 void setJointValue()
00160 {
00161 joint_target.handles.data.clear();
00162 joint_target.setModes.data.clear();
00163 joint_target.values.data.clear();
00164
00165 joint_target.handles.data.push_back(right_joint2Handle);
00166 joint_target.setModes.data.push_back(1);
00167 joint_target.values.data.push_back(omni.angle[0]);
00168
00169 joint_target.handles.data.push_back(right_joint6Handle);
00170 joint_target.setModes.data.push_back(1);
00171 joint_target.values.data.push_back(-omni.angle[0]);
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 command_joints_position_pub.publish(joint_target);
00186 }
00187
00188
00189 void jointsCallback(const sensor_msgs::JointState& msg)
00190 {
00191 int scale_cte = 2700;
00192
00193 base_torque[0] = scale_cte*msg.position[7];
00194
00195 cout << "Balance angle: " << -msg.position[1]*(180/M_PI) << "\n" << endl;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 void sensorsCallback(const vrep_common::ObjectGroupData& msg)
00214 {
00215 cout << "Left sensor 1 value: " << msg.floatData.data[2] << endl;
00216 cout << "Left sensor 2 value: " << msg.floatData.data[8] << endl;
00217 cout << "Left sensor 3 value: " << msg.floatData.data[14] << endl;
00218 cout << "Left sensor 4 value: " << msg.floatData.data[20] << endl;
00219
00220 cout << "Right sensor 1 value: " << msg.floatData.data[26] << endl;
00221
00222 cout << "--------------------------------------" << endl;
00223 }
00224
00225
00226 int main(int argc, char **argv)
00227 {
00228 HDErrorInfo error;
00229 HHD hHD = hdInitDevice(HD_DEFAULT_DEVICE);
00230 if (HD_DEVICE_ERROR(error = hdGetError()))
00231 {
00232 hduPrintError(stderr, &error, "Failed to initialize the device");
00233
00234 fprintf(stderr, "\nPress any key to quit.\n");
00235 getch();
00236 return -1;
00237 }
00238 ROS_INFO("Found %s.\n\n", hdGetString(HD_DEVICE_MODEL_TYPE));
00239
00240 gCallbackHandle = hdScheduleAsynchronous(ServoSchedulerCallback, 0, HD_MAX_SCHEDULER_PRIORITY);
00241 if (HD_DEVICE_ERROR(error = hdGetError()))
00242 {
00243 hduPrintError(stderr, &error, "Failed to schedule servoloop callback");
00244
00245 hdDisableDevice(hHD);
00246
00247 fprintf(stderr, "\nPress any key to quit.\n");
00248 getch();
00249 return -1;
00250 }
00251
00252 hdEnable(HD_FORCE_OUTPUT);
00253
00254 hdStartScheduler();
00255 if (HD_DEVICE_ERROR(error = hdGetError()))
00256 {
00257 hduPrintError(stderr, &error, "Failed to start the scheduler");
00258
00259 fprintf(stderr, "\nPress any key to quit.\n");
00260 getch();
00261 return -1;
00262 }
00263
00264 ros::init(argc, argv, "phantom_B");
00265
00266 ros::NodeHandle n;
00267
00268 object_handles_srv = n.serviceClient<vrep_common::simRosGetObjectHandle>("/vrep/simRosGetObjectHandle");
00269
00270 command_joints_position_pub = n.advertise<vrep_common::JointSetStateData>("/vrep/command_right_joints_position",1000);
00271
00272 ros::Subscriber notify_joints_position_sub = n.subscribe("/vrep/notify_joints_position", 1000, jointsCallback);
00273
00274
00275
00276
00277
00278 ros::Subscriber notify_sensors_data_sub = n.subscribe("/vrep/notify_sensors_data", 1000, sensorsCallback);
00279
00280 objectHandles();
00281
00282 ros::Rate loop_rate(1000);
00283
00284 while (ros::ok())
00285 {
00286 setJointValue();
00287
00288 ros::spinOnce();
00289 loop_rate.sleep();
00290 }
00291
00292 hdStopScheduler();
00293 hdDisableDevice(hHD);
00294
00295 return 0;
00296 }