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

  1. /***********************************************************************
  2.  
  3. FILE
  4.     main2.c  -  d.c. motor speed control example, main program module 2
  5.  
  6. ROUTINES
  7.     main    -  main program
  8.     cmdint  -  command processor
  9.     fstr    -  extract command word
  10.     help    -  print list of commands
  11.  
  12. REMARKS
  13.     This module is similar to main1.c but has a slightly better
  14.     command interpreter.  This particular example is designed for the
  15.     cascade1.c control module of chapter 4 example programs.
  16.  
  17.     To generate the simulation version of the example program:
  18.          cl /DSIMRT main2.c cascade1.c rtsim2.c
  19.  
  20. LAST UPDATE
  21.     18 January 1988
  22.         minor tidying-up
  23.  
  24.     Copyright (c) 1984-1988  D.M. Auslander and C.H. Tham
  25.  
  26. ***********************************************************************/
  27.  
  28. /***********************************************************************
  29.                             I M P O R T S
  30. ***********************************************************************/
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>     /* standard ANSI library function declarations */
  34. #include <string.h>     /* standard string function declarations */
  35.  
  36. #include "envir.h"      /* program environment declarations */
  37. #include "cascade1.h"   /* cascade control definitions */
  38.  
  39. /***********************************************************************
  40.              F O R W A R D    D E C L A R A T I O N S
  41. ***********************************************************************/
  42.  
  43. #ifdef ANSI
  44.  
  45. static void     cmdint(char *);
  46. static char *   getcmd(char *, char *);
  47. static void     menu(void);
  48.  
  49. #else
  50.  
  51. void    cmdint();
  52. char    getcmd();
  53. void    menu();
  54.  
  55. #endif
  56.  
  57. /***********************************************************************
  58.     P R I V A T E    D A T A     A N D    D E C L A R A T I O N S
  59. ***********************************************************************/
  60.  
  61. #define PROMPT  "READY "    /* defines the command processor prompt */
  62. #define BUFSIZE 100         /* command input buffer size */
  63.  
  64. /*-----------------------------------------------------------------
  65.     The following declares a data structure for associating a
  66.     command string with the routine that acts on the contents
  67.     of that command string.  This is done using a C construct
  68.     known as a "struct" (record to you Pascal enthusiasts).
  69.  
  70.     The structure has 2 fields:
  71.  
  72.         1) the command name in the form of a NUL terminated string
  73.         2) a pointer to the associated function
  74. -----------------------------------------------------------------*/
  75.  
  76. #define CMDLEN  12      /* max length of command name */
  77.  
  78. struct command {
  79.  
  80.     char cmd[CMDLEN+1]; /* command string (+1 for NUL terminator) */
  81.  
  82.     void (*cmdfunc)();  /* pointer to command function.  Note the  */
  83.                         /*   parentheses in the declaration - they */
  84.                         /*   are very important and must be placed */
  85.                         /*   exactly right! (See K & R, page 209). */
  86. };
  87.  
  88.  
  89. /*----------------------------------------------------------------------
  90.     This is the command interpreter table, an array of commands and
  91.     their associated function pointers.  Note that commands have a
  92.     long and a short form.
  93.  
  94.     The interpreter table is an array of structures.  Each structure
  95.     in the array is initialized with a command name and an associated
  96.     function pointer (function name used without brackets).
  97.  
  98.     The function pointers must be declared ahead so that the compiler
  99.     knows that the names refer to functions and not data variables,
  100.     and assigns the proper function addresses (entry points).
  101. ----------------------------------------------------------------------*/
  102.  
  103. static struct command cmdtable[] = {
  104.  
  105.     { "posgains",   pgain },        /* specify position control gains */
  106.     { "pk",         pgain },
  107.  
  108.     { "velgains",   vgain },        /* specify velocity control gains */
  109.     { "vk",         vgain },
  110.  
  111.     { "setpoint",   setpos },       /* specify position setpoint */
  112.     { "s",          setpos },
  113.  
  114.     { "sampletime", setsmp },       /* specify sample time */
  115.     { "t",          setsmp },
  116.  
  117.     { "go",         control },      /* start controller */
  118.     { "g",          control },
  119.  
  120.     { "initialize", init },         /* initialize */
  121.     { "i",          init },
  122.  
  123.     { "help",       menu },         /* print help menu */
  124.     { "h",          menu },
  125.  
  126.     { "exit",       exit },         /* program exit */
  127.     { "e",          exit }
  128. };
  129.  
  130. static int ncmds;       /* number of entries in command table */
  131.  
  132. /***********************************************************************
  133.                     M A I N    P R O G R A M
  134. ***********************************************************************/
  135.  
  136. /*----------------------------------------------------------------------
  137. PROCEDURE
  138.     MAIN  -  this is the main program
  139.  
  140. REMARKS
  141.     Note how the number of entries in the command table is determined
  142.     through the use of the sizeof() operator.
  143.  
  144. LAST UPDATE
  145.     18 January 1988
  146.         minor tidy up
  147. ----------------------------------------------------------------------*/
  148.  
  149. main()
  150. {
  151.     char cbuf[BUFSIZE];     /* space for the user-typed command line */
  152.  
  153.  
  154.     ncmds = sizeof(cmdtable) / sizeof(struct command);
  155.  
  156.     menu();                 /* print command list to start. */
  157.  
  158.     for(;;)                 /* an endless loop. */
  159.     {
  160.         fputs(PROMPT, stdout);  /* print a prompt so that user will */
  161.                                 /*   know that the program expects  */
  162.                                 /*   an input.                      */
  163.                                     
  164.         fgets(cbuf, BUFSIZE, stdin);    /* read a NULL terminated string */
  165.                                         /*   from the "standard input"   */
  166.                                         /*   (normally the user console) */
  167.  
  168.         cmdint(cbuf);           /* call the routine that interprets  */
  169.                                 /*   the command line.               */
  170.     }
  171.  
  172. }
  173.  
  174.  
  175. /***********************************************************************
  176.                     P R I V A T E    R O U T I N E S 
  177. ***********************************************************************/
  178.  
  179. /*----------------------------------------------------------------------
  180. PROCEDURE
  181.     CMDINT  -  command interpreter
  182.  
  183. SYNOPSIS
  184.     static void cmdint(cline)
  185.     char cline[];
  186.  
  187. PARAMETERS
  188.     cline  -  command line
  189.  
  190. DESCRIPTION
  191.     The command word is isolated and matched against the list of valid
  192.     commands in the command table.  If a match is found, the routine
  193.     associated with that command is executed.
  194.  
  195. LAST UPDATE
  196.     1 December 1987
  197. ----------------------------------------------------------------------*/
  198.  
  199. static void cmdint(cline)
  200. char cline[];
  201. {
  202.     char cword[CMDLEN+1];   /* storage to hold the command word. */
  203.     char *prest;            /* ptr to rest of line after command word */
  204.     struct command *pcom;   /* ptr to the command structure list. */
  205.     int i;                  /* iteration counter */
  206.  
  207.  
  208.     prest = getcmd(cline, cword);        /* isolate the command word */
  209.  
  210.     /*-----------------------------------------------------------------
  211.         Search the command list for a match on the command word.
  212.     -----------------------------------------------------------------*/
  213.  
  214.     pcom = cmdtable;    /* initialize the pointer to the command list */
  215.  
  216.     for (i = 0; i < ncmds; i++)
  217.     {
  218.         /*  note: strcmp() returns zero if strings match  */
  219.  
  220.         if (strcmp(pcom->cmd, cword) == 0)
  221.         {
  222.             /*-----------------------------------------------------
  223.                 Command word matches a name entry in the command
  224.                 interpreter table.  The associated function is
  225.                 executed  --  again, watch the parentheses!
  226.             ------------------------------------------------------*/
  227.  
  228.             (*pcom->cmdfunc)(prest);    /* execute assocaited func */
  229.  
  230.             break;                      /* break out of "for" loop */
  231.         }
  232.  
  233.         pcom++;                         /* not found, next entry */
  234.     }
  235.  
  236.     if (i >= ncmds)     /* did not match any of the valid commands */
  237.     {
  238.         printf("command not found : %s\n", cline);
  239.     }
  240.  
  241. }
  242.  
  243.  
  244.  
  245. /*----------------------------------------------------------------------
  246. FUNCTION
  247.     GETCMD  -  isolate command word from input string
  248.  
  249. SYNOPSIS
  250.     static char *getcmd(line, word)
  251.     char line[], word[];
  252.  
  253. PARAMETERS
  254.     line  -  command line
  255.     word  -  where to put the command word
  256.  
  257. RETURNS
  258.     pointer to rest of line (after the command word)
  259.  
  260. DESCRIPTION
  261.     Command words are assumed to be delimited by blanks.  Thus this
  262.     routine finds the first blank-terminated word in the command line
  263.     and copies it into the 'word' argument.
  264.  
  265.     The command line is a NUL terminated character array which is
  266.     passed to this routine in the form of a NUL terminated character 
  267.     array.  Note that the command line CANNOT begin with a blank.
  268.  
  269.     getcmd() returns NULL if the input command line begins with a blank
  270.     or carriage-return.
  271.  
  272. LAST UPDATE
  273.     20 March 1988
  274.         dump leading blanks
  275. ----------------------------------------------------------------------*/
  276.  
  277. static char *getcmd(line, word)
  278. char line[], word[];
  279. {
  280.     int i;      /* index into line[] */
  281.     int j;      /* index into word[] */
  282.  
  283.  
  284.     for (i = 0; (line[i] == ' ') || (line[i] == '\t'); i++)
  285.         ;               /* skip leading spaces and tabs */
  286.  
  287.     for (j = 0; j < CMDLEN; j++, i++)
  288.     {
  289.         if ((line[i] == ' ') || (line[i] == '\n') || (line[i] == '\0'))
  290.             break;      /* end of word found, get out of loop */
  291.     
  292.         word[j] = line[i];      /* copy the character to word[] */
  293.     }
  294.  
  295.     word[j] = '\0';     /* terminate command word with NUL character */
  296.  
  297.     return(&line[i]);   /* return a pointer to the remainder of line */
  298.  
  299. }
  300.  
  301.  
  302.  
  303. /*---------------------------------------------------------------------
  304. PROCEDURE
  305.     MENU  -  print a list of commands
  306.  
  307. SYNOPSIS
  308.     static void menu()
  309.  
  310. LAST UPDATE
  311.     22 September 1984
  312. ----------------------------------------------------------------------*/
  313.  
  314. static void menu()
  315. {
  316.  
  317.     fputs("\n                COMMAND LIST\n\n", stdout);
  318.     fputs("full command (or single-letter equivalent) arguments\n\n", stdout);
  319.  
  320.     fputs("initialize    (i)    delta_t\n", stdout);
  321.     fputs("sampletime    (t)    tsamp\n", stdout);
  322.     fputs("posgains      (pk)   kp ki kd\n", stdout);
  323.     fputs("velgains      (vk)   kp ki kd\n", stdout);
  324.     fputs("setpoint      (s)    position setpoint\n", stdout);
  325.     fputs("go            (g)    number_of_iterations print(0/1)\n", stdout);
  326.     fputs("exit          (e) \n", stdout);
  327.     fputs("help          (h) \n\n", stdout);
  328.  
  329. }
  330.  
  331.