00001 // 00002 // Created by tom on 09/05/16. 00003 // 00004 00005 #include <robotican_hardware_interface/Battery.h> 00006 00007 namespace robotican_hardware { 00008 00009 void Battery::write() { 00010 00011 } 00012 00013 void Battery::update(const DeviceMessage *deviceMessage) { 00014 if(isReady()) { 00015 BatteryFeedback *feedback = (BatteryFeedback *) deviceMessage; 00016 _currentRead = feedback->currentRead; 00017 ric_board::Battery msg; 00018 msg.data = (float) (_currentRead * 3.3 / 1023.0 * _voltageDividerRatio); 00019 msg.max = _max; 00020 msg.min = _min; 00021 _pub.publish(msg); 00022 } 00023 } 00024 00025 Battery::Battery(byte id, float voltageDividerRatio, float max, float min, byte batteryPin, 00026 TransportLayer *transportLayer) : Device(id,transportLayer) { 00027 _voltageDividerRatio = voltageDividerRatio; 00028 _max = max; 00029 _min = min; 00030 _currentRead = 0; 00031 _batteryPin = batteryPin; 00032 } 00033 00034 void Battery::buildDevice() { 00035 BuildBattery buildBattery; 00036 buildBattery.id = getId(); 00037 buildBattery.length = sizeof(buildBattery); 00038 buildBattery.pin = _batteryPin; 00039 buildBattery.checkSum = 0; 00040 00041 byte *rawData = (byte*)&buildBattery; 00042 00043 buildBattery.checkSum = _transportLayer->calcChecksum(rawData, buildBattery.length); 00044 _transportLayer->write(rawData, buildBattery.length); 00045 } 00046 00047 void Battery::deviceAck(const DeviceAck *ack) { 00048 Device::deviceAck(ack); 00049 if(isReady()) { 00050 ros_utils::rosInfo("Battery is ready"); 00051 _pub = _nodeHandle.advertise<ric_board::Battery>("battery_monitor", 10); 00052 } 00053 else { 00054 ros_utils::rosError("RiCBoard can't build battery object for spme reason, this program will shut down now"); 00055 ros::shutdown(); 00056 } 00057 } 00058 }