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
00032
00033 #include <ros/ros.h>
00034 #include <ros/time.h>
00035
00036 #include "skin_driver/skin_driver.h"
00037 #include "skin_driver/skin_meas.h"
00038
00039
00040 #include <iostream>
00041 #include <sstream>
00042 #include <string.h>
00043 #include <math.h>
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 Skin::Skin():
00055 bSubDeviceOpen_ (false),
00056 bSubDeviceConfigured_ (false),
00057 bI2CDeviceFound_ (false),
00058 bsub20found_ (false),
00059 strSerial_ (" ")
00060 {
00061 int iI2CErr = 0;
00062 int iI2C_freq_set;
00063 int iI2C_freq_get = 0;
00064
00065 while( ( subdev_ = sub_find_devices(subdev_) ) && (!bsub20found_) )
00066 {
00067 std::cout << std::endl << "+++++++ Scan for Sub20-Device with connection to Skin +++++++" << std::endl;
00068
00069 fd_ = sub_open( subdev_ );
00070
00071
00072 if( fd_ == 0 ) {
00073
00074 std::cout << "sub_open: " << sub_strerror(sub_errno) << std::endl;
00075 }
00076 else
00077 {
00078
00079 bSubDeviceOpen_ = true;
00080 std::cout << "Device Handle : " << fd_ << std::endl;
00081
00082 if( sub_get_serial_number(fd_, const_cast<char*>(strSerial_.c_str()), strSerial_.size() ) >= 0 )
00083 std::cout << "Serial Number : " << strSerial_ << std::endl;
00084 std::cout << "------------Initializing I2C Interface------------" << std::endl;
00085
00086 iI2C_freq_get = 0;
00087 iI2CErr = sub_i2c_freq(fd_, &iI2C_freq_get);
00088 std::cout << "Predefined I2C frequency : " << iI2C_freq_get << std::endl;
00089
00090 iI2C_freq_set = skinc_def::I2C_communication_frequency;
00091 iI2CErr = sub_i2c_freq(fd_, &iI2C_freq_set);
00092 std::cout << "Initialized I2C frequency : " << iI2C_freq_set << std::endl;
00093
00094 iI2C_freq_get = 0;
00095 iI2CErr = sub_i2c_freq( fd_, &iI2C_freq_get );
00096
00097
00098 iI2CErr = sub_i2c_config(fd_, 0x50, 0x00);
00099 std::cout << "Status config: " << sub_i2c_status << std::endl;
00100
00101
00102 if (iI2C_freq_get == iI2C_freq_set )
00103 {
00104 std::cout<< "Frequency (" << iI2C_freq_set << "Hz) successfully stored \n";
00105
00106 bSubDeviceConfigured_ = true;
00107 }
00108 else
00109 {
00110 std::cout<< "ERROR - Frequency:" << iI2C_freq_set << " not accept by device \n";
00111
00112 bSubDeviceConfigured_ = false;
00113 }
00114 }
00115
00116
00117 if ( bSubDeviceConfigured_ )
00118 {
00119
00120 std::cout << "----------------Scan for I2C devices--------------" << std::endl;
00121 int ianz_slave;
00122 int iErrScan;
00123 char cdevice_buf[128];
00124
00125 iErrScan = sub_i2c_scan( fd_, &ianz_slave, cdevice_buf );
00126 if( !iErrScan )
00127 {
00128 std::cout << "I2C Slaves: " << ianz_slave << std::endl;
00129 for(int i = 0; i < ianz_slave; i++ )
00130 {
00131 std::cout << (i+1) << ". Slave adress:" << int (cdevice_buf[i]) << std::endl;
00132
00134
00135 if( skinc_def::I2CAdr == (int (cdevice_buf[i])) )
00136 {
00137 bI2CDeviceFound_ = true;
00138 std::cout << "Slave adress " << skinc_def::I2CAdr <<" exists" << std::endl;
00139 }
00141
00142 }
00143 }
00144
00145
00146 if ( bI2CDeviceFound_ )
00147 {
00149
00150
00152
00153 std::cout << "----check if connected device is a sensor skin----" << std::endl;
00154
00155 char cabuftest[8] = {0};
00156
00157
00158 char camem_adr[2] = {(skinc_def::readadr_hi), skinc_def::readadr_lo};
00159 iI2CErr = sub_i2c_write(fd_, skinc_def::I2CAdr, 0x00, 0, camem_adr, skinc_def::I2C_Mem_Adr);
00160 std::cout << "Status write Mem. Adr.: " << sub_i2c_status << std::endl;
00161
00162 ros::Duration(0.05).sleep();
00163
00164
00165 iI2CErr = iI2CErr + sub_i2c_read(fd_, skinc_def::I2CAdr,0x00, 0, cabuftest, 8);
00166 std::cout << "Status read RAM: " << sub_i2c_status << std::endl;
00167
00168 if( (iI2CErr == 0)&&( ((int (cabuftest[0]))&0xFF) < 64 ) )
00169 {
00170 std::cout << "Connected device is a sensor skin" << std::endl;
00171 bsub20found_ = true;
00172 }
00173 else
00174 {
00175 std::cout << "Found sensor is not a sensor skin" << std::endl;
00176 std::cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++" <<std::endl;
00177 bSubDeviceOpen_ = false;
00178 bSubDeviceConfigured_ = false;
00179 bI2CDeviceFound_ = false;
00180 bsub20found_ = false;
00181 }
00182 }
00183 else
00184 {
00185 std::cout << "Tested Sub20 is not connected with a I2C device" << std::endl;
00186 }
00187 }
00188 else
00189 {
00190 std::cout << " >> Subdevice is not configured " << std::endl;
00191 }
00192 }
00193
00194
00196
00197
00198
00199
00200
00201
00203
00204 isensor_number_ = skinc_def::max_sensor_number;
00205
00206
00207 if ( bSubDeviceConfigured_ & bI2CDeviceFound_ & bsub20found_ )
00208 {
00209 std::cout << "------------Determine number of sensors-----------" << std::endl;
00210
00211 for(int i = 0; (i < 6)&&(isensor_number_ == skinc_def::max_sensor_number); i++)
00212 {
00213 char cbufskinsens[256] = {0};
00214
00215
00216 char camem_adr[2] = {(i+skinc_def::readadr_hi), skinc_def::readadr_lo};
00217 iI2CErr = sub_i2c_write(fd_, skinc_def::I2CAdr, 0x00, 0, camem_adr, skinc_def::I2C_Mem_Adr);
00218 std::cout << "Status write Mem. Adr.: " << sub_i2c_status << std::endl;
00219
00220 ros::Duration(0.05).sleep();
00221
00222
00223 iI2CErr = iI2CErr + sub_i2c_read(fd_, skinc_def::I2CAdr,0x00, 0, cbufskinsens, 256);
00224 std::cout << "Status read RAM: " << sub_i2c_status << std::endl;
00225
00226 for(int k = 4; (k < 256)&&(isensor_number_ == skinc_def::max_sensor_number); k = k + 8)
00227 {
00228 if(((int (cbufskinsens[k-1]))&0xFF) == skinc_def::end_of_chain_marker)
00229 {
00230 isensor_number_ = int((k+4)/8) + 32*i - 1;
00231 }
00232 }
00233 }
00234
00235 std::cout << "sensors: " << isensor_number_ << std::endl;
00236 binit_skin_ok_ = bsub20found_;
00237
00238 }
00239 else
00240 {
00241 std::cout << std::endl <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
00242 std::cout << " Error -Sensor skin not found-" << std::endl;
00243 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl << std::endl;
00244 }
00245
00246 std::cout<< "**************************************************" << std::endl;
00247
00248 }
00249
00250 Skin::~Skin()
00251 {
00252
00253 sub_close( fd_ );
00254
00255 bSubDeviceOpen_ = false;
00256 bSubDeviceConfigured_ = false;
00257 bI2CDeviceFound_ = false;
00258 bsub20found_ = false;
00259 std::cout << "-------------------Sub20 closed-------------------" << std::endl;
00260 }
00261
00262 SKINMEAS Skin::GetOneMeas()
00263 {
00264 SKINMEAS sMeas;
00265 int iI2CErr;
00266 char cbufskinsens[1536] = {0};
00267 char cbufskinlog[2] = {0};
00268
00269 sMeas.bMeasAvailable = false;
00270 sMeas.dyn_thres_cv = 255;
00271 sMeas.status = 0;
00272
00273
00274 if ( bSubDeviceConfigured_ & bI2CDeviceFound_ & bsub20found_ )
00275 {
00277
00279
00280
00281
00282 iI2CErr = 0;
00283
00284
00285 char camem_adr_log[2] = {(skinc_def::readadr_log_hi), skinc_def::readadr_log_lo};
00286 iI2CErr = sub_i2c_write(fd_, skinc_def::I2CAdr, 0x00, 0, camem_adr_log, skinc_def::I2C_Mem_Adr);
00287
00288
00289 ros::Duration(0.05).sleep();
00290
00291
00292 iI2CErr = iI2CErr + sub_i2c_read(fd_, skinc_def::I2CAdr,0x00, 0, cbufskinlog, 2);
00293
00294
00295
00296 sMeas.dyn_thres_cv = uint8_t ((int (cbufskinlog[1]))&0xFF);
00297 sMeas.status = uint8_t ((int (cbufskinlog[0]))&0xFF);
00298
00299
00300
00301
00302
00304
00306
00307
00308 for(int k = 0; k < int( ceil((8*double(isensor_number_))/256) ); k++)
00309 {
00310
00311 char camem_adr[2] = {(k+skinc_def::readadr_hi), skinc_def::readadr_lo};
00312 iI2CErr = iI2CErr + sub_i2c_write(fd_, skinc_def::I2CAdr, 0x00, 0, camem_adr, skinc_def::I2C_Mem_Adr);
00313
00314
00315 ros::Duration(0.05).sleep();
00316
00317 if(k < int((8*isensor_number_)/256))
00318 {
00319
00320 iI2CErr = iI2CErr + sub_i2c_read(fd_, skinc_def::I2CAdr,0x00, 0, &cbufskinsens[k*256], 256);
00321
00322 }
00323 else
00324 {
00325
00326 iI2CErr = iI2CErr + sub_i2c_read(fd_, skinc_def::I2CAdr,0x00, 0, &cbufskinsens[k*256], int( fmod((8*double(isensor_number_)) , 256) ));
00327
00328 }
00329
00330 }
00331
00332 if(iI2CErr != 0)
00333 {
00334 sMeas.bMeasAvailable = false;
00335 std::cout << "--------------------------------------------------------------------------------------------------" << std::endl;
00336 std::cout << " !!! ERROR: master read transaction failed !!! / " << std::endl << std::endl;
00337 std::cout << "--------------------------------------------------------------------------------------------------" << std::endl;
00338 }
00339 else
00340 {
00341 sMeas.bMeasAvailable = true;
00342 convert_data_2_uint64_array(cbufskinsens, sMeas.sensor_data);
00343 }
00344 }
00345 else
00346 {
00347 std::cout << "-------------------------------------------------" << std::endl;
00348 std::cout << " Measurement was not retrieved" << std::endl;
00349 std::cout << "-------------------------------------------------" << std::endl;
00350
00351 sMeas.bMeasAvailable = false;
00352 }
00353
00354 return sMeas;
00355 }
00356
00357 void Skin::convert_data_2_uint64_array(char* inp_buf_data, uint64_t* outp_sen_data)
00358 {
00359 uint64_t u64help_var;
00360
00361 for(int i = 0; i < skinc_def::max_sensor_number; i++)
00362 {
00363 u64help_var = 0;
00364 for(int j = 0; j < skinc_def::anz_byte_per_sensor; j++)
00365 {
00366 if(j == 1)
00367 {
00368 std::cout << ( (int (inp_buf_data[(8*i)+j]) )&0xFF) << " ";
00369 }
00370
00371 u64help_var = (( (uint64_t (inp_buf_data[(8*i)+j]) )&0xFF) << (8 * j) ) | u64help_var;
00372 }
00373 outp_sen_data[i] = u64help_var;
00374 }
00375 std::cout << std::endl;
00376 }
00377
00378 bool Skin::write2ram(uint8_t* start_adr, uint8_t steps, uint8_t number_of_bytes, uint8_t write_data, bool sens_num_change)
00379 {
00380
00381
00382
00383 if ( bSubDeviceConfigured_ & bI2CDeviceFound_ & bsub20found_ )
00384 {
00385 int iI2CErr = 0;
00386 char cwrite_data = char(write_data);
00387 int imem_adr_wr = (start_adr[0] * 256) + start_adr[1];
00388 uint8_t uisteps = steps;
00389 uint8_t uinumber_of_bytes = number_of_bytes;
00390
00391 for(int i = 0; i < uinumber_of_bytes; i++)
00392 {
00393 iI2CErr = iI2CErr + sub_i2c_write( fd_, skinc_def::I2CAdr, imem_adr_wr, skinc_def::I2C_Mem_Adr, &cwrite_data, skinc_def::write_one_byte);
00394 ros::Duration(0.05).sleep();
00395 imem_adr_wr = imem_adr_wr + uisteps;
00396 }
00397
00398
00399 if((sens_num_change == true) && (write_data == skinc_def::end_of_chain_marker))
00400 {
00401
00402 int imem_adr_old_echm;
00403
00404 imem_adr_old_echm = skinc_def::readadr_hi * 256 + skinc_def::readadr_lo + 3 + 8 * isensor_number_;
00405 cwrite_data = char(skinc_def::stat_thres_default);
00406
00407 iI2CErr = iI2CErr + sub_i2c_write( fd_, skinc_def::I2CAdr, imem_adr_old_echm, skinc_def::I2C_Mem_Adr, &cwrite_data, skinc_def::write_one_byte);
00408
00409 isensor_number_ = (imem_adr_wr - 259) / 8;
00410
00411 }
00412 ros::Duration(0.05).sleep();
00413
00414 if(iI2CErr == 0)
00415 {
00416 return true;
00417 }
00418 else
00419 {
00420 return false;
00421 }
00422 }
00423 else
00424 {
00425 std::cout << "-------------------------------------------------" << std::endl;
00426 std::cout << " write2ram failed" << std::endl;
00427 std::cout << "-------------------------------------------------" << std::endl;
00428 return false;
00429 }
00430 }
00431
00432
00433 void Skin::dyn_rec_callback(skin_driver::MyStuffConfig &config, uint32_t level)
00434 {
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 }
00465
00466 bool Skin::conf_int_challback(skin_driver::skin_serv::Request& req, skin_driver::skin_serv::Response& res)
00467 {
00468 uint8_t start_adr_zg[2];
00469 start_adr_zg[0] = req.start_adr_srv[0];
00470 start_adr_zg[1] = req.start_adr_srv[1];
00471
00472 res.write_to_skin_successful_srv = write2ram(start_adr_zg, req.steps_srv, req.number_of_bytes_srv, req.write_data_srv, req.sens_num_change_srv);
00473 return true;
00474 }
00475
00476
00477
00478
00479 int main(int argc, char **argv)
00480 {
00481
00482 ros::init(argc, argv, "skin_data_publisher");
00483 ros::NodeHandle n;
00484
00485
00486 Skin one_skin;
00487 SKINMEAS sMeas;
00488 skin_driver::skin_meas msg;
00489
00490 ros::Publisher skin_pub = n.advertise<skin_driver::skin_meas> ("skin_data", 100);
00491 ros::ServiceServer skin_serv = n.advertiseService("skin_serv", &Skin::conf_int_challback, &one_skin);
00492
00493
00494 dynamic_reconfigure::Server<skin_driver::MyStuffConfig> srv_dyn_rec;
00495 dynamic_reconfigure::Server<skin_driver::MyStuffConfig>::CallbackType f = boost::bind(&Skin::dyn_rec_callback, &one_skin, _1, _2);
00496 srv_dyn_rec.setCallback(f);
00497
00498 ros::Rate loop_rate(5);
00499
00500 while (n.ok() && one_skin.binit_skin_ok_)
00501 {
00502
00503 sMeas = one_skin.GetOneMeas();
00504
00505
00506 if (sMeas.bMeasAvailable == true)
00507 {
00508 for (int i = 0; i < skinc_def::max_sensor_number; i++)
00509 {
00510 msg.sensor_data_msg[i] = sMeas.sensor_data[i];
00511 }
00512 msg.header.stamp = ros::Time::now();
00513 msg.sensor_number_msg = one_skin.isensor_number_;
00514 msg.readadr_hi_msg = skinc_def::readadr_hi;
00515 msg.readadr_lo_msg = skinc_def::readadr_lo;
00516 msg.dyn_thres_cv_msg = sMeas.dyn_thres_cv;
00517 msg.status_go_msg = !(bool(sMeas.status & 0x01));
00518 msg.status_err_msg = bool((sMeas.status & 0x02) >> 1);
00519 skin_pub.publish(msg);
00520
00521
00522 }
00523
00524 ros::spinOnce();
00525 loop_rate.sleep();
00526 }
00527 }