ADD gear event driven

This commit is contained in:
fastium
2024-12-31 13:47:36 +01:00
parent 34a57162ba
commit f64b8bdd9a
4 changed files with 107 additions and 53 deletions

View File

@@ -28,6 +28,8 @@
#include <chrono>
#include "cmsis_os2.h"
#include "constants.hpp"
#include "mbed_trace.h"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "BikeSystem"
@@ -58,22 +60,19 @@ static constexpr std::chrono::milliseconds kCPUTaskDelay = 0
static constexpr std::chrono::milliseconds kCPUTaskComputationTime = 0ms;
BikeSystem::BikeSystem()
: _gearDevice(),
: _gearDevice(callback(this, &BikeSystem::onGearUp),
callback(this, &BikeSystem::onGearDown)),
_pedalDevice(),
_resetDevice(callback(this, &BikeSystem::onReset)),
_speedometer(_timer),
_cpuLogger(_timer) {}
_cpuLogger(_timer),
_isrEventThread(osPriorityNormal, OS_STACK_SIZE, nullptr, "ISR_Event") {}
void BikeSystem::start() {
tr_info("Starting Super-Loop with event handling");
init();
Event<void()> gearEvent(&_eventQueue, callback(this, &BikeSystem::gearTask));
gearEvent.delay(kGearTaskDelay);
gearEvent.period(kGearTaskPeriod);
gearEvent.post();
Event<void()> speedDistanceEvent(&_eventQueue,
callback(this, &BikeSystem::speedDistanceTask));
speedDistanceEvent.delay(kSpeedDistanceTaskDelay);
@@ -98,7 +97,9 @@ void BikeSystem::start() {
osStatus status =
_isrEventThread.start(callback(this, &BikeSystem::dispatch_isr_events));
tr_info("Thread %s started with status %d", _isrEventThread.get_name(), status);
if (status != osOK) {
tr_error("Thread %s started with status %d", _isrEventThread.get_name(), status);
}
dispatch_events();
}
@@ -109,6 +110,19 @@ void BikeSystem::onReset() {
resetEvent.post();
}
void BikeSystem::onGearUp() {
_onGearUpTime = _timer.elapsed_time();
Event<void()> gearUpEvent(&_isrEventQueue, callback(this, &BikeSystem::gearUpTask));
gearUpEvent.post();
}
void BikeSystem::onGearDown() {
_onGearDownTime = _timer.elapsed_time();
Event<void()> gearDownEvent(&_isrEventQueue,
callback(this, &BikeSystem::gearDownTask));
gearDownEvent.post();
}
#if defined(MBED_TEST_MODE)
const advembsof::TaskLogger& BikeSystem::getTaskLogger() { return _taskLogger; }
#endif // defined(MBED_TEST_MODE)
@@ -120,7 +134,7 @@ void BikeSystem::init() {
// initialize the lcd display
disco::ReturnCode rc = _displayDevice.init();
if (rc != disco::ReturnCode::Ok) {
tr_error("Failed to initialized the lcd display: %d", static_cast<int>(rc));
tr_error("Ffalseailed to initialized the lcd display: %ld", static_cast<int>(rc));
}
// initialize the sensor device
@@ -130,16 +144,43 @@ void BikeSystem::init() {
}
// enable/disable task logging
_taskLogger.enable(false);
_taskLogger.enable(true);
}
void BikeSystem::gearTask() {
// gear task
auto taskStartTime = _timer.elapsed_time();
void BikeSystem::gearUpTask() {
auto taskStartTime = _timer.elapsed_time();
std::chrono::microseconds responseTime = _timer.elapsed_time() - _onGearUpTime;
tr_info("Gear up task: response time is %" PRIu64 " usecs", responseTime.count());
// no need to protect access to data members (single threaded)
_currentGear = _gearDevice.getCurrentGear();
_currentGearSize = _gearDevice.getCurrentGearSize();
// CRITICAL SECTION
mutexGear.lock();
if (_currentGear < bike_computer::kMaxGear) {
_currentGear++;
mutexGearSize.lock();
_currentGearSize = bike_computer::kMaxGearSize - _currentGear;
mutexGearSize.unlock();
}
mutexGear.unlock();
// END CRITICAL SECTION
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kGearTaskIndex, taskStartTime);
}
void BikeSystem::gearDownTask() {
auto taskStartTime = _timer.elapsed_time();
std::chrono::microseconds responseTime = _timer.elapsed_time() - _onGearDownTime;
tr_info("Gear down task: response time is %" PRIu64 " usecs", responseTime.count());
// CRITICAL SECTION
mutexGear.lock();
if (_currentGear > bike_computer::kMinGear) {
_currentGear--;
mutexGearSize.lock();
_currentGearSize = bike_computer::kMaxGearSize - _currentGear;
mutexGearSize.unlock();
}
mutexGear.unlock();
// END CRITICAL SECTION
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kGearTaskIndex, taskStartTime);
@@ -150,7 +191,7 @@ void BikeSystem::speedDistanceTask() {
const auto pedalRotationTime = _pedalDevice.getCurrentRotationTime();
_speedometer.setCurrentRotationTime(pedalRotationTime);
_speedometer.setGearSize(_currentGearSize);
_speedometer.setGearSize(getCurrentGearSize());
_currentSpeed = _speedometer.getCurrentSpeed();
_traveledDistance = _speedometer.getDistance();
@@ -190,7 +231,7 @@ void BikeSystem::resetTask() {
void BikeSystem::displayTask1() {
auto taskStartTime = _timer.elapsed_time();
_displayDevice.displayGear(_currentGear);
_displayDevice.displayGear(getCurrentGear());
_displayDevice.displaySpeed(_currentSpeed);
_displayDevice.displayDistance(_traveledDistance);
@@ -227,4 +268,28 @@ void BikeSystem::dispatch_events() {
tr_info("Stop dispatching main events");
}
uint8_t BikeSystem::getCurrentGear() {
uint8_t currentGear;
// CRITICAL SECTION
mutexGear.lock();
currentGear = _currentGear;
mutexGear.unlock();
// END CRITICAL SECTION
return currentGear;
}
uint8_t BikeSystem::getCurrentGearSize() {
uint8_t currentGearSize;
// CRITICAL SECTION
mutexGearSize.lock();
currentGearSize = _currentGearSize;
mutexGearSize.unlock();
// END CRITICAL SECTION
return currentGearSize;
}
} // namespace multi_tasking