A Gazebo Plugin: The Battery Indicator
The course prototype revolved around wheeled mobile robots. Simulation tasks and challenges featured a wheeled robot in a differential drive configuration which can be set up using different battery and motor pairs to observe how the chosen design affects robot movement. In the actuation challenge, four plugins were used. A World Plugin to keep track of the score; two Model Plugins, the differential drive controller and the battery plugin; and finally, the battery indicator, a visual plugin.
The addition of a battery was meant to convey limitations in a robot’s design. For example, having a powerful motor paired with a low capacity battery should reduce the efficiency of the motor and use up the battery faster. This meant constantly checking the published battery messages. A battery indicator sphere in the simulation world that follows the mobile robot around is a nice visual way of checking the status of the battery.
The battery indicator plugin package contains the source c++ file, its header file and the standard catkin files, CMakeLists.txt and package.xml. It is important to note that the battery indicator plugin is dependent on roscpp, gazebo_ros, gazebo_plugins, message_runtime, message_generation and the battery_plugin package .
Battery Indicator Header File 
The header file contains the class declarations. It needs to include the basic gazebo functions and common gazebo headers as well as the headers for visual plugins. Also required is the Boost library and the Battery header from the Battery Plugin since it needs to subscribe to the battery_status topic to report the current battery level. The magic black box for me here is anything connected with the Boost library. Based on its use in the code, it is concerned with managing threads and anything that has to do with subscribing or publishing messages on nodes.
Battery Indicator Plugin Source File 
The plugin has four major functions: Load, DeferredLoad, ChargeCallback and OnUpdate. Starting with the Load Function, the battery subscriber node is initialized and the indicator color set to black. A new thread for the DeferredLoad function and the OnUpdate event is created. When the DeferredLoad thread starts running, it waits for ROS to be initialized and starts listening to the the battery_msg topic. A battery_msg triggers the ChargeCallback function which compares the current charge and assigns a color to the charge value. The OnUpdate function assigns the color value to the Ambience and Diffuse settings of the indicator sphere.