home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
-
- FILE
- cntrl0.c - d.c. servo speed control example, control code
-
- ROUTINES
- control - execute control algorithm
- gain - interpret command line for P gain
- setv - interpret command line for setpoint
- getv - get velocity reading
- mact - send controller output to actuator
-
- REMARKS
- implements simple P control of d.c. motor speed.
-
- LAST UPDATE
- 18 March 1985 by Haam
- 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>
-
- #include "envir.h" /* environment definitions */
- #include "dash8.h" /* declarations for dash8.c */
- #include "dac2.h" /* declarations for dac2.c */
- #include "cntrl0.h" /* exported declarations for this module */
-
-
- /***********************************************************************
- 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.
-
- This section only declares functions that are local to this
- module; functions visible outside this module are already
- declared in the header file cntrl0.h #include'd above.
-
- ***********************************************************************/
-
- #ifdef ANSI
-
- double getv(void);
- void mact(double);
-
- #else
-
- double getv();
- void mact();
-
- #endif
-
-
- /***********************************************************************
- P R I V A T E D A T A V A R I A B L E 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 ADCHANNEL 0 /* A/D channel that we will use */
- #define DACHANNEL 0 /* D/A channel used by this module */
-
- static double vref = 1000.0; /* velocity setpoint */
- static double kp = 1.0; /* proportional gain */
- static double vcon = 0.0; /* constant term in the controller */
- static double mmin; /* lower limit for controller output */
- static double mmax; /* upper limit for controller output */
-
-
- /***********************************************************************
- 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.
-
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- CONTROL - implements P control algorithm
-
- SYNOPSIS
- void control(cline)
- char *cline;
-
- PARAMETERS
- cline - pointer to user's argument string
-
- LAST UPDATE
- 15 October 1984
- ----------------------------------------------------------------------*/
-
- void control(cline)
- char *cline;
- {
- long nitr; /* number of iterations */
- long i; /* iteration counter */
- double v; /* the motor velocity */
- double m; /* controller output */
- int damin, damax; /* output argument range of D/A */
- int prnt; /* 1 = print control output, 0 = no print */
-
- /*---------------------------------------------------------------
- Get number of iterations and print flag from the command
- line, cline. Sscanf() is just like scanf() except that the
- input is a string instead of the standard input (keyboard).
- -----------------------------------------------------------------*/
-
- sscanf(cline, "%ld %d", &nitr, &prnt);
-
- printf("number of iterations = %ld\n", nitr); /* verify */
-
- da_limits(&damin, &damax); /* find D/A output limits */
-
- mmin = (double)damin;
- mmax = (double)damax;
-
- for (i = 1; i <= nitr; i++) /* main iteration loop */
- {
- v = getv(); /* get motor velocity */
-
- m = kp * (vref - v) + vcon; /* the control equation */
-
- if (m > mmax) /* limit value of m to allowable */
- m = mmax; /* d/a and actuator range. */
- else if (m < mmin)
- m = mmin;
-
- if (prnt)
- printf("v = %f, m = %f\n", v, m);
-
- mact(m); /* send controller output to the actuator. */
-
- #ifdef SIMRT
- tstep(); /* time simulation: step ahead one step in time */
- #endif
-
- } /* end of main iteration loop. */
-
- }
-
-
-
- /*----------------------------------------------------------------------
- PROCEDURE
- GAIN - interpret the input line specifying controller gain(s)
-
- SYNOPSIS
- void gain(cline)
- char *cline;
-
- PARAMETERS
- cline - input line
-
- LAST UPDATE
- 15 October 1984
- ----------------------------------------------------------------------*/
-
- void gain(cline)
- char *cline;
- {
-
- sscanf(cline, "%lf %lf", &kp, &vcon); /* decode input line */
-
- #if DEBUG
- printf("<gain> kp = %lf, vcon = %lf\n", kp, vcon);
- #endif
-
- }
-
-
-
- /*----------------------------------------------------------------------
- PROCEDURE
- SETV - get velocity setpoint from the command line
-
- SYNOPSIS
- void setv(cline)
- char *cline;
-
- PARAMETERS
- cline - user's command line
-
- LAST UPDATE
- 15 October 1984 by Haam
- ----------------------------------------------------------------------*/
-
- void setv(cline)
- char *cline;
- {
-
- sscanf(cline,"%lf", &vref);
-
- #if DEBUG
- printf("<setv> vref = %lf\n", vref);
- #endif
-
- }
-
-
-
- /***********************************************************************
-
- The following section contains routines which interface between
- high level control code and functions which perform the hardware
- dependent functions of operating the A/D and D/A. As such, they
- are necessarily dependent on the device driver interface.
-
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- INIT - system initialization
-
- SYNOPSIS
- void init()
-
- REMARKS
- Performs any initializations required.
-
- LAST UPDATE
- 1 December 1987
- ----------------------------------------------------------------------*/
-
- void init(cline)
- char *cline;
- {
-
- ad_init(); /* initialize A/D */
- da_init(); /* initialize D/A */
-
- #ifdef SIMRT
- sim_init(cline); /* initialize simulation module */
- #endif
-
- }
-
-
-
- /*---------------------------------------------------------------------
- FUNCTION
- GETV - returns motor velocity
-
- SYNOPSIS
- static double getv()
-
- RETURNS
- current motor velocity (units unspecified)
-
- REMARKS
- Reads channel 0 of the A/D converter, convert the value to a
- double type and return this converted value.
-
- LAST UPDATE
- 15 October 1984
- ---------------------------------------------------------------------*/
-
- static double getv()
- {
-
- return((double)ad_wread(ADCHANNEL));
- }
-
-
-
- /*----------------------------------------------------------------------
- PROCEDURE
- MACT - send controller output to actuator
-
- SYNOPSIS
- static void mact(value)
- double value;
-
- PARAMETER
- value - controller output
-
- REMARKS
- Convert output to integer and send it to D/A converter
-
- LAST UPDATE
- 15 October 1984
- ----------------------------------------------------------------------*/
-
- static void mact(value)
- double value;
- {
-
- da_write(DACHANNEL, (int)value);
- }
-
-