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 #include <utility>
22 
25 
26 namespace yarp::dev
27 {
28 class 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 
50 class yarp::dev::embObjBattery : public yarp::dev::DeviceDriver, public eth::IethResource, public yarp::dev::IBattery
51 {
52  public:
53  embObjBattery();
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
uint16_t status_
Definition: embObjBattery.h:38
int16_t temperature_
Definition: embObjBattery.h:34
bool operator==(const CanBatteryData &other) const
float32_t current_
Definition: embObjBattery.h:36
eObrd_type_t sensorType_
Definition: embObjBattery.h:42
void decode(eOas_battery_timedvalue_t *data, double timestamp)
std::string sensorName_
Definition: embObjBattery.h:41
float32_t charge_
Definition: embObjBattery.h:37
float32_t voltage_
Definition: embObjBattery.h:35
uint16_t prevStatus_
Definition: embObjBattery.h:39
bool operator!=(const CanBatteryData &other) const
eth::iethresType_t type() override
bool open(yarp::os::Searchable &config)
eOabstime_t firstCanTimestamp_
Definition: embObjBattery.h:97
std::string updateStatusStringStream(const uint16_t &currStatus, const uint16_t &prevStatus, bool isFirstLoop)
bool initialised() override
std::map< eOprotID32_t, eOabstime_t > timeoutUpdate_
Definition: embObjBattery.h:79
bool sendConfig2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool getBatteryVoltage(double &voltage) override
static constexpr bool useBoardTimeFlag_
Definition: embObjBattery.h:91
static constexpr eOabstime_t updateTimeout_
Definition: embObjBattery.h:87
virtual double calculateBoardTime(eOabstime_t current)
std::shared_mutex mutex_
Definition: embObjBattery.h:77
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:76
bool getBatteryInfo(std::string &battery_info) override
bool sendStart2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
std::vector< yarp::dev::MAS_status > masStatus_
Definition: embObjBattery.h:88
bool initRegulars(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool update(eOprotID32_t id32, double timestamp, void *rxdata) override
static constexpr bool checkUpdateTimeoutFlag_
Definition: embObjBattery.h:90
CanBatteryData canBatteryData_
Definition: embObjBattery.h:78
bool getBatteryTemperature(double &temperature) override
bool getBatteryCharge(double &charge) override
iethresType_t
Definition: IethResource.h:62