Skip to content

Commit

Permalink
la til triks
Browse files Browse the repository at this point in the history
  • Loading branch information
Odin Grav committed Sep 24, 2025
1 parent 8ca13db commit 86e1f13
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
51 changes: 39 additions & 12 deletions oving_3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@
#ev3.screen.print("Spiller", player.name, "...")
#player.play()

def doRandomTrick():
trickIndex = randint(0, 1)

if trickIndex == 1:
spin()
elif trickindex == 2:
playMusic()

def spin():
directions = [-1, 1]
directionIndex = randint(0, 1)
robotDriveBase.stop()
speaker.say("Look at my trick")
robotDriveBase.turn(360 * directions[directionIndex])

def playMusic():
robotDriveBase.stop()
player = RTTTLPlayer(ev3, songs[3])
ev3.screen.print("Spiller", player.name, "...")
player.play()

leftMotor = Motor(port=Port.A)
rightMotor = Motor(port=Port.C)
robotDriveBase = DriveBase(leftMotor, rightMotor, wheel_diameter=56, axle_track=160)
Expand All @@ -50,33 +71,39 @@

speaker = ev3.speaker

leftSensorThreshold = 9
rigthSensorThreshold = 9
leftSensorThreshold = 8
rigthSensorThreshold = 7
speed = 50

currentTime = time.time()
lastCorrectino = currentTime
lastDirection = 1
lastTrick = currentTime

while True:
currentTime = time.time()

if currentTime - lastCorrectino > 2:
while leftColorSensor.reflection() > 10 or rightColorSensor.reflection() < 10:
robotDriveBase.drive(-100, 50 * lastDirection)
""" if currentTime - lastCorrectino > 2:
while leftColorSensor.reflection() > leftSensorThreshold or rightColorSensor.reflection() < rigthSensorThreshold:
robotDriveBase.drive(-speed, 50 * lastDirection)
lastCorrectino = time.time()
continue
continue """

if currentTime - lastTrick > 10:
doTrick()
lastTrick = time.time()

if leftColorSensor.reflection() < leftSensorThreshold:
robotDriveBase.drive(-100, 25)
lastCorrectino == currentTime
lastDirection = 1
robotDriveBase.drive(-speed, 25)
lastCorrectino == time.time()
lastDirection = -1
continue

if rightColorSensor.reflection() < rigthSensorThreshold:
robotDriveBase.drive(-100, -25)
robotDriveBase.drive(-speed, -25)
lastCorrectino = currentTime
lastDirection = -1
lastDirection = 1
continue

robotDriveBase.drive(-100, 50)
robotDriveBase.drive(-speed, 50)
9 changes: 4 additions & 5 deletions oving_3/rtttl_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ class RTTTLPlayer:
'g#': 25.96, 'a': 27.50, 'a#': 29.14, 'b': 30.87
}

def __init__(self, ev3, rtttl: str):
def __init__(self, ev3):
"""
:param ev3: EV3Brick instance from pybricks.hubs
:param rtttl: RTTTL string (name:d=4,o=5,b=100:notes)
"""
self.ev3 = ev3
self.name, self.defaults, self.notes = self.parse_rtttl(rtttl)

def parse_rtttl(self, rtttl: str):
name, settings, notes = rtttl.split(':')
Expand All @@ -32,15 +31,15 @@ def note_to_freq(self, note: str, octave: int):
base = self.NOTE_FREQS[note]
return round(base * (2 ** octave), 2)

def play(self, tempo_scale: float = 1.0):
def play(self, name, defaults, notes, tempo_scale: float = 1.0):
"""
Play the parsed RTTTL melody.
:param tempo_scale: Scale factor for speed (1.0 = normal, 2.0 = twice as fast, 0.5 = half speed)
"""
bpm = self.defaults.get('b', 63)
bpm = defaults.get('b', 63)
whole_note = (60 / bpm) * 4 * 1000 # ms

for raw in self.notes:
for raw in notes:
match = re.match(r'(\d+)?([a-gp][#]?)(\d)?(\.*)?', raw.strip(), re.I)
if not match:
continue
Expand Down

0 comments on commit 86e1f13

Please sign in to comment.