controller_mass.cpp 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 
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.  
*/



16
/*
17
 * Mass flow controller: actuate channel according to a signal:
18 19 20 21 22 23 24 25 26
 *
 *
 * Author: Raphael
 */
#include "global.hpp"
#include "controller.hpp"
#include <iostream>
#include <math.h>

27

28 29
namespace AirserverController{

30
  CntrlMass::CntrlMass(MsgConfigurationControllerMass *conf)
31
  {
32 33
  
    //std::cout << "configuring mass controller" << std::endl;
34
    id = conf->header_config.id;
35
    
36 37
    config = *conf; //copy everything
    
38
    this->error= 0.0;
39 40
    
    desired_mass = 0.0;
41

42 43 44
    SetData( config.out_massobserver_term_friction, 0.0 );
    SetData( config.out_massobserver_term_injector, 0.0 );
    SetData( config.out_massobserver,                 0.0 );
45

46 47 48 49 50 51 52 53 54 55 56
//        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;
57

58
 }
59

60
  float CntrlMass::MFLinearModel(float Pi, float Po, float scale) {
61 62 63
    float dp= (Pi - Po);
    float mf= scale * dp;
    return mf;
64 65
  }
   
66
  float CntrlMass::MFLeeCompanyModel(float Pi, float Po, float scale) {
67
    // absolute pressure ratio
68
    float ratio= Pi/Po;
69
    float mf= 0.0;
70 71 72
    
    float dp= (Pi - Po);
    if (dp < 0.0) {dp = 0.0;} //to avoid strange behavior
73 74

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

80
    return mf;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    // 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
 
  }

97
  float CntrlMass::MFPSIModel(float Pi, float Po, float scale) {
98
    float ratio= Po/Pi;
99 100 101 102 103 104 105

    // 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; 
106
    float mf= 0.0;
107 108

    if(ratio > 0.999) {
109
      // This should actually never happen for the soft actuator.
110
      // For inflation supply pressure would reach channel pressure.
111 112
      // For deflation, assuming an abbsolute supply pressure near 300kPa+103kPa,
      // the measured channel pressure would need to be 0.103 kPa.
113 114 115 116 117
      // 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 
118
      //          << "). Massflow= zero." << std::endl;
119 120 121
    } else {
      if(ratio >= critical_pressure_ratio) {
        // 0.528 < ratio < 0.999
122
        mf= scale * Pi * sqrt(2.0/ (gamma-1.0) ) * sqrt(pow(ratio,(2.0/gamma)) - pow(ratio,(gamma+1.0/gamma)) );
123 124
      } else {
        // ratio < 0.528
125
        mf= scale * Pi * sqrt( pow(critical_pressure_ratio,(gamma+1.0)/gamma) );
126 127 128
      } 
    }// ratio > 0.999

129
    return mf;
130 131 132
  }


133
  int CntrlMass::run() {
134

135 136 137 138 139 140 141
    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);
    }

142 143 144 145 146 147 148 149 150 151 152 153
    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;
154 155
    }

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

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

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

168
    // M A S S F L O W
169
    // calculate current massflow
170

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

178
    // D E F L A T I O N
179
    if( GetData(config.out_deflate) == 1.0 ) {
180 181 182
      // 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);
183
   }
184

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

189
    // update mass estimate
190
    observer_term_friction += delta_mass_friction;
191
    observer_term_injector += delta_mass_leeco;
192
    observer_mass_estimate = observer_mass_estimate + delta_mass_friction + delta_mass_leeco; // combine models
193

194 195 196 197
    // 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 );
198

199

200

201

202 203
    //Compute the current desired mass from either desired massflow or just use a user-supplied value:
    if (config.in_desired_massflow != SIGNAL_NONE) {
204
        desired_mass += GetData(config.in_desired_massflow) * PERIOD_S;
205 206 207 208 209 210
    } 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.;
    }

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

215 216
    // stop in-/deflating when error is zero
    // that means, when it switched its sign
217
    // since last time step
218 219 220 221 222
    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);
223 224
    }

225 226 227 228 229 230
    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
             }
231
             if(out_inflation_valvestate < 0.5) {
232 233
               // Add constant inflation error at inflation start
               float mass_bias = config.c_inflate;
234 235
               mass_bias += p_supply * config.c_inflate_Psupply;
               mass_bias += p_out    * config.c_inflate_Pout;
236 237 238 239 240 241 242 243 244 245
               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
246
             if(out_deflation_valvestate < 0.5) {
247
               float mass_bias = config.c_deflate;
248 249
               mass_bias += p_supply * config.c_deflate_Psupply;
               mass_bias += p_out    * config.c_deflate_Pout;
250 251 252 253 254 255 256 257 258 259 260 261
               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 );
262
    return EXIT_SUCCESS;
263

264
  } // run()
265

266
} //namespace