Hekateros  3.4.3
RoadNarrows Robotics Robot Arm Project
hekateros::HekMonitor Class Reference

Hekateros power, joint limits, and alarm monitoring class. More...

#include <hekMonitor.h>

Public Types

enum  MonStateId {
  MonStateIdInit,
  MonStateIdRunNormal,
  MonStateIdRunAlarmed,
  MonStateIdHalting,
  MonStateIdHalted
}
  Hekateros internal monitor states. More...
 
enum  MonThStateId {
  MonThStateIdInit,
  MonThStateIdIdle,
  MonThStateIdRun,
  MonThStateIdExit
}
 monitor thread states. More...
 
typedef map< int, HekJointOpticalMapOpticalLimits
 Map of optical limit switches for all joints in all kinematic chains. More...
 

Public Member Functions

 HekMonitor ()
 Default constructor.
 
virtual ~HekMonitor ()
 Destructor.
 
int open (uint_t uHekHwVer, const std::string &strDevArduino=HekDevArduino, int nBaudRateArduino=HekBaudRateArduino)
 Open resources to monitor hardware. More...
 
int close ()
 Close resources to monitor hardware. More...
 
int start (double fHz=30.0)
 Start monitoring added objects. More...
 
int stop ()
 Stop monitoring. More...
 
void addServoChainToMonitor (DynaChain *pDynaChain)
 Add servo chain to monitor for alarms and health. More...
 
int addJointLimitsToMonitor (HekSpecJoint_T *pSpecJoint, HekRobotJoint *pJoint)
 Add any joint limit switches to quick look-up map. More...
 
void addKinematicsToMonitor (HekKinematics *pKin)
 Add kinematics for safety control. More...
 
MonStateId getMonState ()
 Get the current monitor state. More...
 
void markAlarmCond (bool bAlarmedCond)
 Mark alarm condition. More...
 
void markEStopCond (bool bEStopCond)
 Mark emergency stop condition. More...
 
void markPoweredCond (bool bPoweredCond)
 Mark motors are powered condition. More...
 
byte_t readFwVersion ()
 Read firmware version. More...
 
byte_t readLimitSwitches ()
 Read joint limit switch states. More...
 
byte_t readEEGpio ()
 Read End Effector GPIO states. More...
 
byte_t readEEGpioPin (byte_t byPin)
 Read one End Effector GPIO pin state. More...
 
int writeEEGpioPin (byte_t byPin, byte_t byVal)
 Write state to one End Effector GPIO pin. More...
 
int configEEGpioPin (byte_t byPin, char cDir)
 Configure End Effector GPIO pin direction. More...
 
void writeAlarmLED (bool bState)
 Write alarm LED. More...
 
void writeStatusLED (bool bState)
 Write CPU status LED. More...
 
void writeStartHaltingSystem ()
 Write system is halting.
 
void testInterface ()
 Test interface. More...
 
int getAlarmCond ()
 Get monitoring alarm condition. More...
 
int getEStopCond ()
 Get monitoring emergency stop condition. More...
 
int getPoweredCond ()
 Get motors are powered condition. More...
 
HekOpticalLimit_TgetJointLimitInfo (byte_t byMask)
 Get joint limits information. More...
 
byte_t getJointLimitBits ()
 Get current limit switch bit values. More...
 
byte_t getEEAuxBits ()
 Get current End Effector auxilliary bit values. More...
 
HekTriState getJointLimitTriState (HekRobotJoint *pJoint, int nLimit)
 Get current joint limit tri-state. More...
 

Protected Member Functions

void lock ()
 Lock the monitor thread mutex. More...
 
void unlock ()
 Unlock the monitor thread mutex. More...
 
int createMonThread ()
 Create the monitor thread. More...
 
void destroyMonThread ()
 Destroy the monitor thread. More...
 
void signalMonThread (MonThStateId eNewState)
 Signal monitor thread of new state. More...
 
void idleWait ()
 Monitor thread block wait in idle state.
 
void runWait ()
 Monitor thread block timed wait in run state.
 
const char * getThStateName (MonThStateId eNewState)
 Get thread state name string. More...
 
void monJointLimits ()
 Monitor joint (optical) limits for state transitions. More...
 
void monEEAuxIO ()
 Monitor End Effector auxillary I/O.
 
void monServoAlarms ()
 Monitor servos for alarms.
 

Static Protected Member Functions

static void * monThread (void *pArg)
 Monitor thread. More...
 

Protected Attributes

MonStateId m_eMonState
  Hekateros monitor state
 
bool m_bIsOpen
 hardware is [not] open
 
bool m_bEStopCond
 estop condition
 
bool m_bAlarmCond
 alarm condition
 
bool m_bPoweredCond
 power to motors condition
 
DynaChain * m_pDynaChain
 dynamixel servo chain alarms
 
MapOpticalLimits m_opticalLimits
 joint optical limits map
 
HekKinematicsm_pKin
 kinematics
 
HekUno m_hwif
 arduino compatible subproc i/f
 
byte_t m_byLimitBits
 limit bit state
 
byte_t m_byEEAuxBits
 end effector auxilliary bit state
 
MonThStateId m_eMonThState
 monitoring thread state
 
MonThStateId m_eMonThStateOld
 monitoring thread old state
 
double m_fHz
 monitoring hertz
 
struct timespec m_tPeriod
 derived monitoring period
 
