Listing 2—The gait-generator thread actuates the legs with stored patterns, depending on the current command mode (e.g., walking forward or stopping). It stores the current position of the actuator based on the pattern in a global data structure.

int Command;	/* current command */
extern SetTime[nCHAN];	/* time to set servo channel */
extern int MaxSteps[nCMD];	/* number of steps in pattern */
extern int Pattern[nCMD][nSTEP];	/* gait patterns */
GaitThread() {	/* generate leg actuations depending on current command */
  int CurrStep;
  int LastCommand;
  GetMutex(cmdmutex)
  Command = CMD_STOP;
  LastCommand = Command;
  ReleaseMutec(cmdmutex)
  CurrStep = 0;
  while(1){
    Sleep(STEPTIME);	/* check if command mode has changed */
    GetMutex(cmdmutex)
    if(LastCommand != Command) CurrStep = 0;
    LastCommand = Command;
    ReleaseMutec(cmdmutex)	/* set servo channels */
    foreach (i=0;i<nCHAN;i++){
      GetMutex(servomutex)
      SetTime[i] = Pattern[LastCommand][CurrStep];
      ReleaseMutex(servomutex); }
    CurrStep = (CurrStep + 1) % MaxSteos[LastCommand]; } } /* next step */