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

  1. /**********************************************************************
  2.  
  3. FILE
  4.     rtsim0.c  -  d.c. speed control, simulation module
  5.  
  6. ENTRY ROUTINES
  7.     tstep      -  advance one time step
  8.  
  9.     ad_init    -  simulates A/D initialization
  10.     ad_start   -  simulates starting an A/D read
  11.     ad_read    -  simulates reading A/D value
  12.     ad_wread   -  simulates read from A/D
  13.  
  14.     da_init    -  simulates D/A initialization
  15.     da_limits  -  returns simulation D/A output limits
  16.     da_write   -  simulates write to D/A
  17.  
  18. PRIVATE ROUTINE
  19.     simul   -  motor plant simulation
  20.  
  21. INITIALIZATION ROUTINE
  22.     sim_init   -  initialize simulation
  23.  
  24. REMARKS
  25.     This module simulates the interface and motor hardware and the
  26.     passage of "real" time.  This module must thus provide the same
  27.     interface functions as the modules controlling the hardware.
  28.  
  29.     A simulation allows algorithms to be debugged and fine-tuned
  30.     without the need for an actual hardware setup.
  31.  
  32. LAST UPDATE
  33.     1 December 1987
  34.         restructure and modify for MSC 4.00 and 5.00
  35.  
  36.     Copyright(C) 1984-1987  D.M. Auslander and C.H. Tham
  37.  
  38. **********************************************************************/
  39.  
  40. /***********************************************************************
  41.                             I M P O R T S
  42. ***********************************************************************/
  43.  
  44. #include "envir.h"          /* environment specifications */
  45.  
  46.  
  47. /***********************************************************************
  48.              F O R W A R D    D E C L A R A T I O N S
  49. ***********************************************************************/
  50.  
  51. #ifdef ANSI
  52.  
  53. void simul(void);
  54.  
  55. #else
  56.  
  57. void simul();
  58.  
  59. #endif
  60.  
  61.  
  62. /**********************************************************************
  63.                  P R I V A T E    D A T A
  64. **********************************************************************/
  65.  
  66. #define ADSCALE       512.0     /* scale speed to A/D units */
  67. #define ADMAX        2047       /* max A/D input value */
  68. #define ADMIN       -2048       /* min A/D input value */
  69.  
  70. #define DASCALE      2047.0     /* scale D/A value to torque */
  71. #define DAMIN       -2048       /* min. output value accepted */
  72. #define DAMAX        2047       /* max. output value accepted */
  73.  
  74. static double inertia = 1.0;        /* motor system inertia */
  75. static double speed;                /* simulated motor velocity */
  76. static double t;                    /* time */
  77. static double dt;                   /* step size in time */
  78. static double torque = 0.0;         /* motor torque */
  79.  
  80.  
  81. /***********************************************************************
  82.   E N T R Y    R O U T I N E S    -    T I M E    S I M U L A T I O N
  83. ***********************************************************************/
  84.  
  85. /*---------------------------------------------------------------------
  86. PROCEDURE
  87.     TSTEP  -  advance time forward one step
  88.  
  89. SYNOPSIS
  90.     tstep()
  91.  
  92. LAST UPDATE
  93.     10 October 1984
  94. ---------------------------------------------------------------------*/
  95.  
  96. tstep()
  97. {
  98.  
  99.     simul();        /* compute one time step in the differential */
  100.                     /*   equation for the motor system.      */
  101.  
  102.     t += dt;        /* increment time by dt */
  103.  
  104. }
  105.  
  106.  
  107.  
  108. /***********************************************************************
  109.    E N T R Y    R O U T I N E S    -    A / D    S I M U L A T I O N
  110.  
  111.     These routines dummies the routines in dash8.c which drives the
  112.     Metrabyte DASH8 A/D board.  The channel parameter is ignored.
  113.  
  114. ***********************************************************************/
  115.  
  116. void ad_init()
  117. {
  118.  
  119.     speed = 0.0;
  120.  
  121. }
  122.  
  123.  
  124. void ad_start(channel)
  125. int channel;
  126. {
  127.     /* nothing */
  128. }
  129.  
  130.  
  131. int ad_read(channel)
  132. int channel;
  133. {
  134.     int rval;                   /* return value */
  135.  
  136.  
  137.     rval = ADSCALE * speed;     /* scale and convert to int type */
  138.  
  139.     if (rval > ADMAX)           /* apply converter limits */
  140.         rval = ADMAX;
  141.     else if (rval < ADMIN)
  142.         rval = ADMIN;
  143.  
  144.     return(rval);
  145. }
  146.  
  147.  
  148. int ad_wread(channel)
  149. int channel;
  150. {
  151.  
  152.     return(ad_read(channel));
  153. }
  154.  
  155.  
  156.  
  157. /***********************************************************************
  158.    E N T R Y    R O U T I N E S    -    D / A    S I M U L A T I O N
  159.  
  160.     These routines dummies the routines in dac2.c which drives the
  161.     Metrabyte DAC02 D/A board.  The channel parameter is ignored.
  162.  
  163.     The DAC2 is a 12-bit digital-to-analog converter configured for
  164.     bipolar output.  Thus, the output parameter values range from
  165.     -2048 to 2047.  Only lower 12 bits will be used, treating those
  166.     12 bits as a 2's complement signed number.  Thus if the output
  167.     value is negative, the lower 12 bits will be preserved and all
  168.     higher order bits will be converted to 1's.
  169.  
  170.     Note the use of ~0xfff instead of 0xf000, this makes the program
  171.     more portable across machines with different integer sizes.
  172.  
  173. ***********************************************************************/
  174.  
  175. void da_init()
  176. {
  177.  
  178.     torque = 0.0;
  179.  
  180. }
  181.  
  182.  
  183. void da_limits(lo, hi)
  184. int *lo, *hi;
  185. {
  186.  
  187.     *lo = DAMIN;
  188.     *hi = DAMAX;
  189.  
  190. }
  191.  
  192.  
  193. void da_write(channel, value)
  194. int channel, value;
  195. {
  196.  
  197.     if (value < 0)          /* OR with a binary number having 0's for */
  198.         value |= ~0xfff;    /*   the lower 12 bits and 1's elsewhere  */
  199.     else 
  200.         value &= 0xfff;     /* for positive values, just save the     */
  201.                             /*   lower 12 bits.                       */
  202.  
  203.     torque = value / DASCALE;   /* rescale for motor drive torque. */
  204.  
  205. }
  206.  
  207.  
  208.  
  209. /**********************************************************************
  210.                    P R I V A T E    R O U T I N E S
  211.  
  212.     The following routines are declared "static".  These routines
  213.     can be directly accessed only by routines inside this file; they
  214.     are not "visible" to routines outside this file.  Hence they are
  215.     private to this file.  Private routines enhances data security
  216.     and helps avoid unexpected name conflicts in large programming
  217.     projects.
  218.  
  219. ***********************************************************************/
  220.  
  221. /*----------------------------------------------------------------------
  222. PROCEDURE
  223.     SIMUL  -  simulates motor plant
  224.  
  225. SYNOPSIS
  226.     static void simul()
  227.  
  228. REMARKS
  229.     Solve the motor system differential equation.  This version uses 
  230.     the Euler method for integration; it isn't very efficient, but its 
  231.     easy to program!
  232.  
  233. LAST UPDATE
  234.     10 October 1984
  235. ----------------------------------------------------------------------*/
  236.  
  237. static void simul()
  238. {
  239.  
  240.     speed += torque * dt / inertia;     /* pure inertial load */
  241.  
  242. }
  243.  
  244.  
  245.  
  246. /***********************************************************************
  247.           I N I T I A L I Z A T I O N    R O U T I N E
  248. ***********************************************************************/
  249.  
  250. /*----------------------------------------------------------------------
  251. PROCEDURE
  252.     INIT  -  system initialization
  253.  
  254. SYNOPSIS
  255.     sim_init(cline)
  256.     char *cline;
  257.  
  258. PARAMETER
  259.     cline  -  user's input command line
  260.  
  261. REMARKS
  262.     In a "real" real-time program, this function would be used to set 
  263.     up interrupts, set clock modes, etc.  In this simulation version,
  264.     it sets time to zero and sets the motor simulation initial conditions.
  265.  
  266. LAST UPDATE
  267.     18 October 1984
  268. ----------------------------------------------------------------------*/
  269.  
  270. sim_init(cline)
  271. char *cline;
  272. {
  273.  
  274.     sscanf(cline, "%lf", &dt);  /* get the step size. */
  275.  
  276. #if DEBUG
  277.     printf("<init> dt = %lf\n", dt);
  278. #endif
  279.  
  280.     t = 0.0;                    /* reset time */
  281.  
  282.     speed = 0.0;                /* motor is initially at rest */
  283.     torque = 0.0;
  284.  
  285. }
  286.  
  287.  
  288.