home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
-
- FILE
- main3.c - d.c. motor velocity control, executive control file #3
-
- ROUTINES
- main - main program
- cmdint - command interpreter
- fstr - extract command word
- help - print list of commands
-
- REMARKS
- This version is intended for multi-motor control, to be
- used with modules cntrl3.c and rtsim3.c.
-
- LAST UPDATE
- 19 March 1985
- recast in module format
- 01 April 1988
- copy over from main2.c and adapt for multiple motors
-
- 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 library function declarations */
- #include <string.h> /* standard string function declarations */
-
- #include "envir.h" /* environment definitions */
- #include "dash8.h" /* declarations for dash8.c */
- #include "dac2.h" /* declarations for dac2.c */
- #include "time0.h" /* declarations for time0.c */
- #include "cntrl3.h" /* declarations for cntrl3.c */
-
- /***********************************************************************
- F O R W A R D D E C L A R A T I O N S
- ***********************************************************************/
-
- #ifdef ANSI
-
- void cmdint(char *);
- char * getcmd(char *, char *);
- void menu(void);
-
- #else
-
- void cmdint();
- char getcmd();
- void menu();
-
- #endif
-
- /**********************************************************************
- P R I V A T E D A T A
- **********************************************************************/
-
- #define PROMPT "READY " /* defines the command processor prompt */
- #define BUFSIZE 100 /* command input buffer size */
-
- #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 */
- };
-
- static struct command cmdtable[] = {
-
- { "gains", gain }, /* specify position control gains */
- { "k", gain },
-
- { "setpoint", setv }, /* specify position setpoint */
- { "s", setv },
-
- { "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 },
-
- { "\0", (void (*)())NULL } /* marks end of interpreter table */
- };
-
-
- /**********************************************************************
- E N T R Y R O U T I N E S
- **********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- MAIN - this is the main program
-
- LAST UPDATE
- 1 April 1988
- copy and adapt from main2.c
- ----------------------------------------------------------------------*/
-
- main()
- {
- char cbuf[BUFSIZE]; /* space for the user-typed command line */
-
-
- setmot(); /* set-up motor structures */
-
- 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 April 1988
- adapt from main2.c
- ----------------------------------------------------------------------*/
-
- 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. */
-
-
- 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 */
-
- while (pcom->cmd[0] != '\0') /* while not at end of list */
- {
- /* 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 "while" loop */
- }
-
- pcom++; /* not found, next entry */
- }
-
- if (pcom->cmd[0] == '\0')
- 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
- This is similar to the one in main2.c except that it uses pointers
- to address the 2 character arrays instead of array addressing.
-
- See how much simpler it is compared to the array version.
-
- 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; /* loop index */
-
-
- while ((*line == ' ') || (*line == '\t')) /* skip leading blanks */
- ++line;
-
- for (i = 0; i < CMDLEN; i++)
- {
- if ((*line == ' ') || (*line == '\n') || (*line == '\0'))
- break; /* end of word found, break out of for loop */
-
- *word++ = *line++; /* append character to output word */
- }
-
- *word = '\0'; /* terminate command word with a NULL char */
-
- return(line); /* return a pointer to the remainder of the 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) motor_number tsamp\n", stdout);
- fputs("gains (k) motor_number kp ki c\n", stdout);
- fputs("setpoint (s) motor_number 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);
-
- }
-
-