iCub-main
bcbBattery.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 // Copyright: (C) 2015 iCub Facility
4 // Authors: Marco Randazzo <marco.randazzo@iit.it>
5 // CopyPolicy: Released under the terms of the GNU GPL v2.0.
6 
7 #ifndef __BCBBATTERY_H__
8 #define __BCBBATTERY_H__
9 
10 #include <mutex>
11 #include <yarp/os/PeriodicThread.h>
12 #include <yarp/dev/IBattery.h>
13 #include <yarp/dev/PolyDriver.h>
14 #include <yarp/dev/ISerialDevice.h>
15 #include <yarp/os/ResourceFinder.h>
16 #include <yarp/sig/Vector.h>
17 #include <regex>
18 
19 using namespace yarp::os;
20 using namespace yarp::dev;
21 
22 class batteryReaderThread : public PeriodicThread
23 {
24  public:
25  //debug
26  static const int debugTextBufferSize = 10000;
27  char debugTextBuffer[debugTextBufferSize];
28 
29  //configuration options
30  bool verboseEnable = false;
31  bool screenEnable = true;
32 
33  //the buffer
34  double timeStamp;
35  static const int buff_len = 10000;
36  unsigned char tmp_buff[buff_len];
37  static const int packet_len =10;
38  unsigned char packet[packet_len];
39  std::regex r_exp;
40 
41  ISerialDevice* iSerial = nullptr;
42  std::mutex datamut;
43  double battery_charge = 0;
44  double battery_voltage = 0;
45  double battery_current = 0;
46  std::string battery_info = "icub battery system v1.0";
47  int backpack_status = 0;
48  IBattery::Battery_status battery_status = IBattery::Battery_status::BATTERY_OK_STANBY;
49 
50  batteryReaderThread (ISerialDevice *_iSerial, double period) :
51  PeriodicThread((double)period),
52  iSerial(_iSerial)
53  {
54  // the following regedit expressions means: search for an occurrence of the pattern:
55  // \0........\r\n which is not followed by any other occurrence of the same pattern.
56  // See for example: https://docs.pexip.com/admin/regex_reference.htm
57  //
58  // 01234567 8 9***** 01234567 8 9
59  std::string c_exp ("\\0.......\\r\\n(?!.*\\0.......\\r\\n)");
60  r_exp=c_exp;
61  }
62 
63  void startTransmission();
64  void stopTransmission();
65  virtual bool threadInit() override;
66  virtual void threadRelease() override;
67  virtual void run() override;
68 };
69 
70 class BcbBattery: public yarp::dev::IBattery, public DeviceDriver
71 {
72 protected:
73  batteryReaderThread* batteryReader =nullptr;
74 
75  ResourceFinder rf;
76  PolyDriver driver;
77  ISerialDevice *iSerial = nullptr;
78 
79 public:
82 
83  virtual bool open(yarp::os::Searchable& config);
84  virtual bool close();
85 
86  virtual bool getBatteryVoltage (double &voltage) override;
87  virtual bool getBatteryCurrent (double &current) override;
88  virtual bool getBatteryCharge (double &charge) override;
89  virtual bool getBatteryStatus (Battery_status &status) override;
90  virtual bool getBatteryInfo (std::string &info) override;
91  virtual bool getBatteryTemperature (double &temperature) override;
92 };
93 
94 
95 #endif
ResourceFinder rf
Definition: bcbBattery.h:75
PolyDriver driver
Definition: bcbBattery.h:76
batteryReaderThread(ISerialDevice *_iSerial, double period)
Definition: bcbBattery.h:50
std::mutex datamut
Definition: bcbBattery.h:42
std::regex r_exp
Definition: bcbBattery.h:39