Difference between revisions of "NaviTrack Tutorial:Integrating:Callback Module"

From NAMIC Wiki
Jump to: navigation, search
Line 20: Line 20:
 
To receive and process the coordinate data, create a callback function and register it to CallbackModule.
 
To receive and process the coordinate data, create a callback function and register it to CallbackModule.
  
===Register callback function and start context of NaviTrack===
+
==Register callback function and start context of NaviTrack==
 
In main() of pullpos_cbm.cxx.
 
In main() of pullpos_cbm.cxx.
  
Line 31: Line 31:
 
   context.runAtRate(1000.0/(double)interval);
 
   context.runAtRate(1000.0/(double)interval);
  
===Define callback function itself===
+
==Define callback function itself==
  
 
   void callbackFunc(Node&, Event &event, void *)
 
   void callbackFunc(Node&, Event &event, void *)
Line 67: Line 67:
  
  
===Configure XML file===
+
==Configure XML file==
 +
The same callback name should be specified in the configuration XML file.
 +
For the above example program, "cb1" is specified for a callback module.
  
 
   <OpenTracker>
 
   <OpenTracker>
Line 81: Line 83:
 
    
 
    
 
   </OpenTracker>
 
   </OpenTracker>
 +
 +
 +
=Testing=
 +
 +
In terminal 1 (sending side: pushpos)
 +
$ ./pushpos tutorial_source.xml
 +
 +
In terminal 2 (receiving side: pullpos_cbm)
 +
$ ./pullpos_cbm tutorial_sink_cbm.xml
 +
  
  
 
Go back to [[NaviTrack_Tutorial:Integrating_into_your_application]].
 
Go back to [[NaviTrack_Tutorial:Integrating_into_your_application]].

Revision as of 21:33, 14 June 2007

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

The same callback name should be specified in the configuration XML file. For the above example program, "cb1" is specified for a callback module.

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


Testing

In terminal 1 (sending side: pushpos)

$ ./pushpos tutorial_source.xml

In terminal 2 (receiving side: pullpos_cbm)

$ ./pullpos_cbm tutorial_sink_cbm.xml


Go back to NaviTrack_Tutorial:Integrating_into_your_application.