ADD mail box for pedal device

This commit is contained in:
fastium
2025-01-03 17:10:42 +01:00
parent 946e86025c
commit 95e92e2564
5 changed files with 227 additions and 125 deletions

View File

@@ -28,9 +28,11 @@
#include <chrono>
#include "Callback.h"
#include "cmsis_os2.h"
#include "constants.hpp"
#include "mbed_trace.h"
#include "pedal_device.hpp"
#if MBED_CONF_MBED_TRACE_ENABLE
#define TRACE_GROUP "BikeSystem"
#endif // MBED_CONF_MBED_TRACE_ENABLE
@@ -62,11 +64,36 @@ static constexpr std::chrono::milliseconds kCPUTaskComputationTime = 0
BikeSystem::BikeSystem()
: _gearDevice(callback(this, &BikeSystem::onGearUp),
callback(this, &BikeSystem::onGearDown)),
_pedalDevice(),
_pedalDevice(&_mailPedalDevice),
_resetDevice(callback(this, &BikeSystem::onReset)),
_speedometer(_timer),
_cpuLogger(_timer),
_isrEventThread(osPriorityAboveNormal, OS_STACK_SIZE, nullptr, "ISR_Event") {}
_isrEventThread(osPriorityAboveNormal, OS_STACK_SIZE, nullptr, "ISR_Event"),
_speedDistanceThread(
osPriorityNormal, OS_STACK_SIZE, nullptr, "Speed_distance_Task") {}
#if defined(MBED_TEST_MODE)
const advembsof::TaskLogger& BikeSystem::getTaskLogger() { return _taskLogger; }
#endif // defined(MBED_TEST_MODE)
void BikeSystem::init() {
// start the timer
_timer.start();
// initialize the lcd display
disco::ReturnCode rc = _displayDevice.init();
if (rc != disco::ReturnCode::Ok) {
tr_error("Ffalseailed to initialized the lcd display: %ld", static_cast<int>(rc));
}
// initialize the sensor device
bool present = _sensorDevice.init();
if (!present) {
tr_error("Sensor not present or initialization failed");
}
// enable/disable task logging
_taskLogger.enable(false);
}
void BikeSystem::start() {
tr_info("Starting Super-Loop with event handling");
@@ -101,9 +128,18 @@ void BikeSystem::start() {
tr_error("Thread %s started with status %d", _isrEventThread.get_name(), status);
}
status =
_speedDistanceThread.start(callback(this, &BikeSystem::loop_speed_distance_task));
if (status != osOK) {
tr_error("Thread %s started with status %d", _isrEventThread.get_name(), status);
}
// dispathc the main queue in the main thread
dispatch_events();
}
/* Callback from isr */
void BikeSystem::onReset() {
_resetTime = _timer.elapsed_time();
Event<void()> resetEvent(&_isrEventQueue, callback(this, &BikeSystem::resetTask));
@@ -123,100 +159,7 @@ void BikeSystem::onGearDown() {
gearDownEvent.post();
}
#if defined(MBED_TEST_MODE)
const advembsof::TaskLogger& BikeSystem::getTaskLogger() { return _taskLogger; }
#endif // defined(MBED_TEST_MODE)
void BikeSystem::init() {
// start the timer
_timer.start();
// initialize the lcd display
disco::ReturnCode rc = _displayDevice.init();
if (rc != disco::ReturnCode::Ok) {
tr_error("Ffalseailed to initialized the lcd display: %ld", static_cast<int>(rc));
}
// initialize the sensor device
bool present = _sensorDevice.init();
if (!present) {
tr_error("Sensor not present or initialization failed");
}
// enable/disable task logging
_taskLogger.enable(false);
}
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());
// 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);
}
void BikeSystem::speedDistanceTask() {
auto taskStartTime = _timer.elapsed_time();
const auto pedalRotationTime = _pedalDevice.getCurrentRotationTime();
_speedometer.setCurrentRotationTime(pedalRotationTime);
_speedometer.setGearSize(getCurrentGearSize());
_currentSpeed = _speedometer.getCurrentSpeed();
_traveledDistance = _speedometer.getDistance();
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kSpeedTaskIndex, taskStartTime);
}
void BikeSystem::temperatureTask() {
auto taskStartTime = _timer.elapsed_time();
// tr_warn("Tick1 %" PRIu64, _timer.elapsed_time().count());
// no need to protect access to data members (single threaded)
_currentTemperature = _sensorDevice.readTemperature();
// tr_warn("Tick2 %" PRIu64, _timer.elapsed_time().count());
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kTemperatureTaskComputationTime - (_timer.elapsed_time() - taskStartTime)));
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kTemperatureTaskIndex, taskStartTime);
}
// ISR thread functions
void BikeSystem::resetTask() {
auto taskStartTime = _timer.elapsed_time();
@@ -229,12 +172,106 @@ void BikeSystem::resetTask() {
_timer, advembsof::TaskLogger::kResetTaskIndex, taskStartTime);
}
// Speed distance thread functions
void BikeSystem::speedDistanceTask() {
auto taskStartTime = _timer.elapsed_time();
uint32_t* currentStep = _mailPedalDevice.try_get();
if (currentStep != nullptr) {
const auto pedalRotationTime = PedalDevice::getCurrentRotationTime(*currentStep);
osStatus status = _mailPedalDevice.free(currentStep);
if (status != osOK) {
tr_error("free current step in the speed distance tasks doesn't work !");
}
// ENTER CRITICAL SECTION
_mutexSpeedometer.lock();
_speedometer.setCurrentRotationTime(pedalRotationTime);
_mutexSpeedometer.unlock();
// END CRITICAL SECTION
}
// ENTER CRITICAL SECTION
_mutexSpeedometer.lock();
_speedometer.setGearSize(getCurrentGearSize());
_mutexSpeed.lock();
_currentSpeed = _speedometer.getCurrentSpeed();
_mutexSpeed.unlock();
_mutexDistance.lock();
_traveledDistance = _speedometer.getDistance();
_mutexDistance.unlock();
_mutexSpeedometer.unlock();
// END CRITICAL SECTION
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kSpeedTaskIndex, taskStartTime);
}
/* Main thread functions */
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());
// ENTER 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());
// ENTER 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);
}
void BikeSystem::temperatureTask() {
auto taskStartTime = _timer.elapsed_time();
// no need to protect access to data members (single threaded)
_currentTemperature = _sensorDevice.readTemperature();
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kTemperatureTaskComputationTime - (_timer.elapsed_time() - taskStartTime)));
_taskLogger.logPeriodAndExecutionTime(
_timer, advembsof::TaskLogger::kTemperatureTaskIndex, taskStartTime);
}
void BikeSystem::displayTask1() {
auto taskStartTime = _timer.elapsed_time();
// ENTER CRITICAL SECTION
_displayDevice.displayGear(getCurrentGear());
_displayDevice.displaySpeed(_currentSpeed);
_displayDevice.displayDistance(_traveledDistance);
_displayDevice.displaySpeed(getCurrentSpeed());
_displayDevice.displayDistance(getCurrentDistance());
// END CRITICAL SECTION
ThisThread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(
kDisplayTask1ComputationTime - (_timer.elapsed_time() - taskStartTime)));
@@ -269,13 +306,20 @@ void BikeSystem::dispatch_events() {
tr_info("Stop dispatching main events");
}
void BikeSystem::loop_speed_distance_task() {
tr_info("Start loop speed-distance calculation");
while (true) {
speedDistanceTask();
}
}
uint8_t BikeSystem::getCurrentGear() {
uint8_t currentGear;
// CRITICAL SECTION
mutexGear.lock();
// ENTER CRITICAL SECTION
_mutexGear.lock();
currentGear = _currentGear;
mutexGear.unlock();
_mutexGear.unlock();
// END CRITICAL SECTION
return currentGear;
@@ -284,13 +328,37 @@ uint8_t BikeSystem::getCurrentGear() {
uint8_t BikeSystem::getCurrentGearSize() {
uint8_t currentGearSize;
// CRITICAL SECTION
mutexGearSize.lock();
// ENTER CRITICAL SECTION
_mutexGearSize.lock();
currentGearSize = _currentGearSize;
mutexGearSize.unlock();
_mutexGearSize.unlock();
// END CRITICAL SECTION
return currentGearSize;
}
uint32_t BikeSystem::getCurrentSpeed() {
uint32_t currentSpeed;
// ENTER CRITICAL SECTION
_mutexSpeed.lock();
currentSpeed = _currentSpeed;
_mutexSpeed.unlock();
// END CRITICAL SECTION
return currentSpeed;
}
uint32_t BikeSystem::getCurrentDistance() {
uint32_t currentDistance;
// ENTER CRITICAL SECTION
_mutexDistance.lock();
currentDistance = _traveledDistance;
_mutexDistance.unlock();
// END CRITICAL SECTION
return currentDistance;
}
} // namespace multi_tasking