def endOfCalibrationSound(): # crrrrr count = 0 while count < 6: Ed.PlayTone(5000, 80) Ed.PlayTone(100, 50) count = count + 1
def middle_path(): if Ed.ReadLineState() == Ed.LINE_ON_BLACK: Ed.Drive(Ed.BACKWARD_LEFT, Ed.SPEED_5, 2) elif Ed.ReadLineState() == Ed.Ed.LINE_ON_BLACK: Ed.Drive(Ed.BACKWARD_RIGHT, Ed.SPEED_5, 2) else: Ed.Drive(Ed.FORWARD, Ed.SPEED_5, 5)
def _irSeek(mode): global obstacleDetectionOn if (obstacleDetectionOn == True): Ed.ObstacleDetectionBeam(Ed.OFF) obstacleDetectionOn = False if (mode == 0): return Ed.ReadIRData() elif (mode == 1): return Ed.ReadRemote()
def calibrateTone(): global tone, density if tone >= TONE_COUNT: return tracker = Ed.ReadLineTracker() density[tone] = tracker Ed.PlayTone(tones[tone], 300) tone = tone + 1
def calibrateDensities(): global densityIndex, density if densityIndex >= DENSITY_COUNT: return tracker = Ed.ReadLineTracker() density[densityIndex] = tracker Ed.PlayTone(tones[0], 200); densityIndex = densityIndex + 1
def waitToRoundButton(): global round_button round_button = False Ed.PlayTone(15289, 100) while round_button == False: pass Ed.PlayTone(7644, 100) Ed.TimeWait(PAUSE_LENGTH, Ed.TIME_SECONDS)
def _diffDrive(direction, speed, distance): _reverse = False if speed < 0: _reverse = True _speed = _shorten(-speed) else: _speed = _shorten(speed) if (_speed == 0): Ed.Drive(Ed.STOP, 1, 1) else: Ed.Drive(_getDirection(direction, _reverse), _speed, distance)
def detectRight(): #turn a little to the right and continue forwards Ed.RightLed(Ed.ON) Ed.Drive(Ed.FORWARD_RIGHT, Ed.SPEED_10, 30) Ed.Drive(Ed.FORWARD, Ed.SPEED_5, Ed.DISTANCE_UNLIMITED) Ed.RightLed(Ed.OFF) #To use this code with Edison Version 1: #change the version in the setup to Ed.EdisonVersion = Ed.V1 #change Ed.DistanceUnits = Ed.CM to Ed.DistanceUnits = Ed.TIME #change the duration for backwards drive and drives in functions to a larger value
def _diffTurn(direction, speed, degree): _reverse = False if speed < 0: _reverse = True _speed = _shorten(-speed) else: _speed = _shorten(speed) if (_speed == 0): Ed.Drive(Ed.STOP, 1, 1) elif _reverse: if direction == Ed.SPIN_RIGHT: Ed.Drive(Ed.SPIN_LEFT, _speed, degree) else: Ed.Drive(Ed.SPIN_RIGHT, _speed, degree) else: Ed.Drive(direction, _speed, degree)
def control(): global ___numberVar, ___booleanVar, ___numberList if ___booleanVar: pass elif ___booleanVar: pass if ___booleanVar: pass elif ___booleanVar: pass while True: pass for ___k0 in range(___numberVar): pass while not ___booleanVar: break while ___booleanVar: continue while True: if ___booleanVar: break if ___booleanVar: break pass Ed.TimeWait(___numberVar, Ed.TIME_MILLISECONDS) while True: if ___booleanVar: break pass
def _motorOn(motor, power, distance): _dir = Ed.FORWARD _reverse = False if (power < 0): _power = _shorten(-power) _reverse = True else: _power = _shorten(power) if (motor == Ed.MOTOR_LEFT): if (_power == 0): Ed.DriveLeftMotor(Ed.STOP, 0, 0) else: Ed.DriveLeftMotor(_getDirection(_dir, _reverse), _power, distance) if (motor == Ed.MOTOR_RIGHT): if (_power == 0): Ed.DriveRightMotor(Ed.STOP, 0, 0) else: Ed.DriveRightMotor(_getDirection(_dir, _reverse), _power, distance)
def sendPlayMessage(previousColor, currentColor): testButton() if currentColor == 0: message = MESSAGE_LOW elif currentColor == 1: if previousColor == 0: message = MESSAGE_LOW else: message = MESSAGE_HIGH else: message = MESSAGE_HIGH if previousColor > -1: Ed.SendIRData(message) # \ Ed.SendIRData(message) # send three messages, because one message per 20 is lost Ed.SendIRData(message) # / Ed.TimeWait(slowingPause, Ed.TIME_MILLISECONDS) Ed.DriveRightMotor(Ed.FORWARD, 1, Ed.DISTANCE_UNLIMITED)
def playTone(length, tone): # turn on or off LEDs Ed.WriteModuleRegister8Bit(Ed.MODULE_LEFT_LED, Ed.REG_LED_OUTPUT_8, length >> 7) Ed.WriteModuleRegister8Bit(Ed.MODULE_RIGHT_LED, Ed.REG_LED_OUTPUT_8, length >> 7) # transpose and set tone Ed.WriteModuleRegister16Bit(Ed.MODULE_BEEPER, Ed.REG_BEEP_FREQ_16, tones[tone + TRANSPOSITION]) # real duration (1,2,4,8,16) is separate from input by masked upper bits # expression ((length & 127) * A + B) must be changed carefully together with length of empty cycle and with new commands Ed.WriteModuleRegister16Bit(Ed.MODULE_BEEPER, Ed.REG_BEEP_DURATION_16, (length & 127) * 7 + 1) # length constant for playing (LENGTH_1 .. LENGTH_16) has 7th bit == 1, LENGTH_0 has 7th bit == 0 # we convert it by shift to 2 (for playing) or 0 (for do nothing) Ed.WriteModuleRegister8Bit(Ed.MODULE_BEEPER, Ed.REG_BEEP_ACTION_8, length >> 6) t = 0 # turn off LEDs Ed.WriteModuleRegister8Bit(Ed.MODULE_LEFT_LED, Ed.REG_LED_OUTPUT_8, 0) Ed.WriteModuleRegister8Bit(Ed.MODULE_RIGHT_LED, Ed.REG_LED_OUTPUT_8, 0) # empty cycle for decreasing tempo while t < 5: t = t + 1
def waitClap(): #loop around, waiting for a clap to be detected while Ed.ReadClapSensor() != Ed.CLAP_DETECTED: pass #To use this code with Edison Version 1: #change the version in the setup to Ed.EdisonVersion = Ed.V2 #change Ed.DistanceUnits = Ed.CM to Ed.DistanceUnits = Ed.TIME
def vypis(hodnota): Ed.SendIRData(255) Ed.TimeWait(300, Ed.TIME_MILLISECONDS) Ed.SendIRData(hodnota // 255) Ed.TimeWait(300, Ed.TIME_MILLISECONDS) Ed.SendIRData(hodnota % 255) Ed.TimeWait(300, Ed.TIME_MILLISECONDS)
def scanAndPlayNewTone(): Ed.PlayTone(100, 1) tr = Ed.ReadLineTracker() if tr > interval[3]: if tr > interval[7]: if tr > interval[9]: if tr > interval[10]: new_tone = 11 else: new_tone = 10 else: if tr > interval[8]: new_tone = 9 else: new_tone = 8 else: if tr > interval[5]: if tr > interval[6]: new_tone = 7 else: new_tone = 6 else: if tr > interval[4]: new_tone = 5 else: new_tone = 4 else: if tr > interval[1]: if tr > interval[2]: new_tone = 3 else: new_tone = 2 else: if tr > interval[0]: new_tone = 1 else: new_tone = 0 Ed.TimeWait(50, Ed.TIME_MILLISECONDS) Ed.PlayTone(tones[new_tone], 30000)
def turnBlack(): global SideTurn Ed.LeftLed(Ed.ON) Ed.RightLed(Ed.ON) Ed.PlayBeep() Ed.Drive(Ed.BACKWARD, Speed, mazeWidth+1) if SideTurn ==1: Ed.Drive(Ed.SPIN_RIGHT, Speed, turnAngle) SideTurn = 0 else: SideTurn = 1 Ed.Drive(Ed.SPIN_LEFT, Speed, turnAngle) obstaclePos = Ed.OBSTACLE_NONE Ed.LeftLed(Ed.OFF) Ed.RightLed(Ed.OFF)
def drive(): global ___n, ___b, ___nl _diffDrive(Ed.FORWARD, ___n, ___n) _diffDrive(Ed.BACKWARD, ___n, ___n) _diffDrive(Ed.FORWARD, ___n, Ed.DISTANCE_UNLIMITED) _diffDrive(Ed.BACKWARD, ___n, Ed.DISTANCE_UNLIMITED) Ed.Drive(Ed.STOP, Ed.SPEED_1, 1) _diffTurn(Ed.SPIN_RIGHT, ___n, ___n) _diffTurn(Ed.SPIN_LEFT, ___n, ___n) _diffTurn(Ed.SPIN_RIGHT, ___n, Ed.DISTANCE_UNLIMITED) _diffTurn(Ed.SPIN_LEFT, ___n, Ed.DISTANCE_UNLIMITED) _diffCurve(Ed.FORWARD, ___n, ___n, ___n) _diffCurve(Ed.BACKWARD, ___n, ___n, ___n) _diffCurve(Ed.FORWARD, ___n, ___n, Ed.DISTANCE_UNLIMITED) _diffCurve(Ed.BACKWARD, ___n, ___n, Ed.DISTANCE_UNLIMITED)
def sensorWaitUntil(): global ___numberVar, ___booleanVar, ___numberList while True: if (Ed.ReadKeypad() == Ed.KEYPAD_TRIANGLE) == True: break pass while True: if (Ed.ReadKeypad() == Ed.KEYPAD_ROUND) == True: break pass while True: if (_obstacleDetection(Ed.OBSTACLE_LEFT)) == True: break pass while True: if (_obstacleDetection(Ed.OBSTACLE_RIGHT)) == True: break pass while True: if (_obstacleDetection(Ed.OBSTACLE_AHEAD)) == True: break pass while True: if (_irSeek(1)) < 30: break pass while True: if (Ed.ReadLeftLightLevel() / 10) < 30: break pass while True: if (Ed.ReadRightLightLevel() / 10) < 30: break pass while True: if (Ed.ReadLineTracker() / 10) < 30: break pass while True: if (Ed.ReadLineState() == Ed.LINE_ON_BLACK) == True: break pass while True: if (Ed.ReadClapSensor() == Ed.CLAP_DETECTED) == True: break pass
def readAndCalibrate(validationStep): global previousTracker, density0, density1, density2 Ed.Drive(Ed.FORWARD_LEFT, 1, 20) currentTracker = Ed.ReadModuleRegister16Bit(Ed.MODULE_LINE_TRACKER, Ed.REG_LT_LEVEL_16) if validationStep > 0: Ed.SendIRData(MESSAGE_CALIBRATE) # \ Ed.SendIRData(MESSAGE_CALIBRATE) # send three messages, because one message per 20 is lost Ed.SendIRData(MESSAGE_CALIBRATE) # / if validationStep == 1: # start from 1 (second calibration reading), we reduce the possibility of reading value from border density0 = currentTracker elif validationStep == 2: density1 = currentTracker elif validationStep == 3: density2 = currentTracker previousTracker = currentTracker Ed.TimeWait(250, Ed.TIME_MILLISECONDS) Ed.DriveRightMotor(Ed.FORWARD, 1, Ed.DISTANCE_UNLIMITED)
#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.INCH Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- #Turn Lights On Ed.LeftLed(Ed.ON) Ed.RightLed(Ed.ON) #Turn lights off Ed.LeftLed(Ed.OFF) Ed.RightLed(Ed.OFF)
def buttonEvent(): global round_button round_button = True def waitToRoundButton(): global round_button round_button = False Ed.PlayTone(15289, 100) while round_button == False: pass Ed.PlayTone(7644, 100) Ed.TimeWait(PAUSE_LENGTH, Ed.TIME_SECONDS) Ed.RegisterEventHandler(Ed.EVENT_KEYPAD_ROUND, "buttonEvent") # >> E << Ed.PlayTone(7644, 100) Ed.TimeWait(PAUSE_LENGTH, Ed.TIME_SECONDS) Ed.DriveRightMotor(Ed.BACKWARD, Ed.SPEED_1, Ed.DISTANCE_UNLIMITED) Ed.DriveLeftMotor(Ed.BACKWARD, Ed.SPEED_9, Ed.DISTANCE_UNLIMITED) Ed.TimeWait(1500, Ed.TIME_MILLISECONDS) Ed.Drive(Ed.STOP, 1, 1) waitToRoundButton() Ed.Drive(Ed.BACKWARD, 1, 15) waitToRoundButton() # >> d << Ed.Drive(Ed.SPIN_LEFT, 1, 180) Ed.Drive(Ed.STOP, 1, 1)
#Move in White surface #Stop when find black line import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM Ed.LineTrackerLed(Ed.ON) Ed.Drive(Ed.FORWARD, Ed.SPEED_7, Ed.DISTANCE_UNLIMITED) while True: if (Ed.ReadLineState() == Ed.LINE_ON_BLACK): Ed.PlayBeep() Ed.Drive(Ed.STOP, Ed.SPEED_1, 0)
import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_SLOW obstacleDetectionOn = False Ed.LineTrackerLed(Ed.ON) Ed.ReadClapSensor() Ed.ReadLineState() Ed.TimeWait(250, Ed.TIME_MILLISECONDS) ___numberVar = 0 ___booleanVar = True ___numberList = Ed.List(3, [0, 0, 0]) def math(): global ___numberVar, ___booleanVar, ___numberList ___numberVar = 0 ___numberVar = ___numberVar + ___numberVar ___numberVar = ___numberVar - ___numberVar ___numberVar = ___numberVar * ___numberVar ___numberVar = ___numberVar / ___numberVar ___numberVar = _pow(___numberVar, ___numberVar) ___numberVar = _abs(___numberVar) ___numberVar = -(___numberVar) ___numberVar = _pow(10, ___numberVar) ___booleanVar = (___numberVar % 2) == 0 ___booleanVar = (___numberVar % 2) == 1 ___booleanVar = _isPrime(___numberVar) ___booleanVar = ___numberVar > 0 ___booleanVar = ___numberVar < 0
def _obstacleDetection(mode): global obstacleDetectionOn if (obstacleDetectionOn == False): Ed.ObstacleDetectionBeam(Ed.ON) obstacleDetectionOn = True return Ed.ReadObstacleDetection() == mode
def sensors(): global ___numberVar, ___booleanVar, ___numberList ___booleanVar = Ed.ReadKeypad() == Ed.KEYPAD_TRIANGLE ___booleanVar = Ed.ReadKeypad() == Ed.KEYPAD_ROUND ___booleanVar = _obstacleDetection(Ed.OBSTACLE_LEFT) ___booleanVar = _obstacleDetection(Ed.OBSTACLE_RIGHT) ___booleanVar = _obstacleDetection(Ed.OBSTACLE_AHEAD) ___numberVar = _irSeek(1) ___numberVar = Ed.ReadLeftLightLevel() / 10 ___numberVar = Ed.ReadRightLightLevel() / 10 ___numberVar = Ed.ReadLineTracker() / 10 ___booleanVar = Ed.ReadLineState() == Ed.LINE_ON_BLACK ___booleanVar = Ed.ReadClapSensor() == Ed.CLAP_DETECTED Ed.ReadObstacleDetection() Ed.ReadKeypad() Ed.ReadClapSensor() Ed.ReadRemote() Ed.ReadIRData()
def Wait(): Ed.TimeWait(100, Ed.TIME_MILLISECONDS)
#-------------Setup---------------- import Ed Ed.PlayMyBeep(8000) Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- moves = Ed.List(15) for x in range(15): moves[x] = -1 moves_index = 0 start = 0 Ed.PlayBeep() Ed.ReadKeypad() #Ed.ReadRemote() Ed.LeftLed(Ed.ON) Ed.RightLed(Ed.ON) while start == 0: pressed_button = Ed.ReadKeypad() ir_code = Ed.ReadRemote() # Ed.REMOTE_CODE_0 --> Up Arrow # Ed.REMOTE_CODE_1 --> Down Arrow # Ed.REMOTE_CODE_2 --> Left Arrow # Ed.REMOTE_CODE_3 --> Right Arrow
#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- Ed.LineTrackerLed(Ed.ON) while True: if Ed.ReadLineState() == Ed.LINE_ON_WHITE: Ed.Drive(Ed.FORWARD_RIGHT, Ed.SPEED_1, Ed.DISTANCE_UNLIMITED) else: Ed.Drive(Ed.FORWARD_LEFT, Ed.SPEED_1, Ed.DISTANCE_UNLIMITED)
# # Description: Robot Edison is used as a musical instrument. He plays tones by shades on paper. # Notes: You can see result here: https://www.youtube.com/watch?v=iucFzno07uA # You can upload program to Edison robot by online EdPy environment http://edpyapp.com/# # Here is PDF for printing: https://github.com/ales-eri/edison-robot-ideas/raw/master/episode04-piano/piano_for_edison_robot.pdf # If you have some questions, ask me in comments of the video: https://www.youtube.com/watch?v=iucFzno07uA # import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_FAST Ed.LineTrackerLed(Ed.ON) TONE_COUNT = 12 density = Ed.List(12, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) interval = Ed.List(11, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) # G6 A6 B6 C7 D7 E7 F7 G7 A7 B7 C8 R tones = Ed.List(12, [ 20408, 18181, 16202, 15289, 13622, 12135, 11457, 10207, 9090, 8099, 7644, 100 ]) def scanAndPlayNewTone(): Ed.PlayTone(100, 1) tr = Ed.ReadLineTracker() if tr > interval[3]: if tr > interval[7]: