OpenNI class and error handling

We will show how to create and initialize a context object in C++ and how to use the openni::Status datatype to handle errors thrown by OpenNI core. We will use openni::Status very often in the later recipes and it is a very important part of any successful application.

In this recipe, we try to show the version number of the current OpenNI environment and then initialize the OpenNI framework. This will ask OpenNI to search for any connected device and load their modules and drivers.

Getting ready

Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the Creating a project in Visual Studio 2010 recipe in this chapter.

How to do it...

Have a look at the following steps:

  1. Open your project and then the project's main source code file. Locate this line:
    int _tmain(int argc, _TCHAR* argv[])
    {
  2. Write the following code snippet above the preceding line of code:
    char ReadLastCharOfLine()
    {
      int newChar = 0;
      int lastChar;
      fflush(stdout);
      do 
      {
        lastChar = newChar;
        newChar = getchar();
      }
      while ((newChar != '
    ') && (newChar != EOF));
      return (char)lastChar;
     }
  3. Locate this line again:
    int _tmain(int argc, _TCHAR* argv[])
    {
  4. Write the following code snippet below the preceding line of code:
      printf("OpenNI Version is %d.%d.%d.%d",
          OpenNI::getVersion().major,
          OpenNI::getVersion().minor,
          OpenNI::getVersion().maintenance,
          OpenNI::getVersion().build);
      printf("Scanning machine for devices and loading "
              "modules/drivers ...
    ");
      Status status = STATUS_OK;
      status = OpenNI::initialize();
      if (status != STATUS_OK){
        printf("ERROR: #%d, %s", status,
              OpenNI::getExtendedError());
        return 1;
      }
      printf("Completed.
    ");
      // Requesting device and creating sensor stream, then
      // reading data goes here
    
      printf("Press ENTER to exit.
    ");
    
      ReadLastCharOfLine();
    
      OpenNI::shutdown();
      return 0;

How it works...

In the first step we defined a new method named ReadLastCharOfLine(). This function will wait until the user presses the Enter key and will return the last character typed by the user before Enter or 0 if nothing. We will use this function to wait for the user input or ask the user to make a decision. In this example, we used it for preventing our application from closing before the user command. We will not describe it line by line because this function is not a part of our topic but it is simple and easy to understand.

In the second step we used the openni::OpenNI::getVersion() method from the openni::OpenNI class to get the version of the used OpenNI framework. The return value of this function is of the type openni::Version (the other name for OniVersion). Using different fields of this structure we can access different version number categories.

  printf("OpenNI Version is %d.%d.%d.%d",
      OpenNI::getVersion().major,
      OpenNI::getVersion().minor,
      OpenNI::getVersion().maintenance,
      OpenNI::getVersion().build);

Then we used openni::OpenNI to initialize OpenNI. The initializing process includes initializing and preloading different modules and drivers by OpenNI. We don't need to create an object from the openni::OpenNI class because all methods are static.

  status = OpenNI::initialize();

Also, we checked if the initializing process ended without error, by creating a variable of type openni::Status.

Status status = STATUS_OK;
  ...
if (status != STATUS_OK)

openni::Status will inform us if any error exists but it can't give us more information about this error. But on other hand OpenNI gives us a method that will return the latest error message. This method is openni::OpenNI::getExtendedError().

  printf("ERROR: #%d, %s", status,
          OpenNI::getExtendedError());

If everything is ok, we can proceed to other steps including requesting and reading data from the device or file. These parts will be discussed in later recipes. And at last when we are done with OpenNI it is better to ask it to turn all devices down and release all resources. For doing so we need to execute openni::OpenNI::shutdown().

  OpenNI::shutdown();

In our code we used the printf() function for printing messages and variable's values to console.

How it works...

Defining a method for displaying error message

From now on we will create and use a function in our project to handle openni::Status, returned from different methods of OpenNI. This will help us reduce the number of conditions in our code and improve it by making it more readable and understandable. The following is the function that we will use in the later recipes:

bool HandleStatus(Status status)
{
  if (status == STATUS_OK)
    return true;
  printf("ERROR: #%d, %s", status,
    OpenNI::getExtendedError());
  ReadLastCharOfLine();
  return false;
}

This function will return false in case of an error and will print an error message to the console. There is nothing new to describe here.

Possible values of openni::Status

Our status variable in the previous code is of the type openni::Status. This enum has different possible values that we will try to explain as follows:

  • openni::Status::STATUS_OK: This specifies that there is no problem or error
  • openni::Status::STATUS_ERROR: This specifies that an error has happened in the process of execution of the requested method or function
  • openni::Status::STATUS_NOT_IMPLEMENTED: This specifies that the method or the function you called or used is not implemented yet
  • openni::Status::STATUS_NOT_SUPPORTED: This specifies that the requested task is not supported or not possible
  • openni::Status::STATUS_BAD_PARAMETER: This specifies that the parameter of the method or function is incorrect, null, or irrelevant
  • openni::Status::STATUS_OUT_OF_FLOW: In other words, overflow usually means a problem with the memory/device or stack
  • openni::Status::STATUS_NO_DEVICE: This specifies that no device is connected and available to use
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.136.142