struct timespec m_tStart
 start time for next monitor work
 
pthread_mutex_t m_mutexMon
 monitor mutex
 
pthread_cond_t m_condMon
 monitor condition
 
pthread_t m_threadMon
 monitor pthread identifier
 

Detailed Description

Hekateros power, joint limits, and alarm monitoring class.

Definition at line 100 of file hekMonitor.h.

Member Typedef Documentation

typedef map<int, HekJointOptical> hekateros::HekMonitor::MapOpticalLimits

Map of optical limit switches for all joints in all kinematic chains.

key: i/o expander bit mask
mapped type: joint optical limit data

Definition at line 112 of file hekMonitor.h.

Member Enumeration Documentation

enum hekateros::HekMonitor::MonStateId

Hekateros internal monitor states.

Enumerator
MonStateIdInit 

power on and/or (re)initializing

MonStateIdRunNormal 

running in normal mode

MonStateIdRunAlarmed 

running in alarmed mode

MonStateIdHalting 

halting

MonStateIdHalted 

halted

Definition at line 117 of file hekMonitor.h.

118  {
119  MonStateIdInit, ///< power on and/or (re)initializing
120  MonStateIdRunNormal, ///< running in normal mode
121  MonStateIdRunAlarmed, ///< running in alarmed mode
122  MonStateIdHalting, ///< halting
123  MonStateIdHalted ///< halted
124  };
power on and/or (re)initializing
Definition: hekMonitor.h:119
enum hekateros::HekMonitor::MonThStateId

monitor thread states.

Enumerator
MonThStateIdInit 

S0.

MonThStateIdIdle 

no work

MonThStateIdRun 

process thread work

MonThStateIdExit 

exit/no thread

Definition at line 129 of file hekMonitor.h.

130  {
131  MonThStateIdInit, ///< S0
132  MonThStateIdIdle, ///< no work
133  MonThStateIdRun, ///< process thread work
134  MonThStateIdExit ///< exit/no thread
135  };

Member Function Documentation

int HekMonitor::addJointLimitsToMonitor ( HekSpecJoint_T pSpecJoint,
HekRobotJoint pJoint 
)

Add any joint limit switches to quick look-up map.

Only add when monitor thread does not exist or is in the idle state.

Parameters
[in]pSpecJointPointer to joint spcecification.
[in]pJointPointer to associated master servo joint.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 355 of file hekMonitor.cxx.

References hekateros::degToRad(), hekateros::HekOpticalLimit_T::m_fCenterPos, hekateros::HekOpticalLimit_T::m_fMaxBlackPos, hekateros::HekOpticalLimit_T::m_fMaxEdgePos, hekateros::HekOpticalLimit_T::m_fMinBlackPos, hekateros::HekOpticalLimit_T::m_fMinEdgePos, hekateros::HekSpecJoint_T::m_limit, hekateros::HekJointOptical::m_limit, hekateros::HekJointOptical::m_pJoint, hekateros::HekOpticalLimit_T::m_uBit, hekateros::HekJointOptical::m_uCurVal, and hekateros::HekJointOptical::m_uMask.

Referenced by addServoChainToMonitor().

357 {
358  HekJointOptical opticalLimit;
359  int i;
360 
361  for(i = 0; i < HekOptLimitMaxPerJoint; ++i)
362  {
363  if( pSpecJoint->m_limit[i].m_uBit == HekIOExpUnassigned )
364  {
365  break;
366  }
367 
368  opticalLimit.m_uMask = pSpecJoint->m_limit[i].m_uBit;
369  opticalLimit.m_uCurVal = HekIOExpDark;
370  opticalLimit.m_pJoint = pJoint;
371 
372  opticalLimit.m_limit.m_uBit = pSpecJoint->m_limit[i].m_uBit;
373  opticalLimit.m_limit.m_fMinEdgePos =
374  degToRad(pSpecJoint->m_limit[i].m_fMinEdgePos);
375  opticalLimit.m_limit.m_fMinBlackPos =
376  degToRad(pSpecJoint->m_limit[i].m_fMinBlackPos);
377  opticalLimit.m_limit.m_fCenterPos =
378  degToRad(pSpecJoint->m_limit[i].m_fCenterPos);
379  opticalLimit.m_limit.m_fMaxBlackPos =
380  degToRad(pSpecJoint->m_limit[i].m_fMaxBlackPos);
381  opticalLimit.m_limit.m_fMaxEdgePos =
382  degToRad(pSpecJoint->m_limit[i].m_fMaxEdgePos);
383 
384 
385  m_opticalLimits[opticalLimit.m_uMask] = opticalLimit;
386  }
387 
388  return HEK_OK;
389 }
HekOpticalLimit_T m_limit[HekOptLimitMaxPerJoint]
optical limits
Definition: hekSpec.h:182
HekRobotJoint * m_pJoint
limit switch associated robotic joint
Definition: hekJoint.h:268
byte_t m_uBit
i/o expander bit position
Definition: hekOptical.h:177
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
static const byte_t HekIOExpUnassigned
unassigned port bit
Definition: hekOptical.h:82
double m_fMinBlackPos
minimum complete occlusion position
Definition: hekOptical.h:179
byte_t m_uCurVal
i/o expander current bit value
Definition: hekJoint.h:267
double m_fMaxEdgePos
maxinum edge position of occlusion band
Definition: hekOptical.h:182
double m_fMaxBlackPos
maximum complete occlusion position
Definition: hekOptical.h:181
double m_fMinEdgePos
mininum edge position of occlusion band
Definition: hekOptical.h:178
static const int HekOptLimitMaxPerJoint
max limits/joint
Definition: hekOptical.h:120
byte_t m_uMask
i/o expander bit mask
Definition: hekJoint.h:266
double degToRad(double d)
Convert degrees to radians.
Definition: hekUtils.h:125
MapOpticalLimits m_opticalLimits
joint optical limits map
Definition: hekMonitor.h:448
Operational robotic joint optical limits class.
Definition: hekJoint.h:215
static const byte_t HekIOExpDark
optic switch blocked
Definition: hekOptical.h:83
double m_fCenterPos
center of operation position
Definition: hekOptical.h:180
HekOpticalLimit_T m_limit
optical limit info
Definition: hekJoint.h:269
void hekateros::HekMonitor::addKinematicsToMonitor ( HekKinematics pKin)
inline

