-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c415889
commit 173ca50
Showing
5 changed files
with
248 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* ,---------, ____ _ __ | ||
* | ,-^-, | / __ )(_) /_______________ _____ ___ | ||
* | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ | ||
* | / ,--´ | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ | ||
* +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ | ||
* | ||
* Crazyflie control firmware | ||
* | ||
* Copyright (C) 2019 Bitcraze AB | ||
* | ||
* This program 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, in version 3. | ||
* | ||
* This program 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/>. | ||
* | ||
* | ||
* statsCnt.h - utitlity for logging counters | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
typedef struct { | ||
uint32_t count; | ||
float result; | ||
|
||
uint32_t latestAverage_ms; | ||
} statsCntRate_t; | ||
|
||
void statsCntReset(statsCntRate_t* this, const uint32_t now_ms); | ||
void statsCntInc(statsCntRate_t* this); | ||
float statsCntRate(statsCntRate_t* this, const uint32_t now_ms); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* ,---------, ____ _ __ | ||
* | ,-^-, | / __ )(_) /_______________ _____ ___ | ||
* | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ | ||
* | / ,--´ | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ | ||
* +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ | ||
* | ||
* Crazyflie control firmware | ||
* | ||
* Copyright (C) 2019 Bitcraze AB | ||
* | ||
* This program 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, in version 3. | ||
* | ||
* This program 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/>. | ||
* | ||
* | ||
* statsCnt.c - utitlity for logging counters | ||
*/ | ||
|
||
#include "statsCnt.h" | ||
|
||
void statsCntReset(statsCntRate_t* this, const uint32_t now_ms) { | ||
this->count = 0; | ||
this->result = 0.0f; | ||
this->latestAverage_ms = now_ms; | ||
} | ||
|
||
void statsCntInc(statsCntRate_t* this) { | ||
this->count++; | ||
} | ||
|
||
float statsCntRate(statsCntRate_t* this, const uint32_t now_ms) { | ||
const uint32_t dt_ms = now_ms - this->latestAverage_ms; | ||
if (dt_ms > 0) { | ||
const float oneSecond_ms = 1000.0f; | ||
this->result = this->count * oneSecond_ms / dt_ms; | ||
return this->result; | ||
} else { | ||
return 0.0f; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* ,---------, ____ _ __ | ||
* | ,-^-, | / __ )(_) /_______________ _____ ___ | ||
* | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ | ||
* | / ,--´ | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ | ||
* +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ | ||
* | ||
* Crazyflie control firmware | ||
* | ||
* Copyright (C) 2019 Bitcraze AB | ||
* | ||
* This program 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, in version 3. | ||
* | ||
* This program 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/>. | ||
* | ||
* | ||
* testStatsCnt.h - unit tests for statsCnt | ||
*/ | ||
|
||
// File under test | ||
#include "statsCnt.h" | ||
|
||
#include "unity.h" | ||
|
||
static const uint32_t resetTime = 1234; | ||
static statsCntRate_t data; | ||
|
||
void setUp(void) { | ||
statsCntReset(&data, resetTime); | ||
} | ||
|
||
void tearDown(void) {} | ||
|
||
void testThatDataIsReset() { | ||
// Fixture | ||
data.count = 17; | ||
data.result = 47.11f; | ||
data.latestAverage_ms = 9876; | ||
|
||
// Test | ||
statsCntReset(&data, resetTime); | ||
|
||
// Assert | ||
TEST_ASSERT_EQUAL_UINT32(0, data.count); | ||
TEST_ASSERT_EQUAL_FLOAT(0.0f, data.result); | ||
TEST_ASSERT_EQUAL_UINT32(resetTime, data.latestAverage_ms); | ||
} | ||
|
||
void testThatCounterIsIncreased() { | ||
// Fixture | ||
|
||
// Test | ||
statsCntInc(&data); | ||
|
||
// Assert | ||
TEST_ASSERT_EQUAL_UINT32(1, data.count); | ||
} | ||
|
||
void testThatRateIsComputed() { | ||
// Fixture | ||
int count = 10; | ||
for (int i = 0; i < count; i++) { | ||
statsCntInc(&data); | ||
} | ||
|
||
uint32_t dt_ms = 500; | ||
uint32_t now = resetTime + dt_ms; | ||
|
||
float expected = (float)count * 1000.0f / (float)dt_ms; | ||
|
||
// Test | ||
float actual = statsCntRate(&data, now); | ||
|
||
// Assert | ||
TEST_ASSERT_EQUAL_FLOAT(expected, actual); | ||
TEST_ASSERT_EQUAL_FLOAT(expected, data.result); | ||
} | ||
|
||
void testThatRateIsComputedWhenNoTimeHasPassed() { | ||
// Fixture | ||
int count = 10; | ||
for (int i = 0; i < count; i++) { | ||
statsCntInc(&data); | ||
} | ||
|
||
uint32_t now = resetTime; | ||
float expected = 0.0f; | ||
|
||
// Test | ||
float actual = statsCntRate(&data, now); | ||
|
||
// Assert | ||
TEST_ASSERT_EQUAL_FLOAT(expected, actual); | ||
TEST_ASSERT_EQUAL_FLOAT(expected, data.result); | ||
} |