home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP1 / MAIN0.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-05  |  7.1 KB  |  231 lines

  1. /***********************************************************************
  2.  
  3. FILE
  4.     main0.c  -  d.c. motor speed control example, main program module
  5.  
  6. ROUTINES
  7.     main    -  main program
  8.     cmdint  -  command processor
  9.  
  10. REMARKS
  11.     This program module, together with cntrl0.c and rtsim0.c implements
  12.     simple control of the speed of a d.c. motor.  This is the main
  13.     program module and it implements a command processor.  The other
  14.     two module implements the P control algorithm (cntrl0.c) and a
  15.     simulation of the motor set-up (rtsim0.c).
  16.  
  17.     To generate the simulation program under Microsoft C version 4.00
  18.     and above, type: cl main0.c cntrl0.c rtsim0.c
  19.  
  20. LAST UPDATE
  21.     18 March 1985
  22.         recast in module form
  23.     1 December 1987
  24.         restructure and modify for MSC 4.00 and 5.00
  25.  
  26.     Copyright(C) 1984-1987  D.M. Auslander and C.H. Tham
  27.  
  28. ***********************************************************************/
  29.  
  30. /***********************************************************************
  31.                            I M P O R T S
  32.  
  33.     This section contains ALL the declarations from other modules
  34.     required in this program.  Such "imports" are gathered here
  35.     instead of scattered all over this file to make them easier
  36.     to find and reduces the chance of introducing bugs through
  37.     inconsistent multiple declarations of the same item.
  38.  
  39. ***********************************************************************/
  40.  
  41. #include <stdio.h>      /* standard C header file */
  42.  
  43. #include "envir.h"      /* program environment declarations */
  44. #include "cntrl0.h"     /* extern declarations for cntrl0.c */
  45.  
  46.  
  47. /***********************************************************************
  48.     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
  49.  
  50.     These variables and declarations are visible to all routines in
  51.     this file.  Variables declared as "static" are not visible
  52.     outside of this file.  In computer jargon, their "name space" is
  53.     restricted to this file.  This enhances data security by preventing
  54.     routines outside this file from inadvertently modidifying these
  55.     values though accidental use of an external variable with the
  56.     same name.
  57.  
  58. ***********************************************************************/
  59.  
  60. #define PROMPT "-"      /* defines the command processor prompt */
  61.  
  62.  
  63. /***********************************************************************
  64.              F O R W A R D    D E C L A R A T I O N S
  65.  
  66.     In C, functions are assumed to return an integer unless declared
  67.     otherwise.  Here, the return type of all functions which do not
  68.     return integers (or anything at all) are declared.  The symbol
  69.     string "ANSI" is used by the C macro processor's conditional
  70.     compilation feature to enable "function prototyping" for
  71.     compilers that support this proposed ANSI C feature.
  72.  
  73.     The statement below declares cmdint() and menu() to be functions
  74.     which do not return a value.  If the symbol "ANSI" is defined
  75.     anywhere prior to the #ifdef statement, function prototyping
  76.     is used as an added consistency check.
  77.     
  78. ***********************************************************************/
  79.  
  80. #ifdef ANSI
  81.  
  82. void cmdint(char *);
  83. void menu(void);
  84.  
  85. #else
  86.  
  87. void cmdint();
  88. void menu();
  89.  
  90. #endif
  91.  
  92.  
  93. /***********************************************************************
  94.                   E N T R Y    R O U T I N E S
  95.  
  96.     Entry routines are those that can be called directly by functions
  97.     outside this module or file.  Here, we have only one entry
  98.     routine, the main() function.
  99.  
  100. ***********************************************************************/
  101.  
  102. main()
  103. {
  104.     char cbuf[100];     /* space for the user-typed command line */
  105.  
  106.  
  107.     menu();             /* print menu of commands */
  108.  
  109.     for (;;)            /* an endless loop */
  110.     {
  111.         fputs(PROMPT, stdout);      /* print a prompt */
  112.  
  113.         fgets(cbuf, 100, stdin);    /* get command line from user */
  114.  
  115.         cmdint(cbuf);               /* interpret and execute commands */
  116.     }
  117. }
  118.  
  119.  
  120.  
  121. /**********************************************************************
  122.                    P R I V A T E    R O U T I N E S
  123.  
  124.     The following routines are declared "static".  These routines
  125.     can be directly accessed only by routines inside this file; they
  126.     are not "visible" to routines outside this file.  Hence they are
  127.     private to this file.  Private routines help avoid name conflicts
  128.     in large programming projects.
  129.  
  130. ***********************************************************************/
  131.  
  132. /*----------------------------------------------------------------------
  133. PROCEDURE
  134.     CMDINT  -  command interpreter
  135.  
  136. SYNOPSIS
  137.     static void cmdint(cline)
  138.     char cline[];
  139.  
  140. PARAMETER
  141.     cline  -  command line in the form of an array of characters
  142.  
  143. REMARKS
  144.     The first character of the command string, cline[0], specifies the
  145.     command.  The rest of the command line is passed to the specific
  146.     "command function" as &cline[1], which is the address of the rest
  147.     of the line.
  148.  
  149.     Recognized commands are:
  150.  
  151.         k  -  controller gains
  152.         s  -  set point
  153.         i  -  initialize the system
  154.         g  -  start (i.e. go)
  155.         ?  -  help
  156.  
  157. LAST UPDATE
  158.     18 March 1985
  159. ----------------------------------------------------------------------*/
  160.  
  161. static void cmdint(cline)
  162. char cline[];         /* command string is terminated by NULL char */
  163. {
  164.  
  165.  
  166.     switch (cline[0])
  167.     {
  168.         case 'k':               /* set controller gains; remainder */
  169.                                 /*  of the command line is passed  */
  170.             gain(&cline[1]);    /*  to the function gain() for     */
  171.             break;              /*  decoding.              */
  172.  
  173.         case 's':               /* set the setpoint */
  174.  
  175.             setv(&cline[1]);
  176.             break;
  177.  
  178.         case 'i':               /* initialize the system */
  179.  
  180.             init(&cline[1]);
  181.             break;
  182.  
  183.         case 'g':               /* "go," i.e., start control. */
  184.  
  185.             control(&cline[1]);
  186.             break;
  187.  
  188.         case 'e':               /* return to operating system. */
  189.  
  190.             exit(0);
  191.  
  192.         case '?':               /* print list of commands */
  193.         case 'h':
  194.  
  195.             menu();
  196.             break;
  197.  
  198.         default:                /* unrecognized commands */
  199.  
  200.             printf("command not found: %s", cline);
  201.     }
  202.  
  203. }
  204.  
  205.  
  206.  
  207. /*----------------------------------------------------------------------
  208. PROCEDURE
  209.     MENU  -  print a reminder of commands
  210.  
  211. SYNOPSIS
  212.     static void menu()
  213.  
  214. LAST UPDATE
  215.     17 November 1987
  216. ----------------------------------------------------------------------*/
  217.  
  218. static void menu()
  219. {
  220.  
  221.     printf("k <P gain> <constant term>    -  control gains\n");
  222.     printf("s <speed reference>           -  speed reference\n");
  223.     printf("i <delta t>                   -  sampling time\n");
  224.     printf("g <# of iterations> <print each iteration>  -  start\n");
  225.     printf("h                             -  print menu of commands\n");
  226.     printf("e                             -  exit program\n");
  227.  
  228. }
  229.  
  230.  
  231.