def update(self): self.x += self.xv self.y += self.yv if self.x - self.radius <= racket_left.thickness + racket_left.distance_from_edge: if self.y < racket_left.pos - racket_left.width / 2 or self.y > racket_left.pos + racket_left.width: Sound.tone(250, 100).wait() Sound.tone(500, 100).wait() self.reset() else: self.xv = -self.xv self.yv += 1 if self.y > racket_left.pos else -1 m1.run_timed(time_sp=20, speed_sp=900 * self.yv / abs(1 if self.yv == 0 else self.yv)) if self.x + self.radius >= screen_width - racket_left.thickness - racket_left.distance_from_edge: if self.y < racket_right.pos - racket_right.width / 2 or self.y > racket_right.pos + racket_right.width: Sound.tone(250, 100).wait() Sound.tone(500, 100).wait() self.reset() else: self.xv = -self.xv self.yv += 1 if self.y > racket_right.pos else -1 m2.run_timed(time_sp=20, speed_sp=900 * self.yv / abs(1 if self.yv == 0 else self.yv)) if self.y + self.radius >= screen_height or self.y - self.radius <= 0: self.yv = -self.yv Sound.tone(2000, 5)
def play(self, song): """Play the song""" from ev3dev.ev3 import Sound file_name = song.get('file_name') with open(file_name) as data_file: lyrics = json.load(data_file) Sound.tone(self.convert_lyrics(lyrics)).wait()
def beep(duration=1000, freq=440): """ Potrobi s frekvenco `freq` za čas `duration`. Klic ne blokira. """ Sound.tone(freq, duration)
def playTone(hz, seconds, wait=False): process = _Sound.tone([(hz, seconds * 1000)]) if wait: process.wait()
def follow_line(self, direction, coloursens, linesens, stopcolours, collision): print("PidRunner.follow_line(direction={},stopcolours={})".format(direction, stopcolours)) #Sound.play("/home/robot/tetris.wav") # Coerce into list to avoid annoying errors if not isinstance(stopcolours, list): stopcolours = [stopcolours] stopcolours = list(map(lambda s: colour2num[s] if isinstance(s, str) else s, stopcolours)) # Move forward a bit for motor in (mLeft, mRight): motor.stop_action = "coast" motor.duty_cycle_sp = self.power motor.run_direct() time.sleep(0.15) for motor in (mLeft, mRight): motor.stop() linesens.mode = 'COL-REFLECT' coloursens.mode = 'COL-COLOR' lastError = error = integral = 0 colour = None done = False while not done: colour = coloursens.color mLeft.run_direct() mRight.run_direct() while colour not in stopcolours: refRead = linesens.value() # Collision detection using ultrasonic sensor UDIST_CM = 10 # How close an object has to be for the robot to stop TIME_LIMIT = 10 # How long to wait [s] before raising an Exception # and stopping all action if collision and (sSonic.distance_centimeters <= UDIST_CM): mLeft.stop() mRight.stop() timestart = time.time() while sSonic.distance_centimeters <= UDIST_CM: timeelapsed = time.time() - timestart if timeelapsed > TIME_LIMIT: # raise exception raise SubinstructionError('Obstacle Encountered. Recover Unit') Sound.tone(200, 100).wait() mLeft.run_direct() mRight.run_direct() # PID movement error = TARGET_REFL - (100 * (refRead - MIN_REFL) / (MAX_REFL - MIN_REFL)) derivative = error - lastError lastError = error integral = float(0.5) * integral + error course = (self.Kp * error + self.Kd * derivative + self.Ki * integral) * direction for (motor, pow) in zip((mLeft, mRight), self.steering(course)): motor.duty_cycle_sp = pow colour = coloursens.color mRight.stop() mLeft.stop() # Check again for colour done = True for i in range(0, 4): if colour != coloursens.color: done = False break return colour
#print("no ball seen") IROut = IR.value(0) if IROut != 0: pass motors.spinRight(0.5) IROut = IR.value(0) if IROut != 0: pass time.sleep(1) motors.spinLeft(0.96) IROut = IR.value(0) if IROut != 0: pass time.sleep(1) motors.spinRight(0.5) IROut = IR.value(0) if IROut != 0: pass time.sleep(1) ''' cmpscorr() loop_on = False while True: if loop_on: main() if button.enter: Sound.tone(1500,100).wait() if loop_on: loop_on = False else: loop_on = True while button.enter: pass
def beep(times, beep_duration=1000): for i in range(times): Sound.tone(1500, beep_duration).wait() time.sleep(0.5)
def playTone(hz, seconds, wait = False): process = _Sound.tone([(hz, seconds * 1000)]) if wait: process.wait()