• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

AP_Loop.cpp

Go to the documentation of this file.
00001 /*
00002  * AP_Loop.pde
00003  * Copyright (C) James Goppert 2010 <james.goppert@gmail.com>
00004  *
00005  * This file is free software: you can redistribute it and/or modify it
00006  * under the terms of the GNU General Public License as published by the
00007  * Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This file is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00013  * See the GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "AP_Loop.h"
00020 
00021 namespace apo
00022 {
00023 
00024 Loop::Loop(float frequency, void (*fptr)(void *), void * data) :
00025     _fptr(fptr),
00026     _data(data),
00027     _period(1.0e6/frequency),
00028     _timeStamp(micros()),
00029     _subLoops(),
00030     _load(0),
00031     _dt(0)
00032 {
00033 }
00034 
00035 void Loop::update()
00036 {
00037     // quick exit if not ready
00038     if (micros() - _timeStamp < _period) return;
00039 
00040     // update time stamp
00041     uint32_t timeStamp0 = _timeStamp;
00042     _timeStamp = micros();
00043     _dt = (_timeStamp - timeStamp0)/1.0e6;
00044 
00045     // update sub loops
00046     for (int i=0; i<_subLoops.getSize(); i++) _subLoops[i]->update();
00047 
00048     // callback function
00049     if (_fptr) _fptr(_data);
00050 
00051     // calculated load with a low pass filter
00052     _load = 0.9*_load + 10*(float(micros()-_timeStamp)/(_timeStamp-timeStamp0));
00053 }
00054 
00055 } // apo
00056 
00057 // vim:ts=4:sw=4:expandtab

Generated for ArduPilot Libraries by doxygen