00001
00031
00032
00033 #include <iostream>
00034 #include <fstream>
00035 #include <vector>
00036
00037
00038
00039 #include "cob_sdh/sdh.h"
00040 #include "cob_sdh/util.h"
00041 #include "cob_sdh/sdhlibrary_settings.h"
00042 #include "cob_sdh/basisdef.h"
00043 #include "cob_sdh/dsa.h"
00044 #include "cob_sdh/dsaboost.h"
00045
00046 #include <boost/bind.hpp>
00047 #include <boost/mem_fn.hpp>
00048
00049 USING_NAMESPACE_SDH
00050
00051
00052 #define cdbg std::cout
00053
00054
00055
00056
00057
00058
00068 void cDSAUpdater::Updater()
00069 {
00070 assert( ts != NULL );
00071
00072 int error_level = 0;
00073
00074 cdbg << "cDSAUpdater::Updater(): thread started\n";
00075 while( true )
00076 {
00077 try
00078 {
00079
00080
00081
00082 ts->UpdateFrame();
00083
00084
00085
00086 if ( error_level > 0 )
00087 error_level--;
00088
00089
00090 boost::this_thread::sleep(boost::posix_time::milliseconds(1000/40));
00091
00092 }
00093 catch (cDSAException* e)
00094 {
00095 error_level++;
00096
00097
00098 cdbg << "cDSAUpdater::Updater(): ignoring " << e->what() << "\n";
00099
00100 delete e;
00101
00102 }
00103
00104 if ( error_level > error_threshold )
00105 {
00106 std::cerr << "cDSAUpdater::Updater(): Too many errors from Tactile sensors of SDH. Reconnecting to DSACON32m!\n";
00107
00108 try
00109 {
00110 ts->Close();
00111 boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
00112 }
00113 catch (cDSAException* e)
00114 {
00115
00116 cdbg << "cDSAUpdater::Updater(): ignoring " << e->what() << " while reconnecting step 1\n";
00117 delete e;
00118 }
00119
00120 try
00121 {
00122 ts->Open();
00123 }
00124 catch (cDSAException* e)
00125 {
00126
00127 cdbg << "cDSAUpdater::Updater(): ignoring " << e->what() << " while reconnecting step 2\n";
00128 delete e;
00129 }
00130
00131 try
00132 {
00133 ts->SetFramerate( 30, true );
00134
00135 error_level = 0;
00136 std::cerr << "cDSAUpdater::Updater() Succesfully reconnected to DSACON32m of SDH\n";
00137 }
00138 catch (cDSAException* e)
00139 {
00140
00141 cdbg << "cDSAUpdater::Updater(): ignoring " << e->what() << " while reconnecting step 3\n";
00142 delete e;
00143 }
00144 }
00145 }
00146 }
00147
00148
00149
00150 cDSAUpdater::cDSAUpdater( cDSA* _ts, int _error_threshold ) :
00151 error_threshold(_error_threshold),
00152 ts(_ts)
00153 {
00154 assert( ts != NULL );
00155
00156 ts->SetFramerate( 30, true );
00157
00158
00159 cdbg << "cDSAUpdater::cDSAUpdater(): Starting new updater thread...";
00160 updater_thread = boost::thread( boost::bind(&cDSAUpdater::Updater, this ) );
00161 cdbg << "OK\n";
00162 }
00163
00164
00165
00166 double cIsGraspedByArea::FullArea( int m )
00167 {
00168 return ts->GetMatrixInfo(m).cells_x * ts->GetMatrixInfo(m).texel_width *
00169 ts->GetMatrixInfo(m).cells_y * ts->GetMatrixInfo(m).texel_height;
00170 }
00171
00172
00173 void cIsGraspedByArea::SetCondition( double eaf0p, double eaf0d, double eaf1p, double eaf1d, double eaf2p, double eaf2d )
00174 {
00175 expected_area[0] = eaf0p * FullArea( 0 );
00176 expected_area[1] = eaf0d * FullArea( 1 );
00177 expected_area[2] = eaf1p * FullArea( 2 );
00178 expected_area[3] = eaf1d * FullArea( 3 );
00179 expected_area[4] = eaf2p * FullArea( 4 );
00180 expected_area[5] = eaf2d * FullArea( 5 );
00181 }
00182
00184 void cIsGraspedByArea::SetCondition( double* eafx )
00185 {
00186 for ( int i=0; i<6; i++ )
00187 expected_area[i] = eafx[i] * FullArea( i );
00188 }
00189
00191 cIsGraspedByArea::cIsGraspedByArea( cDSA* _ts )
00192
00193 : cIsGraspedBase( _ts )
00194 {
00195 SetCondition( 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 );
00196 }
00197
00203 bool cIsGraspedByArea::IsGrasped(void)
00204 {
00205 cDSA::sContactInfo contact_info;
00206 bool is_grasped = true;
00207 for ( int i=0; i<6; i++ )
00208 {
00209 contact_info = ts->GetContactInfo(i);
00210
00211 if (contact_info.area < expected_area[ i ])
00212 {
00213 is_grasped = false;
00214 break;
00215 }
00216 }
00217 return is_grasped;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230