diff --git a/libraries/AP_Common/AP_Common.h b/libraries/AP_Common/AP_Common.h index 79a1733273..c65d94317d 100644 --- a/libraries/AP_Common/AP_Common.h +++ b/libraries/AP_Common/AP_Common.h @@ -18,6 +18,8 @@ #include #include "include/menu.h" /// simple menu subsystem #include "c++.h" // c++ additions +#include "AP_Vector.h" +#include "AP_Loop.h" //////////////////////////////////////////////////////////////////////////////// /// @name Warning control diff --git a/libraries/AP_Common/AP_Loop.cpp b/libraries/AP_Common/AP_Loop.cpp new file mode 100644 index 0000000000..1146802b15 --- /dev/null +++ b/libraries/AP_Common/AP_Loop.cpp @@ -0,0 +1,57 @@ +/* + * AP_Loop.pde + * Copyright (C) James Goppert 2010 + * + * This file is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "AP_Loop.h" + +namespace apo +{ + +Loop::Loop(float frequency, void (*fptr)(void *), void * data) : + _fptr(fptr), + _data(data), + _period(1.0e6/frequency), + _timeStamp(micros()), + _subLoops(), + _load(0), + _dt(0) +{ +} + +void Loop::update() +{ + // quick exit if not ready + if (micros() - _timeStamp < _period) return; + + // update time stamp + uint32_t timeStamp0 = _timeStamp; + _timeStamp = micros(); + _dt = (_timeStamp - timeStamp0)/1.0e6; + + // update sub loops + for (int i=0; i<_subLoops.getSize(); i++) _subLoops[i]->update(); + + // callback function + if (_fptr) _fptr(_data); + + // calculated load with a low pass filter + _load = 0.9*_load + 10*(float(micros()-_timeStamp)/(_timeStamp-timeStamp0)); +} + +} // apo + +// vim:ts=4:sw=4:expandtab diff --git a/libraries/AP_Common/AP_Loop.h b/libraries/AP_Common/AP_Loop.h new file mode 100644 index 0000000000..1d1252896c --- /dev/null +++ b/libraries/AP_Common/AP_Loop.h @@ -0,0 +1,70 @@ +/* + * AP_Loop.h + * Copyright (C) James Goppert 2010 + * + * This file is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef AP_Loop_H +#define AP_Loop_H + +#include "AP_Vector.h" + +/** + * Start of apo namespace + * The above functions must be in the global + * namespace for c++ to function properly + */ +namespace apo +{ + +class Loop +{ +public: + Loop() : _fptr(), _data(), _period(), _subLoops(), _timeStamp(), _load(), _dt() {}; + Loop(float frequency, void (*fptr)(void *) = NULL, void * data = NULL); + void update(); + Vector & subLoops() { + return _subLoops; + } + uint32_t frequency() { + return 1.0e6/_period; + } + void frequency(float frequency) { + _period = 1e6/frequency; + } + uint32_t timeStamp() { + return _timeStamp; + } + float dt() { + return _dt; + } + uint8_t load() { + return _load; + } +private: + void (*_fptr)(void *); + void * _data; + uint32_t _period; + Vector _subLoops; + uint32_t _timeStamp; + uint8_t _load; + float _dt; +}; + +} // apo + +#endif + +// vim:ts=4:sw=4:expandtab diff --git a/libraries/AP_Common/AP_Vector.h b/libraries/AP_Common/AP_Vector.h index b757b24dc2..1d25721273 100644 --- a/libraries/AP_Common/AP_Vector.h +++ b/libraries/AP_Common/AP_Vector.h @@ -1,5 +1,5 @@ /* - * AP_Vector.h + * Vector.h * Copyright (C) James Goppert 2010 * * This file is free software: you can redistribute it and/or modify it @@ -16,20 +16,20 @@ * with this program. If not, see . */ -#ifndef AP_Vector_H -#define AP_Vector_H +#ifndef Vector_H +#define Vector_H #include #include #include #ifdef ASSERT -const static char vectorSource[] ="AP_Vector.hpp"; +const static char vectorSource[] ="Vector.hpp"; #endif // vector template -class AP_Vector +class Vector { private: size_t size; @@ -38,38 +38,38 @@ private: dataType* data; public: // default constructor - AP_Vector(const size_t & size=0, const size_t & extraAllocationSize=0) : size(0), extraAllocationSize(extraAllocationSize), sizeAllocated(0), data(NULL) { + Vector(const size_t & size=0, const size_t & extraAllocationSize=0) : size(0), extraAllocationSize(extraAllocationSize), sizeAllocated(0), data(NULL) { setSize(size); } // 3 vector constructor - AP_Vector(const dataType & a, const dataType & b, const dataType & c) : size(3), extraAllocationSize(extraAllocationSize), sizeAllocated(0), data(NULL) { + Vector(const dataType & a, const dataType & b, const dataType & c) : size(3), extraAllocationSize(extraAllocationSize), sizeAllocated(0), data(NULL) { setSize(size); - (*this)(0)=a; - (*this)(1)=b; - (*this)(2)=c; + (*this)[0]=a; + (*this)[1]=b; + (*this)[2]=c; } // construct from array - AP_Vector(const dataType* array, const size_t & size, const size_t & extraAllocationSize=0) : size(0), extraAllocationSize(extraAllocationSize), sizeAllocated(0), data(NULL) { + Vector(const dataType* array, const size_t & size, const size_t & extraAllocationSize=0) : size(0), extraAllocationSize(extraAllocationSize), sizeAllocated(0), data(NULL) { setSize(size); for (size_t i=0; i toFloat() const { - AP_Vector v(getSize()); + Vector toFloat() const { + Vector v(getSize()); for (size_t i=0; i