Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laeWd.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Laelaps
4 //
5 // File: laeWd.h
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2015-08-07 14:25:35 -0600 (Fri, 07 Aug 2015) $
10  * $Rev: 4051 $
11  *
12  * \brief Laelaps WatchDog software class interface.
13  *
14  * The class provides the interface between the library software and the
15  * Arduino sub-processor.
16  *
17  * The Wd class is multi-threading safe.
18  *
19  * \author Robin Knight (robin.knight@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2015-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Unless otherwise stated explicitly, all materials contained are copyrighted
30  * and may not be used without RoadNarrows LLC's written consent,
31  * except as provided in these terms and conditions or in the copyright
32  * notice (documents and software) or other proprietary notice provided with
33  * the relevant materials.
34  *
35  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
36  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
37  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
38  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
39  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
40  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
43  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
44  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
45  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
46  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
47  *
48  * @EulaEnd@
49  */
50 ////////////////////////////////////////////////////////////////////////////////
51 
52 #ifndef _LAE_WD_H
53 #define _LAE_WD_H
54 
55 #include <pthread.h>
56 
57 #include <string>
58 
59 #include "rnr/rnrconfig.h"
60 #include "rnr/log.h"
61 #include "rnr/appkit/Time.h"
62 
63 #include "Laelaps/laelaps.h"
64 #include "Laelaps/laeUtils.h"
65 #include "Laelaps/laeDesc.h"
66 #include "Laelaps/laeTune.h"
67 
68 #include "Laelaps/laeI2C.h"
69 #include "Laelaps/laeWatchDog.h"
70 
71 /*!
72  * \brief The \h_laelaps namespace encapsulates all \h_laelaps related
73  * constructs.
74  */
75 namespace laelaps
76 {
77 
78  //----------------------------------------------------------------------------
79  // LaeWd Class
80  //----------------------------------------------------------------------------
81 
82  /*!
83  * WatchDog sub-processor interface class.
84  */
85  class LaeWd
86  {
87  public:
88  /*!
89  * \brief Initialization constructor.
90  *
91  * \param i2cbus Bound open \h_i2c bus instance.
92  * \param addr Arduino I2C address.
93  */
94  LaeWd(LaeI2C &i2cBus, uint_t addr=LaeI2CAddrArduino);
95 
96  /*!
97  * \brief Destructor.
98  */
99  virtual ~LaeWd();
100 
101  /*!
102  * \brief Synchronize watchdog state with subprocessor state.
103  */
104  virtual void sync();
105 
106  /*!
107  * \brief Configure watchdog from product description.
108  *
109  * Call after connection is opened.
110  *
111  * \param desc Product description.
112  *
113  * \copydoc doc_return_std
114  */
115  virtual int configure(const LaeDesc &desc);
116 
117  /*!
118  * \brief Configure watchdog from tunable parameters.
119  *
120  * Call after connection is opened.
121  *
122  * \param tunes Laelaps tuning parameters.
123  *
124  * \copydoc doc_return_std
125  */
126  virtual int configure(const LaeTunes &tunes);
127 
128  /*!
129  * \brief Reload with new configuration.
130  *
131  * \param tunes Laelaps tuning parameters.
132  *
133  * \copydoc doc_return_std
134  */
135  virtual int reload(const LaeTunes &tunes);
136 
137  /*!
138  * \brief Execute cycle to pet/read/update Watchdog sub-processor.
139  */
140  virtual void exec();
141 
142  //..........................................................................
143  // WatchDog Sub-Processor Member Functions
144  //..........................................................................
145 
146  /*!
147  * \brief Pet the watchdog command.
148  *
149  * \copydoc doc_return_std
150  */
151  virtual int cmdPetTheDog();
152 
153  /*!
154  * \brief Get the firmware version command.
155  *
156  * \param [out] uVerNum Firmware version number.
157  *
158  * \copydoc doc_return_std
159  */
160  virtual int cmdGetFwVersion(uint_t &uVerNum);
161 
162  /*!
163  * \brief Set battery's state of charge state command.
164  *
165  * \param uBatterySoC Battery state of charge as a percent [0-100].
166  *
167  * \copydoc doc_return_std
168  */
169  virtual int cmdSetBatterySoC(uint_t uBatterySoC);
170 
171  /*!
172  * \brief Set (clear) alarms command.
173  *
174  * \param uAlarms Bit-or'ed alarm state.
175  *
176  * \copydoc doc_return_std
177  */
178  virtual int cmdSetAlarms(uint_t uAlarms);
179 
180  /*!
181  * \brief Set the LED RGB color command.
182  *
183  * \param red Red component [0-255].
184  * \param green Green component [0-255].
185  * \param blue Blue component [0-255].
186  *
187  * \copydoc doc_return_std
188  */
189  virtual int cmdSetRgbLed(uint_t red, uint_t green, uint_t blue);
190 
191  /*!
192  * \brief Reset the LED RGB color to state defaults.
193  *
194  * \copydoc doc_return_std
195  */
196  virtual int cmdResetRgbLed();
197 
198  /*!
199  * \brief Configure a digital pin command.
200  *
201  * \note
202  * Deprecated in robot v2.2+ since there now exist an accessory
203  * subprocessor to support various gpio.
204  *
205  * \param pin Digital pin number.
206  * \param dir Pin direction. 0 == input, 1 == output.
207  *
208  * \copydoc doc_return_std
209  */
210  virtual int cmdConfigDPin(uint_t pin, uint_t dir);
211 
212  /*!
213  * \brief Read the value of a digital pin command.
214  *
215  * \note
216  * Deprecated in robot v2.2+ since there now exist an accessory
217  * subprocessor to support various gpio.
218  *
219  * \param pin Digital pin number.
220  * \param [out] val Digital pin low (0) or high (1) value.
221  *
222  * \copydoc doc_return_std
223  */
224  virtual int cmdReadDPin(uint_t pin, uint_t &val);
225 
226  /*!
227  * \brief Write a value to a digital pin command.
228  *
229  * \note
230  * Deprecated in robot v2.2+ since there now exist an accessory
231  * subprocessor to support various gpio.
232  *
233  * \param pin Digital pin number.
234  * \param val Digital pin low (0) or high (1) value.
235  *
236  * \copydoc doc_return_std
237  */
238  virtual int cmdWriteDPin(uint_t pin, uint_t val);
239 
240  /*!
241  * \brief Read the value of an analog pin command.
242  *
243  * \note
244  * Deprecated in robot v2.2+ since there now exist an accessory
245  * subprocessor to support various gpio.
246  *
247  * \param pin Analog pin number.
248  * \param [out] val Analog 10-bit value [0-1023].
249  *
250  * \copydoc doc_return_std
251  */
252  virtual int cmdReadAPin(uint_t pin, uint_t &val);
253 
254  /*!
255  * \brief Write the value to an analog pin command.
256  *
257  * \note
258  * Deprecated in robot v2.2+ since there now exist an accessory
259  * subprocessor to support various gpio.
260  *
261  * \param pin Analog pin number.
262  * \param val Analog 10-bit value [0-1023].
263  *
264  * \copydoc doc_return_std
265  */
266  virtual int cmdWriteAPin(uint_t pin, uint_t val);
267 
268  /*!
269  * \brief Enable/disable power in to motor controllers.
270  *
271  * \param bEnable Disable (false) or enable (true).
272  *
273  * \copydoc doc_return_std
274  */
275  virtual int cmdEnableMotorCtlrs(bool bEnable);
276 
277  /*!
278  * \brief Enable/disable regulated 5 volt auxilliary port power out.
279  *
280  * \param bEnable Disable (false) or enable (true).
281  *
282  * \copydoc doc_return_std
283  */
284  virtual int cmdEnableAuxPort5V(bool bEnable);
285 
286  /*!
287  * \brief Enable/disable battery auxilliary port power out.
288  *
289  * \param bEnable Disable (false) or enable (true).
290  *
291  * \copydoc doc_return_std
292  */
293  virtual int cmdEnableAuxPortBatt(bool bEnable);
294 
295  /*!
296  * \brief Read enable lines.
297  *
298  * \param [out] bMotorCtlrEn Motor controllers enable.
299  * \param [out] bAuxPort5vEn Auxilliary port 5v out enable.
300  * \param [out] bAuxPortBattEn Auxilliary port battery out enable.
301  *
302  * \copydoc doc_return_std
303  */
304  virtual int cmdReadEnables(bool &bMotorCtlrEn,
305  bool &bAuxPort5vEn,
306  bool &bAuxPortBattEn);
307 
308  /*!
309  * \brief Read sensed voltages.
310  *
311  * \param [out] fJackV Sensed charging jack input voltage.
312  * \param [out] fBattV Sensed battery output voltage.
313  *
314  * \copydoc doc_return_std
315  */
316  virtual int cmdReadVoltages(double &fJackV, double &fBattV);
317 
318  /*!
319  * \brief Test the firmware state command.
320  *
321  * \param [out] uSeqNum This command's sequence number modulo 256.
322  * \param [out] uOpState Firmware's operational state.
323  * \param [out] uAlarms Firmware's view of alarms.
324  * \param [out] uLedIndex Firmware's current LED pattern index.
325  *
326  * \copydoc doc_return_std
327  */
328  virtual int cmdTest(uint_t &uSeqNum,
329  uint_t &uOpState,
330  uint_t &uAlarms,
331  uint_t &uLedIndex);
332 
333  /*!
334  * \brief Configure firmware operation.
335  *
336  * \param uTimeout Watchdog timeout (msec).
337  *
338  * \copydoc doc_return_std
339  */
340  virtual int cmdConfigOperation(unsigned long uTimeout);
341 
342  /*!
343  * \brief Enable/disable power in to motor controllers.
344  *
345  * \param pArg Recast pointer to this class instance.
346  * \param bEnable Disable (false) or enable (true).
347  *
348  * \copydoc doc_return_std
349  */
350  static int enableMotorCtlrs(void *pArg, bool bEnable);
351 
352  //..........................................................................
353  // WatchDog Attribute Methods
354  //..........................................................................
355 
356  /*!
357  * \brief Get watchdog's \h_i2c address.
358  *
359  * \return 7-bit address.
360  */
361  uint_t getI2CAddr()
362  {
363  return m_addrSubProc;
364  }
365 
366  /*!
367  * \brief Get previously read firmware version.
368  *
369  * \return Firmware version number.
370  */
371  uint_t getFwVersion()
372  {
373  return m_uFwVer;
374  }
375 
376  /*!
377  * \brief Test if battery is charging.
378  *
379  * \return Returns true or false.
380  */
381  bool isCharging()
382  {
383  return m_bBatteryIsCharging;
384  }
385 
386  protected:
387  // hardware
388  LaeI2C &m_i2cBus; ///< bound \h_i2c bus instance
389  uint_t m_addrSubProc; ///< \h_i2c sub-processor address
390  uint_t m_uFwVer; ///< firmware version number
391 
392  // shadow values
393  ulong_t m_uWatchdogTimeout; ///< watchdog timeout (msec)
394  bool m_bBatteryIsCharging; ///< battery is [not] charging
395  double m_fBatteryVoltage; ///< sensed battery voltage
396  double m_fJackVoltage; ///< sensed power supply jack voltage
397  uint_t m_uBatterySoC; ///< battery state of charge
398  uint_t m_uAlarms; ///< alarms
399  bool m_bMotorCtlrEn; ///< motor controller enable
400  bool m_bAuxPortBattEn; ///< battery auxilliary port enable
401  bool m_bAuxPort5vEn; ///< 5 volt auxilliary port enable
402 
403  // uptimes
404  rnr::chronos::Time m_timeMotorCtlrs; ///< motor controller up time
405 
406  // mutual exclusion
407  pthread_mutex_t m_mutex; ///< mutex
408 
409  /*!
410  * \brief Lock the shared resource.
411  *
412  * The lock()/unlock() primitives provide a safe threading.
413  * registered vServo data.
414  *
415  * \par Context:
416  * Any.
417  */
418  void lock()
419  {
420  pthread_mutex_lock(&m_mutex);
421  }
422 
423  /*!
424  * \brief Unlock the shared resource.
425  *
426  * \par Context:
427  * Any.
428  */
429  void unlock()
430  {
431  pthread_mutex_unlock(&m_mutex);
432  }
433 
434  /*!
435  * \brief Determine watchdog alarm state.
436  *
437  * The current real-time database alarm state is used for the determination.
438  *
439  * \return Watchdog alarm state.
440  */
441  uint_t determineAlarms();
442  };
443 
444 } // namespace laelaps
445 
446 
447 #endif // _LAE_WD_H
virtual int cmdEnableMotorCtlrs(bool bEnable)
Enable/disable power in to motor controllers.
Definition: laeWd.cxx:668
ulong_t m_uWatchdogTimeout
watchdog timeout (msec)
Definition: laeWd.h:393
virtual int cmdReadDPin(uint_t pin, uint_t &val)
Read the value of a digital pin command.
Definition: laeWd.cxx:445
void lock()
Lock the shared resource.
Definition: laeWd.h:418
uint_t m_uAlarms
alarms
Definition: laeWd.h:398
uint_t m_uBatterySoC
battery state of charge
Definition: laeWd.h:397
Laelaps tuning data class.
Definition: laeTune.h:566
Laelaps robotic mobile platform full description class.
Definition: laeDesc.h:451
bool m_bBatteryIsCharging
battery is [not] charging
Definition: laeWd.h:394
bool m_bMotorCtlrEn
motor controller enable
Definition: laeWd.h:399
double m_fJackVoltage
sensed power supply jack voltage
Definition: laeWd.h:396
virtual int cmdWriteDPin(uint_t pin, uint_t val)
Write a value to a digital pin command.
Definition: laeWd.cxx:506
virtual int configure(const LaeDesc &desc)
Configure watchdog from product description.
Definition: laeWd.cxx:159
virtual ~LaeWd()
Destructor.
Definition: laeWd.cxx:137
uint_t m_uFwVer
firmware version number
Definition: laeWd.h:390
virtual int cmdPetTheDog()
Pet the watchdog command.
Definition: laeWd.cxx:215
pthread_mutex_t m_mutex
mutex
Definition: laeWd.h:407
Laelaps I2C class interface.
virtual int cmdEnableAuxPort5V(bool bEnable)
Enable/disable regulated 5 volt auxilliary port power out.
Definition: laeWd.cxx:759
virtual int cmdResetRgbLed()
Reset the LED RGB color to state defaults.
Definition: laeWd.cxx:376
LaeI2C & m_i2cBus
bound I2C bus instance
Definition: laeWd.h:388
virtual int cmdSetBatterySoC(uint_t uBatterySoC)
Set battery&#39;s state of charge state command.
Definition: laeWd.cxx:289
virtual int cmdSetRgbLed(uint_t red, uint_t green, uint_t blue)
Set the LED RGB color command.
Definition: laeWd.cxx:353
void unlock()
Unlock the shared resource.
Definition: laeWd.h:429
bool m_bAuxPortBattEn
battery auxilliary port enable
Definition: laeWd.h:400
The <b><i>Laelaps</i></b> namespace encapsulates all <b><i>Laelaps</i></b> related constructs...
Definition: laeAlarms.h:64
LaeWd(LaeI2C &i2cBus, uint_t addr=LaeI2CAddrArduino)
Initialization constructor.
Definition: laeWd.cxx:113
Laelaps robotic base mobile platform description class interface.
virtual int cmdReadVoltages(double &fJackV, double &fBattV)
Read sensed voltages.
Definition: laeWd.cxx:933
rnr::chronos::Time m_timeMotorCtlrs
motor controller up time
Definition: laeWd.h:404
double m_fBatteryVoltage
sensed battery voltage
Definition: laeWd.h:395
Laelaps common utilities.
virtual int cmdTest(uint_t &uSeqNum, uint_t &uOpState, uint_t &uAlarms, uint_t &uLedIndex)
Test the firmware state command.
Definition: laeWd.cxx:984
bool m_bAuxPort5vEn
5 volt auxilliary port enable
Definition: laeWd.h:401
const byte_t LaeI2CAddrArduino
arduino I2C 7-bit address
Definition: laeWatchDog.h:164
virtual int cmdReadAPin(uint_t pin, uint_t &val)
Read the value of an analog pin command.
Definition: laeWd.cxx:555
Laelaps tuning.
virtual void exec()
Execute cycle to pet/read/update Watchdog sub-processor.
Definition: laeWd.cxx:188
Laelaps built-in Arduino sub-processor.
virtual int cmdGetFwVersion(uint_t &uVerNum)
Get the firmware version command.
Definition: laeWd.cxx:263
uint_t getFwVersion()
Get previously read firmware version.
Definition: laeWd.h:371
uint_t getI2CAddr()
Get watchdog&#39;s I2C address.
Definition: laeWd.h:361
virtual int cmdReadEnables(bool &bMotorCtlrEn, bool &bAuxPort5vEn, bool &bAuxPortBattEn)
Read enable lines.
Definition: laeWd.cxx:857
uint_t m_addrSubProc
I2C sub-processor address.
Definition: laeWd.h:389
uint_t determineAlarms()
Determine watchdog alarm state.
Definition: laeWd.cxx:1073
virtual int cmdConfigDPin(uint_t pin, uint_t dir)
Configure a digital pin command.
Definition: laeWd.cxx:396
virtual int cmdConfigOperation(unsigned long uTimeout)
Configure firmware operation.
Definition: laeWd.cxx:1028
virtual int cmdWriteAPin(uint_t pin, uint_t val)
Write the value to an analog pin command.
Definition: laeWd.cxx:616
static int enableMotorCtlrs(void *pArg, bool bEnable)
Enable/disable power in to motor controllers.
Definition: laeWd.cxx:1117
virtual int cmdEnableAuxPortBatt(bool bEnable)
Enable/disable battery auxilliary port power out.
Definition: laeWd.cxx:808
bool isCharging()
Test if battery is charging.
Definition: laeWd.h:381
virtual void sync()
Synchronize watchdog state with subprocessor state.
Definition: laeWd.cxx:142
virtual int reload(const LaeTunes &tunes)
Reload with new configuration.
Definition: laeWd.cxx:182
Top-level package include file.
virtual int cmdSetAlarms(uint_t uAlarms)
Set (clear) alarms command.
Definition: laeWd.cxx:323