basic.c

This example is discussed in First steps

00001 /*******************************************************************************
00002  * First Steps
00003  *
00004  * This is a program to demonstrate the basic use of the DevBot
00005  * APIs. It covers initializing the library, configuring a device from
00006  * a text file, retrieving that device by name and then using it to do
00007  * something.
00008  *
00009  * This represents the standard use case.
00010  * 
00011  *******************************************************************************/
00012 
00013 /* Include the main devbot header, all devbot-based applications will
00014  * do this */
00015 #include <devbot/bot.h>
00016 
00017 /* Include the headers for any interfaces we're going to use, in this
00018  * case we're just using the distance sensor interface
00019  * BotDistanceInterface */
00020 #include <devbot/distance.h>
00021 
00022 int main(int argc, char **argv)
00023 {
00024   /* 1. Initialize the devbot library specifying a device configuration file */
00025   bot_initialize_from_file("basic.config");
00026   
00027   /* 2. Retrieve a device by name */
00028   BotDevice *rangefinder = bot_get_device("rangefinder");
00029   
00030   /* 3. Check we've got the device and that it supports the distance
00031    * interface.  
00032    *
00033    * It is not *necessary* to do this, but should be considered good
00034    * defensive programming practice */
00035   if (!(rangefinder && bot_device_get_interface(rangefinder, BotDistanceInterface)))
00036   {
00037     exit(1);
00038   }
00039 
00040   /* 4. Retrieve and print the distance */
00041   g_message("Distance was: %lf", bot_distance_get_metre(rangefinder));
00042 
00043   /* 5. Shutdown the libarary
00044    * 
00045    * This will free internal data structures, unload drivers etc.
00046    * It is not *necessary* to do this, but it is good practice */
00047   bot_shutdown();
00048 }

(c) 2006 Edinburgh Robotics Ltd.
Generated on Fri Feb 2 11:24:04 2007 for libbot by doxygen 1.5.1