home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
-
- FILE
- main0.c - d.c. motor speed control example, main program module
-
- ROUTINES
- main - main program
- cmdint - command processor
-
- REMARKS
- This program module, together with cntrl0.c and rtsim0.c implements
- simple control of the speed of a d.c. motor. This is the main
- program module and it implements a command processor. The other
- two module implements the P control algorithm (cntrl0.c) and a
- simulation of the motor set-up (rtsim0.c).
-
- To generate the simulation program under Microsoft C version 4.00
- and above, type: cl main0.c cntrl0.c rtsim0.c
-
- LAST UPDATE
- 18 March 1985
- recast in module form
- 1 December 1987
- restructure and modify for MSC 4.00 and 5.00
-
- Copyright(C) 1984-1987 D.M. Auslander and C.H. Tham
-
- ***********************************************************************/
-
- /***********************************************************************
- I M P O R T S
-
- This section contains ALL the declarations from other modules
- required in this program. Such "imports" are gathered here
- instead of scattered all over this file to make them easier
- to find and reduces the chance of introducing bugs through
- inconsistent multiple declarations of the same item.
-
- ***********************************************************************/
-
- #include <stdio.h> /* standard C header file */
-
- #include "envir.h" /* program environment declarations */
- #include "cntrl0.h" /* extern declarations for cntrl0.c */
-
-
- /***********************************************************************
- 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
-
- These variables and declarations are visible to all routines in
- this file. Variables declared as "static" are not visible
- outside of this file. In computer jargon, their "name space" is
- restricted to this file. This enhances data security by preventing
- routines outside this file from inadvertently modidifying these
- values though accidental use of an external variable with the
- same name.
-
- ***********************************************************************/
-
- #define PROMPT "-" /* defines the command processor prompt */
-
-
- /***********************************************************************
- F O R W A R D D E C L A R A T I O N S
-
- In C, functions are assumed to return an integer unless declared
- otherwise. Here, the return type of all functions which do not
- return integers (or anything at all) are declared. The symbol
- string "ANSI" is used by the C macro processor's conditional
- compilation feature to enable "function prototyping" for
- compilers that support this proposed ANSI C feature.
-
- The statement below declares cmdint() and menu() to be functions
- which do not return a value. If the symbol "ANSI" is defined
- anywhere prior to the #ifdef statement, function prototyping
- is used as an added consistency check.
-
- ***********************************************************************/
-
- #ifdef ANSI
-
- void cmdint(char *);
- void menu(void);
-
- #else
-
- void cmdint();
- void menu();
-
- #endif
-
-
- /***********************************************************************
- E N T R Y R O U T I N E S
-
- Entry routines are those that can be called directly by functions
- outside this module or file. Here, we have only one entry
- routine, the main() function.
-
- ***********************************************************************/
-
- main()
- {
- char cbuf[100]; /* space for the user-typed command line */
-
-
- menu(); /* print menu of commands */
-
- for (;;) /* an endless loop */
- {
- fputs(PROMPT, stdout); /* print a prompt */
-
- fgets(cbuf, 100, stdin); /* get command line from user */
-
- cmdint(cbuf); /* interpret and execute commands */
- }
- }
-
-
-
- /**********************************************************************
- P R I V A T E R O U T I N E S
-
- The following routines are declared "static". These routines
- can be directly accessed only by routines inside this file; they
- are not "visible" to routines outside this file. Hence they are
- private to this file. Private routines help avoid name conflicts
- in large programming projects.
-
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- CMDINT - command interpreter
-
- SYNOPSIS
- static void cmdint(cline)
- char cline[];
-
- PARAMETER
- cline - command line in the form of an array of characters
-
- REMARKS
- The first character of the command string, cline[0], specifies the
- command. The rest of the command line is passed to the specific
- "command function" as &cline[1], which is the address of the rest
- of the line.
-
- Recognized commands are:
-
- k - controller gains
- s - set point
- i - initialize the system
- g - start (i.e. go)
- ? - help
-
- LAST UPDATE
- 18 March 1985
- ----------------------------------------------------------------------*/
-
- static void cmdint(cline)
- char cline[]; /* command string is terminated by NULL char */
- {
-
-
- switch (cline[0])
- {
- case 'k': /* set controller gains; remainder */
- /* of the command line is passed */
- gain(&cline[1]); /* to the function gain() for */
- break; /* decoding. */
-
- case 's': /* set the setpoint */
-
- setv(&cline[1]);
- break;
-
- case 'i': /* initialize the system */
-
- init(&cline[1]);
- break;
-
- case 'g': /* "go," i.e., start control. */
-
- control(&cline[1]);
- break;
-
- case 'e': /* return to operating system. */
-
- exit(0);
-
- case '?': /* print list of commands */
- case 'h':
-
- menu();
- break;
-
- default: /* unrecognized commands */
-
- printf("command not found: %s", cline);
- }
-
- }
-
-
-
- /*----------------------------------------------------------------------
- PROCEDURE
- MENU - print a reminder of commands
-
- SYNOPSIS
- static void menu()
-
- LAST UPDATE
- 17 November 1987
- ----------------------------------------------------------------------*/
-
- static void menu()
- {
-
- printf("k <P gain> <constant term> - control gains\n");
- printf("s <speed reference> - speed reference\n");
- printf("i <delta t> - sampling time\n");
- printf("g <# of iterations> <print each iteration> - start\n");
- printf("h - print menu of commands\n");
- printf("e - exit program\n");
-
- }
-
-