Skip to content

Fix frequent motor on off issue#7

Merged
arghyabi merged 1 commit into
masterfrom
fix_motor_frequent_on_off_issue
Sep 26, 2025
Merged

Fix frequent motor on off issue#7
arghyabi merged 1 commit into
masterfrom
fix_motor_frequent_on_off_issue

Conversation

@arghyabi
Copy link
Copy Markdown
Owner

No description provided.

@arghyabi arghyabi requested a review from Copilot September 26, 2025 05:33
Signed-off-by: Arghya Biswas <arghyabiswas05@gmail.com>
@arghyabi arghyabi force-pushed the fix_motor_frequent_on_off_issue branch from f36247e to 6bc5b86 Compare September 26, 2025 05:34
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a motor control issue by implementing hysteresis logic to prevent frequent on/off switching. The changes introduce new water level checking functions with built-in margins and update the main control logic to use these functions instead of direct distance comparisons.

  • Adds hysteresis logic with 5cm margins to prevent rapid motor switching
  • Updates water level thresholds and improves pre-night fill conditions
  • Refactors main control flow to use new utility functions for consistent water level checks

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
Scripts/Utility.py Adds three new functions with hysteresis logic for water level comparisons
Scripts/Main.py Updates motor control logic to use new utility functions and improves variable naming
Scripts/Common.py Updates water level constants, adds detailed tank diagram, and expands time constants

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread Scripts/Utility.py
import json
import os
from Common import RT_DB_FILE
from Common import *
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using wildcard imports (from Common import *) is discouraged as it can lead to namespace pollution and makes it difficult to track which symbols are being imported. Consider importing only the specific constants needed: from Common import RT_DB_FILE, TWO_THIRD_LEVEL, MAX_WATER_LEVEL, TANK_HEIGHT, BOTTOM_FULL_DISTANCE.

Suggested change
from Common import *
from Common import RT_DB_FILE, TWO_THIRD_LEVEL, MAX_WATER_LEVEL, TANK_HEIGHT, BOTTOM_FULL_DISTANCE

Copilot uses AI. Check for mistakes.
Comment thread Scripts/Utility.py

def ifWaterLevelBelowTwoThird(waterLevel):
# Allow a margin of +/- 5 cm around TWO_THIRD_LEVEL for consistency (hysteresis)
margin = 5
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The margin value of 5 is duplicated across all three functions. Consider defining this as a constant (e.g., HYSTERESIS_MARGIN = 5) in Common.py to improve maintainability and consistency.

Copilot uses AI. Check for mistakes.
Comment thread Scripts/Utility.py

def ifWaterLevelAboveMax(waterLevel):
# Allow a margin of +/- 5 cm around MAX_WATER_LEVEL for consistency (hysteresis)
margin = 5
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The margin value of 5 is duplicated across all three functions. Consider defining this as a constant (e.g., HYSTERESIS_MARGIN = 5) in Common.py to improve maintainability and consistency.

Copilot uses AI. Check for mistakes.
Comment thread Scripts/Utility.py

def ifWaterLevelBelowMin(waterLevel):
# Allow a margin of +/- 5 cm around BOTTOM_FULL_DISTANCE for consistency (hysteresis)
margin = 5
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The margin value of 5 is duplicated across all three functions. Consider defining this as a constant (e.g., HYSTERESIS_MARGIN = 5) in Common.py to improve maintainability and consistency.

Copilot uses AI. Check for mistakes.
Comment thread Scripts/Main.py
@@ -154,7 +155,6 @@ def main():
distance = readDistance(gpio, ULTRASONIC_TRIG, ULTRASONIC_ECHO)
print(f"Distance: {distance} cm")
waterLevel = TANK_HEIGHT - distance
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rtDb variable is being used but the line rtDb = readRtDb() was removed on line 157. This will cause a NameError when this code executes.

Suggested change
waterLevel = TANK_HEIGHT - distance
waterLevel = TANK_HEIGHT - distance
rtDb = readRtDb()

Copilot uses AI. Check for mistakes.
Comment thread Scripts/Common.py
BOTTOM_FULL_DISTANCE = 30

MAX_WATER_LEVEL = TANK_HEIGHT - TOP_EMPTY_DISTANCE
TWO_THIRD_LEVEL = MAX_WATER_LEVEL // 3 * 2 # Two-thirds full level
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to operator precedence, this calculates (MAX_WATER_LEVEL // 3) * 2 instead of the intended two-thirds. This should be TWO_THIRD_LEVEL = MAX_WATER_LEVEL * 2 // 3 to get the correct two-thirds calculation.

Suggested change
TWO_THIRD_LEVEL = MAX_WATER_LEVEL // 3 * 2 # Two-thirds full level
TWO_THIRD_LEVEL = MAX_WATER_LEVEL * 2 // 3 # Two-thirds full level

Copilot uses AI. Check for mistakes.
@arghyabi arghyabi merged commit 1c3e36f into master Sep 26, 2025
1 check passed
@arghyabi arghyabi deleted the fix_motor_frequent_on_off_issue branch September 26, 2025 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants