home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
-
- FILE
- main2.c - d.c. motor speed control example, main program module 2
-
- ROUTINES
- main - main program
- cmdint - command processor
- fstr - extract command word
- help - print list of commands
-
- REMARKS
- This module is similar to main1.c but has a slightly better
- command interpreter. This particular example is designed for the
- cascade1.c control module of chapter 4 example programs.
-
- To generate the simulation version of the example program:
- cl /DSIMRT main2.c cascade1.c rtsim2.c
-
- LAST UPDATE
- 18 January 1988
- minor tidying-up
-
- Copyright (c) 1984-1988 D.M. Auslander and C.H. Tham
-
- ***********************************************************************/
-
- /***********************************************************************
- I M P O R T S
- ***********************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h> /* standard ANSI library function declarations */
- #include <string.h> /* standard string function declarations */
-
- #include "envir.h" /* program environment declarations */
- #include "cascade1.h" /* cascade control definitions */
-
- /***********************************************************************
- F O R W A R D D E C L A R A T I O N S
- ***********************************************************************/
-
- #ifdef ANSI
-
- static void cmdint(char *);
- static char * getcmd(char *, char *);
- static void menu(void);
-
- #else
-
- void cmdint();
- char getcmd();
- void menu();
-
- #endif
-
- /***********************************************************************
- 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
- ***********************************************************************/
-
- #define PROMPT "READY " /* defines the command processor prompt */
- #define BUFSIZE 100 /* command input buffer size */
-
- /*-----------------------------------------------------------------
- The following declares a data structure for associating a
- command string with the routine that acts on the contents
- of that command string. This is done using a C construct
- known as a "struct" (record to you Pascal enthusiasts).
-
- The structure has 2 fields:
-
- 1) the command name in the form of a NUL terminated string
- 2) a pointer to the associated function
- -----------------------------------------------------------------*/
-
- #define CMDLEN 12 /* max length of command name */
-
- struct command {
-
- char cmd[CMDLEN+1]; /* command string (+1 for NUL terminator) */
-
- void (*cmdfunc)(); /* pointer to command function. Note the */
- /* parentheses in the declaration - they */
- /* are very important and must be placed */
- /* exactly right! (See K & R, page 209). */
- };
-
-
- /*----------------------------------------------------------------------
- This is the command interpreter table, an array of commands and
- their associated function pointers. Note that commands have a
- long and a short form.
-
- The interpreter table is an array of structures. Each structure
- in the array is initialized with a command name and an associated
- function pointer (function name used without brackets).
-
- The function pointers must be declared ahead so that the compiler
- knows that the names refer to functions and not data variables,
- and assigns the proper function addresses (entry points).
- ----------------------------------------------------------------------*/
-
- static struct command cmdtable[] = {
-
- { "posgains", pgain }, /* specify position control gains */
- { "pk", pgain },
-
- { "velgains", vgain }, /* specify velocity control gains */
- { "vk", vgain },
-
- { "setpoint", setpos }, /* specify position setpoint */
- { "s", setpos },
-
- { "sampletime", setsmp }, /* specify sample time */
- { "t", setsmp },
-
- { "go", control }, /* start controller */
- { "g", control },
-
- { "initialize", init }, /* initialize */
- { "i", init },
-
- { "help", menu }, /* print help menu */
- { "h", menu },
-
- { "exit", exit }, /* program exit */
- { "e", exit }
- };
-
- static int ncmds; /* number of entries in command table */
-
- /***********************************************************************
- M A I N P R O G R A M
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- MAIN - this is the main program
-
- REMARKS
- Note how the number of entries in the command table is determined
- through the use of the sizeof() operator.
-
- LAST UPDATE
- 18 January 1988
- minor tidy up
- ----------------------------------------------------------------------*/
-
- main()
- {
- char cbuf[BUFSIZE]; /* space for the user-typed command line */
-
-
- ncmds = sizeof(cmdtable) / sizeof(struct command);
-
- menu(); /* print command list to start. */
-
- for(;;) /* an endless loop. */
- {
- fputs(PROMPT, stdout); /* print a prompt so that user will */
- /* know that the program expects */
- /* an input. */
-
- fgets(cbuf, BUFSIZE, stdin); /* read a NULL terminated string */
- /* from the "standard input" */
- /* (normally the user console) */
-
- cmdint(cbuf); /* call the routine that interprets */
- /* the command line. */
- }
-
- }
-
-
- /***********************************************************************
- P R I V A T E R O U T I N E S
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- CMDINT - command interpreter
-
- SYNOPSIS
- static void cmdint(cline)
- char cline[];
-
- PARAMETERS
- cline - command line
-
- DESCRIPTION
- The command word is isolated and matched against the list of valid
- commands in the command table. If a match is found, the routine
- associated with that command is executed.
-
- LAST UPDATE
- 1 December 1987
- ----------------------------------------------------------------------*/
-
- static void cmdint(cline)
- char cline[];
- {
- char cword[CMDLEN+1]; /* storage to hold the command word. */
- char *prest; /* ptr to rest of line after command word */
- struct command *pcom; /* ptr to the command structure list. */
- int i; /* iteration counter */
-
-
- prest = getcmd(cline, cword); /* isolate the command word */
-
- /*-----------------------------------------------------------------
- Search the command list for a match on the command word.
- -----------------------------------------------------------------*/
-
- pcom = cmdtable; /* initialize the pointer to the command list */
-
- for (i = 0; i < ncmds; i++)
- {
- /* note: strcmp() returns zero if strings match */
-
- if (strcmp(pcom->cmd, cword) == 0)
- {
- /*-----------------------------------------------------
- Command word matches a name entry in the command
- interpreter table. The associated function is
- executed -- again, watch the parentheses!
- ------------------------------------------------------*/
-
- (*pcom->cmdfunc)(prest); /* execute assocaited func */
-
- break; /* break out of "for" loop */
- }
-
- pcom++; /* not found, next entry */
- }
-
- if (i >= ncmds) /* did not match any of the valid commands */
- {
- printf("command not found : %s\n", cline);
- }
-
- }
-
-
-
- /*----------------------------------------------------------------------
- FUNCTION
- GETCMD - isolate command word from input string
-
- SYNOPSIS
- static char *getcmd(line, word)
- char line[], word[];
-
- PARAMETERS
- line - command line
- word - where to put the command word
-
- RETURNS
- pointer to rest of line (after the command word)
-
- DESCRIPTION
- Command words are assumed to be delimited by blanks. Thus this
- routine finds the first blank-terminated word in the command line
- and copies it into the 'word' argument.
-
- The command line is a NUL terminated character array which is
- passed to this routine in the form of a NUL terminated character
- array. Note that the command line CANNOT begin with a blank.
-
- getcmd() returns NULL if the input command line begins with a blank
- or carriage-return.
-
- LAST UPDATE
- 20 March 1988
- dump leading blanks
- ----------------------------------------------------------------------*/
-
- static char *getcmd(line, word)
- char line[], word[];
- {
- int i; /* index into line[] */
- int j; /* index into word[] */
-
-
- for (i = 0; (line[i] == ' ') || (line[i] == '\t'); i++)
- ; /* skip leading spaces and tabs */
-
- for (j = 0; j < CMDLEN; j++, i++)
- {
- if ((line[i] == ' ') || (line[i] == '\n') || (line[i] == '\0'))
- break; /* end of word found, get out of loop */
-
- word[j] = line[i]; /* copy the character to word[] */
- }
-
- word[j] = '\0'; /* terminate command word with NUL character */
-
- return(&line[i]); /* return a pointer to the remainder of line */
-
- }
-
-
-
- /*---------------------------------------------------------------------
- PROCEDURE
- MENU - print a list of commands
-
- SYNOPSIS
- static void menu()
-
- LAST UPDATE
- 22 September 1984
- ----------------------------------------------------------------------*/
-
- static void menu()
- {
-
- fputs("\n COMMAND LIST\n\n", stdout);
- fputs("full command (or single-letter equivalent) arguments\n\n", stdout);
-
- fputs("initialize (i) delta_t\n", stdout);
- fputs("sampletime (t) tsamp\n", stdout);
- fputs("posgains (pk) kp ki kd\n", stdout);
- fputs("velgains (vk) kp ki kd\n", stdout);
- fputs("setpoint (s) position setpoint\n", stdout);
- fputs("go (g) number_of_iterations print(0/1)\n", stdout);
- fputs("exit (e) \n", stdout);
- fputs("help (h) \n\n", stdout);
-
- }
-
-