home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP5 / MAIN3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-02  |  9.2 KB  |  295 lines

  1. /***********************************************************************
  2.  
  3. FILE
  4.     main3.c  -  d.c. motor velocity control, executive control file #3
  5.  
  6. ROUTINES
  7.     main    -  main program
  8.     cmdint  -  command interpreter
  9.     fstr    -  extract command word
  10.     help    -  print list of commands
  11.  
  12. REMARKS
  13.     This version is intended for multi-motor control, to be
  14.     used with modules cntrl3.c and rtsim3.c.
  15.  
  16. LAST UPDATE
  17.     19 March 1985
  18.         recast in module format
  19.     01 April 1988
  20.         copy over from main2.c and adapt for multiple motors
  21.  
  22.     Copyright (c) 1984-1988  D.M. Auslander and C.H. Tham
  23.  
  24. ***********************************************************************/
  25.     
  26. /***********************************************************************
  27.                             I M P O R T S
  28. ***********************************************************************/
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>     /* standard library function declarations */
  32. #include <string.h>     /* standard string function declarations */
  33.  
  34. #include "envir.h"      /* environment definitions */
  35. #include "dash8.h"      /* declarations for dash8.c */
  36. #include "dac2.h"       /* declarations for dac2.c */
  37. #include "time0.h"      /* declarations for time0.c */
  38. #include "cntrl3.h"     /* declarations for cntrl3.c */
  39.  
  40. /***********************************************************************
  41.                 F O R W A R D    D E C L A R A T I O N S
  42. ***********************************************************************/
  43.  
  44. #ifdef ANSI
  45.  
  46. void    cmdint(char *);
  47. char *  getcmd(char *, char *);
  48. void    menu(void);
  49.  
  50. #else
  51.  
  52. void    cmdint();
  53. char    getcmd();
  54. void    menu();
  55.  
  56. #endif
  57.  
  58. /**********************************************************************
  59.                     P R I V A T E     D A T A
  60. **********************************************************************/
  61.  
  62. #define PROMPT  "READY "    /* defines the command processor prompt */
  63. #define BUFSIZE 100         /* command input buffer size */
  64.  
  65. #define CMDLEN  12      /* max length of command name */
  66.  
  67. struct command {
  68.     char cmd[CMDLEN+1];     /* command string (+1 for NUL terminator) */
  69.     void (*cmdfunc)();      /* pointer to command function */
  70. };
  71.  
  72. static struct command cmdtable[] = {
  73.  
  74.     { "gains",      gain },     /* specify position control gains */
  75.     { "k",          gain },
  76.  
  77.     { "setpoint",   setv },     /* specify position setpoint */
  78.     { "s",          setv },
  79.  
  80.     { "sampletime", setsmp },   /* specify sample time */
  81.     { "t",          setsmp },
  82.  
  83.     { "go",         control },  /* start controller */
  84.     { "g",          control },
  85.  
  86.     { "initialize", init },     /* initialize */
  87.     { "i",          init },
  88.  
  89.     { "help",       menu },     /* print help menu */
  90.     { "h",          menu },
  91.  
  92.     { "exit",       exit },     /* program exit */
  93.     { "e",          exit },
  94.  
  95.     { "\0", (void (*)())NULL }  /* marks end of interpreter table */
  96. };
  97.  
  98.  
  99. /**********************************************************************
  100.                     E N T R Y     R O U T I N E S
  101. **********************************************************************/
  102.  
  103. /*----------------------------------------------------------------------
  104. PROCEDURE
  105.     MAIN  -  this is the main program
  106.  
  107. LAST UPDATE
  108.     1 April 1988
  109.         copy and adapt from main2.c
  110. ----------------------------------------------------------------------*/
  111.  
  112. main()
  113. {
  114.     char cbuf[BUFSIZE];     /* space for the user-typed command line */
  115.  
  116.  
  117.     setmot();               /* set-up motor structures */
  118.  
  119.     menu();                 /* print command list to start. */
  120.  
  121.     for(;;)                 /* an endless loop. */
  122.     {
  123.         fputs(PROMPT, stdout);  /* print a prompt so that user will */
  124.                                 /*   know that the program expects  */
  125.                                 /*   an input.                      */
  126.                                     
  127.         fgets(cbuf, BUFSIZE, stdin);    /* read a NULL terminated string */
  128.                                         /*   from the "standard input"   */
  129.                                         /*   (normally the user console) */
  130.  
  131.         cmdint(cbuf);           /* call the routine that interprets  */
  132.                                 /*   the command line.               */
  133.     }
  134.  
  135. }
  136.  
  137.  
  138. /***********************************************************************
  139.                     P R I V A T E    R O U T I N E S 
  140. ***********************************************************************/
  141.  
  142. /*----------------------------------------------------------------------
  143. PROCEDURE
  144.     CMDINT  -  command interpreter
  145.  
  146. SYNOPSIS
  147.     static void cmdint(cline)
  148.     char cline[];
  149.  
  150. PARAMETERS
  151.     cline  -  command line
  152.  
  153. DESCRIPTION
  154.     The command word is isolated and matched against the list of valid
  155.     commands in the command table.  If a match is found, the routine
  156.     associated with that command is executed.
  157.  
  158. LAST UPDATE
  159.     1 April 1988
  160.         adapt from main2.c
  161. ----------------------------------------------------------------------*/
  162.  
  163. static void cmdint(cline)
  164. char cline[];
  165. {
  166.     char cword[CMDLEN+1];   /* storage to hold the command word. */
  167.     char *prest;            /* ptr to rest of line after command word */
  168.     struct command *pcom;   /* ptr to the command structure list. */
  169.  
  170.  
  171.     prest = getcmd(cline, cword);        /* isolate the command word */
  172.  
  173.     /*-----------------------------------------------------------------
  174.         Search the command list for a match on the command word.
  175.     -----------------------------------------------------------------*/
  176.  
  177.     pcom = cmdtable;    /* initialize the pointer to the command list */
  178.  
  179.     while (pcom->cmd[0] != '\0')        /* while not at end of list */
  180.     {
  181.         /*  note: strcmp() returns zero if strings match  */
  182.  
  183.         if (strcmp(pcom->cmd, cword) == 0)
  184.         {
  185.             /*-----------------------------------------------------
  186.                 Command word matches a name entry in the command
  187.                 interpreter table.  The associated function is
  188.                 executed  --  again, watch the parentheses!
  189.             ------------------------------------------------------*/
  190.  
  191.             (*pcom->cmdfunc)(prest);    /* execute assocaited func */
  192.  
  193.             break;                      /* break out of "while" loop */
  194.         }
  195.  
  196.         pcom++;                         /* not found, next entry */
  197.     }
  198.  
  199.     if (pcom->cmd[0] == '\0')
  200.         printf("command not found : %s\n", cline);
  201.  
  202. }
  203.  
  204.  
  205.  
  206. /*----------------------------------------------------------------------
  207. FUNCTION
  208.     GETCMD  -  isolate command word from input string
  209.  
  210. SYNOPSIS
  211.     static char *getcmd(line, word)
  212.     char *line, *word;
  213.  
  214. PARAMETERS
  215.     line  -  command line
  216.     word  -  where to put the command word
  217.  
  218. RETURNS
  219.     pointer to rest of line (after the command word)
  220.  
  221. DESCRIPTION
  222.     This is similar to the one in main2.c except that it uses pointers
  223.     to address the 2 character arrays instead of array addressing.
  224.  
  225.     See how much simpler it is compared to the array version.
  226.     
  227.     Command words are assumed to be delimited by blanks.  Thus this
  228.     routine finds the first blank-terminated word in the command line
  229.     and copies it into the 'word' argument.
  230.  
  231.     The command line is a NUL terminated character array which is
  232.     passed to this routine in the form of a NUL terminated character 
  233.     array.  Note that the command line CANNOT begin with a blank.
  234.  
  235.     getcmd() returns NULL if the input command line begins with a blank
  236.     or carriage-return.
  237.  
  238. LAST UPDATE
  239.     20 March 1988
  240.         dump leading blanks
  241. ----------------------------------------------------------------------*/
  242.  
  243. static char *getcmd(line, word)
  244. char *line, *word;
  245. {
  246.     int i;          /* loop index */
  247.  
  248.  
  249.     while ((*line == ' ') || (*line == '\t'))  /* skip leading blanks */
  250.         ++line;
  251.  
  252.     for (i = 0; i < CMDLEN; i++)
  253.     {
  254.         if ((*line == ' ') || (*line == '\n') || (*line == '\0'))
  255.             break;      /* end of word found, break out of for loop */
  256.         
  257.         *word++ = *line++;      /* append character to output word */
  258.     }
  259.  
  260.     *word = '\0';   /* terminate command word with a NULL char */
  261.  
  262.     return(line);   /* return a pointer to the remainder of the line */
  263.  
  264. }
  265.  
  266.  
  267.  
  268. /*---------------------------------------------------------------------
  269. PROCEDURE
  270.     MENU  -  print a list of commands
  271.  
  272. SYNOPSIS
  273.     static void menu()
  274.  
  275. LAST UPDATE
  276.     22 September 1984
  277. ----------------------------------------------------------------------*/
  278.  
  279. static void menu()
  280. {
  281.  
  282.     fputs("\n                COMMAND LIST\n\n", stdout);
  283.     fputs("full command (or single-letter equivalent) arguments\n\n", stdout);
  284.  
  285.     fputs("initialize    (i)    delta_t\n", stdout);
  286.     fputs("sampletime    (t)    motor_number tsamp\n", stdout);
  287.     fputs("gains         (k)    motor_number kp ki c\n", stdout);
  288.     fputs("setpoint      (s)    motor_number setpoint\n", stdout);
  289.     fputs("go            (g)    number_of_iterations print?(0/1)\n", stdout);
  290.     fputs("exit          (e) \n", stdout);
  291.     fputs("help          (h) \n\n", stdout);
  292.  
  293. }
  294.  
  295.