Add kinematics for safety control.

Only add when monitor thread does not exist or is in the idle state.

Parameters
pKinPointer to kinematics.

Definition at line 232 of file hekMonitor.h.

References m_pKin.

233  {
234  m_pKin = pKin;
235  }
HekKinematics * m_pKin
kinematics
Definition: hekMonitor.h:449
void hekateros::HekMonitor::addServoChainToMonitor ( DynaChain *  pDynaChain)
inline

Add servo chain to monitor for alarms and health.

Only add when monitor thread does not exist or is in the idle state.

Parameters
pDynaChainPointer to dynamixel chain.

Definition at line 205 of file hekMonitor.h.

References addJointLimitsToMonitor(), and m_pDynaChain.

206  {
207  m_pDynaChain = pDynaChain;
208  }
DynaChain * m_pDynaChain
dynamixel servo chain alarms
Definition: hekMonitor.h:447
int HekMonitor::close ( )

Close resources to monitor hardware.

Cancels monitor thread.

Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 253 of file hekMonitor.cxx.

254 {
255  if( m_bIsOpen )
256  {
258 
259  m_hwif.close();
260 
262  m_bIsOpen = false;
263  m_bEStopCond = false;
264  m_bAlarmCond = false;
265  m_pDynaChain = NULL;
266  m_pKin = NULL;
267 
268  m_opticalLimits.clear();
269 
270 #ifdef HEK_HAS_SYS_BOARD
271  // expandsion board hardware
272  m_i2c.fd = -1;
273  m_i2c.addr = 0;
274 #endif // HEK_HAS_SYS_BOARD
275 
276  m_byLimitBits = 0x00;
277  m_byEEAuxBits = 0x00;
278 
279  LOGDIAG2("Closed monitoring hardware.");
280  }
281 
282  return HEK_OK;
283 }
bool m_bAlarmCond
alarm condition
Definition: hekMonitor.h:443
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
bool m_bIsOpen
hardware is [not] open
Definition: hekMonitor.h:441
HekUno m_hwif
arduino compatible subproc i/f
Definition: hekMonitor.h:453
MonStateId m_eMonState
<b><i>Hekateros</i></b> monitor state
Definition: hekMonitor.h:440
HekKinematics * m_pKin
kinematics
Definition: hekMonitor.h:449
bool m_bEStopCond
estop condition
Definition: hekMonitor.h:442
DynaChain * m_pDynaChain
dynamixel servo chain alarms
Definition: hekMonitor.h:447
virtual int close()
Close all interfaces to monitoring hardware.
Definition: hekUno.cxx:103
power on and/or (re)initializing
Definition: hekMonitor.h:119
MapOpticalLimits m_opticalLimits
joint optical limits map
Definition: hekMonitor.h:448
byte_t m_byLimitBits
limit bit state
Definition: hekMonitor.h:459
void destroyMonThread()
Destroy the monitor thread.
Definition: hekMonitor.cxx:545
byte_t m_byEEAuxBits
end effector auxilliary bit state
Definition: hekMonitor.h:460
int HekMonitor::configEEGpioPin ( byte_t  byPin,
char  cDir 
)

Configure End Effector GPIO pin direction.

Parameters
byPinGPIO pin.
cDir'i' == input, 'o' == output.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 463 of file hekMonitor.cxx.

Referenced by markPoweredCond().

464 {
465  int rc = HEK_OK;
466 
467  lock();
468 
469  rc = m_hwif.cmdConfigPin(byPin, cDir);
470 
471  unlock();
472 
473  return rc;
474 }
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
HekUno m_hwif
arduino compatible subproc i/f
Definition: hekMonitor.h:453
virtual int cmdConfigPin(int id, char dir)
Command to configure direction of an I/O pin.
Definition: hekUno.cxx:248
void unlock()
Unlock the monitor thread mutex.
Definition: hekMonitor.h:502
void lock()
Lock the monitor thread mutex.
Definition: hekMonitor.h:488
int HekMonitor::createMonThread ( )
protected

Create the monitor thread.

Context:
Calling thread.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 522 of file hekMonitor.cxx.

Referenced by unlock().

