2 Commits

9 changed files with 33 additions and 64 deletions

View File

@@ -84,7 +84,7 @@ std::vector<std::string> default_config_paths_ = {
#### Default Values and Fallbacks
- **host**: `"localhost"` - Local database server
- **port**: `5432` - Standard PostgreSQL port
- **dbname**: `"grades"` - Application-specific database
- **dbname**: `"imu_data"` - Application-specific database
- **user**: `"postgres"` - Default PostgreSQL user
- **password**: `"postgres"` - Default PostgreSQL password
- **timeout**: `30` seconds - connection timeout
@@ -121,7 +121,7 @@ Example complete TOML configuration:
[database]
host = "localhost"
port = 5432
dbname = "grades"
dbname = "imu_data"
user = "postgres"
password = "postgres"
timeout = 30

View File

@@ -1,9 +1,9 @@
# DatabaseManager (`assignments::one::DatabaseManager`)
# DatabaseManager (`assignments::two::DatabaseManager`)
## Overview
The `DatabaseManager` class is a PostgreSQL database interface for the ROS2 grade calculator.
It handles all database operations including connection management, table creation and data insertion.
The `DatabaseManager` class is a PostgreSQL database interface for the ROS2 IMU data collection system.
It handles all database operations including connection management, table creation and data storage.
## Implementation Details
@@ -28,7 +28,7 @@ DatabaseManager(rclcpp::Logger logger)
> Returns `true` on successful connection, `false` on failure
- Establishes connection to PostgreSQL database using connection information from the config TOML
- Connection string format: `"host=localhost port=5432 dbname=grades user=postgres password=postgres"`
- Connection string format: `"host=localhost port=5432 dbname=imu_data user=postgres password=postgres"`
**`bool is_connected() const`**
> Returns `true` if connection exists and is open
@@ -47,56 +47,25 @@ DatabaseManager(rclcpp::Logger logger)
**`void create_tables()`**
- Creates all required database tables using SQL queries from `SQLQueries.hpp`
- Tables created:
- `enrollments`: Student course enrollments
- `exam_results`: Individual exam scores
- `course_results`: Final course grades and statistics
- `imu_data`: IMU sensor readings with linear acceleration and angular velocity data
- Uses transactions for atomic table creation
**`void insert_sample_data()`**
- Inserts predefined sample student data
### Data Operations
#### Student Course Management
#### IMU Data Storage
**`std::vector<StudentCourse> queue_pending_combinations()`**
> Returns vector of StudentCourse objects for processing queue
**`bool store_imu_data(double linear_accel_x, double linear_accel_y, double linear_accel_z, double angular_vel_x, double angular_vel_y, double angular_vel_z)`**
> Returns `true` on successful storage, `false` on failure
- Gets all student-course combinations that need exam results generated
- Executes complex SQL query to find missing exam results
**`bool enroll_student_into_course(const StudentCourse& sc)`**
> Returns `true` on successful enrollment, `false` on failure
- Enrolls a student into a specific course
#### Exam Result Processing
**`bool store_exam_result(const std::string& student_name, const std::string& course_name, int grade)`**
- Stores individual exam results in the database
- Stores IMU sensor readings in the database
- Parameters:
- `student_name`: Name of the student
- `course_name`: Name of the course
- `grade`: Exam score (10-100)
**`bool store_final_course_result(const StudentCourse& sc, int exam_count, int final_grade)`**
- Stores calculated final course results
- Parameters:
- `sc`: StudentCourse object containing student and course names
- `exam_count`: Number of exams taken
- `final_grade`: Calculated final grade
- Used by grade calculation nodes for final result storage
#### Grade Retrieval
**`int get_final_course_grade(const StudentCourse& sc)`**
> Returns:
> - `> 0`: Valid final grade (rounded average)
> - `-1`: No exams taken or no results found
- Gets final calculated grade for a student-course combination
- Performs average calculation with proper rounding
- Used by nodes to check if final grading is complete
- `linear_accel_x`: Linear acceleration on X-axis
- `linear_accel_y`: Linear acceleration on Y-axis
- `linear_accel_z`: Linear acceleration on Z-axis
- `angular_vel_x`: Angular velocity around X-axis
- `angular_vel_y`: Angular velocity around Y-axis
- `angular_vel_z`: Angular velocity around Z-axis
- Automatically adds timestamp on insertion
### Logging
@@ -117,9 +86,9 @@ DatabaseManager db_manager(node->get_logger());
// Check connection status
if (db_manager.is_connected()) {
// Database ready for operations
bool success = db_manager.store_exam_result("Wessel", "ROS2", 85);
bool success = db_manager.store_imu_data(1.2, -0.5, 9.8, 0.01, 0.02, 0.03);
if (success) {
RCLCPP_INFO(logger, "Exam result stored successfully");
RCLCPP_INFO(logger, "IMU data stored successfully");
}
}
```

View File

@@ -36,7 +36,7 @@ sudo docker-compose up
```
You can configure specific database settings in the `docker-compose.yaml` in the root folder or the `config.toml` file in the `src/` folder
### Start the Grade calculator program
### Start the IMU Reader program
```bash
ros2 launch g2_2025_imu_reader_pkg imu_reader.launch.xml
```

View File

@@ -61,7 +61,7 @@ Unit tests for `ConfigManager` are implemented in `src/g2_2025_imu_reader_pkg/te
[database]
host = "localhost"
port = 5432
dbname = "grades"
dbname = "imu_data"
user = "postgres"
password = "postgres"
```

View File

@@ -18,16 +18,16 @@ Unit tests for `DatabaseManager` are implemented in `src/g2_2025_imu_reader_pkg/
- **Test Action:** Call `is_connected()` method
- **Expected Result:** Returns either `true` or `false` (no crashes or invalid states)
### 3. QueuePendingCombinationsTest
### 3. StoreIMUDataWhenNotConnected
**Description:** Verifies retrieval of pending student-course combinations that need exam results.
**Description:** Verifies that storing IMU data without an active database connection fails gracefully.
- Test action: Call `store_imu_data(linear_x, linear_y, linear_z, ang_x, ang_y, ang_z)` without an active DB connection.
- Expected result: Returns `false` and does not throw — method must check connection status before DB operations.
- **Test Action:** Call `store_imu_data(linear_x, linear_y, linear_z, ang_x, ang_y, ang_z)` without an active DB connection
- **Expected Result:** Returns `false` and does not throw — method must check connection status before DB operations
### 4. CreateTablesNoCrash
Description: Verifies calling `create_tables()` without an active connection is safe.
**Description:** Verifies calling `create_tables()` without an active connection is safe.
- Test action: Call `create_tables()` on a manager that is not connected.
- Expected result: No exception thrown; the function should be a no-op when no DB connection exists.
- **Test Action:** Call `create_tables()` on a manager that is not connected
- **Expected Result:** No exception thrown; the function should be a no-op when no DB connection exists

View File

@@ -7,7 +7,7 @@ services:
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=grades
- POSTGRES_DB=imu_data
ports:
- "5432:5432"
mosquitto:

View File

@@ -1,7 +1,7 @@
[database]
host = "localhost"
port = 5432
dbname = "grades"
dbname = "imu_data"
user = "postgres"
password = "postgres"
timeout = 30

View File

@@ -83,7 +83,7 @@ DatabaseConfig ConfigManager::parse_database_config(const toml::table& config) c
db_config.host = database_section["host"].value_or<std::string>("localhost");
db_config.port = database_section["port"].value_or<int>(5432);
db_config.dbname = database_section["dbname"].value_or<std::string>("grades");
db_config.dbname = database_section["dbname"].value_or<std::string>("imu_data");
db_config.user = database_section["user"].value_or<std::string>("postgres");
db_config.password = database_section["password"].value_or<std::string>("postgres");
db_config.timeout = database_section["timeout"].value_or<int>(30);

View File

@@ -26,7 +26,7 @@ static const std::string TEST_CONFIG_NO_POOL_CONTENT = R"(
[database]
host = "localhost"
port = 5432
dbname = "grades"
dbname = "imu_data"
user = "postgres"
password = "postgres"
)";