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
26// This is to provide compatibility with both YARP 3.11 and 3.12
28
29namespace yarp::dev
30{
31class embObjBattery;
32}
33
35{
36 public:
37 int16_t temperature_{0};
38 float32_t voltage_{0};
39 float32_t current_{0};
40 float32_t charge_{0};
41 uint16_t status_{0};
42 uint16_t prevStatus_{0};
43 double timeStamp_{0};
44 std::string sensorName_{};
45 eObrd_type_t sensorType_{eobrd_unknown};
46
47
48 void decode(eOas_battery_timedvalue_t *data, double timestamp);
49 bool operator==(const CanBatteryData &other) const;
50 bool operator!=(const CanBatteryData &other) const;
51};
52
53class yarp::dev::embObjBattery : public yarp::dev::DeviceDriver, public eth::IethResource, public yarp::dev::IBattery
54{
55 public:
57 embObjBattery(std::shared_ptr<yarp::dev::embObjDevPrivData> device); // For Unittesting only
59
60 bool open(yarp::os::Searchable &config);
61 bool close();
62
63 // IethResource interface
64 bool initialised() override;
65 eth::iethresType_t type() override;
66 bool update(eOprotID32_t id32, double timestamp, void *rxdata) override;
67
68 // IBattery
72 YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryStatus(Battery_status &status) override;
73 YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryTemperature(double &temperature) override;
74 YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryInfo(std::string &battery_info) override;
75
76 virtual double calculateBoardTime(eOabstime_t current);
77
78 protected:
79 std::shared_ptr<yarp::dev::embObjDevPrivData> device_;
80 mutable std::shared_mutex mutex_;
82 std::map<eOprotID32_t, eOabstime_t> timeoutUpdate_;
83
87 void cleanup(void);
88 bool checkUpdateTimeout(eOprotID32_t id32, eOabstime_t current);
89 std::string updateStatusStringStream(const uint16_t &currStatus, const uint16_t &prevStatus, bool isFirstLoop);
90 static constexpr eOabstime_t updateTimeout_{11000};
91 std::vector<yarp::dev::MAS_status> masStatus_{MAS_OK, MAS_OK, MAS_OK, MAS_OK};
92
93 static constexpr bool checkUpdateTimeoutFlag_{false}; // Check timer disabled
94 static constexpr bool useBoardTimeFlag_{true}; // Calculate board time if true otherway use yarp time
95
96 bool isCanDataAvailable = false;
97 bool isPastFirstPrint = false;
98
100 eOabstime_t firstCanTimestamp_{0};
101};
102
103#endif
@ data
#define YARP_DEV_RETURN_VALUE_TYPE_CH312
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
YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryInfo(std::string &battery_info) override
YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryCharge(double &charge) override
YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryStatus(Battery_status &status) override
YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryCurrent(double &current) override
eth::iethresType_t type() override
bool open(yarp::os::Searchable &config)
std::string updateStatusStringStream(const uint16_t &currStatus, const uint16_t &prevStatus, bool isFirstLoop)
YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryVoltage(double &voltage) override
bool initialised() override
std::map< eOprotID32_t, eOabstime_t > timeoutUpdate_
bool sendConfig2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
static constexpr bool useBoardTimeFlag_
static constexpr eOabstime_t updateTimeout_
virtual double calculateBoardTime(eOabstime_t current)
std::shared_mutex mutex_
bool checkUpdateTimeout(eOprotID32_t id32, eOabstime_t current)
std::shared_ptr< yarp::dev::embObjDevPrivData > device_
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_
YARP_DEV_RETURN_VALUE_TYPE_CH312 getBatteryTemperature(double &temperature) override
iethresType_t