523 {
524  int rc;
525 
527 
528  rc = pthread_create(&m_threadMon, NULL, HekMonitor::monThread, (void*)this);
529 
530  if( rc == 0 )
531  {
532  rc = HEK_OK;
533  }
534 
535  else
536  {
537  LOGSYSERROR("pthread_create()");
539  rc = -HEK_ECODE_SYS;
540  }
541 
542  return rc;
543 }
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
pthread_t m_threadMon
monitor pthread identifier
Definition: hekMonitor.h:470
static const int HEK_ECODE_SYS
system (errno) error
Definition: hekateros.h:73
MonThStateId m_eMonThState
monitoring thread state
Definition: hekMonitor.h:463
static void * monThread(void *pArg)
Monitor thread.
Definition: hekMonitor.cxx:623
void HekMonitor::destroyMonThread ( )
protected

Destroy the monitor thread.

Context:
Calling thread.

Definition at line 545 of file hekMonitor.cxx.

Referenced by unlock().

546 {
548 
549  usleep(1000);
550 
551  pthread_cancel(m_threadMon);
552  pthread_join(m_threadMon, NULL);
553 
554  LOGDIAG3("Monitor thread canceled.");
555 }
pthread_t m_threadMon
monitor pthread identifier
Definition: hekMonitor.h:470
void signalMonThread(MonThStateId eNewState)
Signal monitor thread of new state.
Definition: hekMonitor.cxx:557
int hekateros::HekMonitor::getAlarmCond ( )
inline

Get monitoring alarm condition.

Returns
Returns true or false.

Definition at line 371 of file hekMonitor.h.

References m_bAlarmCond.

Referenced by hekateros::HekRobot::isAlarmed().

372  {
373  return m_bAlarmCond;
374  }
bool m_bAlarmCond
alarm condition
Definition: hekMonitor.h:443
byte_t hekateros::HekMonitor::getEEAuxBits ( )
inline

Get current End Effector auxilliary bit values.

Returns
Bits.

Definition at line 423 of file hekMonitor.h.

References getJointLimitTriState(), and m_byEEAuxBits.

424  {
425  return m_byEEAuxBits;
426  }
byte_t m_byEEAuxBits
end effector auxilliary bit state
Definition: hekMonitor.h:460
int hekateros::HekMonitor::getEStopCond ( )
inline

Get monitoring emergency stop condition.

Returns
Returns true or false.

Definition at line 381 of file hekMonitor.h.

References m_bEStopCond.

382  {
383  return m_bEStopCond;
384  }
bool m_bEStopCond
estop condition
Definition: hekMonitor.h:442
byte_t hekateros::HekMonitor::getJointLimitBits ( )
inline

Get current limit switch bit values.

Returns
Bits.

Definition at line 413 of file hekMonitor.h.

References m_byLimitBits.

414  {
415  return m_byLimitBits;
416  }
byte_t m_byLimitBits
limit bit state
Definition: hekMonitor.h:459
HekOpticalLimit_T* hekateros::HekMonitor::getJointLimitInfo ( byte_t  byMask)
inline

Get joint limits information.

Parameters
byMaskLimit switch bit mask (index).
Returns
Return pointer to limit switch information.

Definition at line 403 of file hekMonitor.h.

References m_opticalLimits.

404  {
405  return &(m_opticalLimits[byMask].m_limit);
406  }
MapOpticalLimits m_opticalLimits
joint optical limits map
Definition: hekMonitor.h:448
HekTriState HekMonitor::getJointLimitTriState ( HekRobotJoint pJoint,
int  nLimit 
)

Get current joint limit tri-state.

Parameters
pJointJoint.
nLimitJoint limit switch index.
Returns
Return HekTriState value.

Definition at line 499 of file hekMonitor.cxx.

References hekateros::HekRobotJoint::m_byOptLimitMask.

Referenced by getEEAuxBits().

500 {
501  uint_t uMask = pJoint->m_byOptLimitMask[nLimit];
502 
503  if( uMask == HekIOExpUnassigned )
504  {
505  return HekTriStateUnknown;
506  }
507  else if( m_opticalLimits[uMask].m_uCurVal & HekIOExpLight )
508  {
509  return HekTriStateLight;
510  }
511  else
512  {
513  return HekTriStateDark;
514  }
515 }
static const byte_t HekIOExpUnassigned
unassigned port bit
Definition: hekOptical.h:82
static const byte_t HekIOExpLight
optical switch is lit
Definition: hekOptical.h:84
byte_t m_byOptLimitMask[HekOptLimitMaxPerJoint]
optical limit mask array
Definition: hekJoint.h:175
MapOpticalLimits m_opticalLimits
joint optical limits map
Definition: hekMonitor.h:448
MonStateId hekateros::HekMonitor::getMonState ( )
inline

Get the current monitor state.

Returns
MonStateId enum value.

Definition at line 247 of file hekMonitor.h.

References m_eMonState, markAlarmCond(), and markEStopCond().

248  {
249  return m_eMonState;
250  }
MonStateId m_eMonState
<b><i>Hekateros</i></b> monitor state
Definition: hekMonitor.h:440
int hekateros::HekMonitor::getPoweredCond ( )
inline

Get motors are powered condition.

Returns
Returns true or false.

Definition at line 391 of file hekMonitor.h.

References m_bPoweredCond.

392  {
393  return m_bPoweredCond;
394  }
bool m_bPoweredCond
power to motors condition
Definition: hekMonitor.h:444
const char * HekMonitor::getThStateName ( MonThStateId  eNewState)
protected

Get thread state name string.

define/undef to enable/disable debug test of i/f

