Libbarrett  1.2.4
include/barrett/systems/summer.h
00001 /*
00002         Copyright 2009, 2010 Barrett Technology <support@barrett.com>
00003 
00004         This file is part of libbarrett.
00005 
00006         This version of libbarrett is free software: you can redistribute it
00007         and/or modify it under the terms of the GNU General Public License as
00008         published by the Free Software Foundation, either version 3 of the
00009         License, or (at your option) any later version.
00010 
00011         This version of libbarrett is distributed in the hope that it will be
00012         useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License along
00017         with this version of libbarrett.  If not, see
00018         <http://www.gnu.org/licenses/>.
00019 
00020         Further, non-binding information about licensing is available at:
00021         <http://wiki.barrett.com/libbarrett/wiki/LicenseNotes>
00022 */
00023 
00024 /*
00025  * summer.h
00026  *
00027  *  Created on: Sep 10, 2009
00028  *      Author: dc
00029  */
00030 
00031 #ifndef BARRETT_SYSTEMS_SUMMER_H_
00032 #define BARRETT_SYSTEMS_SUMMER_H_
00033 
00034 #include <bitset>
00035 #include <string>
00036 #include <stdexcept>
00037 
00038 #include <boost/array.hpp>
00039 #include <Eigen/Core>
00040 
00041 #include <barrett/detail/ca_macro.h>
00042 #include <barrett/math/traits.h>
00043 #include <barrett/systems/abstract/system.h>
00044 #include <barrett/systems/abstract/single_io.h>
00045 
00046 
00047 namespace barrett {
00048 namespace systems {
00049 
00050 
00051 // FIXME: it might be nice to have a Summer with the number of inputs
00052 //        determined at runtime
00053 template<typename T, size_t numInputs = 2, bool RequiresAlignment = math::Traits<T>::RequiresAlignment>
00054 class Summer : public System, public SingleOutput<T> {
00055 // IO
00056 // protected because of variable number of inputs
00057 protected:      boost::array<Input<T>*, numInputs> inputs;
00058 
00059 
00060 public:
00061         class Polarity {
00062         public:
00063                 Polarity();  // default: all positive
00064                 explicit Polarity(std::string polarityStr) throw(std::invalid_argument);
00065                 explicit Polarity(const std::bitset<numInputs>& inputPolarity) :
00066                         polarity(inputPolarity) {}
00067                 virtual ~Polarity() {}
00068 
00069                 // TODO(dc): operator[]=
00070                 int operator[] (const size_t i) const;
00071 
00072         protected:
00073                 std::bitset<numInputs> polarity;
00074         };
00075 
00076         Polarity polarity;
00077 
00078         explicit Summer(const Polarity& inputPolarity = Polarity(), bool undefinedIsZero = false, const std::string& sysName = "Summer");
00079         explicit Summer(const std::string& inputPolarity, bool undefinedIsZero = false, const std::string& sysName = "Summer");
00080         explicit Summer(const char* inputPolarity, bool undefinedIsZero = false, const std::string& sysName = "Summer");  // Without this, a string literal argument calls the Summer(bool) overload.
00081         explicit Summer(const std::bitset<numInputs>& inputPolarity, bool undefinedIsZero = false, const std::string& sysName = "Summer");
00082         explicit Summer(bool undefinedIsZero, const std::string& sysName = "Summer");
00083         virtual ~Summer();
00084 
00085         Input<T>& getInput(const size_t i);
00086 
00087 protected:
00088         virtual bool inputsValid() {  return true;  };
00089         virtual void operate();
00090         virtual void invalidateOutputs() {}
00091 
00092         void initInputs();
00093 
00094         bool strict;
00095         T sum;
00096 
00097 private:
00098         DISALLOW_COPY_AND_ASSIGN(Summer);
00099 
00100 public:
00101         EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(RequiresAlignment)
00102 };
00103 
00104 
00105 }
00106 }
00107 
00108 
00109 // include template definitions
00110 #include <barrett/systems/detail/summer-inl.h>
00111 #include <barrett/systems/detail/summer-polarity-inl.h>
00112 
00113 
00114 #endif /* BARRETT_SYSTEMS_SUMMER_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines