mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-01 19:07:45 +08:00
Added controllib statistics block.
This commit is contained in:
@@ -558,4 +558,24 @@ int blockRandGaussTest()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int blockStatsTest()
|
||||
{
|
||||
BlockStats<float, 1> stats(NULL, "TEST");
|
||||
ASSERT_CL(equal(0.0f, stats.getMean()(0)));
|
||||
ASSERT_CL(equal(0.0f, stats.getStdDev()(0)));
|
||||
|
||||
|
||||
stats.update(matrix::Scalar<float>(1.0f));
|
||||
ASSERT_CL(equal(1.0f, stats.getMean()(0)));
|
||||
ASSERT_CL(equal(0.5f, stats.getStdDev()(0)));
|
||||
stats.update(matrix::Scalar<float>(2));
|
||||
stats.reset();
|
||||
ASSERT_CL(equal(0.0f, stats.getMean()(0)));
|
||||
ASSERT_CL(equal(0.0f, stats.getStdDev()(0)));
|
||||
printf("PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace control
|
||||
|
||||
@@ -50,6 +50,8 @@
|
||||
#include "block/Block.hpp"
|
||||
#include "block/BlockParam.hpp"
|
||||
|
||||
#include "matrix/math.hpp"
|
||||
|
||||
namespace control
|
||||
{
|
||||
|
||||
@@ -553,4 +555,51 @@ private:
|
||||
|
||||
int __EXPORT blockRandGaussTest();
|
||||
|
||||
template<class Type, size_t M>
|
||||
class __EXPORT BlockStats: public Block
|
||||
{
|
||||
public:
|
||||
// methods
|
||||
BlockStats(SuperBlock *parent,
|
||||
const char *name) :
|
||||
Block(parent, name),
|
||||
_sum(),
|
||||
_sumSq(),
|
||||
_count(0)
|
||||
{
|
||||
};
|
||||
virtual ~BlockStats() {};
|
||||
void update(const matrix::Vector<Type, M> &u)
|
||||
{
|
||||
_sum += u;
|
||||
_sumSq += u.emult(u);
|
||||
_count += 1;
|
||||
}
|
||||
void reset()
|
||||
{
|
||||
_sum.setZero();
|
||||
_sumSq.setZero();
|
||||
_count = 0;
|
||||
}
|
||||
// accessors
|
||||
size_t getCount() { return _count; }
|
||||
matrix::Vector<Type, M> getMean() { return _sum / _count; }
|
||||
matrix::Vector<Type, M> getVar()
|
||||
{
|
||||
return (_sumSq - _sum.emult(_sum) / _count) / _count;
|
||||
}
|
||||
matrix::Vector<Type, M> getStdDev()
|
||||
{
|
||||
return getVar().pow(0.5);
|
||||
}
|
||||
private:
|
||||
// attributes
|
||||
matrix::Vector<Type, M> _sum;
|
||||
matrix::Vector<Type, M> _sumSq;
|
||||
size_t _count;
|
||||
};
|
||||
|
||||
int __EXPORT blockStatsTest();
|
||||
|
||||
|
||||
} // namespace control
|
||||
|
||||
Reference in New Issue
Block a user