MHVLib
20111011
An efficiency oriented runtime library for AVR microcontrollers
|
00001 /* 00002 * Based on Arduino PID Library - Version 1 00003 * by Brett Beauregard <br3ttb@gmail.com> brettbeauregard.com 00004 * 00005 * Original code is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. 00006 * 00007 * MHV version Copyright (c) 2011, Make, Hack, Void Inc 00008 * All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions are met: 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the distribution. 00017 * * Neither the name of the Make, Hack, Void nor the 00018 * names of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00023 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY 00025 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00027 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00028 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00029 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #ifndef MHV_PID_H_ 00034 #define MHV_PID_H_ 00035 00036 #include <MHV_io.h> 00037 00038 class MHV_PID { 00039 protected: 00040 float _kP; 00041 float _kI; 00042 float _kD; 00043 00044 bool _reverse; 00045 bool _enabled; 00046 00047 float _setpoint; 00048 float _integral; 00049 float _lastInput; 00050 float _lastOutput; 00051 00052 float outMin; 00053 float outMax; 00054 00055 inline void clampIntegral(); 00056 00057 public: 00058 MHV_PID(float setpoint, float kP, float kI, float kD, uint16_t period, 00059 bool reverse, uint16_t min, uint16_t max); 00060 void setDirection(bool reverse); 00061 void enable(bool enable); 00062 float compute(float input); 00063 void setTuning(float kP, float kI, float kD, uint16_t period); 00064 void setOutputLimits(float, float); 00065 }; 00066 #endif 00067