From: <to...@us...> - 2008-06-30 12:01:05
|
Revision: 232 http://gearbox.svn.sourceforge.net/gearbox/?rev=232&view=rev Author: tobasco Date: 2008-06-30 05:01:10 -0700 (Mon, 30 Jun 2008) Log Message: ----------- incorporated Geoff's comments Modified Paths: -------------- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.cpp gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.h gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.cpp gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryacfr.dox gearbox/trunk/submitted/gbxsmartbatteryacfr/test/CMakeLists.txt gearbox/trunk/submitted/gbxsmartbatteryacfr/test/example.cmake.in gearbox/trunk/submitted/gbxsmartbatteryacfr/test/example.readme Added Paths: ----------- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.cpp gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.h gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/CMakeLists.txt gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/checksumtest.cpp Removed Paths: ------------- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/checksumtest.cpp Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.cpp =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.cpp 2008-06-29 03:49:54 UTC (rev 231) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.cpp 2008-06-30 12:01:10 UTC (rev 232) @@ -18,227 +18,6 @@ namespace gbxsmartbatteryacfr { -// -// Helper functions -// -string toString( const vector<bool> &flags ) -{ - stringstream ss; - for (unsigned int i=0; i<flags.size(); i++) - { - ss << flags[i] << " "; - } - return ss.str(); -} - -string toLogString( const vector<bool> &flags ) -{ - stringstream ss; - for (unsigned int i=0; i<flags.size(); i++) - { - ss << flags[i]; - } - return ss.str(); -} - - -// -// OceanServerSystem non-member functions -// - -string toString( const OceanServerSystem &system ) -{ - stringstream ss; - ss << "Charge: \t" << system.percentCharge << endl; - ss << "Minutes to empty:\t" << system.minToEmpty << endl; - ss << "Available batt.: \t" << toString( system.availableBatteries ) << endl; - ss << "Charging: \t" << toString( system.chargingStates ) << endl; - ss << "Supplying power: \t" << toString( system.supplyingPowerStates ) << endl; - ss << "Charge power: \t" << toString( system.chargePowerPresentStates ) << endl; - ss << "Power no good: \t" << toString( system.powerNoGoodStates ) << endl; - ss << "Charge inhibited:\t" << toString( system.chargeInhibitedStates ) << endl; - - map<int,SmartBattery>::const_iterator it; - for (it=system.batteries().begin(); it!=system.batteries().end(); it++) - { - ss << "Data from battery number: " << it->first << endl; - ss << toString( it->second ) << endl; - } - - return ss.str(); -} - -string toLogString( const OceanServerSystem &system ) -{ - stringstream ss; - ss << system.percentCharge << " "; - ss << system.minToEmpty << " "; - ss << toLogString( system.availableBatteries ) << " "; - ss << toLogString( system.chargingStates ) << " "; - ss << toLogString( system.supplyingPowerStates ) << " "; - ss << toLogString( system.chargePowerPresentStates ) << " "; - ss << toLogString( system.powerNoGoodStates ) << " "; - ss << toLogString( system.chargeInhibitedStates ) << endl; - - ss << system.batteries().size(); - - map<int,SmartBattery>::const_iterator it; - for (it=system.batteries().begin(); it!=system.batteries().end(); it++) - { - ss << it->first << " " << toLogString( it->second ) << endl; - } - - return ss.str(); -} - -void updateWithNewData( const OceanServerSystem &from, - OceanServerSystem &to ) -{ - typedef map<int,SmartBattery>::const_iterator BatIt; - - to.availableBatteries = from.availableBatteries; - to.percentCharge = from.percentCharge; - to.minToEmpty = from.minToEmpty; - to.messageToSystem = from.messageToSystem; - to.chargingStates = from.chargingStates; - to.supplyingPowerStates = from.supplyingPowerStates; - to.chargePowerPresentStates = from.chargePowerPresentStates; - to.powerNoGoodStates = from.powerNoGoodStates; - to.chargeInhibitedStates = from.chargeInhibitedStates; - - for (BatIt it=from.batteries().begin(); it!=from.batteries().end(); it++) - { - const SmartBattery &fromB = from.battery( it->first ); - SmartBattery &toB = to.battery( it->first ); - - if ( fromB.has( ManufacturerAccess ) ) toB.setManufacturerAccess ( fromB.manufacturerAccess () ); - if ( fromB.has( RemainingCapacityAlarm ) ) toB.setRemainingCapacityAlarm ( fromB.remainingCapacityAlarm () ); - if ( fromB.has( RemainingTimeAlarm ) ) toB.setRemainingTimeAlarm ( fromB.remainingTimeAlarm () ); - if ( fromB.has( BatteryMode ) ) toB.setBatteryMode ( fromB.batteryMode () ); - if ( fromB.has( AtRate ) ) toB.setAtRate ( fromB.atRate () ); - if ( fromB.has( AtRateTimeToFull ) ) toB.setAtRateTimeToFull ( fromB.atRateTimeToFull () ); - if ( fromB.has( AtRateTimeToEmpty ) ) toB.setAtRateTimeToEmpty ( fromB.atRateTimeToEmpty () ); - if ( fromB.has( AtRateOk ) ) toB.setAtRateOk ( fromB.atRateOk () ); - if ( fromB.has( Temperature ) ) toB.setTemperature ( fromB.temperature () ); - if ( fromB.has( Voltage ) ) toB.setVoltage ( fromB.voltage () ); - if ( fromB.has( Current ) ) toB.setCurrent ( fromB.current () ); - if ( fromB.has( AverageCurrent ) ) toB.setAverageCurrent ( fromB.averageCurrent () ); - if ( fromB.has( MaxError ) ) toB.setMaxError ( fromB.maxError () ); - if ( fromB.has( RelativeStateOfCharge ) ) toB.setRelativeStateOfCharge ( fromB.relativeStateOfCharge () ); - if ( fromB.has( AbsoluteStateOfCharge ) ) toB.setAbsoluteStateOfCharge ( fromB.absoluteStateOfCharge () ); - if ( fromB.has( RemainingCapacity ) ) toB.setRemainingCapacity ( fromB.remainingCapacity () ); - if ( fromB.has( FullChargeCapacity ) ) toB.setFullChargeCapacity ( fromB.fullChargeCapacity () ); - if ( fromB.has( RunTimeToEmpty ) ) toB.setRunTimeToEmpty ( fromB.runTimeToEmpty () ); - if ( fromB.has( AverageTimeToEmpty ) ) toB.setAverageTimeToEmpty ( fromB.averageTimeToEmpty () ); - if ( fromB.has( AverageTimeToFull ) ) toB.setAverageTimeToFull ( fromB.averageTimeToFull () ); - if ( fromB.has( ChargingCurrent ) ) toB.setChargingCurrent ( fromB.chargingCurrent () ); - if ( fromB.has( ChargingVoltage ) ) toB.setChargingVoltage ( fromB.chargingVoltage () ); - if ( fromB.has( BatteryStatus ) ) toB.setBatteryStatus ( fromB.batteryStatus () ); - if ( fromB.has( CycleCount ) ) toB.setCycleCount ( fromB.cycleCount () ); - if ( fromB.has( DesignCapacity ) ) toB.setDesignCapacity ( fromB.designCapacity () ); - if ( fromB.has( DesignVoltage ) ) toB.setDesignVoltage ( fromB.designVoltage () ); - if ( fromB.has( SpecificationInfo ) ) toB.setSpecificationInfo ( fromB.specificationInfo () ); - if ( fromB.has( ManufactureDate ) ) toB.setManufactureDate ( fromB.manufactureDate () ); - if ( fromB.has( SerialNumber ) ) toB.setSerialNumber ( fromB.serialNumber () ); - if ( fromB.has( ManufacturerName ) ) toB.setManufacturerName ( fromB.manufacturerName () ); - if ( fromB.has( DeviceName ) ) toB.setDeviceName ( fromB.deviceName () ); - if ( fromB.has( DeviceChemistry ) ) toB.setDeviceChemistry ( fromB.deviceChemistry () ); - if ( fromB.has( ManufacturerData ) ) toB.setManufacturerData ( fromB.manufacturerData () ); - } - - // check if reaping needs to be done, if not return - if ( from.batteries().size() == to.batteries().size() ) - return; - - // store battery ids from batteries which need to be reaped in a vector - vector<int> reapingIds; - - // go through all 'to' batteries and check if they are also in 'from' - for (BatIt it=to.batteries().begin(); it!=to.batteries().end(); it++) - { - const int batId = it->first; - - BatIt itFrom = from.batteries().find( batId ); - if ( itFrom == from.batteries().end() ) { - // battery is in 'to' but not in 'from' -> needs to be reaped - reapingIds.push_back( batId ); - } - } - - // reap batteries - for (unsigned int i=0; i<reapingIds.size(); i++) { - to.eraseBattery( reapingIds[i] ); - } - -} - - -// -// OceanServerSystem member functions -// - -OceanServerSystem::OceanServerSystem() - : percentCharge(0), - minToEmpty(0), - messageToSystem("") -{ - // fixed number of slots for oceanserver system - const int NUM_BATTERY_SLOTS = 8; - availableBatteries.resize(NUM_BATTERY_SLOTS); - chargingStates.resize(NUM_BATTERY_SLOTS); - supplyingPowerStates.resize(NUM_BATTERY_SLOTS); - chargePowerPresentStates.resize(NUM_BATTERY_SLOTS); - powerNoGoodStates.resize(NUM_BATTERY_SLOTS); - chargeInhibitedStates.resize(NUM_BATTERY_SLOTS); -} - -// read access to all batteries -const map<int,SmartBattery>& -OceanServerSystem::batteries() const -{ - return batteries_; -} - -// write access to single battery -SmartBattery& -OceanServerSystem::battery( unsigned int batteryNumber ) -{ - map<int,SmartBattery>::iterator it = batteries_.find(batteryNumber); - if ( it==batteries_.end() ) - { - // we don't have it, so instantiate a new one - SmartBattery b; - batteries_[batteryNumber] = b; - return batteries_[batteryNumber]; - } - - return it->second; -} - -// read access to single battery -const SmartBattery& -OceanServerSystem::battery( unsigned int batteryNumber ) const -{ - map<int,SmartBattery>::const_iterator it = batteries_.find(batteryNumber); - if ( it==batteries_.end() ) - { - stringstream ss; - ss << "Trying to read from non-existent battery " << batteryNumber; - throw ParsingException( ERROR_INFO, ss.str().c_str() ); - } - return it->second; -} - -void -OceanServerSystem::eraseBattery( unsigned int batteryNumber ) -{ - batteries_.erase( batteries_.find( batteryNumber ) ); -} - -// -// OceanServerParser member functions -// - OceanServerParser::OceanServerParser( gbxutilacfr::Tracer &tracer ) : tracer_(tracer) { @@ -488,25 +267,29 @@ // // Debugging output // - stringstream ss; - ss << "OceanServerParser: Received the following input: " << endl; - for (unsigned int i=0; i<stringList.size(); i++) + const int debugLevel = 10; + if (tracer_.verbosity( gbxutilacfr::Tracer::DebugTrace, gbxutilacfr::Tracer::ToAny ) >= debugLevel) { - const string &str = stringList[i]; - ss << i << ": " << str; - - // output in hex - ss << i << " hex: "; - for (unsigned k=0; k<str.size(); k++) + stringstream ss; + ss << "OceanServerParser: Received the following input: " << endl; + for (unsigned int i=0; i<stringList.size(); i++) { - unsigned int charValue = (unsigned int)str[k]; - ss << str[k] << ": " << std::hex << charValue << " "; + const string &str = stringList[i]; + ss << i << ": " << str; + + // output in hex + ss << i << " hex: "; + for (unsigned k=0; k<str.size(); k++) + { + unsigned int charValue = (unsigned int)str[k]; + ss << str[k] << ": " << std::hex << charValue << " "; + } + ss << std::dec; + } - ss << std::dec; - + ss << endl; + tracer_.debug( ss.str(), debugLevel ); } - ss << endl; - tracer_.debug( ss.str(), 10 ); // // Parsing Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.h =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.h 2008-06-29 03:49:54 UTC (rev 231) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserverparser.h 2008-06-30 12:01:10 UTC (rev 232) @@ -13,69 +13,11 @@ #include <map> #include <gbxutilacfr/tracer.h> -#include <gbxsmartbatteryacfr/smartbattery.h> +#include <gbxsmartbatteryacfr/oceanserversystem.h> namespace gbxsmartbatteryacfr { - -//! -//! Class representing the OceanServer battery system data -//! Contains average values of the whole system and values from individual batteries -//! -//! @author Tobias Kaupp -//! -class OceanServerSystem -{ - public: - - OceanServerSystem(); - ~OceanServerSystem() {}; - - //! Read access to all batteries - const std::map<int,SmartBattery>& batteries() const; - - //! Easy write access to single battery, instantiates a new one if it doesn't exist - SmartBattery& battery( unsigned int batteryNumber ); - - //! Easy read access to single battery, battery must exist - const SmartBattery& battery( unsigned int batteryNumber ) const; - - //! Erase a battery - void eraseBattery( unsigned int batteryNumber ); - - //! Average battery values - int percentCharge; - int minToEmpty; - std::string messageToSystem; - - //! Battery module states - //! Each vector is always of size 8 because oceanserver system has 8 slots - std::vector<bool> availableBatteries; - std::vector<bool> chargingStates; - std::vector<bool> supplyingPowerStates; - std::vector<bool> chargePowerPresentStates; - std::vector<bool> powerNoGoodStates; - std::vector<bool> chargeInhibitedStates; - - private: - - // key: slot number, data: a single smart battery module - std::map<int,SmartBattery> batteries_; -}; - -//! Puts OceanServerSystem data into a human-readable string -std::string toString( const OceanServerSystem &system ); - -//! Puts OceanServerSystem data into a machine-readable ASCII string -std::string toLogString( const OceanServerSystem &system ); -//! Updates all fields in 'to' with data from 'from'. Also reapes batteries in 'to' if they are not in 'from'. -//! Has persistence capabilities: if fields in 'from' are not set and corresponding fields in 'to' are set, the ones in 'to' are kept. -//! Use case: a class stores 'to' as a member variable, receives the latest records into 'from', calls this function to update 'to'. -//! The reaping capability makes sure that battery modules which are no longer connected don't persist. -void updateWithNewData( const OceanServerSystem &from, - OceanServerSystem &to ); - //! //! Class to parse the hex data the oceanserver battery controller spits out //! Added: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.cpp =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.cpp (rev 0) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.cpp 2008-06-30 12:01:10 UTC (rev 232) @@ -0,0 +1,239 @@ +/* + * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics + * http://gearbox.sf.net/ + * Copyright (c) 2004-2008 Tobias Kaupp + * + * This distribution is licensed to you under the terms described in + * the LICENSE file included in this distribution. + * + */ + +#include <sstream> +#include <cstring> +#include <gbxsmartbatteryacfr/exceptions.h> + +#include "oceanserversystem.h" + +using namespace std; + +namespace gbxsmartbatteryacfr { + +// +// Helper functions +// + +string toString( const vector<bool> &flags ) +{ + stringstream ss; + for (unsigned int i=0; i<flags.size(); i++) + { + ss << flags[i] << " "; + } + return ss.str(); +} + +string toLogString( const vector<bool> &flags ) +{ + stringstream ss; + for (unsigned int i=0; i<flags.size(); i++) + { + ss << flags[i]; + } + return ss.str(); +} + + +// +// Non-member functions +// + +string toString( const OceanServerSystem &system ) +{ + stringstream ss; + ss << "Charge: \t" << system.percentCharge << endl; + ss << "Minutes to empty:\t" << system.minToEmpty << endl; + ss << "Available batt.: \t" << toString( system.availableBatteries ) << endl; + ss << "Charging: \t" << toString( system.chargingStates ) << endl; + ss << "Supplying power: \t" << toString( system.supplyingPowerStates ) << endl; + ss << "Charge power: \t" << toString( system.chargePowerPresentStates ) << endl; + ss << "Power no good: \t" << toString( system.powerNoGoodStates ) << endl; + ss << "Charge inhibited:\t" << toString( system.chargeInhibitedStates ) << endl; + + map<int,SmartBattery>::const_iterator it; + for (it=system.batteries().begin(); it!=system.batteries().end(); it++) + { + ss << "Data from battery number: " << it->first << endl; + ss << toString( it->second ) << endl; + } + + return ss.str(); +} + +string toLogString( const OceanServerSystem &system ) +{ + stringstream ss; + ss << system.percentCharge << " "; + ss << system.minToEmpty << " "; + ss << toLogString( system.availableBatteries ) << " "; + ss << toLogString( system.chargingStates ) << " "; + ss << toLogString( system.supplyingPowerStates ) << " "; + ss << toLogString( system.chargePowerPresentStates ) << " "; + ss << toLogString( system.powerNoGoodStates ) << " "; + ss << toLogString( system.chargeInhibitedStates ) << endl; + + ss << system.batteries().size(); + + map<int,SmartBattery>::const_iterator it; + for (it=system.batteries().begin(); it!=system.batteries().end(); it++) + { + ss << it->first << " " << toLogString( it->second ) << endl; + } + + return ss.str(); +} + +void updateWithNewData( const OceanServerSystem &from, + OceanServerSystem &to ) +{ + typedef map<int,SmartBattery>::const_iterator BatIt; + + to.availableBatteries = from.availableBatteries; + to.percentCharge = from.percentCharge; + to.minToEmpty = from.minToEmpty; + to.messageToSystem = from.messageToSystem; + to.chargingStates = from.chargingStates; + to.supplyingPowerStates = from.supplyingPowerStates; + to.chargePowerPresentStates = from.chargePowerPresentStates; + to.powerNoGoodStates = from.powerNoGoodStates; + to.chargeInhibitedStates = from.chargeInhibitedStates; + + for (BatIt it=from.batteries().begin(); it!=from.batteries().end(); it++) + { + const SmartBattery &fromB = from.battery( it->first ); + SmartBattery &toB = to.battery( it->first ); + + if ( fromB.has( ManufacturerAccess ) ) toB.setManufacturerAccess ( fromB.manufacturerAccess () ); + if ( fromB.has( RemainingCapacityAlarm ) ) toB.setRemainingCapacityAlarm ( fromB.remainingCapacityAlarm () ); + if ( fromB.has( RemainingTimeAlarm ) ) toB.setRemainingTimeAlarm ( fromB.remainingTimeAlarm () ); + if ( fromB.has( BatteryMode ) ) toB.setBatteryMode ( fromB.batteryMode () ); + if ( fromB.has( AtRate ) ) toB.setAtRate ( fromB.atRate () ); + if ( fromB.has( AtRateTimeToFull ) ) toB.setAtRateTimeToFull ( fromB.atRateTimeToFull () ); + if ( fromB.has( AtRateTimeToEmpty ) ) toB.setAtRateTimeToEmpty ( fromB.atRateTimeToEmpty () ); + if ( fromB.has( AtRateOk ) ) toB.setAtRateOk ( fromB.atRateOk () ); + if ( fromB.has( Temperature ) ) toB.setTemperature ( fromB.temperature () ); + if ( fromB.has( Voltage ) ) toB.setVoltage ( fromB.voltage () ); + if ( fromB.has( Current ) ) toB.setCurrent ( fromB.current () ); + if ( fromB.has( AverageCurrent ) ) toB.setAverageCurrent ( fromB.averageCurrent () ); + if ( fromB.has( MaxError ) ) toB.setMaxError ( fromB.maxError () ); + if ( fromB.has( RelativeStateOfCharge ) ) toB.setRelativeStateOfCharge ( fromB.relativeStateOfCharge () ); + if ( fromB.has( AbsoluteStateOfCharge ) ) toB.setAbsoluteStateOfCharge ( fromB.absoluteStateOfCharge () ); + if ( fromB.has( RemainingCapacity ) ) toB.setRemainingCapacity ( fromB.remainingCapacity () ); + if ( fromB.has( FullChargeCapacity ) ) toB.setFullChargeCapacity ( fromB.fullChargeCapacity () ); + if ( fromB.has( RunTimeToEmpty ) ) toB.setRunTimeToEmpty ( fromB.runTimeToEmpty () ); + if ( fromB.has( AverageTimeToEmpty ) ) toB.setAverageTimeToEmpty ( fromB.averageTimeToEmpty () ); + if ( fromB.has( AverageTimeToFull ) ) toB.setAverageTimeToFull ( fromB.averageTimeToFull () ); + if ( fromB.has( ChargingCurrent ) ) toB.setChargingCurrent ( fromB.chargingCurrent () ); + if ( fromB.has( ChargingVoltage ) ) toB.setChargingVoltage ( fromB.chargingVoltage () ); + if ( fromB.has( BatteryStatus ) ) toB.setBatteryStatus ( fromB.batteryStatus () ); + if ( fromB.has( CycleCount ) ) toB.setCycleCount ( fromB.cycleCount () ); + if ( fromB.has( DesignCapacity ) ) toB.setDesignCapacity ( fromB.designCapacity () ); + if ( fromB.has( DesignVoltage ) ) toB.setDesignVoltage ( fromB.designVoltage () ); + if ( fromB.has( SpecificationInfo ) ) toB.setSpecificationInfo ( fromB.specificationInfo () ); + if ( fromB.has( ManufactureDate ) ) toB.setManufactureDate ( fromB.manufactureDate () ); + if ( fromB.has( SerialNumber ) ) toB.setSerialNumber ( fromB.serialNumber () ); + if ( fromB.has( ManufacturerName ) ) toB.setManufacturerName ( fromB.manufacturerName () ); + if ( fromB.has( DeviceName ) ) toB.setDeviceName ( fromB.deviceName () ); + if ( fromB.has( DeviceChemistry ) ) toB.setDeviceChemistry ( fromB.deviceChemistry () ); + if ( fromB.has( ManufacturerData ) ) toB.setManufacturerData ( fromB.manufacturerData () ); + } + + // check if reaping needs to be done, if not return + if ( from.batteries().size() == to.batteries().size() ) + return; + + // store battery ids from batteries which need to be reaped in a vector + vector<int> reapingIds; + + // go through all 'to' batteries and check if they are also in 'from' + for (BatIt it=to.batteries().begin(); it!=to.batteries().end(); it++) + { + const int batId = it->first; + + BatIt itFrom = from.batteries().find( batId ); + if ( itFrom == from.batteries().end() ) { + // battery is in 'to' but not in 'from' -> needs to be reaped + reapingIds.push_back( batId ); + } + } + + // reap batteries + for (unsigned int i=0; i<reapingIds.size(); i++) { + to.eraseBattery( reapingIds[i] ); + } + +} + + +// +// Member functions +// + +OceanServerSystem::OceanServerSystem() + : percentCharge(0), + minToEmpty(0), + messageToSystem("") +{ + // fixed number of slots for oceanserver system + const int NUM_BATTERY_SLOTS = 8; + availableBatteries.resize(NUM_BATTERY_SLOTS); + chargingStates.resize(NUM_BATTERY_SLOTS); + supplyingPowerStates.resize(NUM_BATTERY_SLOTS); + chargePowerPresentStates.resize(NUM_BATTERY_SLOTS); + powerNoGoodStates.resize(NUM_BATTERY_SLOTS); + chargeInhibitedStates.resize(NUM_BATTERY_SLOTS); +} + +// read access to all batteries +const map<int,SmartBattery>& +OceanServerSystem::batteries() const +{ + return batteries_; +} + +// write access to single battery +SmartBattery& +OceanServerSystem::battery( unsigned int batteryNumber ) +{ + map<int,SmartBattery>::iterator it = batteries_.find(batteryNumber); + if ( it==batteries_.end() ) + { + // we don't have it, so instantiate a new one + SmartBattery b; + batteries_[batteryNumber] = b; + return batteries_[batteryNumber]; + } + + return it->second; +} + +// read access to single battery +const SmartBattery& +OceanServerSystem::battery( unsigned int batteryNumber ) const +{ + map<int,SmartBattery>::const_iterator it = batteries_.find(batteryNumber); + if ( it==batteries_.end() ) + { + stringstream ss; + ss << "Trying to read from non-existent battery " << batteryNumber; + throw ParsingException( ERROR_INFO, ss.str().c_str() ); + } + return it->second; +} + +void +OceanServerSystem::eraseBattery( unsigned int batteryNumber ) +{ + batteries_.erase( batteries_.find( batteryNumber ) ); +} + +} //namespace \ No newline at end of file Added: gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.h =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.h (rev 0) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/oceanserversystem.h 2008-06-30 12:01:10 UTC (rev 232) @@ -0,0 +1,80 @@ +/* + * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics + * http://gearbox.sf.net/ + * Copyright (c) 2004-2008 Tobias Kaupp + * + * This distribution is licensed to you under the terms described in + * the LICENSE file included in this distribution. + * + */ + +#ifndef GBX_OCEANSERVER_SYSTEM_H +#define GBX_OCEANSERVER_SYSTEM_H + +#include <map> +#include <gbxsmartbatteryacfr/smartbattery.h> + +namespace gbxsmartbatteryacfr +{ + +//! +//! Class representing the OceanServer battery system data +//! Contains average values of the whole system and values from individual batteries +//! +//! @author Tobias Kaupp +//! +class OceanServerSystem +{ + public: + + OceanServerSystem(); + ~OceanServerSystem() {}; + + //! Read access to all batteries + const std::map<int,SmartBattery>& batteries() const; + + //! Easy write access to single battery, instantiates a new one if it doesn't exist + SmartBattery& battery( unsigned int batteryNumber ); + + //! Easy read access to single battery, battery must exist + const SmartBattery& battery( unsigned int batteryNumber ) const; + + //! Erase a battery + void eraseBattery( unsigned int batteryNumber ); + + //! Average battery values + int percentCharge; + int minToEmpty; + std::string messageToSystem; + + //! Battery module states + //! Each vector is always of size 8 because oceanserver system has 8 slots + std::vector<bool> availableBatteries; + std::vector<bool> chargingStates; + std::vector<bool> supplyingPowerStates; + std::vector<bool> chargePowerPresentStates; + std::vector<bool> powerNoGoodStates; + std::vector<bool> chargeInhibitedStates; + + private: + + // key: slot number, data: a single smart battery module + std::map<int,SmartBattery> batteries_; +}; + +//! Puts OceanServerSystem data into a human-readable string +std::string toString( const OceanServerSystem &system ); + +//! Puts OceanServerSystem data into a machine-readable ASCII string +std::string toLogString( const OceanServerSystem &system ); + +//! Updates all fields in 'to' with data from 'from'. Also reapes batteries in 'to' if they are not in 'from'. +//! Has persistence capabilities: if fields in 'from' are not set and corresponding fields in 'to' are set, the ones in 'to' are kept. +//! Use case: a class stores 'to' as a member variable, receives the latest records into 'from', calls this function to update 'to'. +//! The reaping capability makes sure that battery modules which are no longer connected don't persist. +void updateWithNewData( const OceanServerSystem &from, + OceanServerSystem &to ); + +} // namespace + +#endif Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.cpp =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.cpp 2008-06-29 03:49:54 UTC (rev 231) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbattery.cpp 2008-06-30 12:01:10 UTC (rev 232) @@ -10,6 +10,7 @@ #include <iostream> #include <sstream> +#include <gbxsmartbatteryacfr/exceptions.h> #include "smartbattery.h" @@ -52,8 +53,12 @@ else if (fieldStr=="21") return DeviceName; else if (fieldStr=="22") return DeviceChemistry; else if (fieldStr=="23") return ManufacturerData; - else cout << "ERROR: Unknown field: " << fieldStr << endl; - + else + { + stringstream ss; + ss << "Unknown field: " << fieldStr; + throw ParsingException( ERROR_INFO, ss.str().c_str() ); + } return NUM_SMARTBATTERY_FIELDS; } Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryacfr.dox =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryacfr.dox 2008-06-29 03:49:54 UTC (rev 231) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/smartbatteryacfr.dox 2008-06-30 12:01:10 UTC (rev 232) @@ -24,8 +24,8 @@ @endverbatim @par Examples -- See 'test/simpletest.cpp' for a simple example of how to use the library. -- See 'test/fancytest.cpp' for a more advanced example. +- See 'test/simpletest.cpp' for a simple example of how to use the library. The test reads a few records from the oceanserver system and prints the results on the screen. +- See 'test/fancytest.cpp' for a more advanced example. The test uses a class to read a few records from the oceanserver system and stores an internal (full) record which only gets updated with new data. @par Style See http://orca-robotics.sourceforge.net/orca/orca_doc_style.html Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/CMakeLists.txt =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/CMakeLists.txt 2008-06-29 03:49:54 UTC (rev 231) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/CMakeLists.txt 2008-06-30 12:01:10 UTC (rev 232) @@ -1,13 +1,15 @@ INCLUDE( ${GBX_CMAKE_DIR}/UseBasicRules.cmake ) -GBX_ADD_EXECUTABLE( gbxsmartbatterychecksumtest checksumtest.cpp ) -GBX_ADD_TEST( GbxSmartBatteryChecksumTest gbxsmartbatterychecksumtest ) - GBX_ADD_EXECUTABLE( gbxsmartbatterysimpletest simpletest.cpp ) TARGET_LINK_LIBRARIES( gbxsmartbatterysimpletest GbxSmartBatteryAcfr ) GBX_ADD_EXECUTABLE( gbxsmartbatteryfancytest fancytest.cpp ) TARGET_LINK_LIBRARIES( gbxsmartbatteryfancytest GbxSmartBatteryAcfr ) -GBX_ADD_EXAMPLE( gbxsmartbatteryacfr example.cmake.in example.cmake checksumtest.cpp simpletest.cpp fancytest.cpp example.readme ) +GBX_ADD_EXAMPLE( gbxsmartbatteryacfr example.cmake.in example.cmake simpletest.cpp fancytest.cpp example.readme ) +IF( GBX_BUILD_TESTS ) + ADD_SUBDIRECTORY( darttest ) +ENDIF( GBX_BUILD_TESTS ) + + Deleted: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/checksumtest.cpp =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/checksumtest.cpp 2008-06-29 03:49:54 UTC (rev 231) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/checksumtest.cpp 2008-06-30 12:01:10 UTC (rev 232) @@ -1,94 +0,0 @@ -#include <iostream> -#include <sstream> - -using namespace std; - -template < typename T > -inline T highbit(T& t) -{ - return t = (((T)(-1)) >> 1) + 1; -} - -template < typename T > -std::ostream& bin(T& value, std::ostream &o) -{ - for ( T bit = highbit(bit); bit; bit >>= 1 ) - { - o << ( ( value & bit ) ? '1' : '0' ); - } - return o; -} - -// Simple checksum test using XOR -int main( int argc, char **argv ) -{ - int opt; - std::string input = "B18,0A,0000"; - std::string resultHex = "3A"; - - // Get some options from the command line - while ((opt = getopt(argc, argv, "i:r:")) != -1) - { - switch ( opt ) - { - case 'i': - input = optarg; - break; - case 'r': - resultHex = optarg; - break; - default: - cout << "Usage: " << argv[0] << " [-i input] [-r resultInHex]" << endl - << "-i input\tInput string. E.g. B18,0A,0000" << endl - << "-r resultInHex\tResult in hex. E.g. 3A "<< endl; - return 1; - } - } - - cout << endl << "Checksum is computed using XOR" << endl; - cout << endl << "Input string is:\t\t " << input << endl; - cout << "Expected checksum result is:\t " << resultHex << endl << endl; - - unsigned int checksum = 0; - - cout << "Table: " << endl - << "char\tdec\thex\tbin" << endl - << "----------------------------------" << endl; - - for (unsigned int i=0; i<input.size(); i++) - { - unsigned int charValue = (unsigned int)input[i]; - cout << input[i] << "\t" - << std::dec << charValue << "\t" - << std::hex << charValue << "\t"; - bin(charValue, cout); - cout << endl; - //checksum computation - checksum ^= charValue; - } - - cout << endl; - cout << "Computed checksum (dec,hex,bin): " << std::dec << checksum << ", " << std::hex << checksum << ", "; - bin(checksum, cout); cout << endl << endl; - - stringstream ss; - ss << std::hex << checksum; - - string checksumStr = ss.str(); - for (unsigned int i=0; i<checksumStr.size(); i++) - checksumStr[i] = toupper( checksumStr[i] ); - - cout << "Expected checksum result is:\t" << resultHex << endl; - cout << "Computed checksum result is:\t" << checksumStr << endl << endl; - - int passTest = checksumStr.compare( resultHex ); - if (passTest!=0) { - cout << "Test not passed" << endl; - return 1; - } - cout << "Test passed" << endl; - - return 0; -} - - Added: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/CMakeLists.txt =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/CMakeLists.txt (rev 0) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/CMakeLists.txt 2008-06-30 12:01:10 UTC (rev 232) @@ -0,0 +1,4 @@ +INCLUDE( ${GBX_CMAKE_DIR}/UseBasicRules.cmake ) + +GBX_ADD_EXECUTABLE( gbxsmartbatterychecksumtest checksumtest.cpp ) +GBX_ADD_TEST( GbxSmartBatteryChecksumTest gbxsmartbatterychecksumtest ) \ No newline at end of file Added: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/checksumtest.cpp =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/checksumtest.cpp (rev 0) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/darttest/checksumtest.cpp 2008-06-30 12:01:10 UTC (rev 232) @@ -0,0 +1,94 @@ +#include <iostream> +#include <sstream> + +using namespace std; + +template < typename T > +inline T highbit(T& t) +{ + return t = (((T)(-1)) >> 1) + 1; +} + +template < typename T > +std::ostream& bin(T& value, std::ostream &o) +{ + for ( T bit = highbit(bit); bit; bit >>= 1 ) + { + o << ( ( value & bit ) ? '1' : '0' ); + } + return o; +} + +// Simple checksum test using XOR +int main( int argc, char **argv ) +{ + int opt; + std::string input = "B18,0A,0000"; + std::string resultHex = "3A"; + + // Get some options from the command line + while ((opt = getopt(argc, argv, "i:r:")) != -1) + { + switch ( opt ) + { + case 'i': + input = optarg; + break; + case 'r': + resultHex = optarg; + break; + default: + cout << "Usage: " << argv[0] << " [-i input] [-r resultInHex]" << endl + << "-i input\tInput string. E.g. B18,0A,0000" << endl + << "-r resultInHex\tResult in hex. E.g. 3A "<< endl; + return 1; + } + } + + cout << endl << "Checksum is computed using XOR" << endl; + cout << endl << "Input string is:\t\t " << input << endl; + cout << "Expected checksum result is:\t " << resultHex << endl << endl; + + unsigned int checksum = 0; + + cout << "Table: " << endl + << "char\tdec\thex\tbin" << endl + << "----------------------------------" << endl; + + for (unsigned int i=0; i<input.size(); i++) + { + unsigned int charValue = (unsigned int)input[i]; + cout << input[i] << "\t" + << std::dec << charValue << "\t" + << std::hex << charValue << "\t"; + bin(charValue, cout); + cout << endl; + //checksum computation + checksum ^= charValue; + } + + cout << endl; + cout << "Computed checksum (dec,hex,bin): " << std::dec << checksum << ", " << std::hex << checksum << ", "; + bin(checksum, cout); cout << endl << endl; + + stringstream ss; + ss << std::hex << checksum; + + string checksumStr = ss.str(); + for (unsigned int i=0; i<checksumStr.size(); i++) + checksumStr[i] = toupper( checksumStr[i] ); + + cout << "Expected checksum result is:\t" << resultHex << endl; + cout << "Computed checksum result is:\t" << checksumStr << endl << endl; + + int passTest = checksumStr.compare( resultHex ); + if (passTest!=0) { + cout << "Test not passed" << endl; + return 1; + } + cout << "Test passed" << endl; + + return 0; +} + + Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/example.cmake.in =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/example.cmake.in 2008-06-29 03:49:54 UTC (rev 231) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/example.cmake.in 2008-06-30 12:01:10 UTC (rev 232) @@ -1,13 +1,6 @@ PROJECT( gbxsmartbatteryacfr_example ) INCLUDE_DIRECTORIES( @CMAKE_INSTALL_PREFIX@/include/gearbox ) - -ADD_EXECUTABLE( gbxsmartbatteryacfrchecksum checksumtest.cpp ) -TARGET_LINK_LIBRARIES( gbxsmartbatteryacfrchecksum GbxSmartBatteryAcfr ) -SET_TARGET_PROPERTIES( gbxsmartbatteryacfrchecksum PROPERTIES - LINK_FLAGS "-L@CMAKE_INSTALL_PREFIX@/lib/gearbox" - INSTALL_RPATH "${INSTALL_RPATH};@CMAKE_INSTALL_PREFIX@/lib/gearbox" - BUILD_WITH_INSTALL_RPATH TRUE ) ADD_EXECUTABLE( gbxsmartbatteryacfrsimple simpletest.cpp ) TARGET_LINK_LIBRARIES( gbxsmartbatteryacfrsimple GbxSmartBatteryAcfr ) Modified: gearbox/trunk/submitted/gbxsmartbatteryacfr/test/example.readme =================================================================== --- gearbox/trunk/submitted/gbxsmartbatteryacfr/test/example.readme 2008-06-29 03:49:54 UTC (rev 231) +++ gearbox/trunk/submitted/gbxsmartbatteryacfr/test/example.readme 2008-06-30 12:01:10 UTC (rev 232) @@ -9,4 +9,10 @@ $ cd ~ $ mkdir gbxsmartbatteryacfr_example $ cd gbxsmartbatteryacfr_example -$ ccmake /usr/local/share/gearbox/gbxsmartbatteryacfr +$ ccmake /usr/local/share/gearbox/gbxsmartbatteryacfr +(press 'c' and 'g') +$ make +$ ./gbxsmartbatteryacfrsimple +$ ./gbxsmartbatteryacfrfancy + +See the library documentation (smartbatteryacfr.dox) for a description of the tests. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |