This project implements an adaptive task scheduler for a multi-sensor system, optimizing CPU usage and power consumption based on sensor priorities and data rates. It uses FreeRTOS to manage tasks, implement inter-task communication, and showcase adaptive scheduling based on runtime task behavior.
- CMake (version 3.12 or higher)
- GCC compiler
- FreeRTOS (included as a submodule)
- Unity (included as a submodule for testing)
-
Clone the repository:
git clone https://github.com/anand2532/adaptive-task-scheduler.git cd adaptive-task-scheduler
-
Initialize and update the submodules:
git submodule init git submodule update
-
Create a build directory:
mkdir build && cd build
-
Run CMake:
cmake ..
-
Build the project:
make
This will create two executables:
AdaptiveTaskScheduler
: The main project executablerun_tests
: The test executable
To run the main program:
./AdaptiveTaskScheduler
-
Scheduler (scheduler.c, scheduler.h):
- Implements the adaptive scheduling algorithm
- Manages task priorities dynamically
-
Sensor Tasks (sensor_tasks.c, sensor_tasks.h):
- Simulates multiple sensor inputs
- Sends data to the data fusion task
-
Data Fusion (data_fusion.c, data_fusion.h):
- Combines data from multiple sensors
- Implements a simple fusion algorithm
-
Main Program (main.c):
- Sets up FreeRTOS tasks
- Initializes the scheduler and starts the FreeRTOS scheduler
-
Configuration (config.h):
- Contains project-wide configuration parameters
-
Tests:
- test_main.c: Main entry point for tests
- test_scheduler.c: Tests for the scheduler component
- test_data_fusion.c: Tests for the data fusion component
-
CMakeLists.txt Updates:
- Added FreeRTOS as a static library
- Included Unity for testing (later removed)
- Separated main executable and test executable builds
- Added pthread and rt library linking for POSIX support
-
Test Files Restructuring:
- Removed duplicate main(), setUp(), and tearDown() functions
- Created a separate test_main.c to run all tests (later removed)
- Implemented Python-based sensor simulator for more realistic testing
-
Build Process Fixes:
- Resolved issues with multiple definitions of main()
- Fixed linking problems with FreeRTOS libraries
- Ensured correct inclusion of FreeRTOS headers
-
Sensor Data Reception:
- Implemented POSIX thread-based sensor data receiver
- Added UDP socket communication for receiving simulated sensor data
-
Data Fusion Implementation:
- Created a separate data fusion task that runs periodically
- Implemented a simple averaging algorithm for fusing sensor data
-
Adaptive Scheduling:
- Added framework for adjusting task priorities based on runtime behavior
- Implemented getLatestSensorData() function for efficient data access
-
Configuration and Flexibility:
- Added config.h for centralized project configuration
- Made the number of sensors and their sampling rates configurable
-
Multiple Definition of main() Error:
- Issue: Compilation fails due to multiple definitions of the main() function.
- Solution: Ensure that only one file (main.c) contains a main() function. Remove any main() functions from test files or other source files.
-
Undefined FreeRTOS Functions:
- Issue: Compiler reports undefined references to FreeRTOS functions.
- Solution:
- Check that the FreeRTOS submodule is properly initialized:
git submodule update --init --recursive
- Verify that CMakeLists.txt correctly includes FreeRTOS headers and links the library:
target_include_directories(${PROJECT_NAME} PRIVATE ${FREERTOS_INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME} PRIVATE freertos)
- Check that the FreeRTOS submodule is properly initialized:
-
Sensor Data Not Being Received:
- Issue: The program runs but doesn't receive any sensor data.
- Solution:
- Ensure the Python sensor simulator is running.
- Check that the UDP port (default 12345) is not blocked by a firewall.
- Verify that the IP address in the simulator matches your local machine's IP.
-
Build Fails Due to Missing pthread:
- Issue: Compilation fails with errors related to pthread functions.
- Solution:
- Install pthread development libraries. On Ubuntu/Debian:
sudo apt-get install libpthread-stubs0-dev
- Ensure CMakeLists.txt links against pthread:
target_link_libraries(${PROJECT_NAME} PRIVATE pthread)
- Install pthread development libraries. On Ubuntu/Debian:
-
Data Fusion Task Not Running:
- Issue: The data fusion task doesn't seem to be executing.
- Solution:
- Check that the task is created with sufficient stack size and appropriate priority.
- Verify that FUSION_INTERVAL_MS in config.h is set to a reasonable value.
- Add debug print statements in the data fusion task to confirm it's being called.
-
Adaptive Scheduling Not Working:
- Issue: Task priorities don't seem to be adjusting as expected.
- Solution:
- Implement the adjustTaskPriority() function in scheduler.c with your desired logic.
- Add debug print statements to show when and how priorities are being adjusted.
- Ensure RUNTIME_THRESHOLD in config.h is set appropriately for your system.
-
CMake Configuration Fails:
- Issue: CMake fails to configure the project.
- Solution:
- Ensure you have CMake version 3.12 or higher installed.
- Check that all required libraries and dependencies are installed on your system.
- Verify that the project structure matches what's expected in CMakeLists.txt.
Remember to always check the console output for specific error messages, as they often provide valuable clues about the root cause of any issues you encounter.
Contributions to this project are welcome. Please follow these steps:
- Fork the repository
- Create a new branch for your feature
- Commit your changes
- Push to your branch
- Create a new Pull Request