iCub-main
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 
24 
25 namespace yarp::dev
26 {
27 class embObjBattery;
28 }
29 
31 {
32  public:
33  int16_t temperature_{0};
34  float32_t voltage_{0};
35  float32_t current_{0};
36  float32_t charge_{0};
37  int8_t status_{0};
38  double timeStamp_{0};
39  std::string sensorName_;
40 
41  void decode(eOas_battery_timedvalue_t *data, double timestamp);
42  bool operator==(const CanBatteryData &other) const;
43  bool operator!=(const CanBatteryData &other) const;
44 };
45 
46 class yarp::dev::embObjBattery : public yarp::dev::DeviceDriver, public eth::IethResource, public yarp::dev::IBattery
47 {
48  public:
49  embObjBattery();
50  embObjBattery(std::shared_ptr<yarp::dev::embObjDevPrivData> device); // For Unittesting only
52 
53  bool open(yarp::os::Searchable &config);
54  bool close();
55 
56  // IethResource interface
57  bool initialised() override;
58  eth::iethresType_t type() override;
59  bool update(eOprotID32_t id32, double timestamp, void *rxdata) override;
60 
61  // IBattery
62  bool getBatteryVoltage(double &voltage) override;
63  bool getBatteryCurrent(double &current) override;
64  bool getBatteryCharge(double &charge) override;
65  bool getBatteryStatus(Battery_status &status) override;
66  bool getBatteryTemperature(double &temperature) override;
67  bool getBatteryInfo(std::string &battery_info) override;
68 
69  virtual double calculateBoardTime(eOabstime_t current);
70 
71  protected:
72  std::shared_ptr<yarp::dev::embObjDevPrivData> device_;
73  mutable std::shared_mutex mutex_;
75  std::map<eOprotID32_t, eOabstime_t> timeoutUpdate_;
76 
80  void cleanup(void);
81  bool checkUpdateTimeout(eOprotID32_t id32, eOabstime_t current);
82  static constexpr eOabstime_t updateTimeout_{11000};
83  std::vector<yarp::dev::MAS_status> masStatus_{MAS_OK, MAS_OK, MAS_OK, MAS_OK};
84 
85  static constexpr bool checkUpdateTimeoutFlag_{false}; // Check timer disabled
86  static constexpr bool useBoardTimeFlag_{true}; // Calculate board time if true otherway use yarp time
87 
89  eOabstime_t firstCanTimestamp_{0};
90 };
91 
92 #endif
@ data
int16_t temperature_
Definition: embObjBattery.h:33
bool operator==(const CanBatteryData &other) const
float32_t current_
Definition: embObjBattery.h:35
void decode(eOas_battery_timedvalue_t *data, double timestamp)
std::string sensorName_
Definition: embObjBattery.h:39
float32_t charge_
Definition: embObjBattery.h:36
float32_t voltage_
Definition: embObjBattery.h:34
bool operator!=(const CanBatteryData &other) const
eth::iethresType_t type() override
bool open(yarp::os::Searchable &config)
eOabstime_t firstCanTimestamp_
Definition: embObjBattery.h:89
bool initialised() override
std::map< eOprotID32_t, eOabstime_t > timeoutUpdate_
Definition: embObjBattery.h:75
bool sendConfig2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool getBatteryVoltage(double &voltage) override
static constexpr bool useBoardTimeFlag_
Definition: embObjBattery.h:86
static constexpr eOabstime_t updateTimeout_
Definition: embObjBattery.h:82
virtual double calculateBoardTime(eOabstime_t current)
std::shared_mutex mutex_
Definition: embObjBattery.h:73
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_
Definition: embObjBattery.h:72
bool getBatteryInfo(std::string &battery_info) override
bool sendStart2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
std::vector< yarp::dev::MAS_status > masStatus_
Definition: embObjBattery.h:83
bool initRegulars(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool update(eOprotID32_t id32, double timestamp, void *rxdata) override
static constexpr bool checkUpdateTimeoutFlag_
Definition: embObjBattery.h:85
CanBatteryData canBatteryData_
Definition: embObjBattery.h:74
bool getBatteryTemperature(double &temperature) override
bool getBatteryCharge(double &charge) override
iethresType_t
Definition: IethResource.h:61