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