home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Car / Motor.m < prev    next >
Encoding:
Text File  |  1992-06-23  |  4.5 KB  |  174 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import <appkit/nextstd.h>
  5. #import "Car_main.h"
  6. #import "Motor.h"
  7. #import "Transmission.h"
  8. #import "defs.h"
  9. #import "DataView.h"
  10. #import "Battery.h"
  11. #import "Cycle.h"
  12.  
  13. @implementation Motor
  14.  
  15. - init
  16. {
  17.     [super init];
  18.     motor = self;
  19.     return self;
  20. }
  21.  
  22. - read:(NXTypedStream *)stream
  23. {
  24.     [super read:stream];
  25.     NXReadTypes(stream,"fffff",&crossover,&efficiency,&mass,&max,®enerationEfficiency);
  26.     NXReadArray(stream,"f",6,highCoefficients);
  27.     NXReadArray(stream,"f",6,lowCoefficients);
  28.     return self;
  29. }
  30.  
  31. - write:(NXTypedStream *)stream
  32. {
  33.     [super write:stream];
  34.     NXWriteTypes(stream,"fffff",&crossover,&efficiency,&mass,&max,®enerationEfficiency);
  35.     NXWriteArray(stream,"f",6,highCoefficients);
  36.     NXWriteArray(stream,"f",6,lowCoefficients);
  37.     return self;
  38. }
  39.  
  40. - (float)crossover
  41. {
  42.     return crossover;
  43. }
  44.  
  45. - setCrossover:(float)aNumber
  46. {
  47.     crossover = aNumber;
  48.     return self;
  49. }
  50.  
  51. - (float)efficiency
  52. {
  53.     return efficiency;
  54. }
  55.  
  56. - setEfficiency:(float)aNumber
  57. {
  58.     efficiency = aNumber;
  59.     return self;
  60. }
  61.  
  62. - (float *)highCoefficients
  63. {
  64.     return highCoefficients;
  65. }
  66.  
  67. - setHighCoefficients:(float *)coefficients
  68. {
  69. int i;
  70.  
  71.     for ( i = 0 ; i < 6 ; i++ )
  72.         highCoefficients[i] = coefficients[i];
  73.     return self;
  74. }
  75.  
  76. - (float *)lowCoefficients
  77. {
  78.     return lowCoefficients;
  79. }
  80.  
  81. - setLowCoefficients:(float *)coefficients
  82. {
  83. int i;
  84.  
  85.     for ( i = 0 ; i < 6 ; i++ )
  86.         lowCoefficients[i] = coefficients[i];
  87.     return self;
  88. }
  89.  
  90. - (float)mass
  91. {
  92.     return mass;
  93. }
  94.  
  95. - setMass:(float)aNumber
  96. {
  97.     mass = aNumber;
  98.     return self;
  99. }
  100.  
  101. - (float)max
  102. {
  103.     return max;
  104. }
  105.  
  106. - setMax:(float)aNumber
  107. {
  108.     max = aNumber;
  109.     return self;
  110. }
  111.  
  112. - (float)regenerationEfficiency
  113. {
  114.     return regenerationEfficiency;
  115. }
  116.  
  117. - setRegenerationEfficiency:(float)aNumber
  118. {
  119.     regenerationEfficiency = aNumber;
  120.     return self;
  121. }
  122.  
  123. /******************************************************************************************************************************
  124.  *    This is actually the only useful part of this object.  It is called when the simulation is run.   Note that the motor   *
  125.  *    Will hand out as much negative (stopping) power is asked for.  This is definitely not right.                            *
  126.  *    On second thought, this whole method needs to be rewritten.                                                             *
  127.  ******************************************************************************************************************************/
  128. - powerRequired:(float)power
  129. {
  130. float angVelocity;
  131. float torqueRequired;
  132. float torqueAvailable;
  133. float powerToRequest;
  134. float powerAvailable;
  135. float outputTorque;
  136. float rpm;
  137.     
  138.     angVelocity = [transmission inputSpeed];
  139.     rpm = angVelocity * 60 / (2 * PI);
  140.  
  141.     if ( angVelocity == 0 )        // This is just a kludge we're using because some formulae won't work with the
  142.         angVelocity = 0.01;        // velocity 0. ( That is, the car is trying to accelerate from a standstill. )
  143.                     // It is difficult to calculate a power with no velocity.
  144.     if ( power > 0 )
  145.         {
  146.         torqueRequired = power / angVelocity;                        // Calculate the torque required
  147.         if ( rpm <= crossover )                                // Calculate available torque
  148.             torqueAvailable = evaluatePolynomial(lowCoefficients,6,rpm );          // Remember the map is in rpm.
  149.         else 
  150.             torqueAvailable = evaluatePolynomial(highCoefficients,6,rpm);
  151.         powerToRequest = MIN(torqueAvailable,torqueRequired) * angVelocity / efficiency; 
  152.         powerAvailable = [battery powerRequested:powerToRequest forTime:[cycle timeStep]];  // Check with battery
  153.         outputTorque = powerAvailable * efficiency / angVelocity;            // Determine output torque
  154.         }
  155.     else if ( power < 0 )
  156.         {
  157.         powerToRequest = power * regenerationEfficiency;    // Regeneration going on with efficiency.
  158.         [battery powerRequested:powerToRequest forTime:[cycle timeStep]];    // Send power to battery
  159.         outputTorque = power / angVelocity;    //All the negative power needed will be given.
  160.         }
  161.     else if ( power == 0 )
  162.         {
  163.         outputTorque = 0;
  164.         }
  165.  
  166. /******************************************************************************************************************************
  167.  *    Here is where power is passed on to the transmission.                                                                   *
  168.  ******************************************************************************************************************************/
  169.     [transmission motorInput:outputTorque];
  170.     return self;
  171. }
  172.  
  173. @end
  174.