controller_pressurelimiter.cpp 3.35 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.  
*/

/*
 * Pressure Watchdog: Monitor pressure sensors and trigger EmergencySoftStop
 *
 * Author: Raphael
 */
#include "global.hpp"
#include "controller.hpp"
#include <math.h>

  
namespace AirserverController{



CntrlPressureLimiter::CntrlPressureLimiter(MsgConfigurationControllerPressureLimiter *conf)
{    
    id = conf->header_config.id;
    k_current = PERIOD_S / conf->filtertime_signal_to_watch; //set filter coefficient
    limit(0.0, k_current, 1.0);
    signal_to_watch = conf->signal_to_watch;
    if (LOG_INFO) {cout << " " << k_current << " "<< signal_to_watch <<endl;}
    current_pressure_filtered = GetData(signal_to_watch); //initialize pressure value
    limit_value = conf->limit_value;
    limit_signal = conf->limit_signal;
    inflate_valve = conf->inflate_valve;
    triggered = false;
}



int CntrlPressureLimiter::run()
{
    AirServerSignalValue current_pressure = GetData(signal_to_watch);
    current_pressure_filtered += (current_pressure - current_pressure_filtered) * k_current;

    if (activated) { //should this controller not be deactivate-able?        
        //stop valve if we exceed the fixed pressure limit
        if (current_pressure_filtered >= limit_value) {SetData(inflate_valve, 0.0);}  //make inflate valve to stop immediately
        //stop valve if we exceed the pressure limit signal:
        if (limit_signal != AirserverAPI::SIGNAL_NONE) {
            if (current_pressure_filtered >= GetData(limit_signal)) {SetData(inflate_valve, 0.0);}
        }
        
	    if (LOG_INFO) {
                static int sometimes = 0;
                 if (sometimes++ > 500 ) {
                     cout << signal_to_watch << "  " << current_pressure_filtered << "        " << current_pressure << "        " << limit_value <<"        " << k_current << endl;
                     sometimes = 0;
                }
        }
    }


    return EXIT_SUCCESS;	   
}
  


}