def main(): trigger = 26.0 lcd.setup() lcd.initDisplay() file = open('/home/pi/temp.txt', 'a') led.setup() led.off(led.green) led.off(led.yellow) last = 0.0 while 1: t = return_temp_from_file('/sys/bus/w1/devices/') lcd.clearLCD() lcd.write('Temp: ') tc = str(float(t) / 1000) lcd.write(tc + ' ') lcd.cmd8b(0b11011111, True) lcd.write('C') file.write(time.strftime('%d.%m.%Y') + '; ' + time.strftime('%H:%M:%S') + '; ' + tc + '\n') file.flush() tt = float(tc) if tt < trigger and last >= trigger: led.off(led.yellow) led.on(led.green) elif tt >= trigger and last < trigger: led.off(led.green) led.on(led.yellow) last = tt
async def startLed(ledPin): # cleanupGpio() # await startLedTake(ledPin) print('ok') print(ledPin) setup(ledPin) startLum(ledPin)
def __init__(self): # noinspection PyArgumentList self.camera = None self.uart = serial.Serial(config.UART_PATH) self.uart.baudrate = config.UART_BAUDRATE self.uart.timeout = 10 self.initialize_camera() led.setup() self.led_event = Event()
async def trigger_method(message): print(message) y = json.loads(message) print(y["method"]) if y["method"] == "takeBottle": import RPi.GPIO as GPIO import time # ledPin = y.ledPin setup() loop()
def setup(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(line_pin_right, GPIO.IN) GPIO.setup(line_pin_middle, GPIO.IN) GPIO.setup(line_pin_left, GPIO.IN) try: led.setup() motor.setup() except: pass
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()
async def pub_sub(websocket, path): global connected if path == '/temperature/read' : connected.add(websocket) print("READER "+str(websocket.remote_address)+" connected") while True: await asyncio.sleep(100) elif path == '/temperature/write' : print("WRITER "+str(websocket.remote_address)+" connected") try : while True: data = await websocket.recv() print("MULTICAST: "+data) still_connected = set() for ws in connected : if ws.open: still_connected.add(ws) await asyncio.wait([ws.send(data)]) else: print("READER "+str(ws.remote_address)+" disconnected") connected=still_connected except: print("WRITER "+str(websocket.remote_address)+" disconnected") elif path == '/led/start' : print("WRITER "+str(websocket.remote_address)+" connected") try : while True: data = await websocket.recv() led.setup(data) led.loop(data) still_connected = set() for ws in connected : if ws.open: still_connected.add(ws) await asyncio.wait([ws.send(data)]) else: print("READER "+str(ws.remote_address)+" disconnected") connected=still_connected except: print("WRITER "+str(websocket.remote_address)+" disconnected")
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 main(): if len(sys.argv) != 2: print 'pass in config file' print 'Run as:' print 'python script.py test.cfg' exit(1) config_file = sys.argv[1] photo_scripter = PhotoScript(config_file) salient_pin = 27 entrance_pin = 22 exit_pin = 17 input_pin = 23 output_pin = 24 sensor_pin = 18 current_state = None led.setup() GPIO.setup(salient_pin, GPIO.OUT) GPIO.setup(entrance_pin, GPIO.OUT) GPIO.setup(exit_pin, GPIO.OUT) GPIO.setup(sensor_pin, GPIO.IN) GPIO.setup(input_pin, GPIO.IN) GPIO.setup(output_pin, GPIO.IN) while True: current_state = check_state(input_pin, output_pin, current_state) enable_state(entrance_pin, exit_pin, current_state) if check_sensor(sensor_pin): print GPIO.input(sensor_pin) file_name = take_picture(photo_scripter, salient_pin) photo_scripter.send_photo(file_name) url = photo_scripter.url photo_scripter.send_request(url, make_payload(file_name, photo_scripter)) time.sleep(0.2) time.sleep(0.2)
# Date : 2019/10/28 import socket import threading import time import os import LEDapp as LED import led import motor import turn motor.setup() turn.ahead() LED = LED.LED() LED.colorWipe(80, 255, 0) led.setup() step_set = 1 speed_set = 100 rad = 0.6 direction_command = 'no' turn_command = 'no' servo_command = 'no' pos_input = 1 catch_input = 1 cir_input = 6 servo_speed = 11
def run(): #Main loop global hoz_mid, vtr_mid, ip_con, led_status, auto_status, opencv_mode, findline_mode, speech_mode, auto_mode, data, addr, footage_socket, ap_status, turn_status, wifi_status led.setup() while True: #Connection try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("1.1.1.1", 80)) ipaddr_check = s.getsockname()[0] s.close() print(ipaddr_check) wifi_status = 1 except: if ap_status == 0: ap_threading = threading.Thread( target=ap_thread) #Define a thread for data receiving ap_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes ap_threading.start() #Thread starts led.both_off() led.yellow() time.sleep(5) wifi_status = 0 if wifi_status == 1: print('waiting for connection...') led.red() tcpCliSock, addr = tcpSerSock.accept( ) #Determine whether to connect led.both_off() led.green() print('...connected from :', addr) #time.sleep(1) tcpCliSock.send( ('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % look_up_max + ' %s' % look_down_max).encode()) print('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % left + ' %s' % right) break else: led.both_off() led.blue() print('waiting for connection...') tcpCliSock, addr = tcpSerSock.accept( ) #Determine whether to connect led.both_off() led.green() print('...connected from :', addr) #time.sleep(1) tcpCliSock.send( ('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % look_up_max + ' %s' % look_down_max).encode()) print('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % left + ' %s' % right) ap_status = 1 break #FPV initialization context = zmq.Context() footage_socket = context.socket(zmq.PUB) footage_socket.connect('tcp://%s:5555' % addr[0]) print(addr[0]) #Threads start video_threading = threading.Thread( target=opencv_thread) #Define a thread for FPV and OpenCV video_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes video_threading.start() #Thread starts ws2812_threading = threading.Thread( target=ws2812_thread) #Define a thread for ws_2812 leds ws2812_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes ws2812_threading.start() #Thread starts findline_threading = threading.Thread( target=findline_thread) #Define a thread for line tracking findline_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes findline_threading.start() #Thread starts speech_threading = threading.Thread( target=speech_thread) #Define a thread for speech recognition speech_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes speech_threading.start() #Thread starts auto_threading = threading.Thread( target=auto_thread) #Define a thread for ultrasonic tracking auto_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes auto_threading.start() #Thread starts scan_threading = threading.Thread( target=dis_scan_thread) #Define a thread for ultrasonic scan scan_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes scan_threading.start() #Thread starts while True: data = '' data = tcpCliSock.recv(BUFSIZ).decode() if not data: continue elif 'exit' in data: os.system("sudo shutdown -h now\n") elif 'spdset' in data: global spd_ad spd_ad = float((str(data))[7:]) #Speed Adjustment elif 'scan' in data: dis_can = scan() #Start Scanning str_list_1 = dis_can #Divide the list to make it samller to send str_index = ' ' #Separate the values by space str_send_1 = str_index.join(str_list_1) + ' ' tcpCliSock.sendall((str(str_send_1)).encode()) #Send Data tcpCliSock.send( 'finished'.encode() ) #Sending 'finished' tell the client to stop receiving the list of dis_can elif 'EC1set' in data: #Camera Adjustment new_EC1 = int((str(data))[7:]) turn.camera_turn(new_EC1) replace_num('E_C1:', new_EC1) elif 'EC2set' in data: #Ultrasonic Adjustment new_EC2 = int((str(data))[7:]) replace_num('E_C2:', new_EC2) turn.ultra_turn(new_EC2) elif 'EM1set' in data: #Motor A Speed Adjustment new_EM1 = int((str(data))[7:]) replace_num('E_M1:', new_EM1) elif 'EM2set' in data: #Motor B Speed Adjustment new_EM2 = int((str(data))[7:]) replace_num('E_M2:', new_EM2) elif 'LUMset' in data: #Motor A Turningf Speed Adjustment new_ET1 = int((str(data))[7:]) replace_num('look_up_max:', new_ET1) turn.camera_turn(new_ET1) elif 'LDMset' in data: #Motor B Turningf Speed Adjustment new_ET2 = int((str(data))[7:]) replace_num('look_down_max:', new_ET2) turn.camera_turn(new_ET2) elif 'stop' in data: #When server receive "stop" from client,car stops moving tcpCliSock.send('9'.encode()) setup() motor.motorStop() setup() if led_status == 0: led.setup() led.both_off() colorWipe(strip, Color(0, 0, 0)) continue elif 'lightsON' in data: #Turn on the LEDs led.both_on() led_status = 1 tcpCliSock.send('lightsON'.encode()) elif 'lightsOFF' in data: #Turn off the LEDs led.both_off() led_status = 0 tcpCliSock.send('lightsOFF'.encode()) elif 'middle' in data: #Go straight if led_status == 0: led.side_color_off(left_R, left_G) led.side_color_off(right_R, right_G) else: led.side_on(left_B) led.side_on(right_B) turn_status = 0 turn.middle() elif 'Left' in data: #Turn left if led_status == 0: led.side_color_on(left_R, left_G) else: led.side_off(left_B) turn.left() turn_status = 1 tcpCliSock.send('3'.encode()) elif 'Right' in data: #Turn right if led_status == 0: led.side_color_on(right_R, right_G) else: led.side_off(right_B) turn.right() turn_status = 2 tcpCliSock.send('4'.encode()) elif 'backward' in data: #When server receive "backward" from client,car moves backward tcpCliSock.send('2'.encode()) motor.motor_left(status, backward, left_spd * spd_ad) motor.motor_right(status, forward, right_spd * spd_ad) colorWipe(strip, Color(255, 0, 0)) elif 'forward' in data: #When server receive "forward" from client,car moves forward tcpCliSock.send('1'.encode()) motor.motor_left(status, forward, left_spd * spd_ad) motor.motor_right(status, backward, right_spd * spd_ad) colorWipe(strip, Color(0, 0, 255)) elif 'l_up' in data: #Camera look up if vtr_mid < look_up_max: vtr_mid += turn_speed turn.camera_turn(vtr_mid) tcpCliSock.send('5'.encode()) elif 'l_do' in data: #Camera look down if vtr_mid > look_down_max: vtr_mid -= turn_speed turn.camera_turn(vtr_mid) print(vtr_mid) tcpCliSock.send('6'.encode()) elif 'l_le' in data: #Camera look left if hoz_mid < look_left_max: hoz_mid += turn_speed turn.ultra_turn(hoz_mid) tcpCliSock.send('7'.encode()) elif 'l_ri' in data: #Camera look right if hoz_mid > look_right_max: hoz_mid -= turn_speed turn.ultra_turn(hoz_mid) tcpCliSock.send('8'.encode()) elif 'ahead' in data: #Camera look ahead turn.ahead() elif 'Stop' in data: #When server receive "Stop" from client,Auto Mode switches off opencv_mode = 0 findline_mode = 0 speech_mode = 0 auto_mode = 0 auto_status = 0 dis_scan = 1 tcpCliSock.send('auto_status_off'.encode()) motor.motorStop() led.both_off() turn.middle() time.sleep(0.1) motor.motorStop() led.both_off() turn.middle() elif 'auto' in data: #When server receive "auto" from client,start Auto Mode if auto_status == 0: tcpCliSock.send('0'.encode()) auto_status = 1 auto_mode = 1 dis_scan = 0 else: pass continue elif 'opencv' in data: #When server receive "auto" from client,start Auto Mode if auto_status == 0: auto_status = 1 opencv_mode = 1 tcpCliSock.send('oncvon'.encode()) continue elif 'findline' in data: #Find line mode start if auto_status == 0: tcpCliSock.send('findline'.encode()) auto_status = 1 findline_mode = 1 else: pass continue elif 'voice_3' in data: #Speech recognition mode start if auto_status == 0: auto_status = 1 speech_mode = 1 tcpCliSock.send('voice_3'.encode()) else: pass continue
import nptime print('UTC time:', nptime.settime()) #print('\tTODO -local time') ''' test loop: get temperature --------- import test_ds18b20 test_ds18b20.run(5.0) #''' # loop: get temperature --------- import ds18b20 ds = ds18b20.setup() #get sensor import led led = led.setup() #default pin #TEST: led.blink() # --- location --------------- _LOCATION = 'studyroom' ''' print data to serial port print('... print sensor data to serial port') def serialDisplay(dt=2.0): import time import utime while True: led.on() temp = ds18b20.temperature(ds) timestamp = utime.localtime() print('{0},{1},{2},{3},{4},{5},{6},{7:0.2f}'.format(_LOCATION,timestamp[0],timestamp[1],timestamp[2],timestamp[3]+2,timestamp[4],timestamp[5],temp))
def run(): '''Main loop''' #??? Do these globals make sense global hoz_mid, vtr_mid, ip_con, led_status, auto_status, opencv_mode, findline_mode, speech_mode, auto_mode, data, addr, footage_socket, ap_status, turn_status, wifi_status led.setup() while True: #??? only exits from breaks, seems wrong #??? should this be if else rather than try except #??? while not-connected try: # try to ping to see if there is wifi # checking self ip addr s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("1.1.1.1", 80)) ipaddr_check = s.getsockname()[0] s.close() print(ipaddr_check) wifi_status = 1 except: # if not set up hotspot if ap_status == 0: #??? it always is #Define a thread for data receiving ap_threading = threading.Thread(target=ap_thread) ap_threading.setDaemon(True) ap_threading.start() led.both_off() led.yellow() time.sleep(5) #??? why sleep wifi_status = 0 #??? seems unecessary if wifi_status == 1: print('waiting for connection...') led.red() #??? pause here and wait for connection? tcpCliSock, addr = tcpSerSock.accept() led.both_off() led.green() print('...connected from :', addr) #time.sleep(1) #TODO: change to format #??? is this the best way to do this tcpCliSock.send( ('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % look_up_max + ' %s' % look_down_max).encode()) print('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % left + ' %s' % right) break else: #??? now assuming ap-hotspot, but never explained led.both_off() led.blue() #??? a lot of this is duplicated in the if statement print('waiting for connection...') # wait for connection tcpCliSock, addr = tcpSerSock.accept() led.both_off() led.green() print('...connected from :', addr) #time.sleep(1) tcpCliSock.send( ('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % look_up_max + ' %s' % look_down_max).encode()) print('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % left + ' %s' % right) ap_status = 1 break #??? this is outside the while loop now #FPV initialization context = zmq.Context() footage_socket = context.socket(zmq.PUB) footage_socket.connect('tcp://%s:5555' % addr[0]) print(addr[0]) #??? why are we starting all these threads now #Threads start #Define a thread for FPV and OpenCV video_threading = threading.Thread(target=opencv_thread) video_threading.setDaemon(True) video_threading.start() #Define a thread for ws_2812 leds ws2812_threading = threading.Thread(target=ws2812_thread) ws2812_threading.setDaemon(True) ws2812_threading.start() #Define a thread for line tracking findline_threading = threading.Thread(target=findline_thread) findline_threading.setDaemon(True) findline_threading.start() #Define a thread for speech recognition speech_threading = threading.Thread(target=speech_thread) speech_threading.setDaemon(True) speech_threading.start() #Define a thread for ultrasonic tracking auto_threading = threading.Thread(target=auto_thread) auto_threading.setDaemon(True) auto_threading.start() #Define a thread for ultrasonic scan scan_threading = threading.Thread(target=dis_scan_thread) scan_threading.setDaemon(True) scan_threading.start() while True: #??? infinite loop data = '' #??? why do we have to define early data = tcpCliSock.recv(BUFSIZ).decode() print(data) #??? BUFSIZ is global #??? this should be handled by a dictionary or interrupts if not data: continue elif 'exit' in data: quit() #??? shutting down is a bad idea #TODO: Make a shutdown function #TODO: Include proper shutdowns #os.system("sudo shutdown -h now\n") elif 'spdset' in data: #Speed Adjustment global spd_ad spd_ad = float((str(data))[7:]) elif 'scan' in data: #Start Scanning dis_can = scan() #Divide the list to make it samller to send str_list_1 = dis_can #Separate the values by space str_index = ' ' str_send_1 = str_index.join(str_list_1) + ' ' #??? this send is surprising #Send Data tcpCliSock.sendall((str(str_send_1)).encode()) #Sending 'finished' #tell the client to stop receiving the list of dis_can tcpCliSock.send('finished'.encode()) elif 'scan_rev' in data: #Start Scanning #??? what is scan_rev #??? a lot of this is repeated dis_can = scan_rev() #Divide the list to make it samller to send str_list_1 = dis_can #Separate the values by space str_index = ' ' str_send_1 = str_index.join(str_list_1) + ' ' #Send Data tcpCliSock.sendall((str(str_send_1)).encode()) #Sending 'finished' #tell the client to stop receiving the list of dis_can tcpCliSock.send('finished'.encode()) #??? what are there names elif 'EC1set' in data: #Camera Adjustment new_EC1 = int((str(data))[7:]) turn.camera_turn(new_EC1) replace_num('E_C1:', new_EC1) elif 'EC2set' in data: #Ultrasonic Adjustment new_EC2 = int((str(data))[7:]) replace_num('E_C2:', new_EC2) turn.ultra_turn(new_EC2) #TODO: fix the code for single motor elif 'EM1set' in data: #Motor A Speed Adjustment new_EM1 = int((str(data))[7:]) replace_num('E_M1:', new_EM1) elif 'EM2set' in data: #Motor B Speed Adjustment new_EM2 = int((str(data))[7:]) replace_num('E_M2:', new_EM2) #TODO: fix the code for single motor elif 'LUMset' in data: #Motor A Turningf Speed Adjustment new_ET1 = int((str(data))[7:]) replace_num('look_up_max:', new_ET1) turn.camera_turn(new_ET1) elif 'LDMset' in data: #Motor B Turningf Speed Adjustment new_ET2 = int((str(data))[7:]) replace_num('look_down_max:', new_ET2) turn.camera_turn(new_ET2) elif 'stop' in data: #??? what is the goal of stop #??? what is this 9 tcpCliSock.send('9'.encode()) #??? why are we doing setup stop setup setup() motor.motorStop() setup() #??? why are we doing this if led_status == 0: led.setup() led.both_off() #??? what is the colorwipe colorWipe(strip, Color(0, 0, 0)) continue elif 'lightsON' in data: # Turn on the LEDs led.both_on() #TODO statuses should be handled by a dictionary led_status = 1 tcpCliSock.send('lightsON'.encode()) elif 'lightsOFF' in data: # Turn off the LEDs led.both_off() led_status = 0 tcpCliSock.send('lightsOFF'.encode()) elif 'middle' in data: # Go straight if led_status == 0: led.side_color_off(left_R, left_G) led.side_color_off(right_R, right_G) else: led.side_on(left_B) led.side_on(right_B) #TODO status handled better turn_status = 0 turn.middle() elif 'Left' in data: # Turn left #TODO: fix the capital case if led_status == 0: led.side_color_on(left_R, left_G) else: led.side_off(left_B) turn.left() turn_status = 1 #??? what do these numbers map too #??? maybe use a dictionary tcpCliSock.send('3'.encode()) elif 'Right' in data: # Turn right #TODO: fix the capital case if led_status == 0: #TODO: clarify variable names led.side_color_on(right_R, right_G) else: led.side_off(right_B) turn.right() turn_status = 2 tcpCliSock.send('4'.encode()) #TODO: fix for single motor elif 'backward' in data: # Go backward motor.motor_left(status, backward, left_spd * spd_ad) motor.motor_right(status, forward, right_spd * spd_ad) colorWipe(strip, Color(255, 0, 0)) tcpCliSock.send('2'.encode()) #TODO: fix for single motor elif 'forward' in data: # Go forward motor.motor_left(status, forward, left_spd * spd_ad) motor.motor_right(status, backward, right_spd * spd_ad) colorWipe(strip, Color(0, 0, 255)) tcpCliSock.send('1'.encode()) #TODO: make better names elif 'l_up' in data: #Camera look up if vtr_mid < look_up_max: vtr_mid += turn_speed turn.camera_turn(vtr_mid) tcpCliSock.send('5'.encode()) elif 'l_do' in data: # Camera look down if vtr_mid > look_down_max: vtr_mid -= turn_speed turn.camera_turn(vtr_mid) print(vtr_mid) tcpCliSock.send('6'.encode()) elif 'l_le' in data: # Camera look left if hoz_mid < look_left_max: hoz_mid += turn_speed turn.ultra_turn(hoz_mid) tcpCliSock.send('7'.encode()) elif 'l_ri' in data: # Camera look right if hoz_mid > look_right_max: hoz_mid -= turn_speed turn.ultra_turn(hoz_mid) tcpCliSock.send('8'.encode()) elif 'ahead' in data: # Camera look ahead turn.ahead() elif 'Stop' in data: #TODO: fix capitalization opencv_mode = 0 findline_mode = 0 speech_mode = 0 auto_mode = 0 auto_status = 0 dis_scan = 1 #??? why is this 1 tcpCliSock.send('auto_status_off'.encode()) motor.motorStop() led.both_off() turn.middle() time.sleep(0.1) #??? why #??? why do this twice motor.motorStop() led.both_off() turn.middle() elif 'auto' in data: # start Auto Mode #??? which automode is this if auto_status == 0: tcpCliSock.send('0'.encode()) auto_status = 1 auto_mode = 1 #?? dif from automode and autostatus dis_scan = 0 #??? dis_scan else: pass continue elif 'opencv' in data: # Start opencv mode if auto_status == 0: auto_status = 1 opencv_mode = 1 #??? what is this name tcpCliSock.send('oncvon'.encode()) continue elif 'findline' in data: # Find line mode start if auto_status == 0: tcpCliSock.send('findline'.encode()) auto_status = 1 findline_mode = 1 else: pass continue #??? why voice 3 elif 'voice_3' in data: #Speech recognition mode start if auto_status == 0: auto_status = 1 speech_mode = 1 tcpCliSock.send('voice_3'.encode()) else: pass continue
resposta = """ <html> <head> <meta http-equiv="refresh" content="0; url=http://pi.g2.asi.itic.cat:808/led_on.html"> </head> </html> """ self.wfile.write(resposta) led.on() def _Led_OFF(self): self._head_html() resposta = """ <html> <head> <meta http-equiv="refresh" content="0; url=http://pi.g2.asi.itic.cat:808/led_off.html"> </head> </html> """ self.wfile.write(resposta) led.off() led.setup(18) PORT = 8000 Handler = LEDHTTPRequestHandler httpd = base.HTTPServer(("", PORT), Handler) print "Serving at port: " + str(PORT) httpd.serve_forever()
def setup(): #initialization motor.setup() led.setup()
<title>Weatherstation</title> <meta http-equiv="refresh" content="10"> </head> <body> <h1>Studyroom</h1> <table border="1"> <tr><th>Temperature</th></tr> %s </table> </body> </html> """ # loop: get temperature - #added 2017-1105 import ds18b20 ds = ds18b20.setup() #get sensor #alert led - #added 2017-1105 import led led = led.setup() #default pin WeMOS D1 mini: GPIO12 import socket addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(1) print('listening on', addr) rows = [] #empty rows #added 2017-1105 max_row = 12 #added 2017-1105 while True: led.on() #show I'm alive cl, addr = s.accept() #wait for client
def setup(): motor.setup() led.setup() turn.turn_middle() led.green()
def run(): #Main loop global hoz_mid, vtr_mid, ip_con, led_status, auto_status, opencv_mode, findline_mode, speech_mode, auto_mode, data, addr, footage_socket, ap_status, turn_status, wifi_status led.setup() while True: #Connection print('waiting for connection...') led.red() tcpCliSock, addr = tcpSerSock.accept() #Determine whether to connect led.both_off() led.green() print('...connected from :', addr) #time.sleep(1) tcpCliSock.send(('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % look_up_max + ' %s' % look_down_max).encode()) print('SET %s' % vtr_mid + ' %s' % hoz_mid + ' %s' % left_spd + ' %s' % right_spd + ' %s' % left + ' %s' % right) break #Threads start findline_threading = threading.Thread( target=findline_thread) #Define a thread for line tracking findline_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes findline_threading.start() #Thread starts auto_threading = threading.Thread( target=auto_thread) #Define a thread for ultrasonic tracking auto_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes auto_threading.start() #Thread starts time.sleep(0.5) tcpCliSock.send('TestVersion'.encode()) while True: data = '' data = tcpCliSock.recv(BUFSIZ).decode() if not data: continue elif 'exit' in data: pass elif 'spdset' in data: global spd_ad spd_ad = float((str(data))[7:]) #Speed Adjustment elif 'scan' in data: dis_can = scan() #Start Scanning str_list_1 = dis_can #Divide the list to make it samller to send str_index = ' ' #Separate the values by space str_send_1 = str_index.join(str_list_1) + ' ' tcpCliSock.sendall((str(str_send_1)).encode()) #Send Data tcpCliSock.send( 'finished'.encode() ) #Sending 'finished' tell the client to stop receiving the list of dis_can elif 'scan_rev' in data: dis_can = scan_rev() #Start Scanning str_list_1 = dis_can #Divide the list to make it samller to send str_index = ' ' #Separate the values by space str_send_1 = str_index.join(str_list_1) + ' ' tcpCliSock.sendall((str(str_send_1)).encode()) #Send Data tcpCliSock.send( 'finished'.encode() ) #Sending 'finished' tell the client to stop receiving the list of dis_can elif 'EC1set' in data: #Camera Adjustment new_EC1 = int((str(data))[7:]) turn.camera_turn(new_EC1) replace_num('E_C1:', new_EC1) elif 'EC2set' in data: #Ultrasonic Adjustment new_EC2 = int((str(data))[7:]) replace_num('E_C2:', new_EC2) turn.ultra_turn(new_EC2) elif 'EM1set' in data: #Motor A Speed Adjustment new_EM1 = int((str(data))[7:]) replace_num('E_M1:', new_EM1) elif 'EM2set' in data: #Motor B Speed Adjustment new_EM2 = int((str(data))[7:]) replace_num('E_M2:', new_EM2) elif 'LUMset' in data: #Motor A Turningf Speed Adjustment new_ET1 = int((str(data))[7:]) replace_num('look_up_max:', new_ET1) turn.camera_turn(new_ET1) elif 'LDMset' in data: #Motor B Turningf Speed Adjustment new_ET2 = int((str(data))[7:]) replace_num('look_down_max:', new_ET2) turn.camera_turn(new_ET2) elif 'stop' in data: #When server receive "stop" from client,car stops moving tcpCliSock.send('9'.encode()) setup() motor.motorStop() setup() if led_status == 0: led.setup() led.both_off() continue elif 'lightsON' in data: #Turn on the LEDs led.both_on() led_status = 1 tcpCliSock.send('lightsON'.encode()) elif 'lightsOFF' in data: #Turn off the LEDs led.both_off() led_status = 0 tcpCliSock.send('lightsOFF'.encode()) elif 'middle' in data: #Go straight if led_status == 0: led.side_color_off(left_R, left_G) led.side_color_off(right_R, right_G) else: led.side_on(left_B) led.side_on(right_B) turn_status = 0 turn.middle() elif 'Left' in data: #Turn left if led_status == 0: led.side_color_on(left_R, left_G) else: led.side_off(left_B) turn.left() tcpCliSock.send('3'.encode()) elif 'Right' in data: #Turn right if led_status == 0: led.side_color_on(right_R, right_G) else: led.side_off(right_B) turn.right() tcpCliSock.send('4'.encode()) elif 'backward' in data: #When server receive "backward" from client,car moves backward tcpCliSock.send('2'.encode()) motor.motor_left(status, backward, left_spd * spd_ad) motor.motor_right(status, forward, right_spd * spd_ad) elif 'forward' in data: #When server receive "forward" from client,car moves forward tcpCliSock.send('1'.encode()) motor.motor_left(status, forward, left_spd * spd_ad) motor.motor_right(status, backward, right_spd * spd_ad) elif 'l_up' in data: #Camera look up if vtr_mid < look_up_max: vtr_mid += turn_speed turn.camera_turn(vtr_mid) tcpCliSock.send('5'.encode()) elif 'l_do' in data: #Camera look down if vtr_mid > look_down_max: vtr_mid -= turn_speed turn.camera_turn(vtr_mid) tcpCliSock.send('6'.encode()) elif 'l_le' in data: #Camera look left if hoz_mid < look_left_max: hoz_mid += turn_speed turn.ultra_turn(hoz_mid) tcpCliSock.send('7'.encode()) elif 'l_ri' in data: #Camera look right if hoz_mid > look_right_max: hoz_mid -= turn_speed turn.ultra_turn(hoz_mid) tcpCliSock.send('8'.encode()) elif 'ahead' in data: #Camera look ahead turn.ahead() elif 'Stop' in data: #When server receive "Stop" from client,Auto Mode switches off findline_mode = 0 auto_mode = 0 auto_status = 0 tcpCliSock.send('auto_status_off'.encode()) motor.motorStop() led.both_off() turn.middle() time.sleep(0.1) motor.motorStop() led.both_off() turn.middle() elif 'auto' in data: #When server receive "auto" from client,start Auto Mode if auto_status == 0: tcpCliSock.send('0'.encode()) auto_status = 1 auto_mode = 1 dis_scan = 0 else: pass continue elif 'findline' in data: #Find line mode start if auto_status == 0: tcpCliSock.send('findline'.encode()) auto_status = 1 findline_mode = 1 else: pass continue
else: print "ID: \033[31m0x%s\033[0m" % (ID) speak(settings.ACCESSDENIED) led.set(1, 0, 0) sleep(1) led.set(0, 0, 0) def lock(): thr = threading.Thread(target = led.unlock, args=(), kwargs={}) thr.start() motor.lock() thr.join() def unlock(): speak("Access Granted.") thr = threading.Thread(target = led.lock, args=(), kwargs={}) thr.start() motor.unlock() thr.join() def speak(msg): wget = Popen(["echo \"%s\" | espeak -s %d -p %d" % (msg, settings.SPEAKSPEED, settings.SPEAKPITCH), ""], stdout=PIPE, stderr=STDOUT, shell=True) def exit_handler(signal, frame): led.exit() sys.exit(0) signal.signal(signal.SIGINT, exit_handler) led.setup() main()
def __init__(self): motor.setup() led.setup()
def setup(): motor.setup() led.setup() breathingLed.setup() activeBuzzer.setup()
spectrum_label.setText('Spectrum', color=active_color) # Create effect "buttons" (labels with click event) energy_label = pg.LabelItem('Energy') scroll_label = pg.LabelItem('Scroll') spectrum_label = pg.LabelItem('Spectrum') energy_label.mousePressEvent = energy_click scroll_label.mousePressEvent = scroll_click spectrum_label.mousePressEvent = spectrum_click energy_click(0) # Layout layout.nextRow() layout.addItem(freq_label, colspan=3) layout.nextRow() layout.addItem(freq_slider, colspan=3) layout.nextRow() layout.addItem(energy_label) layout.addItem(scroll_label) layout.addItem(spectrum_label) # Initialize LEDs dsp.create_mel_bank(config) led.setup(config) led.update(config) # Start listening to live audio stream tmic = threading.Thread(target=microphone.start_stream, args=(microphone_update, config)) tmic.start() remote_control.control_loop(config) tmic.join() # microphone.start_stream(microphone_update)
def setup(): '''initialization''' motor.setup() led.setup()
import speech_recognition as sr import os import time import led as l import utility as util from espeak import espeak wake_word = 'hello' keywords = ['time', 'class'] r = sr.Recognizer() l.setup() 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:
<html> <head> <meta http-equiv="refresh" content="0; url=http://pi.g2.asi.itic.cat:808/led_on.html"> </head> </html> """ self.wfile.write(resposta) led.on() def _Led_OFF(self): self._head_html() resposta = """ <html> <head> <meta http-equiv="refresh" content="0; url=http://pi.g2.asi.itic.cat:808/led_off.html"> </head> </html> """ self.wfile.write(resposta) led.off() led.setup(18) PORT = 8000 Handler = LEDHTTPRequestHandler httpd = base.HTTPServer(("",PORT),Handler) print "Serving at port: " + str(PORT) httpd.serve_forever()