![]() |
Dynamixel
2.9.5
RoadNarrows Robotics Dynamixel Package
|
#include <DynaBgThread.h>
Public Types | |
enum | BgThreadState { BgThreadStateZombie, BgThreadStateReady, BgThreadStateRunning, BgThreadStatePaused, BgThreadStateExit } |
Background thread states. More... | |
Public Member Functions | |
DynaBgThread (double fHz=HZ_EXEC_DFT, double fTolerance=TOLERANCE_DFT) | |
Default initializer constructor. More... | |
virtual | ~DynaBgThread () |
Desctructor. | |
virtual void | RegisterServoAgent (DynaServo *pServo) |
Register the Dynamixel servo for control and monitoring. More... | |
virtual void | RegisterChainAgent (DynaChain *pChain) |
Register the Dynamixel chain for control and monitoring. More... | |
virtual void | UnregisterAgent () |
Unregister any chain or servo from control and monitoring. More... | |
virtual void | RegisterUserCallback (void(*fnUserCb)(void *), void *pUserArg) |
Register user-defined callback function. More... | |
virtual void | UnregisterUserCallback () |
Unregister user-defined callback function. | |
virtual int | Run () |
Run the background thread control and monitoring tasks. More... | |
virtual int | Stop () |
Stop control and monitoring tasks. More... | |
virtual int | Pause () |
Pause control and monitoring tasks. More... | |
virtual int | Resume () |
Resume control and monitoring tasks. More... | |
BgThreadState | GetCurrentState () |
Get current background thread state. More... | |
void | setHz (double fHz) |
Set new control and monitoring execution hertz rate. More... | |
double | getHz () |
Get control and monitoring hetrz rate. More... | |
bool | isServoBeingControlled (int nServoId) |
Checks if servo is currently being controlled. More... | |
Static Public Member Functions | |
static int | WriteGoalPos (int nServoId, int nOdGoalPos, void *pUserArg) |
Start controlling servo to rotate it to the given goal position. More... | |
static int | WriteGoalSpeed (int nServoId, int nGoalSpeed, void *pUserArg) |
static int | WriteGoalSpeedPos (int nServoId, int nGoalSpeed, int nGoalPos, void *pUserArg) |
Static Public Attributes | |
static const double | HZ_EXEC_MIN = 1.0 |
minimum exec hertz | |
static const double | HZ_EXEC_DFT = 50.0 |
default exec hertz | |
static const long | T_EXEC_MIN = 100 |
task exec period min (100usec) | |
static const double | TOLERANCE_DFT = 1.0 |
default pos. tolerance (deg) | |
Protected Types | |
typedef map< int, DynaVServo > | MapVServo |
Virtual Servo Associative Map Type. | |
Protected Member Functions | |
void | lock () |
Lock the background thread. More... | |
void | unlock () |
Unlock the background thread. More... | |
void | changeState (DynaBgThread::BgThreadState eNewState) |
Change the background thread state. More... | |
void | readyWait () |
Wait indefinitely in ready state. More... | |
void | timeWait (long lMicroSecs) |
Wait until state change or time out. More... | |
void | setTolerance (double fTolerance) |
Set position tolerance. More... | |
virtual DynaServo * | getRegisteredServo (int nServoId) |
Get registered servo. More... | |
virtual DynaVServo * | getNextVServo (MapVServo::iterator &iter, int &nServoId) |
Get the next virtual servo. More... | |
virtual DynaVServo * | getPrevVServo (MapVServo::iterator &iter, int &nServoId) |
Get the previous virtual servo. More... | |
virtual void | sched (long lPeriod) |
Schedule the Dynamixel background thread for the next task(s) to perform. More... | |
virtual void | exec () |
Execute background tasks. More... | |
virtual void | execDynamics (DynaServo *pServo, DynaVServo *pVServo) |
Execute servo dynamics monitoring and control. More... | |
virtual void | execTorqueCtl (DynaServo *pServo, DynaVServo *pVServo) |
Execute servo torque control. More... | |
virtual void | execPosCtl (DynaServo *pServo, DynaVServo *pVServo) |
Execute servo position control. More... | |
virtual void | stopPosCtl (DynaServo *pServo, DynaVServo *pVServo) |
Stop servo position control. More... | |
virtual void | stopMotion (DynaServo *pServo) |
Stop servo motion. More... | |
virtual void | monitorDynamics (DynaServo *pServo, DynaVServo *pVServo) |
Execute servo dynamics monitoring background task. More... | |
virtual void | monitorHealth (DynaServo *pServo, DynaVServo *pVServo) |
Execute servo health monitoring background task. More... | |
void | createThread () |
Create the background thread. More... | |
void | terminateThread () |
Terminate the background thread. More... | |
Static Protected Member Functions | |
static void * | bgThread (void *pArg) |
Dynamixel background thread. More... | |
Protected Attributes | |
BgThreadState | m_eState |
thread state | |
pthread_mutex_t | m_mutexSync |
synchonization mutex | |
pthread_cond_t | m_condSync |
synchonization condition | |
pthread_t | m_thread |
pthread identifier | |
DynaServo * | m_pServo |
registered dynamixel servo | |
DynaChain * | m_pChain |
or registered dynamixel chain | |
MapVServo | m_mapVServo |
virtual servo associative map | |
uint_t | m_uNumServos |
number of servos to monitor | |
double | m_fTolerance |
position ± tolerance (degrees) | |
DynaAgent_T | m_agent |
servo/chain agent | |
void(* | m_fnUserCb )(void *) |
user callback function | |
void * | m_pUserArg |
user callback argument | |
double | m_fHz |
thread execution hertz | |
long | m_TExec |
full execution cycle μs period | |
struct timeval | m_tvSched |
working scheduler time stamp | |
long | m_TSched |
scheduler μs period | |
long | m_dTSched |
scheduler delta time | |
MapVServo::iterator | m_iterDynamics |
dynamics iterator | |
MapVServo::iterator | m_iterHealth |
health iterator | |
int | m_nHealthServoId |
current health monitoring servo id | |
Dynamixel background thread.
The DynaBgThread class supports background control and monitoring of registered servos.
Definition at line 240 of file DynaBgThread.h.
Background thread states.
Definition at line 251 of file DynaBgThread.h.
DynaBgThread::DynaBgThread | ( | double | fHz = HZ_EXEC_DFT , |
double | fTolerance = TOLERANCE_DFT |
||
) |
Default initializer constructor.
fHz | Execution hertz. |
fTolerance | Position tolerance (degrees). |
Definition at line 290 of file DynaBgThread.cxx.
References BG_TRACE_OPEN, DYNA_ID_NONE, and DynaVServo::m_mutexSync.
|
staticprotected |
Dynamixel background thread.
Cannot have gui calls in this thread.
pArg | Thread argument (point to DynaBgThread object). |
Definition at line 1187 of file DynaBgThread.cxx.
References exec(), m_eState, m_TSched, m_uNumServos, readyWait(), and sched().
Referenced by createThread().
|
protected |
Change the background thread state.
eNewState | New state. |
Definition at line 769 of file DynaBgThread.cxx.
References DynaVServo::m_eState.
|
protected |
Create the background thread.
Definition at line 1239 of file DynaBgThread.cxx.
References bgThread(), and DynaVServo::m_eState.
|
protectedvirtual |
Execute background tasks.
Definition at line 868 of file DynaBgThread.cxx.
Referenced by bgThread().
|
protectedvirtual |
Execute servo dynamics monitoring and control.
pServo | Pointer to registered servo. |
pVServo | Pointer to associated virtual servo. |
Definition at line 916 of file DynaBgThread.cxx.
References DYNA_GET_DIR, DYNA_SPEED_CONT_STOP, DynaVServo::getGoalDir(), DynaServo::GetState(), DynaVServo::lock(), DynaVServo::m_bOverTorqueCond, DynaVServo::m_bOverTorqueCtl, DynaServoState_T::m_bTorqueEnabled, DynaVServo::m_eState, DynaVServo::m_fTorqueOut, DynaVServo::m_nGoalSpeed, DynaVServo::StateIdle, and DynaVServo::unlock().
|
protectedvirtual |
Execute servo position control.
pServo | Pointer to registered servo. |
pVServo | Pointer to associated virtual servo. |
Definition at line 1030 of file DynaBgThread.cxx.
References BG_TRACE, DynaPid::Control(), DYNA_MODE_SERVO, DYNA_OK, DYNA_SPEED_CONT_STOP, DynaPid::GetError(), DynaServo::GetOdometer(), DynaServo::GetServoId(), DynaServo::GetServoMode(), iabs(), DynaVServo::m_eState, DynaVServo::m_nGoalSpeed, DynaVServo::m_nOdGoalPos, DynaVServo::m_nTolerance, DynaVServo::m_pidPos, and DynaVServo::StateIdle.
|
protectedvirtual |
Execute servo torque control.
pServo | Pointer to registered servo. |
pVServo | Pointer to associated virtual servo. |
Definition at line 972 of file DynaBgThread.cxx.
References BG_TRACE, DYNA_GET_DIR, DYNA_MODE_CONTINUOUS, DYNA_MODE_SERVO, DYNA_SPEED_CONT_STOP, DynaServo::GetOdometer(), DynaServo::GetServoId(), DynaServo::GetServoMode(), DynaServo::GetSoftTorqueThresholds(), DynaVServo::GOAL_SPEED_DFT, DynaVServo::m_bOverTorqueCtl, and DynaVServo::m_fTorqueOut.
|
inline |
Get current background thread state.
Definition at line 415 of file DynaBgThread.h.
Referenced by DynaShellCmdCreate::Exec(), DynaShellCmdDestroy::Exec(), DynaShellCmdScan::Exec(), DynaShellCmdBgtStart::Exec(), DynaShellCmdBgtStop::Exec(), and DynaShellCmdBgtGetState::Exec().
|
inline |
Get control and monitoring hetrz rate.
Definition at line 457 of file DynaBgThread.h.
Referenced by DynaShellCmdBgtStart::Exec(), and DynaShellCmdBgtGetState::Exec().
|
inlineprotectedvirtual |
Get the next virtual servo.
[in,out] | iter | Iterator at current position. |
[out] | nServoId | Servo id. |
Definition at line 608 of file DynaBgThread.h.
|
inlineprotectedvirtual |
Get the previous virtual servo.
[in,out] | iter | Iterator at current position. |
[out] | nServoId | Servo id. |
Definition at line 634 of file DynaBgThread.h.
|
protectedvirtual |
Get registered servo.
nServoId | Servo id. |
Definition at line 810 of file DynaBgThread.cxx.
References DynaServo::GetServoId().
Referenced by WriteGoalPos().
|
inline |
Checks if servo is currently being controlled.
nServoId | Servo Id. |
Definition at line 469 of file DynaBgThread.h.
References DynaVServo::StateControl.
|
inlineprotected |
Lock the background thread.
The calling thread will block while waiting for the mutex to become available. Once locked, the background thread will block.
The lock()/unlock() primitives provide a safe mechanism to modify the registered vServo data.
Definition at line 533 of file DynaBgThread.h.
|
protectedvirtual |
Execute servo dynamics monitoring background task.
pServo | Pointer to registered servo. |
pVServo | Pointer to associated virtual servo. |
Definition at line 1129 of file DynaBgThread.cxx.
References BG_TRACE, DynaVServo::filterTorques(), DynaServo::GetServoId(), DynaServo::GetSoftTorqueThresholds(), DynaServo::HasSoftTorqueOverCond(), DynaVServo::m_bOverTorqueCond, DynaVServo::m_fTorqueOut, and DynaServo::SetSoftTorqueOverCond().
|
protectedvirtual |
Execute servo health monitoring background task.
pServo | Pointer to registered servo. |
pVServo | Pointer to associated virtual servo. |
Definition at line 1169 of file DynaBgThread.cxx.
References BG_TRACE, and DynaServo::GetServoId().
|
virtual |
Pause control and monitoring tasks.
The Pause()/Resume() actions provide a safe mechanism to modify the registered dynamixel chain and/or alter the time periods. When the thread is resumed, the time periods are automatically recalculated since the mix of servos in the chain could have changed.
Definition at line 490 of file DynaBgThread.cxx.
References DYNA_ECODE_GEN, DYNA_LOG_ERROR, DYNA_OK, and DynaVServo::m_eState.
|
protected |
Wait indefinitely in ready state.
Definition at line 775 of file DynaBgThread.cxx.
References DynaVServo::lock(), DynaVServo::m_eState, DynaVServo::m_mutexSync, and DynaVServo::unlock().
Referenced by bgThread().
|
virtual |
Register the Dynamixel chain for control and monitoring.
The chain will replace the currently monitored servo or chain.
If the thread is running, it should be paused or locked prior to calling this function.
The time periods are automatically recalculated.
pChain | Dynamixel chain. May be NULL to deregister. |
Definition at line 367 of file DynaBgThread.cxx.
References DYNA_ID_NONE, DynaChain::GetNumberInChain(), DynaServo::RegisterAgent(), and DynaVServo::setToleranceInTicks().
Referenced by DynaShellCmdCreate::Exec(), DynaShellCmdScan::Exec(), and MainInitArgs().
|
virtual |
Register the Dynamixel servo for control and monitoring.
The servo will replace the currently monitored servo or chain.
If the thread is running, it should be paused or locked prior to calling this function.
The time periods are automatically recalculated.
pServo | Dynamixel servo. May be NULL to deregister. |
Definition at line 336 of file DynaBgThread.cxx.
References DynaServo::GetServoId(), DynaServo::RegisterAgent(), and DynaVServo::setToleranceInTicks().
|
inlinevirtual |
Register user-defined callback function.
fnUserCb | User callback function. |
pUserArg | User callback argument. |
Definition at line 320 of file DynaBgThread.h.
|
virtual |
Resume control and monitoring tasks.
Definition at line 517 of file DynaBgThread.cxx.
References DYNA_ECODE_GEN, DYNA_LOG_ERROR, DYNA_OK, and DynaVServo::m_eState.
|
virtual |
Run the background thread control and monitoring tasks.
Definition at line 435 of file DynaBgThread.cxx.
References DYNA_ECODE_GEN, DYNA_LOG_ERROR, DYNA_OK, and DynaVServo::m_eState.
Referenced by DynaShellCmdScan::Exec(), and DynaShellCmdBgtStart::Exec().
|
protectedvirtual |
Schedule the Dynamixel background thread for the next task(s) to perform.
Definition at line 826 of file DynaBgThread.cxx.
References dt_usec().
Referenced by bgThread().
void DynaBgThread::setHz | ( | double | fHz | ) |
Set new control and monitoring execution hertz rate.
fHz | New execution hertz. |
Definition at line 738 of file DynaBgThread.cxx.
Referenced by DynaShellCmdBgtStart::Exec().
|
inlineprotected |
Set position tolerance.
The servo is considered at a goal position when it is within ± of the tolerance.
Definition at line 586 of file DynaBgThread.h.
|
virtual |
Stop control and monitoring tasks.
Any servos being actively position controlled will be erased from the position control map.
Definition at line 463 of file DynaBgThread.cxx.
References DYNA_ECODE_GEN, DYNA_LOG_ERROR, DYNA_OK, and DynaVServo::m_eState.
Referenced by DynaShellCmdCreate::Exec(), DynaShellCmdDestroy::Exec(), DynaShellCmdScan::Exec(), DynaShellCmdBgtStop::Exec(), and DynaShell::~DynaShell().
|
protectedvirtual |
Stop servo motion.
pServo | Pointer to registered servo. |
Definition at line 1101 of file DynaBgThread.cxx.
References DYNA_MODE_CONTINUOUS, DYNA_MODE_SERVO, DYNA_OK, DYNA_SPEED_CONT_STOP, and DynaServo::GetServoMode().
|
protectedvirtual |
Stop servo position control.
pServo | Pointer to registered servo. |
pVServo | Pointer to associated virtual servo. |
Definition at line 1084 of file DynaBgThread.cxx.
References BG_TRACE, DynaServo::GetOdometer(), DynaServo::GetServoId(), DynaVServo::m_eState, DynaVServo::m_nOdGoalPos, and DynaVServo::StateIdle.
Referenced by WriteGoalPos().
|
protected |
Terminate the background thread.
This function does not return until the thread actually terminates.
Definition at line 1254 of file DynaBgThread.cxx.
|
protected |
Wait until state change or time out.
lMicroSec | Maximum wait duration (microseconds). |
Definition at line 785 of file DynaBgThread.cxx.
References DynaVServo::lock(), DynaVServo::m_mutexSync, and DynaVServo::unlock().
|
inlineprotected |
Unlock the background thread.
The background thread will be available to run.
Definition at line 547 of file DynaBgThread.h.
|
virtual |
Unregister any chain or servo from control and monitoring.
If the thread is running, it should be paused or locked prior to calling this function.
The time periods are automatically recalculated.
pChain | Dynamixel chain. May be NULL to deregister. |
Definition at line 405 of file DynaBgThread.cxx.
References DYNA_ID_NONE, and DynaServo::UnregisterAgent().
Referenced by DynaShellCmdCreate::Exec(), DynaShellCmdDestroy::Exec(), and DynaShellCmdScan::Exec().
|
static |
Start controlling servo to rotate it to the given goal position.
When the servo attains the given goal position within tolerance, the servo will be stopped. The servo speed to the goal position is controlled externally.
The servo is expected to be in continuous mode and to support 306 ° position information.
nServoId | Servo id. |
uGoalPos | Goal position in odometer virtual raw units. |
pUserArg | Pointer to this class instance. |
Definition at line 545 of file DynaBgThread.cxx.
References BG_TRACE, DYNA_ECODE_NO_SERVO, DYNA_MODE_CONTINUOUS, DYNA_MODE_SERVO, DYNA_OK, DYNA_SPEED_CONT_STOP, DynaServo::GetCurSpeed(), DynaServo::GetOdometer(), getRegisteredServo(), DynaServo::GetServoMode(), DynaServo::IsOdometerReversed(), DynaVServo::lock(), DynaVServo::m_eState, m_mapVServo, DynaVServo::m_nGoalSpeed, DynaVServo::m_nOdGoalPos, DynaVServo::m_nTolerance, DynaVServo::m_pidPos, DynaPidPos::SpecifySetPoint(), DynaVServo::StateControl, stopPosCtl(), and DynaVServo::unlock().