ADD reset button with its own dispatch queue

This commit is contained in:
fastium 2024-12-31 10:31:00 +01:00
parent 031b98a0f7
commit 34a57162ba
3 changed files with 71 additions and 10 deletions

View File

@ -7,7 +7,7 @@
#include "mbed.h" // NOLINT #include "mbed.h" // NOLINT
#include "mbed_trace.h" // NOLINT #include "mbed_trace.h" // NOLINT
// #include "static_scheduling/bike_system.hpp" // #include "static_scheduling_with_event/bike_system.hpp"
#include "multi_tasking/bike_system.hpp" #include "multi_tasking/bike_system.hpp"
#if defined(MBED_CONF_MBED_TRACE_ENABLE) #if defined(MBED_CONF_MBED_TRACE_ENABLE)
@ -21,7 +21,8 @@ int main() {
// static_scheduling::BikeSystem bikeSystem; // static_scheduling::BikeSystem bikeSystem;
// bikeSystem.start(); // bikeSystem.start();
// bikeSystem.startWithEventQueue(); // static_scheduling_with_event::BikeSystem bikeSystem;
// bikeSystem.start();
multi_tasking::BikeSystem bikeSystem; multi_tasking::BikeSystem bikeSystem;
bikeSystem.start(); bikeSystem.start();

View File

@ -35,6 +35,28 @@
namespace multi_tasking { namespace multi_tasking {
static constexpr std::chrono::milliseconds kGearTaskPeriod = 800ms;
static constexpr std::chrono::milliseconds kGearTaskDelay = 0ms;
static constexpr std::chrono::milliseconds kGearTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kSpeedDistanceTaskPeriod = 400ms;
static constexpr std::chrono::milliseconds kSpeedDistanceTaskDelay = 0ms; // 0 or 100ms
static constexpr std::chrono::milliseconds kSpeedDistanceTaskComputationTime = 200ms;
static constexpr std::chrono::milliseconds kDisplayTask1Period = 1600ms;
static constexpr std::chrono::milliseconds kDisplayTask1Delay = 300ms;
static constexpr std::chrono::milliseconds kDisplayTask1ComputationTime = 200ms;
static constexpr std::chrono::milliseconds kResetTaskPeriod = 800ms;
static constexpr std::chrono::milliseconds kResetTaskDelay = 700ms;
static constexpr std::chrono::milliseconds kResetTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kTemperatureTaskPeriod = 1600ms;
static constexpr std::chrono::milliseconds kTemperatureTaskDelay = 1100ms;
static constexpr std::chrono::milliseconds kTemperatureTaskComputationTime = 100ms;
static constexpr std::chrono::milliseconds kDisplayTask2Period = 1600ms;
static constexpr std::chrono::milliseconds kDisplayTask2Delay = 1200ms;
static constexpr std::chrono::milliseconds kDisplayTask2ComputationTime = 100ms;
static constexpr std::chrono::milliseconds kCPUTaskPeriod = 1600ms;
static constexpr std::chrono::milliseconds kCPUTaskDelay = 0ms;
static constexpr std::chrono::milliseconds kCPUTaskComputationTime = 0ms;
BikeSystem::BikeSystem() BikeSystem::BikeSystem()
: _gearDevice(), : _gearDevice(),
_pedalDevice(), _pedalDevice(),
@ -43,17 +65,46 @@ BikeSystem::BikeSystem()
_cpuLogger(_timer) {} _cpuLogger(_timer) {}
void BikeSystem::start() { void BikeSystem::start() {
// new thread dedicated for ISRs with its event queue
tr_info("Starting Super-Loop with event handling"); tr_info("Starting Super-Loop with event handling");
init(); init();
_isrEventThread.start(callback(this, &BikeSystem::dispatch_isr_events)); Event<void()> gearEvent(&_eventQueue, callback(this, &BikeSystem::gearTask));
_mainEventThread.start(callback(this, &BikeSystem::dispatch_events)); gearEvent.delay(kGearTaskDelay);
gearEvent.period(kGearTaskPeriod);
gearEvent.post();
Event<void()> speedDistanceEvent(&_eventQueue,
callback(this, &BikeSystem::speedDistanceTask));
speedDistanceEvent.delay(kSpeedDistanceTaskDelay);
speedDistanceEvent.period(kSpeedDistanceTaskPeriod);
speedDistanceEvent.post();
Event<void()> display1Event(&_eventQueue, callback(this, &BikeSystem::displayTask1));
display1Event.delay(kDisplayTask1Delay);
display1Event.period(kDisplayTask1Period);
display1Event.post();
Event<void()> temperatureEvent(&_eventQueue,
callback(this, &BikeSystem::temperatureTask));
temperatureEvent.delay(kTemperatureTaskDelay);
temperatureEvent.period(kTemperatureTaskPeriod);
temperatureEvent.post();
Event<void()> display2Event(&_eventQueue, callback(this, &BikeSystem::displayTask2));
display2Event.delay(kDisplayTask2Delay);
display2Event.period(kDisplayTask2Period);
display2Event.post();
osStatus status =
_isrEventThread.start(callback(this, &BikeSystem::dispatch_isr_events));
tr_info("Thread %s started with status %d", _isrEventThread.get_name(), status);
dispatch_events();
} }
void BikeSystem::onReset() { void BikeSystem::onReset() {
_resetTime = _timer.elapsed_time();
Event<void()> resetEvent(&_isrEventQueue, callback(this, &BikeSystem::resetTask)); Event<void()> resetEvent(&_isrEventQueue, callback(this, &BikeSystem::resetTask));
resetEvent.post(); resetEvent.post();
} }
@ -79,7 +130,7 @@ void BikeSystem::init() {
} }
// enable/disable task logging // enable/disable task logging
_taskLogger.enable(true); _taskLogger.enable(false);
} }
void BikeSystem::gearTask() { void BikeSystem::gearTask() {
@ -128,6 +179,7 @@ void BikeSystem::temperatureTask() {
void BikeSystem::resetTask() { void BikeSystem::resetTask() {
auto taskStartTime = _timer.elapsed_time(); auto taskStartTime = _timer.elapsed_time();
std::chrono::microseconds responseTime = _timer.elapsed_time() - _resetTime;
tr_info("Reset task: response time is %" PRIu64 " usecs", responseTime.count()); tr_info("Reset task: response time is %" PRIu64 " usecs", responseTime.count());
_speedometer.reset(); _speedometer.reset();
@ -163,8 +215,16 @@ void BikeSystem::displayTask2() {
void BikeSystem::cpuTask() { _cpuLogger.printStats(); } void BikeSystem::cpuTask() { _cpuLogger.printStats(); }
void BikeSystem::dispatch_isr_events() { _isrEventQueue.dispatch_forever(); } void BikeSystem::dispatch_isr_events() {
tr_info("Start dispatching isr events");
_isrEventQueue.dispatch_forever();
tr_info("Stop dispatching isr events");
}
void BikeSystem::dispatch_events() { _eventQueue.dispatch_forever(); } void BikeSystem::dispatch_events() {
tr_info("Start dispatching main events");
_eventQueue.dispatch_forever();
tr_info("Stop dispatching main events");
}
} // namespace multi_tasking } // namespace multi_tasking

View File

@ -25,7 +25,6 @@
#pragma once #pragma once
// from advembsof // from advembsof
#include "EventQueue.h"
#include "cpu_logger.hpp" #include "cpu_logger.hpp"
#include "display_device.hpp" #include "display_device.hpp"
#include "mbed.h" #include "mbed.h"
@ -77,6 +76,7 @@ class BikeSystem {
void cpuTask(); void cpuTask();
// timer instance used for loggint task time and used by ResetDevice // timer instance used for loggint task time and used by ResetDevice
std::chrono::microseconds _resetTime = std::chrono::microseconds::zero();
Timer _timer; Timer _timer;
// data member that represents the device for manipulating the gear // data member that represents the device for manipulating the gear
GearDevice _gearDevice; GearDevice _gearDevice;