Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaShellCmdLink Class Reference

Link <master_id> <slaf_id> <dir> command. More...

Inheritance diagram for DynaShellCmdLink:
DynaShellCmd

Public Member Functions

void Exec (DynaShell &shell, int argc, char *argv[])
 Link two servo together. More...
 
virtual char * TabCompletion (DynaShell &shell, const char *sText, size_t uTextLen, int nState, const char *sContext)
 Command tab completion generator. More...
 
- Public Member Functions inherited from DynaShellCmd
 DynaShellCmd (int nArgCntMin=0, int nArgCntMax=0)
 Default constructor.
 
virtual ~DynaShellCmd ()
 Default destructor.
 
const char * GetCmdName ()
 Get dynamixel shell command name. More...
 
const char * GetCmdHelpBrief ()
 Get shell command name brief description. More...
 
virtual void PrintHelp (int indent=0, int width=80)
 Print command help with the description aligned at the given indentation. More...
 
int GetPublishedLevel () const
 
const char * GetPublishedName () const
 
int SetPublishedInfo (int nLevel, const char *sParent=NULL)
 
virtual bool ChkArgCnt (DynaShell &shell, int argc)
 Check that the argument count is within the class (min,max). More...
 
bool ChkArgCnt0 (DynaShell &shell, int argc)
 Check that the argument count is zero. More...
 
bool ChkArgCntEQ (DynaShell &shell, int argc, int eq)
 Check that the argument count is equal to the required. More...
 
bool ChkArgCntGE (DynaShell &shell, int argc, int min)
 Check argument count against minimum required. More...
 
bool ChkArgCntLE (DynaShell &shell, int argc, int max)
 Check argument count against maximum allowed. More...
 
bool ChkComm (DynaShell &shell)
 Check that Dynamixel communication exists and is open. More...
 
bool ChkChain (DynaShell &shell)
 Check that the servo chain exists. More...
 
bool ChkChainNotEmpty (DynaShell &shell)
 Check that the servo chain exists and is not empty. More...
 
bool ChkChainHasServo (DynaShell &shell, int nServoId)
 Check that the given servo is present in the chain. More...
 
bool ChkChainIsMasterServo (DynaShell &shell, int nServoId)
 Check that the given servo is a master. More...
 
bool ToInt (DynaShell &shell, const char *sArg, int *pVal)
 Convert command argument to integer. More...
 
bool ToUInt (DynaShell &shell, const char *sArg, uint_t *pVal)
 Convert command argument to unsigned integer. More...
 
bool ToDouble (DynaShell &shell, const char *sArg, double *pVal)
 Convert command argument to double. More...
 
bool ToBool (DynaShell &shell, const char *sArg, bool *pVal)
 Convert command argument to boolean. More...
 

Protected Attributes

int m_nTabIter
 tab completion: servo id iterator
 
int m_nTabServoId
 tab completion: current servo id
 
int m_nTabIndex
 tab completion: keyword index
 
- Protected Attributes inherited from DynaShellCmd
const char * m_sCmdName
 command name
 
const char * m_sCmdHelpBrief
 command help brief
 
const char * m_sCmdHelpArgs
 command help arguments
 
const char * m_sCmdHelpDesc
 command help description
 
const int m_nArgCntMin
 minimum argument count
 
const int m_nArgCntMax
 maximum argument count (0 if not max)
 
int m_nPubLevel
 command's published level (depth)
 
char * m_sPubName
 command's published name
 

Additional Inherited Members

- Protected Member Functions inherited from DynaShellCmd
virtual void PrintSynopses (int indent, int width)
 Print synsopses. More...
 
virtual void PrintBlock (int col, int indent, int width, const char *sText)
 Print a block of indented text of width. More...
 
virtual void PrintDelim (int width, const char cDelim)
 
char * eow (const char *s)
 Find end of word. More...
 

Detailed Description

Link <master_id> <slaf_id> <dir> command.

Definition at line 514 of file dynashell_cmd_if.cxx.

Member Function Documentation

void DynaShellCmdLink::Exec ( DynaShell shell,
int  argc,
char *  argv[] 
)
inlinevirtual

Link two servo together.

Parameters
shellDynamixel shell.
argcCommand argument count.
argvArray of arguments.

Implements DynaShellCmd.

Definition at line 543 of file dynashell_cmd_if.cxx.

References DYNA_ECODE_BAD_VAL, DYNA_OK, DynaShell::Error(), DynaChain::LinkServos(), DynaShell::m_pDynaChain, DynaShell::Ok(), and TRY.

