maglev_control.cc

Go to the documentation of this file.
00001 
00008 #include "maglev_control.h"
00009 #include <string.h>
00010 
00011 maglev_control::maglev_control(const char * mod_name) : cd_control_module(mod_name)
00012 {
00013         plog("maglev_control\n");
00014 }
00015 
00016 void maglev_control::initialize_data()
00017 {
00018     
00019     double set_point    = 0.01; 
00020     double rate_gain    = 0.02;
00021     double forward_gain = 630.414316714576;
00022     double i_equilibrium = 3.15319192637756;
00023     
00024     forward_gain_ref    = create_data( NULL, "forward_gain",    sd_type_double, "A/m",  "Forward Gain",     &forward_gain );     
00025     mark_command( forward_gain_ref ); mark_telemetry(forward_gain_ref);
00026     
00027     rate_gain_ref       = create_data( NULL, "rate_gain",       sd_type_double, "A/m",  "Rate Gain" ,       &rate_gain );     
00028     mark_command( rate_gain_ref ); mark_telemetry(rate_gain_ref);
00029     
00030     set_point_ref       = create_data( NULL, "set_point",      sd_type_double, "A/m",  "Set Point",     &set_point );     
00031     mark_command( set_point_ref ); mark_telemetry(set_point_ref);
00032     
00033     i_equilibrium_ref   = create_data( NULL, "i_equilibrium",      sd_type_double, "A/m",  "Equilibrium current",     &i_equilibrium );     
00034     mark_command( i_equilibrium_ref ); mark_telemetry(i_equilibrium_ref);
00035 
00036         
00037         // Request events
00038         request_event_notification( NULL, "MAGLEV_CONTROL_TRIGGER", 1 );
00039 }
00040 
00041 void maglev_control::initialize_data_requests()
00042 {
00043     i_ref           = request_data( NULL, "current_sim",    sd_type_double, true, "A", "Current" );
00044     position_ref    = request_data( NULL, "position_sim",   sd_type_double, true, "", "Position" );
00045     velocity_ref    = request_data( NULL, "velocity_sim",   sd_type_double, true, "", "Velocity" );
00046 
00047 }
00048 
00049 void maglev_control::handle_event(int code)
00050 {
00051         if (code == 1)
00052         {
00053         int valid[6];
00054                 
00055                 double k_f, k_r, i_eq, x_eq, x, v;
00056                 
00057         // Make sure that all incoming data is valide
00058                 int k = 0;
00059         
00060                 valid[k++] = value_as_type( forward_gain_ref,   sd_type_double, &k_f);
00061                 valid[k++] = value_as_type( rate_gain_ref,      sd_type_double, &k_r);
00062                 valid[k++] = value_as_type( set_point_ref,      sd_type_double, &x_eq);
00063                 valid[k++] = value_as_type( i_equilibrium_ref,  sd_type_double, &i_eq);
00064                 valid[k++] = value_as_type( position_ref,       sd_type_double, &x);
00065                 valid[k++] = value_as_type( velocity_ref,       sd_type_double, &v);
00066                 
00067         
00068                 int ok = valid[0];
00069                 
00070                 for ( int j = 1; j < k; j++ )
00071                         ok = ok && valid[j];
00072                 
00073                 if( !ok )
00074                 {
00075                         int k = 0;
00076                         if( !valid[k++] ) plog("forward_gain is not valid\n"); 
00077                         if( !valid[k++] ) plog("rate_gain is not valid\n"); 
00078                         if( !valid[k++] ) plog("set_point is not valid\n"); 
00079                         if( !valid[k++] ) plog("i_equilibrium is not valid\n"); 
00080                         if( !valid[k++] ) plog("position  is not valid\n"); 
00081                         if( !valid[k++] ) plog("velocity  is not valid\n"); 
00082                         return;
00083                 }
00084         
00085         // Compute the control
00086         double x_err = x - x_eq;
00087         double i = k_f*(x_err + k_r*v) + i_eq;
00088  
00089         // Send to the simulation
00090         set_value_as_type( i_ref,     sd_type_double, &i    ); 
00091 
00092         }
00093 }
00094 
00095 cd_control_module *maglev_control_builder(const char *name)
00096 {
00097     return new maglev_control(name);
00098 }
 All Classes Files Functions Variables