diff --git a/scripts/generators/templates/FSMController.cpp.template b/scripts/generators/templates/FSMController.cpp.template index 2ea5f355f54898bee5e1eb88647bcb87b97ac4e9..0f4c88f9578467108cd8f183f50f2c6a4ea10047 100644 --- a/scripts/generators/templates/FSMController.cpp.template +++ b/scripts/generators/templates/FSMController.cpp.template @@ -58,7 +58,7 @@ void {state_machine_name}Controller::logStatus({state_machine_name}ControllerSta void {state_machine_name}Controller::logStatus() {{ - status.timestamp = miosix::getTick(); + status.timestamp = miosix::getTime(); logger.log(status); StackLogger::getInstance().updateStack(THID); }} diff --git a/src/shared/utils/Debug.h b/src/shared/utils/Debug.h index 9056bcdbd40b3d845e95aa9776d0457c8585f79b..7f27affe6d74d218f1d06663c23fb0203ac26bcb 100644 --- a/src/shared/utils/Debug.h +++ b/src/shared/utils/Debug.h @@ -24,6 +24,7 @@ #ifdef DEBUG #include <miosix.h> +#include <utils/Constants.h> #include <cstdarg> #include <cstdio> @@ -42,14 +43,18 @@ inline void TRACE(const char* format, ...) va_list argptr; va_start(argptr, format); - printf("%.2f> ", miosix::getTick() / 1000.0f); + printf("%.2f> ", (float)miosix::getTime() / Boardcore::Constants::NS_IN_S); vprintf(format, argptr); va_end(argptr); } -// #define TRACE(...) printf("%.2f> ", miosix::getTick()/1000.0f); -// printf(__VA_ARGS__) +/* +#define TRACE(...) \ + printf("%.2f> ", \ + (float)miosix::getTime() / Boardcore::Constants::NS_IN_S); \ + printf(__VA_ARGS__) +*/ #else diff --git a/src/shared/utils/TestUtils/TestHelper.cpp b/src/shared/utils/TestUtils/TestHelper.cpp index 6d0111bd1ab69255962dc27b49961a53337c2f9d..8103a2efcddbc215f8ecfcbcb54cb3f6d0c7abce 100644 --- a/src/shared/utils/TestUtils/TestHelper.cpp +++ b/src/shared/utils/TestUtils/TestHelper.cpp @@ -22,13 +22,16 @@ #include "TestHelper.h" -namespace Boardcore -{ +#include <events/utils/EventCounter.h> + +#include <cmath> -long long tickToMilliseconds(long long tick) +using miosix::FastMutex; +using miosix::getTime; +using miosix::Lock; + +namespace Boardcore { - return tick * 1000 / miosix::TICK_FREQ; -} bool expectEvent(uint8_t eventId, uint8_t topic, long long when, long long uncertainty, EventBroker& broker) @@ -36,23 +39,23 @@ bool expectEvent(uint8_t eventId, uint8_t topic, long long when, EventCounter c{broker}; c.subscribe(topic); - long long windowStart = when - uncertainty; - long long windowEnd = when + uncertainty; + long long windowStart = when - uncertainty * Constants::NS_IN_MS; + long long windowEnd = when + uncertainty * Constants::NS_IN_MS; - while (getTick() < windowEnd) + while (getTime() < windowEnd) { if (c.getCount(eventId) > 0) { - long long recvTick = getTick(); - if (recvTick < windowStart) + long long recvTime = getTime(); + if (recvTime < windowStart) { TRACE( "[expectEvent] Event %d on topic %d receveid %d ms before " "the opening of " "the window.\n", eventId, topic, - static_cast<int>( - tickToMilliseconds(windowStart - recvTick))); + static_cast<int>((windowStart - recvTime) / + Constants::NS_IN_MS)); return false; } TRACE( @@ -60,7 +63,7 @@ bool expectEvent(uint8_t eventId, uint8_t topic, long long when, "window, %d ms from " "the target time.\n", eventId, topic, - static_cast<int>(tickToMilliseconds(abs(recvTick - when)))); + static_cast<int>(abs(recvTime - when) / Constants::NS_IN_MS)); return true; } @@ -80,8 +83,8 @@ bool waitForEvent(uint8_t event, uint8_t topic, long long timeout, { EventCounter c{broker}; c.subscribe(topic); - long long end = getTick() + timeout; - while (timeout == 0 || getTick() < end) + long long end = getTime() + timeout * Constants::NS_IN_MS; + while (timeout == 0 || getTime() < end) { if (c.getCount(event) > 0) return true; diff --git a/src/shared/utils/TestUtils/TestHelper.h b/src/shared/utils/TestUtils/TestHelper.h index 2e7592f4273969efca7d3ea6a4d38c70b7d02695..d4e1150da09d9dfdb1ccc8888136c238e8e02caf 100644 --- a/src/shared/utils/TestUtils/TestHelper.h +++ b/src/shared/utils/TestUtils/TestHelper.h @@ -25,17 +25,8 @@ #include <events/EventBroker.h> #include <events/FSM.h> #include <events/HSM.h> -#include <events/utils/EventCounter.h> #include <miosix.h> -#include <cmath> -#include <map> - -using miosix::FastMutex; -using miosix::getTick; -using miosix::Lock; -using std::map; - namespace Boardcore { @@ -45,11 +36,6 @@ namespace Boardcore */ static const int EVENT_TIMING_UNCERTAINTY = 1; -/** - * @brief Helper function used convert system ticks to milliseconds - */ -long long tickToMilliseconds(long long tick); - /** * Tests if a specific transition occurs in a Finite State Machine * in response to an event. @@ -176,7 +162,7 @@ bool testHSMAsyncTransition(HSM_type& hsm, const Event& ev, uint8_t topic, * * @param eventId The event to be checked * @param topic The topic the event will be posted on - * @param when Expected time at which the event will be posted, in system ticks + * @param when Absolute system time the event will be posted at [ns] * @param uncertainty Size of the time window * @param broker * @return True if the event is posted inside the time window diff --git a/src/shared/utils/TestUtils/TestSensor.h b/src/shared/utils/TestUtils/TestSensor.h index 07b19e98b8c415c4f98e4388d1e42b02dec8414b..38892b3688c1c6006cede341544ac2d587cbcb24 100644 --- a/src/shared/utils/TestUtils/TestSensor.h +++ b/src/shared/utils/TestUtils/TestSensor.h @@ -37,12 +37,12 @@ struct TestData : public TimestampData float value; TestData(float v) - : TimestampData{static_cast<uint64_t>(miosix::getTick())}, value(v) + : TimestampData{static_cast<uint64_t>(miosix::getTime())}, value(v) { } TestData() - : TimestampData{static_cast<uint64_t>(miosix::getTick())}, value(0.0) + : TimestampData{static_cast<uint64_t>(miosix::getTime())}, value(0.0) { } }; @@ -61,8 +61,8 @@ public: { TRACE("[TestSensor] sampleImpl() \n"); return TestData( - 10 * sin(Constants::PI * static_cast<float>(miosix::getTick()) / - static_cast<float>(miosix::TICK_FREQ))); + 10 * sin(Constants::PI * static_cast<float>(miosix::getTime()) / + static_cast<float>(Constants::NS_IN_MS))); } }; diff --git a/src/shared/utils/TestUtils/ThroughputCalculator.h b/src/shared/utils/TestUtils/ThroughputCalculator.h index 6de5a3fe071ad49f9503cc4622bc013adbded170..13f1ff4e47bc8bbdd1064696fca5316183c576d5 100644 --- a/src/shared/utils/TestUtils/ThroughputCalculator.h +++ b/src/shared/utils/TestUtils/ThroughputCalculator.h @@ -54,8 +54,9 @@ public: * * @param expectedPktInterval Expected packet delivery interval for packet * loss estimation - * @param windowDuration Duration of the observation window. Longer windows - * means more stable values but slow response to abrupt changes + * @param windowDuration Duration of the observation window in milliseconds. + * Longer windows means more stable values but slow response to abrupt + * changes */ ThroughputCalculator(unsigned int expectedPktInterval, unsigned int windowDuration = 2000) @@ -74,7 +75,7 @@ public: { Lock<FastMutex> lock(mutexPkt); - long long ts = miosix::getTick(); + long long ts = miosix::getTime(); unsigned int interval = 0; if (packets.size() > 0) @@ -95,7 +96,7 @@ public: { Lock<FastMutex> lock(mutexPkt); - long long ts = miosix::getTick(); + long long ts = miosix::getTime(); removeOldPackets(ts); float sum = 0; @@ -113,7 +114,7 @@ public: float getPacketLoss() { Lock<FastMutex> lock(mutexPkt); - long long ts = miosix::getTick(); + long long ts = miosix::getTime(); removeOldPackets(ts); float avgInterval = std::numeric_limits<float>::infinity(); @@ -137,7 +138,7 @@ public: float getPacketsPerSecond() { Lock<FastMutex> lock(mutexPkt); - long long ts = miosix::getTick(); + long long ts = miosix::getTime(); removeOldPackets(ts); return (float)packets.size() / (windowDuration / 1000.0f); @@ -156,22 +157,26 @@ public: private: struct Packet { - long long timestamp; + long long timestamp; //!< Timestamp of the packet in nanoseconds size_t size; - unsigned int interval; + unsigned int interval; //!< Time interval from the previous packet in + //!< nanoseconds }; void removeOldPackets(long long ts) { while (packets.size() > 0 && - packets.front().timestamp < ts - windowDuration) + packets.front().timestamp < + ts - (windowDuration * Constants::NS_IN_MS)) { packets.pop_front(); } } - unsigned int expectedPktInterval; - unsigned int windowDuration; + unsigned int expectedPktInterval; //!< Expected packet delivery interval in + //!< nanoseconds + unsigned int windowDuration; //!< Duration of the observation window in + //!< milliseconds deque<Packet> packets; FastMutex mutexPkt; diff --git a/src/shared/utils/gui/ScreenManager.h b/src/shared/utils/gui/ScreenManager.h index ce731f437f638bdf904b44e0f5d1fb89ff11a69b..d150d835eccf3d5bb08b222538a288cdf69b1c4f 100644 --- a/src/shared/utils/gui/ScreenManager.h +++ b/src/shared/utils/gui/ScreenManager.h @@ -82,11 +82,12 @@ protected: screens[activeScreen]->invalidateTree(); } - long long start = miosix::getTick(); + long long start = miosix::getTime(); drawViewTree(screens[activeScreen], dc); - miosix::Thread::sleepUntil(start + refreshInterval); + miosix::Thread::nanoSleepUntil( + start + (refreshInterval * Constants::NS_IN_MS)); } } @@ -119,7 +120,7 @@ private: mxgui::DrawingContext dc; - unsigned int refreshInterval; + unsigned int refreshInterval; ///< Refresh interval in ms NavController controller; std::map<uint8_t, View*> screens;