544  {
545  int nServoIdMaster;
546  int nServoIdSlave;
547  bool bIsReversed;
548  int rc;
549 
550  TRY( ChkArgCnt(shell, argc) );
551 
552  TRY( ToInt(shell, argv[0], &nServoIdMaster) );
553  TRY( ToInt(shell, argv[1], &nServoIdSlave) );
554 
555  if( !strncmp("forward", argv[2], strlen(argv[2])) )
556  {
557  bIsReversed = false;
558  }
559  else if( !strncmp("reverse", argv[2], strlen(argv[2])) )
560  {
561  bIsReversed = true;
562  }
563  else
564  {
565  shell.Error(-DYNA_ECODE_BAD_VAL, "Unknown linkage <dir> '%s'", argv[2]);
566  return;
567  }
568 
569  TRY( ChkComm(shell) );
570  TRY( ChkChain(shell) );
571  TRY( ChkChainHasServo(shell, nServoIdMaster) );
572  TRY( ChkChainHasServo(shell, nServoIdSlave) );
573 
574  rc = shell.m_pDynaChain->LinkServos(nServoIdMaster,
575  nServoIdSlave,
576  bIsReversed);
577 
578  if( rc != DYNA_OK )
579  {
580  shell.Error(rc, "Cannot link servos %d and %d.",
581  nServoIdMaster, nServoIdSlave);
582  }
583  else
584  {
585  shell.Ok();
586  }
587  }
#define DYNA_OK
not an error, success
Definition: Dynamixel.h:78
bool ChkChain(DynaShell &shell)
Check that the servo chain exists.
bool ChkChainHasServo(DynaShell &shell, int nServoId)
Check that the given servo is present in the chain.
virtual int LinkServos(int nServoIdMaster, int nServoIdSlave, bool bIsReversed)
Software link two unlinked servos in a master-slave configuration.
Definition: DynaChain.cxx:105
bool ToInt(DynaShell &shell, const char *sArg, int *pVal)
Convert command argument to integer.
virtual void Ok()
Print standard ok success response.
Definition: dynashell.h:425
virtual bool ChkArgCnt(DynaShell &shell, int argc)
Check that the argument count is within the class (min,max).
#define TRY(boolexpr,...)
Try boolean expression.
Definition: dynashell_cmd.h:89
bool ChkComm(DynaShell &shell)
Check that Dynamixel communication exists and is open.
#define DYNA_ECODE_BAD_VAL
bad value
Definition: Dynamixel.h:85
DynaChain * m_pDynaChain
dynamixel chain
Definition: dynashell.h:360
void Error(int rc, const char *sFmt,...)
Raise error on dynamixel error code.
Definition: dynashell.cxx:808
virtual char* DynaShellCmdLink::TabCompletion ( DynaShell shell,
const char *  sText,
size_t  uTextLen,
int  nState,
const char *  sContext 
)
inlinevirtual

Command tab completion generator.

Completes <master_id> <slave_id> <dir>

Parameters
shellDynamixel shell.
sTextPartial text string to complete.
uTextLenLength of text.
nStateGenerator state. If FIRST, then initialize any statics.
sContextGenerator context (i.e. canonical command path).
Returns
If a first/next match is made, return allocated completed match.
Otherwise return NULL.

Reimplemented from DynaShellCmd.

Definition at line 604 of file dynashell_cmd_if.cxx.

References ReadLine::dupstr(), DYNA_ID_NONE, ReadLine::FIRST, DynaChain::GetNumberInChain(), DynaChain::IsUnlinked(), DynaChain::IterNext(), DynaChain::IterStart(), DynaShell::m_pDynaChain, and ReadLine::wc().

609  {
610  // direction keyword table
611  static const char *keywordsDir[] = {"forward", "reverse"};
612 
613  int nArgNum;
614  int nServoId;
615  const char *s;
616  char buf[16];
617 
618  if( (shell.m_pDynaChain == NULL) ||
619  (shell.m_pDynaChain->GetNumberInChain() == 0) )
620  {
621  return NULL;
622  }
623 
624  // argument number of already (expanded) arguments
625  nArgNum = ReadLine::wc(sContext) - ReadLine::wc(m_sPubName);
626 
627  //
628  // New command argument to complete - initialize.
629  //
630  if( nState == ReadLine::FIRST )
631  {
633  m_nTabIndex = 0;
634  }
635 
636  // <master_id> or <slave_id>
637  if( nArgNum < 2 )
638  {
639  while( m_nTabServoId != DYNA_ID_NONE )
640  {
641  nServoId = m_nTabServoId;
642 
644 
645  if( !shell.m_pDynaChain->IsUnlinked(m_nTabServoId) )
646  {
647  continue;
648  }
649 
650  snprintf(buf, sizeof(buf), "%d", nServoId);
651  buf[sizeof(buf)-1] = 0;
652 
653  if( !strncmp(buf, sText, uTextLen) )
654  {
655  return ReadLine::dupstr(buf);
656  }
657  }
658  }
659 
660  // <dir>
661  else if( nArgNum == 2 )
662  {
663  while( m_nTabIndex < arraysize(keywordsDir) )
664  {
665  s = keywordsDir[m_nTabIndex++];
666 
667  if( !strncmp(s, sText, uTextLen) )
668  {
669  return ReadLine::dupstr(s);
670  }
671  }
672  }
673 
674  // no more matches
675  return NULL;
676  }
bool IsUnlinked(int nServoId) const
Test if this servo is unlinked.
Definition: DynaChain.h:224
static const int FIRST
first state
static char * dupstr(const string &str)
Duplicate string.
virtual uint_t GetNumberInChain() const
Get the number of servos currently in chain.
Definition: DynaChain.h:140
char * m_sPubName
command&#39;s published name
static int wc(const string &str)
Count the words in the string.
virtual int IterStart(int *pIter)
Start iteration over of entire servo chain.
Definition: DynaChain.cxx:1008
#define DYNA_ID_NONE
no servo id
Definition: Dynamixel.h:145
virtual int IterNext(int *pIter)
Next iteration over of entire servo chain.
Definition: DynaChain.cxx:1020
DynaChain * m_pDynaChain
dynamixel chain
Definition: dynashell.h:360

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