controller_mass.cpp 11 KB
/* 
Copyright 2013-2016 Robotics and Biology Lab, TU Berlin. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.  
*/



/*
 * Mass flow controller: actuate channel according to a signal:
 *
 *
 * Author: Raphael
 */
#include "global.hpp"
#include "controller.hpp"
#include <iostream>
#include <math.h>


namespace AirserverController{

  CntrlMass::CntrlMass(MsgConfigurationControllerMass *conf)
  {
  
    //std::cout << "configuring mass controller" << std::endl;
    id = conf->header_config.id;
    
    config = *conf; //copy everything
    
    this->error= 0.0;
    
    desired_mass = 0.0;

    SetData( config.out_massobserver_term_friction, 0.0 );
    SetData( config.out_massobserver_term_injector, 0.0 );
    SetData( config.out_massobserver,                 0.0 );

//        std::cout <<"[MASS CTRL] Coeff: " << std::endl
//                  <<"  inflation path: " << config.c_inflate
//                              << ", " << config.c_inflate_friction << ", "
//                              << config.c_inflate_injector << ", " << config.c_inflate_Psupply << ", "
//                              << config.c_inflate_Pout   << std::endl;
//       std::cout <<"  deflation path: " << config.c_deflate
//                              << ", " << config.c_deflate_friction << ", "
//                              << config.c_deflate_injector << ", " << config.c_deflate_Psupply << ", "
//                              << config.c_deflate_Pout   << std::endl;
//        std::cout <<" inflation threshold: " << config.c_inflation_threshold  << std::endl;
//        std::cout <<" deflation threshold: " << config.c_deflation_threshold  << std::endl;

 }

  float CntrlMass::MFLinearModel(float Pi, float Po, float scale) {
    float dp= (Pi - Po);
    float mf= scale * dp;
    return mf;
  }
   
  float CntrlMass::MFLeeCompanyModel(float Pi, float Po, float scale) {
    // absolute pressure ratio
    float ratio= Pi/Po;
    float mf= 0.0;
    
    float dp= (Pi - Po);
    if (dp < 0.0) {dp = 0.0;} //to avoid strange behavior

    if(ratio >= 1.9) { // sonic
      mf= scale * Pi;   
    }else{ //subsonic
      mf= 2 * scale * sqrt(dp*Po); 
    }

    return mf;

    // scaling factor includes:
    // non linear model: (injector equation)
    // R= 2 K f_t sqrt(dP P_1) / Q
    // constants K and f_t depending on units
    // for flow Q and temperature T
    // for Q in kg/60s:      K  = 0.0481
    // for T= 20deg Celsius: f_t= 1
    // P1: absolute pressure in front of resistor
    // P2: absolute pressure behind resistor
    //     P2= pressure_out + pressure environment
    // dP= P1 - P2
    // Q= massflow [kg/s]= 2 * 0.0481 * 1 * sqrt(dP*P_2)/ R*60
 
  }

  float CntrlMass::MFPSIModel(float Pi, float Po, float scale) {
    float ratio= Po/Pi;

    // The ratio of specific heats at constant pressure 
    // and constant volume, c_p / c_vv
    float gamma = 1.4; // for dry air
    float critical_pressure_ratio= pow(2.0/(gamma+1.0), gamma/(gamma-1.0));
    //std::cout << "[MASSFLOW CONTROLLER] critical pressure ratio: "
    //          << critical_pressure_ratio << std::endl; 
    float mf= 0.0;

    if(ratio > 0.999) {
      // This should actually never happen for the soft actuator.
      // For inflation supply pressure would reach channel pressure.
      // For deflation, assuming an abbsolute supply pressure near 300kPa+103kPa,
      // the measured channel pressure would need to be 0.103 kPa.
      // That equals a complete deflation that can be "manually" handeld
      // on the client side, knowing that the channel pressure will be Zero.

      //std::cout << "[MASSFLOW CONTROLLER] PSI Model: inlet pressure ("
      //          << Pi << ") equals outlet pressure ("<< Po 
      //          << "). Massflow= zero." << std::endl;
    } else {
      if(ratio >= critical_pressure_ratio) {
        // 0.528 < ratio < 0.999
        mf= scale * Pi * sqrt(2.0/ (gamma-1.0) ) * sqrt(pow(ratio,(2.0/gamma)) - pow(ratio,(gamma+1.0/gamma)) );
      } else {
        // ratio < 0.528
        mf= scale * Pi * sqrt( pow(critical_pressure_ratio,(gamma+1.0)/gamma) );
      } 
    }// ratio > 0.999

    return mf;
  }