Parameters
eNewStateMonitor thread's new state.
Returns
State name string.

Definition at line 604 of file hekMonitor.cxx.

Referenced by monThread(), and unlock().

void hekateros::HekMonitor::lock ( )
inlineprotected

Lock the monitor thread mutex.

The calling thread will block while waiting for the mutex to become available. Once locked, the monitor thread will block.

The lock()/unlock() primitives provide a safe mechanism safely access monitor resources.

Context:
Any.

Definition at line 488 of file hekMonitor.h.

489  {
490  pthread_mutex_lock(&m_mutexMon);
491  }
pthread_mutex_t m_mutexMon
monitor mutex
Definition: hekMonitor.h:468
void HekMonitor::markAlarmCond ( bool  bAlarmedCond)

Mark alarm condition.

Parameters
bAlarm

Definition at line 396 of file hekMonitor.cxx.

Referenced by getMonState().

397 {
398  m_bAlarmCond = bAlarmCond;
399 
401 }
bool m_bAlarmCond
alarm condition
Definition: hekMonitor.h:443
bool m_bEStopCond
estop condition
Definition: hekMonitor.h:442
void writeAlarmLED(bool bState)
Write alarm LED.
Definition: hekMonitor.cxx:476
void HekMonitor::markEStopCond ( bool  bEStopCond)

Mark emergency stop condition.

Parameters
bEStopCond

Definition at line 403 of file hekMonitor.cxx.

Referenced by getMonState(), and hekateros::HekRobot::resetEStop().

404 {
405  m_bEStopCond = bEStopCond;
406 
407  if( m_bEStopCond )
408  {
410  }
411 
413 }
bool m_bAlarmCond
alarm condition
Definition: hekMonitor.h:443
bool m_bEStopCond
estop condition
Definition: hekMonitor.h:442
void writeAlarmLED(bool bState)
Write alarm LED.
Definition: hekMonitor.cxx:476
void hekateros::HekMonitor::markPoweredCond ( bool  bPoweredCond)
inline

Mark motors are powered condition.

Parameters
bPoweredCond

Definition at line 271 of file hekMonitor.h.

References configEEGpioPin(), m_bPoweredCond, readEEGpio(), readEEGpioPin(), readFwVersion(), readLimitSwitches(), writeAlarmLED(), and writeEEGpioPin().

272  {
273  m_bPoweredCond = bPoweredCond;
274  }
bool m_bPoweredCond
power to motors condition
Definition: hekMonitor.h:444
void HekMonitor::monJointLimits ( )
protected

Monitor joint (optical) limits for state transitions.

Any limited rotation joint will be stopped when an optical limit switch transitions from lit to blocked.

Definition at line 681 of file hekMonitor.cxx.

References hekateros::degToRad(), hekateros::HekRobotJoint::doStopAtLimits(), hekateros::HekRobotJoint::m_fMaxSoftLimitRads, hekateros::HekRobotJoint::m_fMinSoftLimitRads, and hekateros::HekRobotJoint::m_strName.

Referenced by monThread(), and unlock().

682 {
683  // past limit backoff speed (rads/sec)
684  static double TuneBackOffVel = degToRad(10.0);
685 
686  MapOpticalLimits::iterator iter;
687  byte_t byMask;
688  byte_t byVal;
689  HekRobotJoint *pJoint;
690  DynaServo *pServo;
691  int nOdPos;
692 
693  if( (m_pDynaChain == NULL) || (m_pKin == NULL) )
694  {
695  return;
696  }
697 
699 
700  for(iter = m_opticalLimits.begin(); iter != m_opticalLimits.end(); ++iter)
701  {
702  byMask = iter->first;
703  pJoint = iter->second.m_pJoint;
704 
705  byVal = m_byLimitBits & byMask;
706 
707  //
708  // Transitioned from light to dark.
709  //
710  if( (byVal == HekIOExpDark) && (byVal != iter->second.m_uCurVal) )
711  {
712  // have power to servos and this limit set to automatically stop
713  if( m_bPoweredCond && pJoint->doStopAtLimits() )
714  {
715  string strJointName;
716  double fJointCurPos, fJointCurVel;
717 
718  strJointName = pJoint->m_strName;
719 
720  m_pKin->getJointCurPosVel(strJointName, fJointCurPos, fJointCurVel);
721 
722  if( fJointCurPos < pJoint->m_fMinSoftLimitRads )
723  {
724  m_pKin->move(strJointName, pJoint->m_fMinSoftLimitRads,
725  TuneBackOffVel);
726  }
727  else if( fJointCurPos > pJoint->m_fMaxSoftLimitRads )
728  {
729  m_pKin->move(strJointName, pJoint->m_fMaxSoftLimitRads,
730  TuneBackOffVel);
731  }
732  else
733  {
734  m_pKin->stop(strJointName);
735  }
736  }
737  else
738  {
739  // could validate/resync position(s) here
740  }
741  }
742 
743  iter->second.m_uCurVal = byVal;
744  }
745 }
virtual int stop()
Stop kinematics chain at the current position.
Definition: hekKin.cxx:399
virtual void getJointCurPosVel(const std::string &strJointName, double &fJointCurPos, double &fJointCurVel)
Get the current instantaneous joint position and velocity.
Definition: hekKin.cxx:242
double m_fMinSoftLimitRads
joint min soft limit (radians)
Definition: hekJoint.h:166
Operational robotic joint description class.
Definition: hekJoint.h:80
bool doStopAtLimits() const
Get auto-stop at joint electronic limits state.
Definition: hekJoint.h:142
HekKinematics * m_pKin
kinematics
Definition: hekMonitor.h:449
DynaChain * m_pDynaChain
dynamixel servo chain alarms
Definition: hekMonitor.h:447
std::string m_strName
joint name
Definition: hekJoint.h:149
double degToRad(double d)
Convert degrees to radians.
Definition: hekUtils.h:125
double m_fMaxSoftLimitRads
joint max soft limit (radians)
Definition: hekJoint.h:167
MapOpticalLimits m_opticalLimits
joint optical limits map
Definition: hekMonitor.h:448
byte_t m_byLimitBits
limit bit state
Definition: hekMonitor.h:459
bool m_bPoweredCond
power to motors condition
Definition: hekMonitor.h:444
byte_t readLimitSwitches()
Read joint limit switch states.
Definition: hekMonitor.cxx:420
virtual int move(HekJointTrajectoryPoint &trajectoryPoint)
Move kinematic chain through a trajectory point.
Definition: hekKin.cxx:500
static const byte_t HekIOExpDark
optic switch blocked
Definition: hekOptical.h:83
void * HekMonitor::monThread ( void *  pArg)
staticprotected

