gloveServer_impl.cc
Go to the documentation of this file.
00001 
00018 /* system includes */
00019 #include <stdio.h>
00020 #include <sys/time.h>
00021 #include <math.h>
00022 #include <time.h>
00023 #include <vector>
00024 #include <algorithm>
00025 
00026 /* ros */
00027 #include <ros/ros.h>
00028 
00029 #include <sstream>
00030 
00031 /* my includes */
00032 #include "gloveConstants.h"
00033 #include "gloveServer_impl.h"
00034 
00035 extern unsigned int debugLevel;
00036 
00037 gloveServer_impl::gloveServer_impl(gloveID id, std::string name) {
00038 
00039         printf("Assuming %lu sensors.\n", sizeof(GLOVERAW_MAP2_GLOVEINVENTOR) / sizeof(unsigned int));
00040         for (unsigned int i = 0; i < sizeof(GLOVERAW_MAP2_GLOVEINVENTOR) / sizeof(unsigned int); i++)
00041                 GLOVERAW_MAP2_GLOVEINVENTOR[i] = GLOVERAW_MAP2_GLOVEINVENTOR_DEFAULT[i];
00042 
00043         myID = id;
00044         myName = name;
00045         threadRunning = false;
00046 }
00047 //
00048 gloveServer_impl::~gloveServer_impl() {
00049         delete gloveDev;
00050         threadRunning = false;
00051 }
00052 
00053 bool gloveServer_impl::init(std::string serialPort) {
00054         /* initial ros node */
00055         std::string pubName = (myID == rightGlove) ? "rightGloveData" : "leftGloveData";
00056     gloveDataPub = rosNode.advertise<asr_msgs::AsrGlove>(pubName, 1000);
00057     gloveDataPub_radian = rosNode.advertise<asr_msgs::AsrGlove>(pubName+"_radian", 1000);
00058 
00059         /* start gloveDevice */
00060         gloveDev = new gloveDevice;
00061         if (!gloveDev->init(serialPort.c_str())) {
00062                 printf("Could not start GloveDevice... gloveDev->init(%s) failed. Bailing out\n", serialPort.c_str());
00063                 return false;
00064         }
00065         gloveDev->showCurrentStatus();
00066 
00067         return true;
00068 }
00069 
00070 bool gloveServer_impl::loadCalibFile(const char * srcFileName) {
00071         printf("Loading calibration file \"%s\"\n", srcFileName);
00072 
00073         FILE *f;
00074 
00075         // open file
00076         if ((f = fopen(srcFileName, "r")) == NULL) {
00077                 printf("ERROR loading calibration file - open file error!\n");
00078 
00079                 std::string tmp(std::string(DEFAULT_CALIBRATION_PATH) + srcFileName);
00080                 printf("Trying to load calibration file \"%s\"\n", tmp.c_str());
00081                 if ((f = fopen(tmp.c_str(), "r")) == NULL) {
00082                         printf("Could not open file.\n");
00083                         return false;
00084                 }
00085         }
00086         printf("Successfully opened file.\n");
00087 
00089         bool threadState = threadRunning;
00090 
00091         if (threadState) {
00092                 printf("Stopping server thread.\n");
00093                 this->stop();
00094                 usleep(65000);
00095         }
00096 
00098         FulcRaw1.clear();
00099         FulcRad1.clear();
00100         FulcRaw2.clear();
00101         FulcRad2.clear();
00102 
00103         double tmpraw1, tmprad1, tmpraw2, tmprad2;
00104         // read calibration data from file
00105         for (int i = 0; i < MAX_SENSORS; i++) {
00106                 if (!fscanf(f, "%lf:%lf %lf:%lf\n", &tmprad1, &tmpraw1, &tmprad2, &tmpraw2)) {
00107                         printf("load calibration file - error: read from file !\n");
00108                         fclose(f);
00109                         return false;
00110                 }
00112                 if (fabs(tmpraw2 - tmpraw1) < MIN_JOINT_RESOLUTION) {
00113 
00114                         printf("ERROR in calibration data!\nJoint %s does not achieve minimum resolution.\n", JOINT_NAME[i]);
00115                         printf("Joint resolution: %d Minimum resolution: %d\n\n", (int) fabs(tmpraw2 - tmpraw1),
00116                                         MIN_JOINT_RESOLUTION);
00117 
00118                         if (fabs(tmpraw2 - tmpraw1) < 2) {
00119 
00120                                 printf("Bailing out...");
00121                                 fclose(f);
00122                                 exit(-1);
00123                         }
00124                 }
00125 
00126                 FulcRaw1.push_back(tmpraw1);
00127                 FulcRad1.push_back(tmprad1);
00128                 FulcRaw2.push_back(tmpraw2);
00129                 FulcRad2.push_back(tmprad2);
00130         }
00131         fclose(f);
00132 
00133         printf("load calibration file successful !\n");
00134 
00135         if (threadState) {
00136                 printf("Starting server thread again.\n");
00137                 this->start();
00138         }
00139 
00140         return true;
00141 }
00142 
00143 void gloveServer_impl::start() {
00144                 threadRunning = true;
00145 }
00146 
00147 void gloveServer_impl::stop() {
00149         threadRunning = false;
00150 }
00151 
00152 void gloveServer_impl::join() {
00153         boostWorkerThread->join();
00154 }
00155 
00156 #define MAX_SETS 3
00157 #define PAUSE_SET 10000
00158 #define PAUSE_WRITE 10000
00159 
00160 unsigned char getMedian(unsigned char (&vAcc)[MAX_SETS][MAX_SENSORS], unsigned int index) {
00161         if (MAX_SETS == 1)
00162                 return vAcc[0][index];
00163 
00164         std::vector<unsigned int> data;
00165         for (unsigned int i = 0; i < MAX_SETS; i++)
00166                 data.push_back((unsigned int) vAcc[i][index]);
00167         std::sort(data.begin(), data.end());
00168 
00169         if (MAX_SETS % 2 == 0)
00170                 return (unsigned char) ((data[MAX_SETS / 2 - 1] + data[MAX_SETS / 2]) / 2);
00171         else
00172                 return (unsigned char) data[(MAX_SETS - 1) / 2];
00173 }
00174 
00175 void gloveServer_impl::workerThread() {
00176 
00177         unsigned char v[MAX_SENSORS], n, v_perm[MAX_SENSORS];
00178         unsigned char vAcc[MAX_SETS][MAX_SENSORS];
00179         statusQuery_t queryStatus;
00180         gloveStatusByte_t gloveStatus;
00181 
00182     asr_msgs::AsrGlove gloveData;
00183     asr_msgs::AsrGlove gloveData_radian;
00184         gloveData.id = (myID == leftGlove) ? "L" : "R";
00185         gloveData_radian.id = (myID == leftGlove) ? "L" : "R";
00186 
00187         while (threadRunning & ros::ok()) {
00188 
00190                 for (unsigned int i = 0; i < MAX_SETS; i++) {
00191                         gloveDev->cmdGetSensorValues(vAcc[i], &n, &queryStatus, &gloveStatus);
00192 //                      gloveDev->cmdGetSensorDummyValues(vAcc[i], &n, &queryStatus, &gloveStatus);
00193 
00194                         if (debugLevel > 2) {
00195                                 printf("vAcc: ");
00196                                 for (unsigned int j = 0; j < MAX_SENSORS; j++)
00197                                         printf("%3d ", vAcc[i][j]);
00198                                 printf("\n");
00199                         }
00200 
00201                         if (i + 1 < MAX_SETS)
00202                                 usleep(PAUSE_SET);
00203                         else if (debugLevel > 2)
00204                                 printf("\n");
00205                 }
00206 
00207                 for (unsigned int i = 0; i < MAX_SENSORS; i++) {
00208                         v[i] = getMedian(vAcc, i);
00209                 }
00210 
00211                 if (debugLevel > 2) {
00212                         printf("v: ");
00213                         for (unsigned int j = 0; j < MAX_SENSORS; j++)
00214                                 printf("%3d ", v[j]);
00215 
00216                         printf("\n");
00217                 }
00218 
00220                 if (!permuteValues(v, v_perm)) {
00221 
00222                         printf("Could not permute values. Bailing out.\n");
00223                 }
00224 
00225                 time_t now;
00226                 time(&now);
00227                 if (debugLevel > 2)
00228                         printf("%s", ctime(&now));
00229 
00230                 if (debugLevel > 0) {
00231                         printf("v_perm %s:", myID == leftGlove ? "L" : "R");
00232                         for (unsigned int i = 0; i < n; i++)
00233                                 printf("%3d ", (int) v_perm[i]);
00234                         printf("\n");
00235                 }
00236 
00237                 double *temp = new double[n];
00238                 for (unsigned int i = 0 ; i < n ; i++) temp[i] = (double) v_perm[i];
00239                 if (!sendNotificationDataToRos(temp, n, gloveData, gloveDataPub)) {
00240                         printf("Couldn't write Notification to NotificationManager. Error!\n");
00241                         exit(-1);
00242                 }
00243 
00244                 if (!sendNotificationDataToRos(getRadianArray(v_perm, n), n, gloveData_radian, gloveDataPub_radian)) {
00245                         printf("Couldn't write Notification to NotificationManager. Error!\n");
00246                         exit(-1);
00247                 }
00248 
00249                 usleep(PAUSE_WRITE); //500000);//30000);
00250         }
00251         threadRunning = false;
00252 }
00253 
00254 bool gloveServer_impl::permuteValues(unsigned char* valIn, unsigned char* valOut) {
00255 
00256         if (sizeof(valIn) > sizeof(valOut)) {
00257                 printf("ERROR in permuteValues(). Array sizes do not fit for permutation!\n");
00258                 return false;
00259         }
00260 
00261         for (unsigned int i = 0; i < MAX_SENSORS; i++) {
00262                 //    printf( "valOut[%d] = valIn[%d] = %c\n", i, GLOVERAW_MAP2_GLOVEINVENTOR[i], valIn[GLOVERAW_MAP2_GLOVEINVENTOR[i]]);
00263                 valOut[i] = valIn[GLOVERAW_MAP2_GLOVEINVENTOR[i]];
00264         }
00265         return true;
00266 }
00267 
00268 double gloveServer_impl::getRadian(int index, unsigned char val) {
00269         /*
00270          unsigned char zero  = calibVals[0][index];
00271          unsigned char min   = calibVals[1][index];
00272          unsigned char max   = calibVals[2][index];
00273          */
00274         double alfa;
00275 
00276         if (RADSIGN[index] == 1)
00277                 alfa = -alfa;
00278 
00279         alfa =
00280                         ((FulcRad2[index] - FulcRad1[index]) * ((double) val - FulcRaw1[index])
00281                                         / (FulcRaw2[index] - FulcRaw1[index])) + FulcRad1[index];
00282         //  printf( "rad2 %f rad1 %f raw2 %f raw1 %f val %f alfa %f\n", FulcRad2[index], FulcRad1[index], FulcRaw2[index], FulcRaw1[index], (double)val, alfa);
00283         return alfa;
00284 }
00285 
00286 double *gloveServer_impl::getRadianArray(unsigned char* data, unsigned int size){
00287         double * radianArray = new double[size];
00288         for ( unsigned int i = 0 ; i < size; i++){
00289           radianArray[i] = getRadian(i, data[i]);
00290         }
00291         return radianArray;
00292 }
00293 
00294 bool gloveServer_impl::sendNotificationDataToRos(double* data, unsigned int size, asr_msgs::AsrGlove gloveData, ros::Publisher publisher) {
00295         for (unsigned int i = 0; i < size; i++) {
00296                 gloveData.data[i] = data[i];
00297         }
00298 
00299         std::stringstream output;
00300         output << "Glove Data Message published: " << myID << " ";
00301         for (unsigned int i = 0; i < size; i++) {
00302                 output << gloveData.data[i] << " ";
00303         }
00304         ROS_INFO("%s", output.str().c_str());
00305         publisher.publish(gloveData);
00306         ros::spinOnce();
00307 
00308         return true;
00309 }
00310 
00311 #if gloveServer_impl_test
00312 int main(int argc, char **argv)
00313 {
00314 
00315         // Create a new instance of the implementation
00316         gloveServer_impl *impl = new gloveServer_impl;
00317 
00318         while (1);
00319 
00320         delete impl;
00321 
00322         return retval;
00323 }
00324 #endif /* gloveServer_impl_test */
00325 


asr_cyberglove_lib
Author(s): Heller Florian, Meißner Pascal, Nguyen Trung, Yi Xie
autogenerated on Thu Jun 6 2019 22:02:33