  int CntrlMass::run() {

    if (gotDeactivated) { //on deactivation, make sure no valves stay open:
        out_inflation_valvestate = 0.0;
        out_deflation_valvestate = 0.0;
        SetData(config.out_inflate, out_inflation_valvestate);
        SetData(config.out_deflate, out_deflation_valvestate);
    }

    if(!activated){   
        //reset all internal states: 
        this->error= 0.0;
        desired_mass = 0.0;
        observer_term_friction = 0.0;
        observer_term_injector = 0.0;
        observer_mass_estimate = 0.0;
        // export internal states to signals for monitoring purposes:
        SetData( config.out_massobserver_term_friction,  observer_term_friction );
        SetData( config.out_massobserver_term_injector,  observer_term_injector );
        SetData( config.out_massobserver,                observer_mass_estimate );
        return EXIT_SUCCESS;
    }

    //compute the absolute pressures:
    float p_supply = P_ENV + GetData(config.pressure_supply);
    float p_out = P_ENV + GetData(config.pressure_out);

    // I N I T  V A R I A B L E S
    // current massflow for multiple models
    float mf_curr_friction = 0.0;
    float mf_curr_leeco  = 0.0;

    // error can change in each step
    float last_step_error= this->error;

    // M A S S F L O W
    // calculate current massflow

    // I N F L A T I O N
    if( GetData(config.out_inflate) == 1.0 ) { 
      // compute current mass flow      
      mf_curr_friction= MFLinearModel(     p_supply, p_out, config.c_inflate_friction);
      mf_curr_leeco = MFLeeCompanyModel( p_supply, p_out, config.c_inflate_injector);
   }

    // D E F L A T I O N
    if( GetData(config.out_deflate) == 1.0 ) {
      // pressure out = ambient pressure
      mf_curr_friction= MFLinearModel(     p_out, P_ENV, config.c_deflate_friction);
      mf_curr_leeco = MFLeeCompanyModel( p_out, P_ENV, config.c_deflate_injector);
   }

    //compute the mass change in the last timestep given the flow estimate:
    float delta_mass_friction= mf_curr_friction* PERIOD_S;
    float delta_mass_leeco = mf_curr_leeco * PERIOD_S;

    // update mass estimate
    observer_term_friction += delta_mass_friction;
    observer_term_injector += delta_mass_leeco;
    observer_mass_estimate = observer_mass_estimate + delta_mass_friction + delta_mass_leeco; // combine models

    // export internal states to signals for monitoring purposes:
    SetData( config.out_massobserver_term_friction,  observer_term_friction );
    SetData( config.out_massobserver_term_injector,  observer_term_injector );
    SetData( config.out_massobserver,                observer_mass_estimate );




    //Compute the current desired mass from either desired massflow or just use a user-supplied value:
    if (config.in_desired_massflow != SIGNAL_NONE) {
        desired_mass += GetData(config.in_desired_massflow) * PERIOD_S;
    } else if (config.in_desired_mass != SIGNAL_NONE) {
        desired_mass = GetData(config.in_desired_mass); //else just get value from a signal
    } else {
        desired_mass = 0.;
    }

    // E V A L U A T E   E R R O R
    // calculate current error:
    this->error= desired_mass - observer_mass_estimate;

    // stop in-/deflating when error is zero
    // that means, when it switched its sign
    // since last time step
    if(last_step_error*this->error < 0) { //if error sign changes
        out_inflation_valvestate = 0.0;
        out_deflation_valvestate = 0.0;
        SetData(config.out_inflate, out_inflation_valvestate);
        SetData(config.out_deflate, out_deflation_valvestate);
    }

    if(   (this->error <= config.c_inflation_threshold) && (this->error >= config.c_deflation_threshold) ) {// I N S I D E  T O L E R A N C E S
    } else { // O U T S I D E   T O L E R A N C E S
        if( this->error > 0 ) { // mass is lower than desired -> start to inflate
             if (config.out_inflate != SIGNAL_NONE) {
                out_inflation_valvestate = GetData(config.out_inflate); //make sure to use real out state
             }
             if(out_inflation_valvestate < 0.5) {
               // Add constant inflation error at inflation start
               float mass_bias = config.c_inflate;
               mass_bias += p_supply * config.c_inflate_Psupply;
               mass_bias += p_out    * config.c_inflate_Pout;
               observer_mass_estimate += mass_bias;
             }
             out_inflation_valvestate = 1.0;
             out_deflation_valvestate = 0.0;
     
        } else {// mass higher than desired -> start to deflate
             if (config.out_deflate != SIGNAL_NONE) {
                out_deflation_valvestate = GetData(config.out_deflate); //make sure to use real out state
             }
             // add constants deflation model  when deflation starts
             if(out_deflation_valvestate < 0.5) {
               float mass_bias = config.c_deflate;
               mass_bias += p_supply * config.c_deflate_Psupply;
               mass_bias += p_out    * config.c_deflate_Pout;
               observer_mass_estimate += mass_bias;
             }
             out_inflation_valvestate = 0.0;
             out_deflation_valvestate = 1.0;
        } // error in-/outside tolerance checking    
        SetData(config.out_inflate, out_inflation_valvestate);
        SetData(config.out_deflate, out_deflation_valvestate);    
    } // error threshold checking

    SetData( config.out_massobserver_term_friction,  observer_term_friction );
    SetData( config.out_massobserver_term_injector,  observer_term_injector );
    SetData( config.out_massobserver,                observer_mass_estimate );
    return EXIT_SUCCESS;

  } // run()

} //namespace