Monitor thread.

Parameters
pArgThread argument (point to HekMonitor object).
Returns
Returns NULL on thread exit.

Definition at line 623 of file hekMonitor.cxx.

References getThStateName(), idleWait(), m_eMonThState, m_eMonThStateOld, monEEAuxIO(), monJointLimits(), monServoAlarms(), runWait(), testInterface(), and unlock().

Referenced by unlock().

624 {
625  HekMonitor *pThis = (HekMonitor *)pArg;
626  int oldstate;
627  int rc;
628 
629  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
630  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
631  //pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldstate);
632 
634 
635  LOGDIAG3("Monitor thread created.");
636 
637  while( pThis->m_eMonThState != MonThStateIdExit )
638  {
639  if( pThis->m_eMonThStateOld != pThis->m_eMonThState )
640  {
641  LOGDIAG3("Monitor thread: %s -> %s state.",
642  pThis->getThStateName(pThis->m_eMonThStateOld),
643  pThis->getThStateName(pThis->m_eMonThState));
644  }
645 
646  switch( pThis->m_eMonThState )
647  {
648  case MonThStateIdIdle:
649  pThis->idleWait();
650  break;
651 
652  case MonThStateIdRun:
653  pThis->runWait();
654 #ifdef HEK_MON_TEST_IF
655  pThis->testInterface();
656 #endif
657  pThis->monJointLimits();
658  pThis->monEEAuxIO();
659  pThis->monServoAlarms();
660  break;
661 
662  case MonThStateIdExit:
663  break;
664 
665  default:
666  LOGERROR("%d: Unknown monitor state.", pThis->m_eMonThState);
668  break;
669  }
670 
671  pThis->m_eMonThStateOld = pThis->m_eMonThState;
672  }
673 
674  pThis->unlock();
675 
676  LOGDIAG3("Monitor thread exited.");
677 
678  return NULL;
679 }
void runWait()
Monitor thread block timed wait in run state.
Definition: hekMonitor.cxx:581
void idleWait()
Monitor thread block wait in idle state.
Definition: hekMonitor.cxx:568
void monJointLimits()
Monitor joint (optical) limits for state transitions.
Definition: hekMonitor.cxx:681
MonThStateId m_eMonThState
monitoring thread state
Definition: hekMonitor.h:463
const char * getThStateName(MonThStateId eNewState)
Get thread state name string.
Definition: hekMonitor.cxx:604
void monEEAuxIO()
Monitor End Effector auxillary I/O.
Definition: hekMonitor.cxx:747
void unlock()
Unlock the monitor thread mutex.
Definition: hekMonitor.h:502
<b><i>Hekateros</i></b> power, joint limits, and alarm monitoring class.
Definition: hekMonitor.h:100
void testInterface()
Test interface.
Definition: hekMonitor.cxx:485
void monServoAlarms()
Monitor servos for alarms.
Definition: hekMonitor.cxx:751
MonThStateId m_eMonThStateOld
monitoring thread old state
Definition: hekMonitor.h:464
int HekMonitor::open ( uint_t  uHekHwVer,
const std::string &  strDevArduino = HekDevArduino,
int  nBaudRateArduino = HekBaudRateArduino 
)

Open resources to monitor hardware.

Creates monitor thread.

Parameters
uHekHwVer

Definition at line 216 of file hekMonitor.cxx.

