00001 /* 00002 File rangernoise.cc: ranger noise plugin demo for Stage 00003 Author: Richard Vaughan 00004 Date: 3 March 2008 00005 CVS: $Id: rangernoise.cc,v 1.1 2008-03-04 02:09:56 rtv Exp $ 00006 */ 00007 00008 #include "stage.hh" 00009 using namespace Stg; 00010 00011 const double DEVIATION = 0.05; 00012 00013 double simple_normal_deviate( double mean, double stddev ) 00014 { 00015 double x = 0.0; 00016 00017 for( int i=0; i<12; i++ ) 00018 x += rand()/(RAND_MAX+1.0); 00019 00020 return ( stddev * (x - 6.0) + mean ); 00021 } 00022 00023 // process the ranger data 00024 int RangerUpdate( ModelRanger* mod, void* dummy ) 00025 { 00026 // get the data 00027 std::vector<meters_t>& scan = mod->GetSensorsMutable()[0].ranges; 00028 00029 if( scan.size()>0 ) 00030 FOR_EACH( it, scan ) 00031 *it *= simple_normal_deviate( 1.0, DEVIATION ); 00032 00033 return 0; // run again 00034 } 00035 00036 // Stage calls this when the model starts up. we just add a callback to 00037 // the model that gets called just after the sensor update is done. 00038 extern "C" int Init( Model* mod ) 00039 { 00040 mod->AddCallback( Model::CB_UPDATE, (model_callback_t)RangerUpdate, NULL ); 00041 00042 // add this so we can see the effects immediately, without needing 00043 // anyone else to subscribe to the ranger 00044 mod->Subscribe(); 00045 00046 return 0; // ok 00047 } 00048