Files
venus/dbus-generator-ramp/config.py
2026-03-26 18:23:55 +00:00

262 lines
10 KiB
Python

"""
Configuration for Generator Current Ramp Controller
All tunable parameters in one place for easy adjustment.
"""
# =============================================================================
# D-BUS SERVICE CONFIGURATION
# =============================================================================
DBUS_CONFIG = {
# VE.Bus inverter/charger service
'vebus_service': 'com.victronenergy.vebus.ttyS4',
# Generator start/stop service
'generator_service': 'com.victronenergy.generator.startstop0',
# Which AC input the generator is connected to (1 or 2)
'generator_ac_input': 1,
}
# =============================================================================
# GENERATOR STATE VALUES
# =============================================================================
GENERATOR_STATE = {
'STOPPED': 0,
'RUNNING': 1,
'WARMUP': 2,
'COOLDOWN': 3,
'ERROR': 10,
}
# =============================================================================
# CURRENT LIMIT CONFIGURATION
# =============================================================================
RAMP_CONFIG = {
# Starting current limit after generator warm-up (Amps)
'initial_current': 40.0,
# Target current limit to ramp up to (Amps)
# This is the max achievable at high output loads (2500-5000W)
# Actual limit is constrained by PowerCorrelationModel based on output power
'target_current': 54.0,
# Absolute minimum current limit - safety floor (Amps)
'minimum_current': 30.0,
# Maximum current limit - will be read from inverter but this is a sanity check
'maximum_current': 100.0,
# Initial ramp rate: Amps per minute
# 10A over 30 minutes = 0.333 A/min
'initial_ramp_rate': 0.333,
# Recovery ramp rate after overload: Amps per minute (faster)
'recovery_ramp_rate': 0.5,
# How long to wait at initial_current after overload before ramping again (seconds)
'cooldown_duration': 300, # 5 minutes
# Safety margin below last stable point for recovery target (Amps)
'recovery_margin': 2.0,
# Additional margin per overload event (Amps)
'margin_per_overload': 2.0,
# Time stable at target before clearing overload history (seconds)
# Allows system to attempt full power again after extended stable operation
'history_clear_time': 1800, # 30 minutes
# How often to update the current limit (seconds)
# More frequent = smoother ramp, but more D-Bus writes
'ramp_update_interval': 30,
# --- Fast Recovery Settings ---
# When recovering, fast-ramp to (overload_current - fast_recovery_margin)
# then slow-ramp from there to the target
'fast_recovery_margin': 4.0, # Amps below overload point
# If overload occurs again within this time, use larger margin
'rapid_overload_threshold': 120, # 2 minutes
# Additional margin when overloads happen in rapid succession
'rapid_overload_extra_margin': 2.0, # Added to fast_recovery_margin
# Fast ramp rate (Amps per minute) - much faster than normal
'fast_ramp_rate': 5.0, # 5A per minute = 12 seconds per amp
# --- Return to prior stable current after overload ---
# If we were stable at current limit for this long, assume overload was
# e.g. a step load (e.g. heater switched on) and recovery target = that current.
'return_to_stable_after_overload': True,
'return_to_stable_min_duration': 1800, # Seconds (30 minutes) stable before returning to that current
}
# =============================================================================
# OUTPUT POWER CORRELATION LEARNING
# =============================================================================
# Output power determines how much generator power smoothing is applied by the
# inverter/charger. Higher output loads pass through more directly, allowing
# higher input current limits. Lower output loads stress the inverter more
# (all input goes to battery charging), requiring lower input limits.
#
# Key insight: Quick INPUT power fluctuations indicate overload/instability
# regardless of output load level - these should always trigger detection.
LEARNING_CONFIG = {
# Enable/disable learning system
'enabled': True,
# How many data points to keep for correlation analysis
'max_data_points': 100,
# Minimum stable time before recording a data point (seconds)
'min_stable_time': 60,
# Output power zones for adaptive current limits
# Based on observed relationship between output load and achievable input current:
# - Low output (0-1500W): Inverter does most smoothing, ~45A achievable
# - Medium output (1500-2500W): Transitional zone, ~49A achievable
# - High output (2500-5000W): Direct passthrough, ~54A achievable
'power_zones': {
# (min_output_watts, max_output_watts): max_input_current_offset
# Offset is added to base learned limit (45A)
'LOW': (0, 1500, 0.0), # Low loads: 45A limit
'MEDIUM': (1500, 2500, 4.0), # Transitional: 49A limit
'HIGH': (2500, 5000, 9.0), # High loads: 54A limit
'VERY_HIGH': (5000, 10000, 9.0), # Very high: cap at 54A (same as HIGH)
},
# Initial model parameters (linear: max_current = base + slope * output_power)
# Base current is achievable at LOW output (0-1500W)
# Slope is 0 because we use discrete zones for step changes
'initial_base_current': 45.0,
'initial_slope': 0.0, # Use zones for discrete steps instead of continuous slope
# Learning rate for updating model (0-1, higher = faster adaptation)
'learning_rate': 0.1,
# Minimum confidence (data points) before using learned model
'min_confidence': 5,
# --- Output Power Change Detection ---
# If output power increases by this much, re-evaluate recovery target
# Higher output = more power passes through to loads = less inverter stress
'output_power_increase_threshold': 2000, # Watts
# Minimum time between target re-evaluations (prevents rapid changes)
'min_reevaluation_interval': 60, # Seconds
}
# =============================================================================
# OVERLOAD DETECTION CONFIGURATION
# =============================================================================
# CRITICAL: Overload/instability is detected via INPUT power fluctuations.
# Quick fluctuations on INPUT (not output) indicate generator stress.
# Output power only affects the achievable input current limit, NOT detection.
# Any quick INPUT fluctuations at ANY output load level should trigger detection.
OVERLOAD_CONFIG = {
# Sampling interval for power readings (milliseconds)
'sample_interval_ms': 500,
# --- Method 1: Rate of Change Reversal Detection ---
# Detects rapid oscillations in INPUT power (sign changes in derivative)
# Minimum power change to be considered significant (Watts)
'derivative_threshold': 150,
# Number of significant reversals in window to trigger
'reversal_threshold': 5,
# Window size for reversal counting (samples)
'reversal_window_size': 20, # 10 seconds at 500ms
# --- Method 2: Detrended Standard Deviation ---
# Detects erratic INPUT power fluctuations after removing trend
# Standard deviation threshold after removing linear trend (Watts)
# Note: 400W threshold avoids false positives during normal ramp settling
'std_dev_threshold': 400,
# Window size for std dev calculation (samples)
'std_dev_window_size': 40, # 20 seconds at 500ms
# --- Confirmation (prevents false positives) ---
# How many samples to consider for confirmation
'confirmation_window': 10,
# How many positive detections needed to confirm overload
# Note: 7/10 threshold requires more persistent detection to reduce false positives
'confirmation_threshold': 7,
# --- Lockout after detection ---
# Prevent re-detection for this many seconds after triggering
'lockout_duration': 10,
# --- Ramp Start Grace Period ---
# After ramp starts, suppress detection for this many seconds
# Allows system to settle without triggering false overload detection
'ramp_start_grace_period': 30,
# --- Minimum Output Power Requirement ---
# IMPORTANT: Set to 0 to detect fluctuations at ANY output load level
# Quick INPUT fluctuations indicate overload/instability regardless of output
# Output power only affects achievable limit, not detection sensitivity
'min_output_power_for_overload': 0, # CRITICAL: Must be 0 - detect at any load
# --- Trend Direction Check ---
# If power is trending DOWN faster than this rate, ignore as load drop (not overload)
# A true overload has power oscillating at/near ceiling, not dropping
# Watts per second - negative values mean power is dropping
'trend_drop_threshold': -100, # -100W/s = ignore if dropping faster than this
# --- Input Power Drop Check ---
# If INPUT power is significantly below recent maximum, it's not an overload
# True overloads oscillate AT/NEAR a ceiling - not dropping away from it
# This catches step-change load drops that trend detection might miss
# Note: We check INPUT power (generator load), NOT output power
# Output power is unreliable because charger can absorb freed capacity
# (e.g., load turns off but charger takes over - input stays high)
'max_power_drop_for_overload': 1000, # Watts - ignore if INPUT dropped more than this
# --- Smoothing for trend detection (optional) ---
# If true, apply EMA smoothing before derivative calculation
'use_smoothing': False,
'smoothing_alpha': 0.3,
}
# =============================================================================
# LOGGING CONFIGURATION
# =============================================================================
LOGGING_CONFIG = {
# Log level: DEBUG, INFO, WARNING, ERROR
'level': 'INFO',
# Log to console (stdout)
'console': True,
# Log file path (set to None to disable file logging)
'file_path': None, # Venus uses multilog, so we log to stdout
# Include timestamps in console output (multilog adds its own)
'include_timestamp': False,
}
# =============================================================================
# MAIN LOOP TIMING
# =============================================================================
TIMING_CONFIG = {
# Main loop interval (milliseconds)
# This controls how often we check state and update
'main_loop_interval_ms': 1000,
# Timeout for D-Bus operations (seconds)
'dbus_timeout': 5,
}