def run(): status_right = not GPIO.input(line_pin_right) status_middle = not GPIO.input(line_pin_middle) status_left = not GPIO.input(line_pin_left) if status_left == 1: turn.left() led.both_off() led.side_on(left_R) motor.motor_left(status, forward, left_spd * spd_ad_2) motor.motor_right(status, backward, right_spd * spd_ad_2) elif status_middle == 1: turn.middle() led.both_off() led.yellow() motor.motor_left(status, forward, left_spd * spd_ad_1) motor.motor_right(status, backward, right_spd * spd_ad_1) elif status_right == 1: turn.right() led.both_off() led.side_on(right_R) motor.motor_left(status, forward, left_spd * spd_ad_2) motor.motor_right(status, backward, right_spd * spd_ad_2) else: turn.middle() led.both_off() led.cyan() motor.motor_left(status, backward, left_spd) motor.motor_right(status, forward, right_spd) pass
def main(): #app = QtGui.QApplication(sys.argv) signal.signal(signal.SIGINT, exit_handler) #d = display.display() while True: time.sleep(0.1) temp = th02.readTemp() time.sleep(0.1) temp = th02.readTemp() #temp = 0 time.sleep(0.1) hum = th02.readHum() time.sleep(0.1) hum = th02.readHum() #print('tempureture: %.2f , humidity: %.2f' % (temp, hum)) di = th02.outputDI(temp, hum) #print('不快指数: %.2f \n' % di) if di < 65: led.blue() elif di < 70: led.cyan() elif di < 75: led.green() elif di < 80: led.yellow() elif di < 85: led.magenta() elif di < 100: led.red() else: led.white() #d.initUI(temp, hum, di) time.sleep(0.1)
def run(): with sr.Microphone() as source: print("Please wait. Calibrating microphone...") r.adjust_for_ambient_noise(source, duration=2) while (True): l.both_off() l.green() audio = r.listen(source) l.both_off() l.red() try: v_command = r.recognize_google(audio) print("(recognize_google) Google thinks you said '" + v_command + "'") if wake_word in v_command: espeak.synth("hi, I'm listening") l.both_off() l.cyan() audio2 = r.listen(source) l.both_off() l.red() try: v_command2 = r.recognize_google(audio2) print("(recognize_google) Google thinks you said '" + v_command2 + "'") if keywords[0] in v_command2: say_time() break if keywords[1] in v_command2: print("Say a course no.") espeak.synth('Say a course number') l.both_off() l.cyan() audio3 = r.listen(source) l.both_off() l.red() course_id_str = r.recognize_google(audio3) print(course_id_str) try: course_id = int(course_id_str) say_class_schedule(course_id) except ValueError: print( "I could not recognize any course number.") break except sr.UnknownValueError: print("I could not understand audio") except sr.UnknownValueError: print("I could not understand audio")
async def read_gamepad_inputs(): global head_light_flag print("Ready to drive!!") turn_sound = SoundPlayer( "/home/pi/xbox-raspberrypi-rover/soundfiles/turn-signal.mp3", card) horn_sound = SoundPlayer( "/home/pi/xbox-raspberrypi-rover/soundfiles/Horn.mp3", card) while not is_connected(): time.sleep(2) # Wait 2 seconds for controller to come up and try again while is_connected() and remote_control.button_b == False: #print(" trigger_right = ", round(remote_control.trigger_right,2),end="\r") x = round(remote_control.joystick_left_x, 2) y = round(remote_control.joystick_left_y, 2) angle = get_angle_from_coords(x, y) if angle > 180: angle = 360 - angle #print("x:", x, " y:", y, " angle: ",angle,end="\r") turn_head(angle) direction = get_motor_direction(x, y) y = adjust_speed(y, angle) #print("x:", x, " y:", y, " direction: ",direction,end="\r") drive_motor(direction, y) if round(remote_control.trigger_right, 2) > 0.0: horn_sound.play(1.0) led.blue() elif round(remote_control.trigger_left, 2) > 0.0: led.cyan() elif remote_control.bump_left: turn_sound.play(1.0) led.turn_left(5) elif remote_control.bump_right: turn_sound.play(1.0) led.turn_right(5) elif remote_control.dpad_up: remote_control.dpad_up = False elif remote_control.dpad_left: remote_control.dpad_left = False elif remote_control.dpad_right: remote_control.dpad_right = False elif remote_control.button_a: remote_control.button_a = False elif head_light_flag == False: led.both_off() led_strip.colorWipe(strip, Color(0, 0, 0)) if turn_sound.isPlaying(): turn_sound.stop() await asyncio.sleep(100e-3) #100ms return
def track_object(distance_stay, distance_range, speed_adj=0.4): # distance_stay,distance_range in inches #Tracking with Ultrasonic motor.setup() led.setup() turn.ahead() turn.middle() dis = checkdist_inches() if dis < distance_range: #Check if the target is in distance range if dis > ( distance_stay + 4 ): #If the target is in distance range and out of distance stay, then move forward to track turn.ahead() moving_time = (dis - distance_stay) / 0.38 if moving_time > 1: moving_time = 1 print('mf') led.both_off() led.cyan() motor.motor_left(status, backward, int(left_spd * spd_ad_u * speed_adj)) motor.motor_right(status, forward, int(right_spd * spd_ad_u * speed_adj)) time.sleep(moving_time) motor.motorStop() elif dis < ( distance_stay - 4 ): #Check if the target is too close, if so, the car move back to keep distance at distance_stay moving_time = (distance_stay - dis) / 0.38 print('mb') led.both_off() led.pink() motor.motor_left(status, forward, int(left_spd * spd_ad_u * spd_adj)) motor.motor_right(status, backward, int(right_spd * spd_ad_u * spd_adj)) time.sleep(moving_time) motor.motorStop() else: #If the target is at distance, then the car stay still motor.motorStop() led.both_off() led.yellow() else: motor.motorStop()
def test_led(): led.setup() print("both_on()") led.both_on() time.sleep(3) print("red()") led.red() time.sleep(3) print("both_off()") led.both_off() time.sleep(1) print("yellow()") led.yellow() time.sleep(3) print("pink()") led.pink() time.sleep(3) print("both_off()") led.both_off() time.sleep(1) print("cyan()") led.cyan() time.sleep(3) # Flashing in 2 cycles print("police_on(2)") led.police_on(2) print("both_off()") led.both_off() time.sleep(3) # Flashing in 6 seconds print("police_on()") led.police_on() time.sleep(6) #6 seconds led.police_off()
def loop(distance_stay,distance_range): '''Tracking with Ultrasonic''' motor.setup() led.setup() turn.ahead() turn.middle() dis = checkdist() if dis < distance_range: #Check if the target is in diatance range if dis > (distance_stay+0.1) : #If the target is in distance range and out of distance stay, #then move forward to track turn.ahead() moving_time = (dis-distance_stay)/0.38 #??? magic number if moving_time > 1: moving_time = 1 print('mf') led.both_off() led.cyan() motor.motor_left(status, backward,left_spd*spd_ad_u) motor.motor_right(status,forward,right_spd*spd_ad_u) time.sleep(moving_time) motor.motorStop() elif dis < (distance_stay-0.1) : #Check if the target is too close, #if so, the car move back to keep distance at distance_stay moving_time = (distance_stay-dis)/0.38 print('mb') led.both_off() led.pink() motor.motor_left(status, forward,left_spd*spd_ad_u) motor.motor_right(status,backward,right_spd*spd_ad_u) time.sleep(moving_time) motor.motorStop() else: #If the target is at distance, then the car stay still motor.motorStop() led.both_off() led.yellow() else: motor.motorStop()
def run(): global v_command # obtain audio from the microphone r = sr.Recognizer() with sr.Microphone(device_index=2, sample_rate=48000) as source: r.record(source, duration=2) #r.adjust_for_ambient_noise(source) led.both_off() led.yellow() print("Command?") audio = r.listen(source) led.both_off() led.blue() try: v_command = r.recognize_sphinx(audio, keyword_entries=[ ('forward', 1.0), ('backward', 1.0), ('left', 1.0), ('right', 1.0), ('stop', 1.0) ]) #You can add your own command here print(v_command) led.both_off() led.cyan() except sr.UnknownValueError: print("say again") led.both_off() led.red() except sr.RequestError as e: led.both_off() led.red() pass #print('pre') if 'forward' in v_command: motor.motor_left(status, forward, left_spd * spd_ad_2) motor.motor_right(status, backward, right_spd * spd_ad_2) time.sleep(2) motor.motorStop() elif 'backward' in v_command: motor.motor_left(status, backward, left_spd) motor.motor_right(status, forward, right_spd) time.sleep(2) motor.motorStop() elif 'left' in v_command: turn.left() motor.motor_left(status, forward, left_spd * spd_ad_2) motor.motor_right(status, backward, right_spd * spd_ad_2) time.sleep(2) motor.motorStop() elif "right" in v_command: turn.right() motor.motor_left(status, forward, left_spd * spd_ad_2) motor.motor_right(status, backward, right_spd * spd_ad_2) time.sleep(2) motor.motorStop() elif 'stop' in v_command: motor.motorStop() else: pass