219 {
220  int rc;
221 
222  if( m_bIsOpen )
223  {
224  LOGWARN("Monitoring hardware already opened.");
225  return HEK_OK;
226  }
227 
228  if( (rc = m_hwif.open(uHekHwVer, strDevArduino, nBaudRateArduino)) < 0 )
229  {
230  LOGERROR("Hekateros monitor hardware open failed.");
231  return rc;
232  }
233  else if( (rc = m_hwif.scan()) < 0 )
234  {
235  LOGERROR("Hekateros monitor hardware scan failed.");
236  return rc;
237  }
238 
239  if( (rc = createMonThread()) < 0 )
240  {
241  LOGERROR("Hekateros monitor thread creation failed.");
242  return rc;
243  }
244 
246  m_bIsOpen = true;
247 
248  LOGDIAG2("Opened monitoring hardware.");
249 
250  return HEK_OK;
251 }
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
bool m_bIsOpen
hardware is [not] open
Definition: hekMonitor.h:441
HekUno m_hwif
arduino compatible subproc i/f
Definition: hekMonitor.h:453
virtual int scan()
Scan and initialize hardware.
Definition: hekUno.cxx:115
MonStateId m_eMonState
<b><i>Hekateros</i></b> monitor state
Definition: hekMonitor.h:440
power on and/or (re)initializing
Definition: hekMonitor.h:119
virtual int open(uint_t uHekHwVer, const std::string &dev=HekDevArduino, int baud=HekBaudRateArduino)
Open all interfaces monitoring hardware.
Definition: hekUno.cxx:81
int createMonThread()
Create the monitor thread.
Definition: hekMonitor.cxx:522
byte_t HekMonitor::readEEGpio ( )

Read End Effector GPIO states.

Returns
GPIO bit values.

Definition at line 435 of file hekMonitor.cxx.

Referenced by markPoweredCond().

436 {
437  byte_t byVal = 0;
438 
439  lock();
440 
441  //LOGDIAG3("Reading optical switch pins");
442 
443  byVal = m_hwif.cmdReadAux();
444 
445  unlock();
446 
447  return byVal;
448 }
HekUno m_hwif
arduino compatible subproc i/f
Definition: hekMonitor.h:453
virtual byte_t cmdReadAux()
Command to read auxilliary bit state.
Definition: hekUno.cxx:188
void unlock()
Unlock the monitor thread mutex.
Definition: hekMonitor.h:502
void lock()
Lock the monitor thread mutex.
Definition: hekMonitor.h:488
byte_t hekateros::HekMonitor::readEEGpioPin ( byte_t  byPin)

Read one End Effector GPIO pin state.

Parameters
byPinGPIO pin.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Referenced by markPoweredCond().

byte_t hekateros::HekMonitor::readFwVersion ( )

Read firmware version.

Returns
Version number

Referenced by markPoweredCond().

byte_t HekMonitor::readLimitSwitches ( )

Read joint limit switch states.

Returns
Limit switch bit values.

Definition at line 420 of file hekMonitor.cxx.

Referenced by markPoweredCond().

421 {
422  byte_t byVal = 0;
423 
424  lock();
425 
426  //LOGDIAG3("Reading optical limits.");
427 
428  byVal = m_hwif.cmdReadLimits();
429 
430  unlock();
431 
432  return byVal;
433 }
virtual byte_t cmdReadLimits()
Command to read limit bit state.
Definition: hekUno.cxx:162
HekUno m_hwif
arduino compatible subproc i/f
Definition: hekMonitor.h:453
void unlock()
Unlock the monitor thread mutex.
Definition: hekMonitor.h:502
void lock()
Lock the monitor thread mutex.
Definition: hekMonitor.h:488
void HekMonitor::signalMonThread ( MonThStateId  eNewState)
protected

Signal monitor thread of new state.

Parameters
eNewStateMonitor thread's new state.

Definition at line 557 of file hekMonitor.cxx.

Referenced by unlock().

558 {
559  lock();
560 
562  m_eMonThState = eNewThState;
563  pthread_cond_signal(&m_condMon);
564 
565  unlock();
566 }
MonThStateId m_eMonThState
monitoring thread state
Definition: hekMonitor.h:463
void unlock()
Unlock the monitor thread mutex.
Definition: hekMonitor.h:502
void lock()
Lock the monitor thread mutex.
Definition: hekMonitor.h:488
MonThStateId m_eMonThStateOld
monitoring thread old state
Definition: hekMonitor.h:464
pthread_cond_t m_condMon
monitor condition
Definition: hekMonitor.h:469
int HekMonitor::start ( double  fHz = 30.0)

Start monitoring added objects.

Parameters
fHzMonitoring hertz.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 290 of file hekMonitor.cxx.

291 {
292  double fPeriod;
293 
294  if( !m_bIsOpen )
295  {
296  LOGERROR("Monitor hardware not open.");
297  return -HEK_ECODE_NO_RSRC;
298  }
299  else if( m_eMonThState != MonThStateIdIdle )
300  {
301  LOGERROR("Monitor thread not in idle state.");
302  return -HEK_ECODE_BAD_OP;
303  }
304  else if( fHz <= 0.0 )
305  {
306  LOGERROR("Monitor thread run Hz=%lf invalid.", fHz);
307  return -HEK_ECODE_BAD_OP;
308  }
309 
310  m_fHz = fHz;
311  fPeriod = 1.0/fHz;
312 
313  m_tPeriod.tv_sec = (time_t)fPeriod;
314  fPeriod -= (double)m_tPeriod.tv_sec;
315  m_tPeriod.tv_nsec = (time_t)(fPeriod * GIGASEC);
316 
317  m_tStart = now();
318 
319  LOGDIAG3("Monitor thread started running at %.1lf Hz.", m_fHz);
320 
322 
324 
325  return HEK_OK;
326 }
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
bool m_bIsOpen
hardware is [not] open
Definition: hekMonitor.h:441
static const int HEK_ECODE_BAD_OP
invalid operation error
Definition: hekateros.h:79
MonStateId m_eMonState
<b><i>Hekateros</i></b> monitor state
Definition: hekMonitor.h:440
struct timespec m_tStart
start time for next monitor work
Definition: hekMonitor.h:467
MonThStateId m_eMonThState
monitoring thread state
Definition: hekMonitor.h:463
struct timespec m_tPeriod
derived monitoring period
Definition: hekMonitor.h:466
double m_fHz
monitoring hertz
Definition: hekMonitor.h:465
static const int HEK_ECODE_NO_RSRC
no resource available error
Definition: hekateros.h:82
void signalMonThread(MonThStateId eNewState)
Signal monitor thread of new state.
Definition: hekMonitor.cxx:557
int HekMonitor::stop ( )

