This project implements a city simulation system using FreeRTOS on an STM32F756ZG development board. The simulation models the dispatching of resources to various departments like Police, Fire, Ambulance, and Corona response.
The simulation handles the following:
- Randomly generated dispatch requests for each department.
- Resource allocation and management for vehicles.
- Synchronization between tasks using semaphores.
- Performance tracking and logging of system activity.
The system is modular, ensuring each department operates independently while coordinating through shared resources and communication.
- FreeRTOS-based task management.
- Resource synchronization using semaphores and mutexes.
- Dynamic resource reallocation across departments.
- Performance statistics logging.
- Dispatcher Task: Handles event generation and dispatching to departments.
- Department Tasks: Separate tasks for Police, Fire, Ambulance, and Corona, each processing dispatch requests.
- Logger Task: Logs messages and statistics for system performance monitoring.
Used for task signaling and completion synchronization.
- Each department has a dedicated binary semaphore to signal task readiness (e.g.,
policeSemaphore
,fireSemaphore
). - Completion semaphores (e.g.,
policeCompletionSemaphore
) signal when a task has finished processing an event.
Used for protecting shared resources.
- The
vehicleMutex
ensures synchronized access to vehicle counts across departments to avoid race conditions.
- The
dispatchQueue
is used for managing dispatch requests.
- Purpose: Generates random dispatch requests and checks resource availability for each department.
- Synchronization:
- Uses
dispatchSemaphore
to ensure exclusive access to resource allocation. - Signals department tasks using their respective semaphores after ensuring resource availability.
- Uses
- Purpose: Process dispatch requests for their respective departments.
- Synchronization:
- Wait for binary semaphore signals (e.g.,
xSemaphoreTake(policeSemaphore)
). - Signal completion using completion semaphores (e.g.,
xSemaphoreGive(policeCompletionSemaphore)
).
- Wait for binary semaphore signals (e.g.,
- Purpose: Manages vehicle allocation, borrowing, and reallocation among departments.
- Synchronization:
vehicleMutex
ensures thread-safe access to vehicle counts.- Implements borrowing logic to fulfill requests when a department lacks sufficient vehicles.
- Purpose: Logs messages for system events and generates performance reports.
- Key Functions:
logMessage
: Logs formatted messages.recordTaskExecution
: Tracks task execution counts per department.recordVehicleUsage
: Tracks the number of vehicles used per department.generateStatisticsReport
: Outputs task execution and vehicle usage statistics.
-
Initialize Resources:
initVehicleManagement()
creates the vehicle mutex.initDispatcher()
creates semaphores and initializes the dispatcher task.- Each department's initialization function creates the task and semaphore for the department.
-
Start Scheduler:
- Call
vTaskStartScheduler()
to start the FreeRTOS scheduler.
- Call
void initDispatcher(void) {
dispatchQueue = xQueueCreate(10, sizeof(DispatchRequest));
dispatchSemaphore = xSemaphoreCreateBinary();
policeCompletionSemaphore = xSemaphoreCreateBinary();
// ... initialize other semaphores
xTaskCreate(randomEventAndDispatchTask, "RandomEvent", RANDOM_EVENT_STACK_SIZE, NULL, RANDOM_EVENT_PRIORITY, NULL);
}
bool checkAndAllocateVehicles(uint8_t department, int requiredVehicles) {
xSemaphoreTake(vehicleMutex, portMAX_DELAY);
int currentVehicles = getVehicleCount(department);
if (currentVehicles >= requiredVehicles) {
xSemaphoreGive(vehicleMutex);
return true;
}
reallocateVehicles((DispatchRequest){.department = department, .requiredVehicles = requiredVehicles});
xSemaphoreGive(vehicleMutex);
return false;
}
void policeTask(void *params) {
while (1) {
if (xSemaphoreTake(policeSemaphore, portMAX_DELAY) == pdTRUE) {
// Process the event
vTaskDelay(PROCESS_DELAY);
xSemaphoreGive(policeCompletionSemaphore);
}
}
}
- Execution counts and vehicle usage are tracked per department.
- Statistics are logged periodically using the
generateStatisticsReport
function.
- STM32F756ZG Development Board.
- FreeRTOS: Real-time operating system for task management.
- GCC ARM Compiler: For compiling the project.
- Build and flash the project to the STM32F756ZG board.
- Observe log outputs through UART for system behavior and performance statistics.
- Monitor vehicle allocation and task processing via the log messages.
- Dynamic priority adjustment for tasks based on resource availability.
- Implementing fault-tolerant mechanisms for task failures.
- Real-time visualization of system statistics.
Author: Haim
Date: December 29, 2024