Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

simctrlintf.cc

Go to the documentation of this file.
00001 
00002 
00003 #include "simctrlintf.h"
00004 #include "simmot_n.h"
00005 #include "inifile.h"
00006 
00007 extern "C" {
00008 #include <gtk/gtk.h>
00009 #include "sc_interface.h"
00010 #include "sc_support.h"
00011 }
00012 
00013 static AMPLIFIER_STRUCT amplifier[SIM_MAX_AXIS];
00014 static double ampOutput[SIM_MAX_AXIS];
00015 static DC_MOTOR_STRUCT dcmotor[SIM_MAX_AXIS];
00016 static ENCODER_STRUCT encoder[SIM_MAX_AXIS];
00017 
00018 enum TORQUE_UNITS_TYPE_ENUM
00019 {
00020   TORQUE_UNITS_INVALID = 0, 
00021   N_M, 
00022   OZ_IN,
00023   LB_FT
00024  };
00025 
00026 enum TORQUE_UNITS_TYPE_ENUM torque_units[SIM_MAX_AXIS];
00027 
00028 struct widget_pointers
00029 {
00030   GtkWidget *AxisSpinbutton;
00031   GtkWidget *AmpGainSpinbutton;
00032   GtkWidget *AmpMaxOutSpinbutton;
00033   GtkWidget *AmpLoadResistanceSpinbutton;
00034   GtkWidget *RaSpinbutton;
00035   GtkWidget *LaSpinbutton;
00036   GtkWidget *KbSpinbutton;
00037   GtkWidget *JmSpinbutton;
00038   GtkWidget *BmSpinbutton;
00039   GtkWidget *countsPerRevSpinbutton;
00040 };
00041 
00042 static widget_pointers *wp=NULL;
00043 
00044 extern GtkWidget *SimCtrlWindow;
00045 
00046 static void initwp()
00047 {
00048   wp = new widget_pointers();
00049   wp->AxisSpinbutton = lookup_widget(SimCtrlWindow,"AxisSpinbutton");
00050   wp->AmpGainSpinbutton = lookup_widget(SimCtrlWindow,"AmpGainSpinbutton");
00051   wp->AmpMaxOutSpinbutton = lookup_widget(SimCtrlWindow,"AmpMaxOutSpinbutton");
00052   wp->AmpLoadResistanceSpinbutton = lookup_widget(SimCtrlWindow,"AmpLoadResistanceSpinbutton");
00053   wp->RaSpinbutton = lookup_widget(SimCtrlWindow,"RaSpinbutton");
00054   wp->LaSpinbutton = lookup_widget(SimCtrlWindow,"LaSpinbutton");
00055   wp->KbSpinbutton = lookup_widget(SimCtrlWindow,"KbSpinbutton");
00056   wp->JmSpinbutton = lookup_widget(SimCtrlWindow,"JmSpinbutton");
00057   wp->BmSpinbutton = lookup_widget(SimCtrlWindow,"BmSpinbutton");
00058   wp->BmSpinbutton = lookup_widget(SimCtrlWindow,"BmSpinbutton");
00059   wp->countsPerRevSpinbutton = lookup_widget(SimCtrlWindow,"countsPerRevSpinbutton");
00060 };
00061 
00062 
00063 int loadSimulationIniFile(const char *filename)
00064 {
00065   if(NULL == wp)
00066     {
00067       initwp();
00068     }
00069   int t,retval;
00070   char sectionString[INIFILE_MAX_LINELEN];
00071 
00072   for (t = 0; t < SIM_MAX_AXIS; t++)
00073     {
00074       sprintf(sectionString, "AXIS_%d", t);
00075 
00076       if (0 != amplifierIniLoad(&amplifier[t], filename, sectionString))
00077         {
00078           retval = -1;
00079         }
00080       ampOutput[t] = 0.0;
00081 
00082       if (0 != dcmotorIniLoad(&dcmotor[t], filename, sectionString))
00083         {
00084           retval = -1;
00085         }
00086 
00087       if (0 != encoderIniLoad(&encoder[t], filename, sectionString))
00088         {
00089           retval = -1;
00090         }
00091     }
00092   int axis = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(wp->AxisSpinbutton));
00093   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->AmpGainSpinbutton),amplifier[axis].gain);
00094   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->AmpMaxOutSpinbutton),amplifier[axis].maxOutputCurrent);
00095   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->AmpLoadResistanceSpinbutton),amplifier[axis].loadResistance);
00096   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->RaSpinbutton),dcmotor[axis].Ra);
00097   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->LaSpinbutton),dcmotor[axis].La);
00098   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->KbSpinbutton),dcmotor[axis].Kb);
00099   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->JmSpinbutton),dcmotor[axis].Jm);
00100   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->BmSpinbutton),dcmotor[axis].Bm);
00101   gtk_spin_button_set_value(GTK_SPIN_BUTTON(wp->countsPerRevSpinbutton),encoder[axis].countsPerRev);
00102 
00103 }
00104 
00105 
00106 
00107 int saveSimulationIniFile(const char *filename)
00108 {
00109 #if 0
00110 /*
00111   Taken from dumpAxis in iniaxis.cc but modified to do Simulation stuff instead.
00112 
00113   dumpSimAxis(int axis, const char *filename, EMC_AXIS_STAT *stat)
00114 
00115   Takes the file name for the ini file, makes a backup copy, opens the
00116   backup copy, then overwrites the original with a line-by-line version
00117   of the original, parsing the lines for ones it understands and
00118   replacing the values with the current ones.
00119  */
00120   char ourAxisSection[256];
00121   int ourAxis = 0;
00122   FILE *infp = NULL;
00123   FILE *outfp = NULL;
00124   char line[256];
00125   char section[256];
00126   char var[256], val[256];
00127   char fmt[256];
00128   char *inistring;
00129 
00130   // rename with backup suffix
00131   strcpy(line, filename);
00132   strcat(line, ".bak");
00133   if (0 != rename(filename, line)) {
00134     fprintf(stderr, "can't make backup copy of INI file %s\n", filename);
00135     return -1;
00136   }
00137 
00138   // open backup for reading
00139   if (NULL == (infp = fopen(line, "r"))) {
00140     fprintf(stderr, "can't open backup copy of INI file %s\n", line);
00141     return -1;
00142   }
00143 
00144   // open original for writing
00145   if (NULL == (outfp = fopen(filename, "w"))) {
00146     fprintf(stderr, "can't open original copy of INI file %s\n", line);
00147     return -1;
00148   }
00149 
00150   int ourSection=0;
00151   int axis = 0;
00152 
00153   while (!feof(infp)) {
00154     if (NULL == fgets(line, 256, infp)) {
00155       break;
00156     }
00157 
00158     if (iniIsSection(line, section)) {
00159       // if this is "AXIS_0,1,...", it's what we want
00160       if (!strncmp(section, "AXIS_",5)) {
00161         ourSection = 1;
00162         axis = section[5]-'0';
00163         if(axis > 8 || axis < 0)
00164           {
00165             axis = 0;
00166             ourSection = 0;
00167           }
00168         /* look up torque units */
00169         if (NULL != (inistring = iniFind(infp, "TORQUE_UNITS", section)))
00170           {
00171             /* found the entry in the ini file */
00172             if (!strcmp(inistring, "N_M"))
00173               {
00174                 torque_units = N_M;
00175               }
00176             else if (!strcmp(inistring, "LB_FT"))
00177               {
00178                 torque_units = LB_FT;
00179               }
00180             else if (!strcmp(inistring, "OZ_IN"))
00181               {
00182                 torque_units = OZ_IN;
00183               }
00184             else
00185               {
00186                 torque_units = TORQUE_UNITS_INVALID;
00187                 rcs_print_error( "bad torque units specified in ini file: %s\n",
00188                                  inistring);
00189                 retval = -1;
00190               }
00191           }
00192   }
00193   else
00194   {
00195     /* not in ini file-- use default */
00196     torque_units = N_M;
00197   }
00198       }
00199       else {
00200         ourSection = 0;
00201       }
00202     }
00203 
00204     if (ourSection) {
00205       if (iniIsEntry(line, var, val)) {
00206         if (!strcmp(var, "AMPLIFIER_GAIN")) {
00207           iniFormatFloat(fmt, var, val);
00208           fprintf(outfp, fmt, amplifier[axis].gain);
00209           continue;             // avoid fputs() below, since we wrote it
00210         }
00211         else if (!strcmp(var, "MAX_OUTPUT_CURRENT")) {
00212           iniFormatFloat(fmt, var, val);
00213           fprintf(outfp, fmt, amplifier[axis].maxOutputCurrent);
00214           continue;             // avoid fputs() below, since we wrote it
00215         }
00216         else if (!strcmp(var, "LOAD_RESISTANCE")) {
00217           iniFormatFloat(fmt, var, val);
00218           fprintf(outfp, fmt, amplifier[axis].loadResistance);
00219           continue;             // avoid fputs() below, since we wrote it
00220         }
00221         else if (!strcmp(var, "ARMATURE_RESISTANCE")) {
00222           iniFormatFloat(fmt, var, val);
00223           fprintf(outfp, fmt, dcmotor[axis].Ra);
00224           continue;             // avoid fputs() below, since we wrote it
00225         }
00226         else if (!strcmp(var, "ARMATURE_INDUCTANCE")) {
00227           iniFormatFloat(fmt, var, val);
00228           fprintf(outfp, fmt, dcmotor[axis].La);
00229           continue;             // avoid fputs() below, since we wrote it
00230         }
00231         else if (!strcmp(var, "BACK_EMF_CONSTANT")) {
00232           iniFormatFloat(fmt, var, val);
00233           fprintf(outfp, fmt, dcmotor[axis].Kb);
00234           continue;             // avoid fputs() below, since we wrote it
00235         }       
00236         else if (!strcmp(var, "ROTOR_INERTIA")) {
00237           iniFormatFloat(fmt, var, val);
00238           fprintf(outfp, fmt, dcmotor[axis].Jm);
00239           continue;             // avoid fputs() below, since we wrote it
00240         }
00241         else if (!strcmp(var, "DAMPING_FRICTION_COEFFICIENT")) {
00242           iniFormatFloat(fmt, var, val);
00243           fprintf(outfp, fmt, dcmotor[axis].Bm);
00244           continue;             // avoid fputs() below, since we wrote it
00245         }
00246       }
00247     }
00248 
00249     // write it out
00250     fputs(line, outfp);
00251   }
00252 
00253   fclose(infp);
00254   fclose(outfp);
00255 #endif  
00256   return 0;
00257 
00258 }
00259 
00260 
00261 
00262 
00263 

Generated on Sun Dec 2 15:27:43 2001 for EMC by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001