Stop monitoring.

Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 328 of file hekMonitor.cxx.

329 {
330  if( !m_bIsOpen )
331  {
332  LOGERROR("Monitor hardware not open.");
333  return -HEK_ECODE_NO_RSRC;
334  }
335  else if( m_eMonThState != MonThStateIdRun )
336  {
337  LOGERROR("Monitor thread not in run state.");
338  return -HEK_ECODE_BAD_OP;
339  }
340 
341  LOGDIAG3("Monitor thread stopped - blocked waiting.");
342 
344 
346 
347  return HEK_OK;
348 }
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
bool m_bIsOpen
hardware is [not] open
Definition: hekMonitor.h:441
static const int HEK_ECODE_BAD_OP
invalid operation error
Definition: hekateros.h:79
MonStateId m_eMonState
<b><i>Hekateros</i></b> monitor state
Definition: hekMonitor.h:440
MonThStateId m_eMonThState
monitoring thread state
Definition: hekMonitor.h:463
static const int HEK_ECODE_NO_RSRC
no resource available error
Definition: hekateros.h:82
power on and/or (re)initializing
Definition: hekMonitor.h:119
void signalMonThread(MonThStateId eNewState)
Signal monitor thread of new state.
Definition: hekMonitor.cxx:557
void HekMonitor::testInterface ( )

Test interface.

Diagnostics printed to stderr.

Definition at line 485 of file hekMonitor.cxx.

Referenced by monThread(), and writeStartHaltingSystem().

486 {
487  lock();
488 
490 
491  unlock();
492 }
HekUno m_hwif
arduino compatible subproc i/f
Definition: hekMonitor.h:453
virtual int cmdTestInterface()
Test serial interface command.
Definition: hekUno.cxx:360
void unlock()
Unlock the monitor thread mutex.
Definition: hekMonitor.h:502
void lock()
Lock the monitor thread mutex.
Definition: hekMonitor.h:488
void hekateros::HekMonitor::unlock ( )
inlineprotected

Unlock the monitor thread mutex.

The monitor thread will be available to run.

Context:
Any.

Definition at line 502 of file hekMonitor.h.

References createMonThread(), destroyMonThread(), getThStateName(), idleWait(), monEEAuxIO(), monJointLimits(), monServoAlarms(), monThread(), runWait(), and signalMonThread().

Referenced by monThread().

503  {
504  pthread_mutex_unlock(&m_mutexMon);
505  }
pthread_mutex_t m_mutexMon
monitor mutex
Definition: hekMonitor.h:468
void HekMonitor::writeAlarmLED ( bool  bState)

Write alarm LED.

Parameters
bStateOn/off state of LED.

Definition at line 476 of file hekMonitor.cxx.

Referenced by markPoweredCond().

477 {
478  lock();
479 
480  m_hwif.cmdSetAlarmLED(bState? 1: 0);
481 
482  unlock();
483 }
virtual int cmdSetAlarmLED(int val)
Command to set Alarm LED.
Definition: hekUno.cxx:283
HekUno m_hwif
arduino compatible subproc i/f
Definition: hekMonitor.h:453
void unlock()
Unlock the monitor thread mutex.
Definition: hekMonitor.h:502
void lock()
Lock the monitor thread mutex.
Definition: hekMonitor.h:488
int HekMonitor::writeEEGpioPin ( byte_t  byPin,
byte_t  byVal 
)

Write state to one End Effector GPIO pin.

Parameters
byPinGPIO pin.
byValGPIO pin new value. 0 or 1.
Returns
On success, HEK_OK is returned.
On error, the appropriate < 0 negated Hekateros Error Code is returned.

Definition at line 450 of file hekMonitor.cxx.

Referenced by markPoweredCond().

451 {
452  int rc = HEK_OK;
453 
454  lock();
455 
456  rc = m_hwif.cmdWritePin(byPin, byVal);
457 
458  unlock();
459 
460  return rc;
461 }
static const int HEK_OK
not an error, success
Definition: hekateros.h:70
HekUno m_hwif
arduino compatible subproc i/f
Definition: hekMonitor.h:453
virtual int cmdWritePin(int id, int val)
Command to write value to an I/O pin.
Definition: hekUno.cxx:227
void unlock()
Unlock the monitor thread mutex.
Definition: hekMonitor.h:502
void lock()
Lock the monitor thread mutex.
Definition: hekMonitor.h:488
void hekateros::HekMonitor::writeStatusLED ( bool  bState)
inline

Write CPU status LED.

Parameters
bStateOn/off state of LED.

Definition at line 343 of file hekMonitor.h.

344  {
345  }

The documentation for this class was generated from the following files: