Files
ros2-assignments/doc/nodes/GradeCalculator.md

2.6 KiB

GradeCalculator (assignments::one::grade_calculator::GradeCalculator)

Overview

The GradeCalculator node provides a ROS2 service for calculating student exam grades. It processes exam scores, applies custom logic for specific student names, and ensures grade results are within valid bounds.

Implementation Details

Constructor

GradeCalculator()
  • Initializes ROS2 node with name grade_calculator
  • Creates a ROS2 service server for grade_calculator_service
  • Binds the service callback to handle grade calculation requests
  • Logs service startup

Core Functions

void grade_calculator_callback(const Exams::Request::SharedPtr request, const Exams::Response::SharedPtr response)

  • Checks if exam grades are provided
  • Calculates the total and average of exam grades
  • Converts student name to lowercase for comparison
  • Adds a bonus of 10 points if the student name is "wessel"
  • Ensures the final grade is clamped between 10 and 100
  • Sends the calculated grade back through the service response

Unit Tests

Unit tests for GradeCalculator are implemented in src/g2_2025_grade_calculator_pkg/test/GradeCalculator.test.cpp using Google Test and ROS2 test utilities. The tests use a client to call the grade calculation service and verify the results.

Test Cases

1. NormalAverage

Description: Verifies that the service returns the average of the provided grades for a normal student name.

  • Input:
    • Student: "Alice"
    • Grades: [80, 90, 70]
  • Expected Output:
    • Result: 80 (average of 80, 90, 70)

2. WesselBonus

Description: Checks that the student name "Wessel" (case-insensitive) receives a 10-point bonus, and the result is clamped to 100 if necessary.

  • Input:
    • Student: "Wessel"
    • Grades: [80, 90, 70]
  • Expected Output:
    • Result: 90 (average 80 + 10 bonus)

3. wesselBonus

Description: Checks that the bonus logic is case-insensitive for the student name "wessel".

  • Input:
    • Student: "wessel"
    • Grades: [80, 90, 70]
  • Expected Output:
    • Result: 90 (average 80 + 10 bonus)

4. GradeTooHigh

Description: Ensures that the grade is clamped to a maximum of 100, even after applying the bonus.

  • Input:
    • Student: "Wessel"
    • Grades: [100, 100, 100]
  • Expected Output:
    • Result: 100 (average 100 + 10 bonus, clamped to 100)

5. GradeTooLow

Description: Ensures that the grade is clamped to a minimum of 10.

  • Input:
    • Student: "Alice"
    • Grades: [0, 0, 0]
  • Expected Output:
    • Result: 10 (average 0, clamped to 10)