battery.cpp
Go to the documentation of this file.
00001 
00010 /*****************************************************************************
00011 ** Includes
00012 *****************************************************************************/
00013 
00014 #include "../../include/kobuki_driver/modules/battery.hpp"
00015 
00016 /*****************************************************************************
00017 ** Namespaces
00018 *****************************************************************************/
00019 
00020 namespace kobuki {
00021 
00022 /*****************************************************************************
00023 ** Statics
00024 *****************************************************************************/
00025 
00026 double Battery::capacity  = 16.5;
00027 double Battery::low       = 14.0;
00028 double Battery::dangerous = 13.2;
00029 
00030 /*****************************************************************************
00031 ** Implementation
00032 *****************************************************************************/
00039 Battery::Battery (const uint8_t &new_voltage, const uint8_t &charger_flag) :
00040   voltage(static_cast<double>(new_voltage)/10.0)
00041 {
00042   uint8_t state = (charger_flag & CoreSensors::Flags::BatteryStateMask);
00043   if ( state == CoreSensors::Flags::Charging) {
00044     charging_state = Charging;
00045   } else if ( state == CoreSensors::Flags::Charged ) {
00046     charging_state = Charged;
00047     capacity = voltage;
00048   } else {
00049     charging_state = Discharging;
00050   }
00051 
00052   if (charging_state == Discharging) {
00053     charging_source = None;
00054   } else if ( charger_flag & CoreSensors::Flags::AdapterType ) {
00055     charging_source = Adapter;
00056   } else {
00057     charging_source = Dock;
00058   }
00059 };
00060 
00061 Battery::Level Battery::level() const {
00062   if ( charging_state == Charged ) { return Maximum; }
00063   if ( voltage > low ) { return Healthy; }
00064   if ( voltage > dangerous ) { return Low; }
00065   return Dangerous;
00066 }
00067 
00068 float Battery::percent() const {
00069   // convert battery voltage to percent: 100% -> capacity / 5% -> dangerous
00070   float percent = ((95*(voltage - dangerous)) / (capacity - dangerous)) + 5;
00071   return std::max(std::min(percent, 100.0f), 0.0f);
00072 }
00073 
00074 } // namespace kobuki


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Thu Aug 27 2015 13:43:57