# SoftBank Robotics documentation What's new in NAOqi 2.8?

# Example code: Bumper module

This is an example of a module subscribing to an event. It implements a callback function that makes NAO speak every time its right bumper is pressed.

The whole example is available here: `bumper.zip`

Here are the sources of the module:

## Header:

`bumper.h`

    /**
     * @author Emilie Wirbel
     *
     * This file was generated by Aldebaran Robotics ModuleGenerator
     */

    #ifndef BUMPER_BUMPER_H
    #define BUMPER_BUMPER_H

    #include <boost/shared_ptr.hpp>
    #include <alcommon/almodule.h>
    #include <string>

    #include <alproxies/almemoryproxy.h>
    #include <alproxies/altexttospeechproxy.h>
    #include <althread/almutex.h>

    namespace AL
    {
      class ALBroker;
    }

    class Bumper : public AL::ALModule
    {
      public:

        Bumper(boost::shared_ptr<AL::ALBroker> broker, const std::string& name);

        virtual ~Bumper();

        /** Overloading ALModule::init().
        * This is called right after the module has been loaded
        */
        virtual void init();

        /**
        * This method will be called every time the event RightBumperPressed is raised.
        */
        void onRightBumperPressed();

      private:
        AL::ALMemoryProxy fMemoryProxy;
        AL::ALTextToSpeechProxy fTtsProxy;

        boost::shared_ptr<AL::ALMutex> fCallbackMutex;

        float fState;

    };

    #endif  // BUMPER_BUMPER_H

## Source:

`bumper.cpp`

    /**
     * Copyright (c) 2011 Aldebaran Robotics
     */

    #include "bumper.h"

    #include <alvalue/alvalue.h>
    #include <alcommon/alproxy.h>
    #include <alcommon/albroker.h>
    #include <qi/log.hpp>
    #include <althread/alcriticalsection.h>

    Bumper::Bumper(
      boost::shared_ptr<AL::ALBroker> broker,
      const std::string& name): AL::ALModule(broker, name),
        fCallbackMutex(AL::ALMutex::createALMutex())
    {
      setModuleDescription("This module presents how to subscribe to a simple event (here RightBumperPressed) and use a callback method.");

      functionName("onRightBumperPressed", getName(), "Method called when the right bumper is pressed. Makes a LED animation.");
      BIND_METHOD(Bumper::onRightBumperPressed);
    }

    Bumper::~Bumper() {
      fMemoryProxy.unsubscribeToEvent("onRightBumperPressed", "Bumper");
    }

    void Bumper::init() {
      try {
        /** Create a proxy to ALMemory.
        */
        fMemoryProxy = AL::ALMemoryProxy(getParentBroker());

        fState = fMemoryProxy.getData("RightBumperPressed");
        /** Subscribe to event LeftBumperPressed
        * Arguments:
        * - name of the event
        * - name of the module to be called for the callback
        * - name of the bound method to be called on event
        */
        fMemoryProxy.subscribeToEvent("RightBumperPressed", "Bumper",
                                      "onRightBumperPressed");
      }
      catch (const AL::ALError& e) {
        qiLogError("module.example") << e.what() << std::endl;
      }
    }

    void Bumper::onRightBumperPressed() {
      qiLogInfo("module.example") << "Executing callback method on right bumper event" << std::endl;
      /**
      * As long as this is defined, the code is thread-safe.
      */
      AL::ALCriticalSection section(fCallbackMutex);

      /**
      * Check that the bumper is pressed.
      */
      fState =  fMemoryProxy.getData("RightBumperPressed");
      if (fState  > 0.5f) {
        return;
      }
      try {
        fTtsProxy = AL::ALTextToSpeechProxy(getParentBroker());
        fTtsProxy.say("Right bumper pressed");
      }
      catch (const AL::ALError& e) {
        qiLogError("module.example") << e.what() << std::endl;
      }
    }

## Main:

`main.cpp`

    /**
     * @author Emilie Wirbel
     *
     * \section Description
     * This file was generated by Aldebaran Robotics ModuleGenerator
     */

    #include <signal.h>
    #include <boost/shared_ptr.hpp>
    #include <alcommon/albroker.h>
    #include <alcommon/almodule.h>
    #include <alcommon/albrokermanager.h>
    #include <alcommon/altoolsmain.h>

    #include "bumper.h"

    #ifdef BUMPER_IS_REMOTE
    # define ALCALL
    #else
    # ifdef _WIN32
    #  define ALCALL __declspec(dllexport)
    # else
    #  define ALCALL
    # endif
    #endif

    extern "C"
    {
      ALCALL int _createModule(boost::shared_ptr<AL::ALBroker> pBroker)
      {
        // init broker with the main broker instance
        // from the parent executable
        AL::ALBrokerManager::setInstance(pBroker->fBrokerManager.lock());
        AL::ALBrokerManager::getInstance()->addBroker(pBroker);
          AL::ALModule::createModule<Bumper>( pBroker, "Bumper" );

        return 0;
      }

      ALCALL int _closeModule()
      {
        return 0;
      }
    }

    #ifdef BUMPER_IS_REMOTE
      int main(int argc, char *argv[])
      {
        // pointer to createModule
        TMainType sig;
        sig = &_createModule;
        // call main
        ALTools::mainFunction("bumper", argc, argv, sig);
      }
    #endif

## CMakeLists.txt:

The corresponding CMakeLists.txt file is:

`CMakeLists.txt`

    cmake_minimum_required(VERSION 2.8)
    project(bumper)
    find_package(qibuild)

    option(BUMPER_IS_REMOTE
      "module is compiled as a remote module (ON or OFF)"
      ON)

    set(_srcs
      main.cpp
      bumper.h
      bumper.cpp
    )

    if(BUMPER_IS_REMOTE)
      add_definitions(" -DBUMPER_IS_REMOTE ")
      qi_create_bin(bumper ${_srcs})
    else()
      qi_create_lib(bumper SHARED ${_srcs} SUBFOLDER naoqi)
    endif()

    qi_use_lib(bumper ALCOMMON ALPROXIES ALTHREAD)

Glossary Site map Index Support Contact Legal Notices