ArPiRobot-CoreLib C++
C++ library for ArPiRobot robots
INA260PowerSensor.hpp
1 /*
2  * Copyright 2021 Marcus Behel
3  *
4  * This file is part of ArPiRobot-CoreLib.
5  *
6  * ArPiRobot-CoreLib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * ArPiRobot-CoreLib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with ArPiRobot-CoreLib. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include <arpirobot/core/device/BaseDevice.hpp>
23 #include <arpirobot/core/scheduler.hpp>
24 #include <arpirobot/core/network/MainVmon.hpp>
25 #include <arpirobot/core/io/IoDevice.hpp>
26 
27 namespace arpirobot{
28 
29  namespace internal{
33  class AdafruitINA260 : public IoDevice{
34  public:
39  AdafruitINA260(int address, int bus = 1);
40 
41  AdafruitINA260(const AdafruitINA260 &other) = delete;
42 
43  ~AdafruitINA260();
44 
45  AdafruitINA260 &operator=(const AdafruitINA260 &other) = delete;
46 
47  bool begin();
48 
49  void reset();
50 
51  // mA
52  double readCurrent();
53 
54  // mW
55  double readPower();
56 
57 
58  // mV
59  double readBusVoltage();
60 
61  void setMode(int mode);
62 
63  int getMode();
64 
65  void setAveragingCount(int count);
66 
67  int getAveragingCount();
68 
69  void setCurrentConversionTime(int time);
70 
71  int getCurrentConversionTime();
72 
73  void setVoltageConversionTime(int time);
74 
75  int getVoltageConversiontime();
76 
77  bool conversionReady();
78 
79 
80  // INA260 Mode values
81  const static int MODE_SHUTDOWN;
82  const static int MODE_TRIGGERED;
83  const static int MODE_CONTINUOUS;
84 
85  // INA260 Conversion Time values
86  const static int TIME_140_US;
87  const static int TIME_204_US;
88  const static int TIME_332_US;
89  const static int TIME_558_US;
90  const static int TIME_1_1_MS;
91  const static int TIME_2_116_MS;
92  const static int TIME_4_156_MS;
93  const static int TIME_8_244_MS;
94 
95  // INA260 Averaging Count values
96  const static int COUNT_1;
97  const static int COUNT_2;
98  const static int COUNT_16;
99  const static int COUNT_64;
100  const static int COUNT_128;
101  const static int COUNT_256;
102  const static int COUNT_512;
103  const static int COUNT_1024;
104 
105  protected:
106 
107  void close() override;
108 
109  private:
110  int i2cReadWordHelper(int reg);
111 
112  void i2cWriteWordHelper(int reg, int data);
113 
114  // Registers
115  const static int REG_CONFIG;
116  const static int REG_CURRENT;
117  const static int REG_BUSVOLTAGE;
118  const static int REG_POWER;
119  const static int REG_MASK_ENABLE;
120  const static int REG_ALERT_LIMIT;
121  const static int REG_MFG_UID;
122  const static int REG_DIE_UID;
123 
124  int handle = -1;
125  };
126  }
127 
132  class INA260PowerSensor : public BaseDevice, public MainVmon{
133  public:
134  INA260PowerSensor(int bus = -1);
135 
136  INA260PowerSensor(const INA260PowerSensor &other) = delete;
137 
139 
140  INA260PowerSensor &operator=(const INA260PowerSensor &other) = delete;
141 
146  double getCurrent();
147 
152  double getVolgate();
153 
158  double getPower();
159 
160  protected:
161  void begin() override;
162 
163  bool isEnabled() override;
164 
165  bool shouldMatchRobotState() override;
166 
167  bool shouldDisableWithWatchdog() override;
168 
169  void enable() override;
170 
171  void disable() override;
172 
173  private:
174 
175  void feed();
176 
177  std::shared_ptr<internal::AdafruitINA260> sensor;
178 
179  double current = 0, voltage = 0, power = 0;
180 
181  bool stop = false;
182 
183  int bus;
184  };
185 
186 }
Definition: BaseDevice.hpp:35
Definition: INA260PowerSensor.hpp:132
Definition: IoDevice.hpp:27
Definition: MainVmon.hpp:30
Definition: INA260PowerSensor.hpp:33
AdafruitINA260(int address, int bus=1)
Definition: ArduinoDevice.hpp:27