Browse Source
git-svn-id: https://arducopter.googlecode.com/svn/trunk@1251 f9c3cf11-9bcb-44bc-f272-b75c42450872master
4 changed files with 192 additions and 63 deletions
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* AP_Loop.pde |
||||
* Copyright (C) James Goppert 2010 <james.goppert@gmail.com> |
||||
* |
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#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
|
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* AP_Loop.h |
||||
* Copyright (C) James Goppert 2010 <james.goppert@gmail.com> |
||||
* |
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#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<Loop *> & 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<Loop *> _subLoops; |
||||
uint32_t _timeStamp; |
||||
uint8_t _load; |
||||
float _dt; |
||||
}; |
||||
|
||||
} // apo
|
||||
|
||||
#endif |
||||
|
||||
// vim:ts=4:sw=4:expandtab
|
Loading…
Reference in new issue