00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef NO_STDIO_SUPPORT
00017 #ifndef UNDER_CE
00018 #include <stdio.h>
00019 #include <string.h>
00020 #endif
00021 #include "inetfile.hh"
00022 #include "inifile.h"
00023 #include "rcs_prnt.hh"
00024 #endif
00025 #include <math.h>
00026 #include "amplifier.h"
00027
00028
00029 #ifndef __GNUC__
00030 #ifndef __attribute__
00031 #define __attribute__(x)
00032 #endif
00033 #endif
00034
00035 static char __attribute__((unused)) ident[] = "$Id: amplifier.c,v 1.2 2000/10/27 20:34:42 terrylr Exp $";
00036
00037 #define GAIN_SET 0x01
00038 #define MAX_OUTPUT_CURRENT_SET 0x02
00039 #define LOAD_RESISTANCE_SET 0x04
00040 #define ALL_SET (GAIN_SET | MAX_OUTPUT_CURRENT_SET | LOAD_RESISTANCE_SET)
00041
00042 int amplifierInit(AMPLIFIER_STRUCT * amp)
00043 {
00044
00045 if (0 == amp)
00046 {
00047 return -1;
00048 }
00049
00050 amp->gain = 0.0;
00051 amp->maxOutputCurrent = 0.0;
00052 amp->loadResistance = 1.0;
00053
00054
00055 amp->configured = 0;
00056 amp->enabled = 0;
00057 amp->tripped = 0;
00058
00059 return 0;
00060 }
00061
00062 int amplifierSetGain(AMPLIFIER_STRUCT * amp, double gain)
00063 {
00064 if (0 == amp ||
00065 gain <= 0.0)
00066 {
00067 return -1;
00068 }
00069
00070 amp->gain = gain;
00071 amp->configured |= GAIN_SET;
00072
00073 return 0;
00074 }
00075
00076 double amplifierGetGain(AMPLIFIER_STRUCT * amp)
00077 {
00078 if (0 == amp ||
00079 ! (amp->configured & GAIN_SET))
00080 {
00081 return 0.0;
00082 }
00083
00084 return amp->gain;
00085 }
00086
00087 int amplifierSetMaxOutputCurrent(AMPLIFIER_STRUCT * amp,
00088 double maxOutputCurrent)
00089 {
00090 if (0 == amp ||
00091 maxOutputCurrent <= 0.0)
00092 {
00093 return -1;
00094 }
00095
00096 amp->maxOutputCurrent = maxOutputCurrent;
00097 amp->configured |= MAX_OUTPUT_CURRENT_SET;
00098
00099 return 0;
00100 }
00101
00102 double amplifierGetMaxOutputCurrent(AMPLIFIER_STRUCT * amp)
00103 {
00104 if (0 == amp ||
00105 ! (amp->configured & MAX_OUTPUT_CURRENT_SET))
00106 {
00107 return 0.0;
00108 }
00109
00110 return amp->maxOutputCurrent;
00111 }
00112
00113 int amplifierSetLoadResistance(AMPLIFIER_STRUCT * amp, double loadResistance)
00114 {
00115 if (0 == amp ||
00116 loadResistance <= 0.0)
00117 {
00118 return -1;
00119 }
00120
00121 amp->loadResistance = loadResistance;
00122 amp->configured |= LOAD_RESISTANCE_SET;
00123
00124 return 0;
00125 }
00126
00127 double amplifierGetLoadResistance(AMPLIFIER_STRUCT * amp)
00128 {
00129 if (0 == amp ||
00130 ! (amp->configured & LOAD_RESISTANCE_SET))
00131 {
00132 return 0.0;
00133 }
00134
00135 return amp->loadResistance;
00136 }
00137
00138
00139 int amplifierEnable(AMPLIFIER_STRUCT * amp)
00140 {
00141 if (0 == amp)
00142 {
00143 return -1;
00144 }
00145
00146 if (! amp->tripped &&
00147 amp->configured == ALL_SET)
00148 {
00149 amp->enabled = 1;
00150 }
00151
00152 return 0;
00153 }
00154
00155
00156 int amplifierDisable(AMPLIFIER_STRUCT * amp)
00157 {
00158 if (0 == amp)
00159 {
00160 return -1;
00161 }
00162
00163 amp->enabled = 0;
00164
00165 return 0;
00166 }
00167
00168
00169 int amplifierIsEnabled(AMPLIFIER_STRUCT * amp)
00170 {
00171 if (0 == amp)
00172 {
00173 return -1;
00174 }
00175
00176 return amp->enabled;
00177 }
00178
00179
00180 int amplifierReset(AMPLIFIER_STRUCT * amp)
00181 {
00182 if (0 == amp)
00183 {
00184 return -1;
00185 }
00186
00187 if (amp->configured == ALL_SET)
00188 {
00189 amp->enabled = 0;
00190 amp->tripped = 0;
00191 }
00192
00193 return 0;
00194 }
00195
00196
00197 int amplifierIsTripped(AMPLIFIER_STRUCT * amp)
00198 {
00199 if (0 == amp)
00200 {
00201 return -1;
00202 }
00203
00204 return amp->tripped;
00205 }
00206
00207 double amplifierRunCycle(AMPLIFIER_STRUCT * amp, double input)
00208 {
00209 double output;
00210 double outputCurrent;
00211
00212
00213 if (0 == amp ||
00214 amp->configured != ALL_SET)
00215 {
00216 return 0.0;
00217 }
00218
00219
00220 if (! amp->enabled ||
00221 amp->tripped)
00222 {
00223 return 0.0;
00224 }
00225
00226
00227 output = amp->gain * input;
00228 outputCurrent = fabs(output / amp->loadResistance);
00229
00230
00231 if (outputCurrent > amp->maxOutputCurrent)
00232 {
00233 amp->tripped = 1;
00234 amp->enabled = 0;
00235 output = 0.0;
00236 }
00237
00238 return output;
00239 }
00240
00241 int amplifierIniLoad(AMPLIFIER_STRUCT * amplifier, const char * filename, const char *section)
00242 {
00243 #ifndef NO_STDIO_SUPPORT
00244
00245 AMPLIFIER_STRUCT amp;
00246 int retval = 0;
00247 const char *inistring;
00248
00249 #ifndef UNDER_CE
00250 FILE *fp;
00251
00252 if (NULL == (fp = fopen(filename, "r")))
00253 {
00254 rcs_print_error( "can't open ini file %s\n", filename);
00255 return -1;
00256 }
00257 #else
00258 INET_FILE *fp;
00259
00260 if (NULL == (fp = inet_file_open(filename, "r")))
00261 {
00262 rcs_print_error( "can't open ini file %s\n", filename);
00263 return -1;
00264 }
00265 #endif
00266
00267 memset(&,0,sizeof(amp));
00268 if (NULL != (inistring = iniFind(fp, "AMPLIFIER_GAIN", section)))
00269 {
00270 #ifndef UNDER_CE
00271 if (1 != sscanf((char *) inistring, "%lf", &.gain))
00272 {
00273
00274 rcs_print_error( "invalid GAIN: %s\n", inistring);
00275 retval = -1;
00276 }
00277 else
00278 {
00279 amplifierSetGain(amplifier, amp.gain);
00280 }
00281 #else
00282 amp.gain = RCS_CE_ATOF(inistring);
00283 amplifierSetGain(amplifier, amp.gain);
00284 #endif
00285 }
00286
00287 if (NULL != (inistring = iniFind(fp, "MAX_OUTPUT_CURRENT", section)))
00288 {
00289 #ifndef UNDER_CE
00290 if (1 != sscanf((char *) inistring, "%lf", &.maxOutputCurrent))
00291 {
00292
00293 rcs_print_error( "invalid MAX_OUTPUT_CURRENT: %s\n", inistring);
00294 retval = -1;
00295 }
00296 else
00297 {
00298 amplifierSetMaxOutputCurrent(amplifier, amp.maxOutputCurrent);
00299 }
00300 #else
00301 amp.maxOutputCurrent = RCS_CE_ATOF(inistring);
00302 amplifierSetMaxOutputCurrent(amplifier, amp.maxOutputCurrent);
00303 #endif
00304 }
00305
00306 if (NULL != (inistring = iniFind(fp, "LOAD_RESISTANCE", section)))
00307 {
00308 #ifndef UNDER_CE
00309 if (1 != sscanf((char *) inistring, "%lf", &.loadResistance))
00310 {
00311
00312 rcs_print_error( "invalid LOAD_RESISTANCE: %s\n", inistring);
00313 retval = -1;
00314 }
00315 else
00316 {
00317 amplifierSetLoadResistance(amplifier, amp.loadResistance);
00318 }
00319 #else
00320 amp.loadResistance = RCS_CE_ATOF(inistring);
00321 amplifierSetLoadResistance(amplifier, amp.loadResistance);
00322 #endif
00323 }
00324
00325
00326 #ifndef UNDER_CE
00327 fclose(fp);
00328 #else
00329 inet_file_close(fp);
00330 #endif
00331
00332
00333 return retval;
00334
00335 #else
00336
00337 return -1;
00338
00339 #endif
00340 }