Skip to content

Refactor: Extract hard-coded magic numbers in main.py and car.py into config.py #4

@Rishik-Y

Description

@Rishik-Y

Problem

Several important simulation constants appear as magic numbers inline in main.py and car.py rather than being declared in config.py. This makes tuning the simulation very difficult.

Examples found in main.py:

  • MPC_INTERVAL = 0.1 (10Hz) — should be MPC_RATE_HZ = 10 in config
  • TRAFFIC_UPDATE_INTERVAL = 1.0 (1Hz) — should be TRAFFIC_UPDATE_HZ = 1 in config
  • GLOBAL_OPT_INTERVAL = 30.0 — should be GLOBAL_OPT_INTERVAL_S = 30 in config
  • dt=1.0/60.0 hardcoded in KalmanFilter instantiation — should use SIMULATION_HZ = 60
  • 1 / ZOOM_FACTOR inline in event handler — clean up zoom logic

Requested Changes

  1. Move all timing constants (MPC_INTERVAL, TRAFFIC_UPDATE_INTERVAL, GLOBAL_OPT_INTERVAL, SIMULATION_HZ) to config.py
  2. Add a SIMULATION_HZ constant and derive dt from it
  3. Replace all inline magic numbers in main.py, car.py, and mpc_controller.py with named constants from config.py
  4. Group constants in config.py into logical sections with comments:
    • Display constants (WIDTH, HEIGHT, PADDING, etc.)
    • Physics constants (SPEED_MS_EMPTY, SPEED_MS_LOADED, etc.)
    • Simulation timing (SIMULATION_HZ, MPC_RATE_HZ, etc.)
    • Algorithm tuning (POINTS_PER_SEGMENT, ZOOM_FACTOR, etc.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions