Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Morse code fun again #200

Merged
merged 7 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions components/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ def climbing_arm_retracted(self) -> None:
self.high_priority_pattern = None

def morse(self) -> None:
self.pattern = Morse(HsvColour.ORANGE)
match wpilib.DriverStation.getAlliance():
case wpilib.DriverStation.Alliance.kBlue:
colour = HsvColour.BLUE
case wpilib.DriverStation.Alliance.kRed:
colour = HsvColour.RED
case _:
colour = HsvColour.ORANGE
if isinstance(self.pattern, Morse):
self.pattern.colour = colour
else:
self.pattern = Morse(colour)

def rainbow(self) -> None:
self.pattern = Rainbow(HsvColour.RED)
Expand Down Expand Up @@ -173,7 +183,6 @@ class Morse(TimeBasedPattern):
MESSAGES = (
"KILL ALL HUMANS",
"MORSE CODE IS FOR NERDS",
"HONEYBADGER DONT CARE",
"GLHF",
"I HATE MORSE CODE",
)
Expand Down Expand Up @@ -221,16 +230,20 @@ class Morse(TimeBasedPattern):

def __post_init__(self) -> None:
self.pick_new_message()
self.start_clock()

def start_clock(self) -> None:
self.start_time = self.clock()

def elapsed_time(self) -> float:
return self.clock() - self.start_time

def update(self) -> Hsv:
elapsed_time = self.elapsed_time()

# End of the message. Pick new one
if elapsed_time > self.message_time:
self.pick_new_message()
# End of message, repeat the message
self.start_clock()
return HsvColour.OFF.value

# TODO Might be better to store current token index and time?
Expand All @@ -255,7 +268,6 @@ def update(self) -> Hsv:

def pick_new_message(self) -> None:
# QUESTION? Should functions take args or assume previous step already done
self.start_time = self.clock() # Reset the time of the pattern
self.message = self.random_message()
self.morse_message = self.translate_message(self.message)
self.message_length = self.calculate_message_length(self.morse_message)
Expand All @@ -268,16 +280,16 @@ def random_message(self) -> str:
@classmethod
def translate_message(cls, message: str) -> str:
message = message.upper()
morse_message = ""
morse_message = []
for letter in message:
if letter == " ":
morse_message += " "
morse_message.append("")
continue
morse_message += cls.MORSE_TRANSLATION[letter] + " "
morse_message.append(cls.MORSE_TRANSLATION[letter])

# Add some space at end of message
morse_message += " "
return morse_message
morse_message.append(" ")
return " ".join(morse_message)

@classmethod
def calculate_message_length(cls, morse_message: str) -> int:
Expand Down
5 changes: 3 additions & 2 deletions robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ def disabledPeriodic(self) -> None:
self.chassis.update_odometry()

self.intake_component.maybe_reindex_deployment_encoder()
self.status_lights.execute()
self.vision_port.execute()
self.vision_starboard.execute()

Expand All @@ -223,14 +222,16 @@ def disabledPeriodic(self) -> None:
distance_between(intended_start_pose, current_pose)
< self.START_POS_TOLERANCE
):
self.status_lights.rainbow()
self.status_lights.morse()
else:
self.status_lights.invalid_start()
else:
self.status_lights.missing_start_pose()
else:
self.status_lights.missing_start_pose()

self.status_lights.execute()

def autonomousInit(self) -> None:
self.field.getObject("Intended start pos").setPoses([])

Expand Down