iCub-main
Loading...
Searching...
No Matches
embObjBattery.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT)
3 * All rights reserved.
4 * Author: Luca Tricerri
5 * This software may be modified and distributed under the terms of the
6 * BSD-3-Clause license. See the accompanying LICENSE file for details.
7 */
8
9#ifndef _EMBOBJCANBATTERYSENSORS_H_
10#define _EMBOBJCANBATTERYSENSORS_H_
11
12#include <yarp/dev/DeviceDriver.h>
13#include <yarp/dev/MultipleAnalogSensorsInterfaces.h>
14#include <yarp/dev/IBattery.h>
15#include <yarp/sig/Vector.h>
16
17#include <map>
18#include <shared_mutex>
19#include <memory>
20#include <string>
21#include <utility>
22
25
26namespace yarp::dev
27{
28class embObjBattery;
29}
30
32{
33 public:
34 int16_t temperature_{0};
35 float32_t voltage_{0};
36 float32_t current_{0};
37 float32_t charge_{0};
38 uint16_t status_{0};
39 uint16_t prevStatus_{0};
40 double timeStamp_{0};
41 std::string sensorName_{};
42 eObrd_type_t sensorType_{eobrd_unknown};
43
44
45 void decode(eOas_battery_timedvalue_t *data, double timestamp);
46 bool operator==(const CanBatteryData &other) const;
47 bool operator!=(const CanBatteryData &other) const;
48};
49
50class yarp::dev::embObjBattery : public yarp::dev::DeviceDriver, public eth::IethResource, public yarp::dev::IBattery
51{
52 public:
54 embObjBattery(std::shared_ptr<yarp::dev::embObjDevPrivData> device); // For Unittesting only
56
57 bool open(yarp::os::Searchable &config);
58 bool close();
59
60 // IethResource interface
61 bool initialised() override;
62 eth::iethresType_t type() override;
63 bool update(eOprotID32_t id32, double timestamp, void *rxdata) override;
64
65 // IBattery
66 bool getBatteryVoltage(double &voltage) override;
67 bool getBatteryCurrent(double &current) override;
68 bool getBatteryCharge(double &charge) override;
69 bool getBatteryStatus(Battery_status &status) override;
70 bool getBatteryTemperature(double &temperature) override;
71 bool getBatteryInfo(std::string &battery_info) override;
72
73 virtual double calculateBoardTime(eOabstime_t current);
74
75 protected:
76 std::shared_ptr<yarp::dev::embObjDevPrivData> device_;
77 mutable std::shared_mutex mutex_;
79 std::map<eOprotID32_t, eOabstime_t> timeoutUpdate_;
80
84 void cleanup(void);
85 bool checkUpdateTimeout(eOprotID32_t id32, eOabstime_t current);
86 std::string updateStatusStringStream(const uint16_t &currStatus, const uint16_t &prevStatus, bool isFirstLoop);
87 static constexpr eOabstime_t updateTimeout_{11000};
88 std::vector<yarp::dev::MAS_status> masStatus_{MAS_OK, MAS_OK, MAS_OK, MAS_OK};
89
90 static constexpr bool checkUpdateTimeoutFlag_{false}; // Check timer disabled
91 static constexpr bool useBoardTimeFlag_{true}; // Calculate board time if true otherway use yarp time
92
93 bool isCanDataAvailable = false;
94 bool isPastFirstPrint = false;
95
97 eOabstime_t firstCanTimestamp_{0};
98};
99
100#endif
@ data
int16_t temperature_
float32_t current_
eObrd_type_t sensorType_
void decode(eOas_battery_timedvalue_t *data, double timestamp)
std::string sensorName_
float32_t charge_
float32_t voltage_
uint16_t prevStatus_
bool operator!=(const CanBatteryData &other) const
eth::iethresType_t type() override
bool open(yarp::os::Searchable &config)
std::string updateStatusStringStream(const uint16_t &currStatus, const uint16_t &prevStatus, bool isFirstLoop)
bool initialised() override
std::map< eOprotID32_t, eOabstime_t > timeoutUpdate_
bool sendConfig2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool getBatteryVoltage(double &voltage) override
static constexpr bool useBoardTimeFlag_
static constexpr eOabstime_t updateTimeout_
virtual double calculateBoardTime(eOabstime_t current)
std::shared_mutex mutex_
bool getBatteryStatus(Battery_status &status) override
bool getBatteryCurrent(double &current) override
bool checkUpdateTimeout(eOprotID32_t id32, eOabstime_t current)
std::shared_ptr< yarp::dev::embObjDevPrivData > device_
bool getBatteryInfo(std::string &battery_info) override
bool sendStart2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
std::vector< yarp::dev::MAS_status > masStatus_
bool initRegulars(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool update(eOprotID32_t id32, double timestamp, void *rxdata) override
static constexpr bool checkUpdateTimeoutFlag_
CanBatteryData canBatteryData_
bool getBatteryTemperature(double &temperature) override
bool getBatteryCharge(double &charge) override
iethresType_t