def transmit_data(data): line = DigitalOutputDevice(17) start_time = time_ns() / 1e9 i = 0 while i < len(data): entry = data[i] time = entry['time'] if time_ns() / 1e9 - start_time > time: line.value = entry['signal'] i = i + 1 line.value = 0
cv2.circle(frame, (x, y), 5, (75, 13, 180), -1) # If object is in inside box, increment inside counter variable if ((x > TL_inside[0]) and (x < BR_inside[0]) and (y > TL_inside[1]) and (y < BR_inside[1])): inside_counter = inside_counter + 1 if inside_counter > 3: ##within box for two seconds detected_inside = True inside_counter = 0 # Pause pet detection by setting "pause" flag pause = 1 if pause == 1: if detected_inside == True: #activating motor forward.value = True reverse.value = False drive.value = 1.0 sleep(5) forward.value = False reverse.value = False drive.value = 0.0 sleep(.5) forward.value = False reverse.value = True drive.value = 1.0 sleep(5) forward.value = False reverse.value = False drive.value = 0.0 cv2.putText(frame, 'WHEELCHAIR',
value = 0.4425 # Change this value as well! dac.normalized_value = value try: leftStickCenter = None while leftStickCenter == None: events = get_gamepad() for event in events: if event.code == "ABS_X": leftStickCenter = event.state while True: events = get_gamepad() for event in events: if event.code == "BTN_WEST": reverse.value = event.state if event.code == "ABS_X": steeringInput = event.state - leftStickCenter steeringNormalized = steeringInput / 16384 setSteering(steeringNormalized) elif event.code == "ABS_RZ": throttleInput = event.state throttleNormalized = throttleInput / 2047 # 1535 1/2 speed, 2047 1/4 speed, 1023 full speed (This adjusts the sensitivity) setThrottle(throttleNormalized) except: dac.normalized_value = 0 reverse.off() print("WARNING: AN EXCEPTION OCCURRED. MOTOR HAS BEEN SHUT OFF AND SCRIPT WILL EXIT.")
idleHSI = temp['hsi'] elif t == topics[0][0]: global mainLight # needed to merge data into stream mainLight = isON(msg) # save new light command client.on_message = on_message light = DigitalOutputDevice(4) # define GPIO pin for control relay strip = RGBLED(13, 6, 5) # define GPIO pins for led strip strip_color = HSI_LED() connected = hollaBroker() # is broker found while connected: try: sec = time.monotonic() if (int(sec) % 2 == 0): client.loop_start() isListening = True offTime = sec + 1.5 elif (sec >= offTime and isListening): client.loop_stop() strip_color.tick() strip.value = (strip_color.rgb[0], strip_color.rgb[1],strip_color.rgb[2]) light.value = mainLight except KeyboardInterrupt: client.disconnect() client.loop_stop() break #time.sleep(0.5) #end client connected loop
from gpiozero import Motor import time import math STBY = 19 MOTOR_A_PWM = 12 MOTOR_A_FORWARD = 5 MOTOR_A_REVERSE = 6 MOTOR_B_PWM = 13 MOTOR_B_FORWARD = 20 MOTOR_B_REVERSE = 16 stby = DigitalOutputDevice(STBY) stby.value = True motorA = Motor(MOTOR_A_FORWARD, MOTOR_A_REVERSE, pwm=False) driveA = PWMOutputDevice(MOTOR_A_PWM, active_high=True, initial_value=0, frequency=1000) motorB = Motor(MOTOR_B_FORWARD, MOTOR_B_REVERSE, pwm=False) driveB = PWMOutputDevice(MOTOR_B_PWM, active_high=True, initial_value=0, frequency=1000) def get_devices(m): if m == "a":
def run(window, device, host, port): run_window = window factory = PiGPIOFactory(host=host, port=port) if device in (N_DigitalOutputDevice, N_LED, N_Buzzer): run_window.Layout(led_layout()) switch = False device_open = False while True: event, values = run_window.read() if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = DigitalOutputDevice(values['-pin-'], pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False switch = False run_window['-open-'].update(image_data=icon_open) run_window['-switch-'].update(image_data=icon_switch_off) d.close() if event == '-switch-': if device_open: switch = not switch run_window['-switch-'].update(image_data=icon_switch_on if switch else icon_switch_off) d.on() if switch else d.off() else: sg.popup_no_titlebar("Open device first!") if event in (sg.WIN_CLOSED, 'Exit'): break elif device in (N_PWMOutputDevice, N_PWMLED): run_window.Layout(pwmled_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break # if not exit-event, get param cycle = 0 if str(values['-cycle-']).startswith('Select') else values['-cycle-'] frequency = 100 if values['-frequency-'].startswith('Select') else int( values['-frequency-'].replace('Hz', '')) if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-frequency-': if device_open: d.frequency = frequency if event == '-cycle-': if device_open: d.value = cycle if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = PWMOutputDevice(values['-pin-'], initial_value=cycle, frequency=frequency, pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) if event == '-pulse-': if device_open: d.pulse() else: sg.popup_no_titlebar("Open device first!") elif device == N_Servo: run_window.Layout(servo_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break value = 0 if str(values['-value-']).startswith('Select') else values['-value-'] min_pulse_width = (1 if values['-min_pulse_width-'].startswith('Select') else float( values['-min_pulse_width-'].replace('ms', ''))) / 1000 max_pulse_width = (2 if values['-max_pulse_width-'].startswith('Select') else float( values['-max_pulse_width-'].replace('ms', ''))) / 1000 if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-value-': if device_open: d.value = value if event in ('-min_pulse_width-', '-max_pulse_width-'): if device_open: sg.popup_no_titlebar('Pulse-width param only work before open!') if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = Servo(values['-pin-'], initial_value=value, min_pulse_width=min_pulse_width, max_pulse_width=max_pulse_width, pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) elif device == N_AngularServo: run_window.Layout(angularservo_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break angle = 0 if str(values['-angle-']).startswith('Select') else values['-angle-'] min_angle = -90 if str(values['-min_angle-']).startswith('Select') else values['-min_angle-'] max_angle = 90 if str(values['-max_angle-']).startswith('Select') else values['-max_angle-'] min_pulse_width = (1 if values['-min_pulse_width-'].startswith('Select') else float( values['-min_pulse_width-'].replace('ms', ''))) / 1000 max_pulse_width = (2 if values['-max_pulse_width-'].startswith('Select') else float( values['-max_pulse_width-'].replace('ms', ''))) / 1000 if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-angle-': if device_open: d.angle = angle if event in ('-min_pulse_width-', '-max_pulse_width-', '-min_angle-', '-max_angle-'): if device_open: sg.popup_no_titlebar('Pulse-width param only work before open!') if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = AngularServo(values['-pin-'], initial_angle=angle, min_angle=min_angle, max_angle=max_angle, min_pulse_width=min_pulse_width, max_pulse_width=max_pulse_width, pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) elif device == N_PhaseEnableMotor: run_window.Layout(phaseenablemotor_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break # if not exit-event, get param speed = 0 if str(values['-speed-']).startswith('Select') else values['-speed-'] if event == '-direction_pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-speed_pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-speed-': if device_open: d.value = speed if event == '-open-': if not device_open: select = 'Select direction pin' if values['-direction_pin-'] == select or values['-speed_pin-'] == select: sg.popup_no_titlebar("Select your pin!") continue else: d = PhaseEnableMotor(phase=values['-direction_pin-'], enable=values['-speed_pin-'], pin_factory=factory) d.value = 0 device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) elif device == N_Button: run_window.Layout(button_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break # if not exit-event, get param if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-pull_up-': if device_open: sg.popup_no_titlebar('pull-up param only work before open!') if event == '-test-': if device_open: sg.popup_no_titlebar('Now you can test button!') d.wait_for_press() sg.popup_no_titlebar('Yuu pressed button!') d.wait_for_release() sg.popup_no_titlebar('Yuu released button!') else: sg.popup_no_titlebar("Open device first!") if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: pull_up = True if str(values['-pull_up-']).startswith('Select') else values['-pull_up-'] d = Button(values['-pin-'], pull_up=pull_up, bounce_time=0.1, pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open) elif device in (N_LineSensor, N_MotionSensor, N_LightSensor): run_window.Layout(linesensor_layout()) device_open = False while True: event, values = run_window.read() if event in (sg.WIN_CLOSED, 'Exit'): break if event == '-pin-': if device_open: sg.popup_no_titlebar("Close device first!") if event == '-test-': if device_open: sg.popup_no_titlebar('Now you can test sensor!') d.wait_for_active() sg.popup_no_titlebar('device now is active!') d.wait_for_inactive() sg.popup_no_titlebar('device now is inactive, Test over!') else: sg.popup_no_titlebar("Open device first!") if event == '-open-': if not device_open: if values['-pin-'] == 'Select pin': sg.popup_no_titlebar("Select your pin!") continue else: d = eval(device)(pin=values['-pin-'], pin_factory=factory) device_open = True run_window['-open-'].update(image_data=icon_close) else: device_open = False d.close() run_window['-open-'].update(image_data=icon_open)
slider = Slider(box, end=100, start=-100, grid=[2, 1]) micLab = Text(box, text="Mic", grid=[0, 3]) """ Auto Components """ modeChange() t = 0 while True: t += 1 stopLab.clear() micLab.clear() v = round(mcp.read_adc(0) * (9.9 / 1023), 2) s = round(mcp.read_adc(1), 2) stopLab.append(v) micLab.append(s) # io.output(4,1) speed = slider.value forwardRight.value = speed >= 0 reverseRight.value = speed < 0 if t % 5 == 0: print(speed) driveRight.value = abs((int(slider.value)) / 100) io.output(12, True) # time.sleep(.1) io.output(3, False) # time.sleep(.1) app.update()
import time from gpiozero import DigitalInputDevice, DigitalOutputDevice # Setup devices, an input device and an output device, with pin numbers for the sensors. print "Prepare GPIO pins" # Left sensor left_trigger = DigitalOutputDevice(17) left_echo = DigitalInputDevice(27) left_trigger.value = False # Right sensor right_trigger = DigitalOutputDevice(5) right_echo = DigitalInputDevice(6) right_trigger.value = False # wait a little, to iron out spurious responses. print "Warm up time" time.sleep(0.5) def make_measurement(trig_device, echo_device): """Function to get the distance measurement""" # Timeout - we'll use this to stop it getting stuck time_out = time.time() + 1 # This off-on-off pulse tells the device to make a measurement trig_device.value = True time.sleep(0.00001) # This is the 10 microseconds