NaviTrack Tutorial:Integrating:Callback Module

From NAMIC Wiki
Jump to: navigation, search
Home < NaviTrack Tutorial:Integrating:Callback Module

Overview of CallbackModule

In the previous pages, we have implemented data transfer using NaviTrack into our own programs. The problem here is that they need to poll MyTutorialModule frequently to check if new data have arrived. This couldn't be a serious problem in case of seding small piece of data such as coordinate values, but might cause a waste of CPU time when the data is large.

We can do the almost same thing by using CallbackModule instead of polling. This module offers callback mechanism to process recieved data on its arrival. All we need to do is to define a callback function to process recieved data, and register it to CallbackModule.

Once a callback function has been registered, it is invoked every after events arrive at a corresponding node in NaviTrack data flow, which is defined in a configulation XML file.


Sending coordinate data

See lxxxxx.


Receiving coordinate data

To receive and process the coordinate data, create a callback function and register it to CallbackModule.

Register callback function and start context of NaviTrack

In main() of pullpos_cbm.cxx.

 CallbackModule* cbm = (CallbackModule*) context.getModule("CallbackConfig");
 if (cbm == NULL) {
   cerr << "Failed to load CallbackConfig Module." << endl;
 }
   
 cbm->setCallback( "cb1", (OTCallbackFunction*)&callbackFunc , NULL);
 context.runAtRate(1000.0/(double)interval);

Define callback function itself

 void callbackFunc(Node&, Event &event, void *)
 {
   vector<float> position(3, 0.0);
   vector<float> orientation(4, 0.0);
   
   if (event.hasAttribute("position")) {
     for(int i = 0; i < 3; i ++)
       position[i]=event.getPosition()[i];
   } else {
     position[0]=0.0;
     position[1]=0.0;
     position[2]=0.0;
   }
  
   if (event.hasAttribute("orientation")) {
     for  (int i = 0; i < 4; i ++) {
       orientation[i]= event.getOrientation()[i];
     }  
   } else {
     orientation[0]=0.0;
     orientation[1]=0.0;
     orientation[2]=0.0;
     orientation[3]=0.0;
   } 
 
   cout << "Received position = ("
             << position[0] << ", " << position[1] << ", " << position[2] << "),  ";
   cout << "orientation = ("
             << orientation[0] << ", " << orientation[1] << ", " << orientation[2] << ", "
             << orientation[3] << ")"  << endl;
    
 }


Configure XML file

 <OpenTracker>
 
   <configuration>
     <CallbackConfig/>
     <NetworkSinkConfig name="Network"/>
   </configuration>
 
   <Callback  name="cb1">
     <NetworkSource mode="unicast" number="1"  address="b2_d4_7" port="12345"/>
   </Callback>
 
 </OpenTracker>


Go back to NaviTrack_Tutorial:Integrating_into_your_application.