Пример #1
0
def run_with_callback(host):
    """ Start a wsgiref server instance with control over the main loop.
        This is a function that I derived from the bottle.py run()
    """
    handler = default_app()
    server = wsgiref.simple_server.make_server(host, NETWORK_PORT, handler)
    server.timeout = 0.01
    server.quiet = True
    print "-----------------------------------------------------------------------------"
    print "Bottle server starting up ..."
    print "Serial is set to %d bps" % BITSPERSECOND
    print "Point your browser to: "
    print "http://%s:%d/      (local)" % ("127.0.0.1", NETWORK_PORT)
    if host == "":
        print "http://%s:%d/   (public)" % (socket.gethostbyname(socket.gethostname()), NETWORK_PORT)
    print "Use Ctrl-C to quit."
    print "-----------------------------------------------------------------------------"
    print
    try:
        webbrowser.open_new_tab("http://127.0.0.1:" + str(NETWORK_PORT))
    except webbrowser.Error:
        print "Cannot open Webbrowser, please do so manually."
    sys.stdout.flush()  # make sure everything gets flushed
    while 1:
        try:
            SerialManager.send_queue_as_ready()
            server.handle_request()
        except KeyboardInterrupt:
            break
    print "\nShutting down..."
    SerialManager.close()
Пример #2
0
def run_with_callback(host, port=4444, timeout=0.01):
    """ Start a wsgiref server instance with control over the main loop.
        This is a function that I derived from the bottle.py run()
    """
    handler = default_app()
    server = wsgiref.simple_server.make_server(host, port, handler)
    server.timeout = timeout
    print "-----------------------------------------------------------------------------"
    print "Bottle server starting up ..."
    print "Serial is set to %d bps" % BITSPERSECOND
    print "Point your browser to: "    
    print "http://%s:%d/      (local)" % ('127.0.0.1', port)    
    if host == '':
        print "http://%s:%d/   (public)" % (socket.gethostbyname(socket.gethostname()), port)
    print "Use Ctrl-C to quit."
    print "-----------------------------------------------------------------------------"    
    print
    while 1:
        try:
            SerialManager.send_queue_as_ready()
            server.handle_request()
        except KeyboardInterrupt:
            break
    print "\nShutting down..."
    SerialManager.close()
Пример #3
0
def flash_firmware_handler():
    if SerialManager.is_connected():
        SerialManager.close()
    global SERIAL_PORT, GUESS_PREFIX
    if not SERIAL_PORT:
        SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX)        
    flash_upload(SERIAL_PORT, resources_dir())
    return '<h2>flashing finished!</h2> Check Log window for possible errors.<br><a href="/">return</a>'
Пример #4
0
def flash_firmware_handler(firmware_file=FIRMWARE):
    global SERIAL_PORT, GUESS_PREFIX
    return_code = 1
    if SerialManager.is_connected():
        SerialManager.close()
    # get serial port by url argument
    # e.g: /flash_firmware?port=COM3
    if 'port' in request.GET.keys():
        serial_port = request.GET['port']
        if serial_port[:3] == "COM" or serial_port[:4] == "tty.":
            SERIAL_PORT = serial_port
    # get serial port by enumeration method
    # currenty this works on windows only for updating the firmware
    if not SERIAL_PORT:
        SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND)
    # resort to brute force methode
    # find available com ports and try them all
    if not SERIAL_PORT:
        comport_list = SerialManager.list_devices(BITSPERSECOND)
        for port in comport_list:
            print "Trying com port: " + port
            return_code = flash_upload(port, resources_dir(), firmware_file,
                                       HARDWARE)
            if return_code == 0:
                print "Success with com port: " + port
                SERIAL_PORT = port
                break
    else:
        return_code = flash_upload(SERIAL_PORT, resources_dir(), firmware_file,
                                   HARDWARE)
    ret = []
    ret.append('Using com port: %s<br>' % (SERIAL_PORT))
    ret.append('Using firmware: %s<br>' % (firmware_file))
    if return_code == 0:
        print "SUCCESS: Arduino appears to be flashed."
        ret.append('<h2>Successfully Flashed!</h2><br>')
        ret.append('<a href="/">return</a>')
        return ''.join(ret)
    else:
        print "ERROR: Failed to flash Arduino."
        ret.append(
            '<h2>Flashing Failed!</h2> Check terminal window for possible errors. '
        )
        ret.append(
            'Most likely LasaurApp could not find the right serial port.')
        ret.append('<br><a href="/flash_firmware/' + firmware_file +
                   '">try again</a> or <a href="/">return</a><br><br>')
        if os.name != 'posix':
            ret.append(
                'If you know the COM ports the Arduino is connected to you can specifically select it here:'
            )
            for i in range(1, 13):
                ret.append(
                    '<br><a href="/flash_firmware?port=COM%s">COM%s</a>' %
                    (i, i))
        return ''.join(ret)
Пример #5
0
def run_with_callback(host, port, rfidreader, logger, powertimer):
    """ Start a wsgiref server instance with control over the main loop.
        This is a function that I derived from the bottle.py run()
    """
    debug(True)
    handler = default_app()
    handler.catchall = False
    handler.rfidreader = rfidreader
    handler.logger = logger
    handler.powertimer = powertimer
    server = make_server(host, port, handler, handler_class=HackedWSGIRequestHandler)
    server.timeout = 0.01
    #server.quiet = True
    print "Persistent storage root is: " + storage_dir()
    print "-----------------------------------------------------------------------------"
    print "Bottle server starting up ..."
    print "Serial is set to %d bps" % BITSPERSECOND
    print "Point your browser to: "
    print "http://%s:%d/      (local)" % ('127.0.0.1', port)
    # if host == '':
    #     try:
    #         print "http://%s:%d/   (public)" % (socket.gethostbyname(socket.gethostname()), port)
    #     except socket.gaierror:
    #         # print "http://beaglebone.local:4444/      (public)"
    #         pass
    print "Use Ctrl-C to quit."
    print "-----------------------------------------------------------------------------"
    print
    # auto-connect on startup
    global SERIAL_PORT
    if not SERIAL_PORT:
        SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND)
    SerialManager.connect(SERIAL_PORT, BITSPERSECOND)
    # open web-browser
    try:
        webbrowser.open_new_tab('http://127.0.0.1:'+str(port))
        pass
    except webbrowser.Error:
        print "Cannot open Webbrowser, please do so manually."
    sys.stdout.flush()  # make sure everything gets flushed
    server.timeout = 0
    while 1:
        try:
            SerialManager.send_queue_as_ready()
            server.handle_request()
            time.sleep(0.0004)
        except KeyboardInterrupt:
            break
    print "\nShutting down..."
    SerialManager.close()
Пример #6
0
def flash_firmware_handler(firmware_file=FIRMWARE):
    global user_approved
    global user_admin
    if not user_approved and user_admin:
        return 'Access denied'
    logger.log(current_user, 'Flashing ATMEGA')
    return_code = 1
    if SerialManager.is_connected():
        SerialManager.close()
    # get serial port by url argument
    # e.g: /flash_firmware?port=COM3
    if 'port' in request.GET.keys():
        serial_port = request.GET['port']
        if serial_port[:3] == "COM" or serial_port[:4] == "tty.":
            SERIAL_PORT = serial_port
    # get serial port by enumeration method
    # currenty this works on windows only for updating the firmware
    if not SERIAL_PORT:
        SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND)
    # resort to brute force methode
    # find available com ports and try them all
    if not SERIAL_PORT:
        comport_list = SerialManager.list_devices(BITSPERSECOND)
        for port in comport_list:
            print "Trying com port: " + port
            return_code = flash_upload(port, resources_dir(), firmware_file, HARDWARE)
            if return_code == 0:
                print "Success with com port: " + port
                SERIAL_PORT = port
                break
    else:
        return_code = flash_upload(SERIAL_PORT, resources_dir(), firmware_file, HARDWARE)
    ret = []
    ret.append('Using com port: %s<br>' % (SERIAL_PORT))
    ret.append('Using firmware: %s<br>' % (firmware_file))
    if return_code == 0:
        print "SUCCESS: Arduino appears to be flashed."
        ret.append('<h2>Successfully Flashed!</h2><br>')
        ret.append('<a href="/">return</a>')
        return ''.join(ret)
    else:
        print "ERROR: Failed to flash Arduino."
        ret.append('<h2>Flashing Failed!</h2> Check terminal window for possible errors. ')
        ret.append('Most likely LasaurApp could not find the right serial port.')
        ret.append('<br><a href="/flash_firmware/'+firmware_file+'">try again</a> or <a href="/">return</a><br><br>')
        if os.name != 'posix':
            ret. append('If you know the COM ports the Arduino is connected to you can specifically select it here:')
            for i in range(1,13):
                ret. append('<br><a href="/flash_firmware?port=COM%s">COM%s</a>' % (i, i))
        return ''.join(ret)
Пример #7
0
def run_with_callback(host, port):
    """ Start a wsgiref server instance with control over the main loop.
        This is a function that I derived from the bottle.py run()
    """
    handler = default_app()
    server = make_server(host,
                         port,
                         handler,
                         handler_class=HackedWSGIRequestHandler)
    server.timeout = 0.01
    server.quiet = True
    print "Persistent storage root is: " + storage_dir()
    print "-----------------------------------------------------------------------------"
    print "Bottle server starting up ..."
    print "Serial is set to %d bps" % BITSPERSECOND
    print "Point your browser to: "
    print "http://%s:%d/      (local)" % ('127.0.0.1', port)
    # if host == '':
    #     try:
    #         print "http://%s:%d/   (public)" % (socket.gethostbyname(socket.gethostname()), port)
    #     except socket.gaierror:
    #         # print "http://beaglebone.local:4444/      (public)"
    #         pass
    print "Use Ctrl-C to quit."
    print "-----------------------------------------------------------------------------"
    print
    # auto-connect on startup
    global SERIAL_PORT
    if not SERIAL_PORT:
        SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND)
    SerialManager.connect(SERIAL_PORT, BITSPERSECOND)
    # open web-browser
    try:
        webbrowser.open_new_tab('http://127.0.0.1:' + str(port))
        pass
    except webbrowser.Error:
        print "Cannot open Webbrowser, please do so manually."
    sys.stdout.flush()  # make sure everything gets flushed
    server.timeout = 0
    while 1:
        try:
            SerialManager.send_queue_as_ready()
            server.handle_request()
            time.sleep(0.0004)
        except KeyboardInterrupt:
            break
    print "\nShutting down..."
    SerialManager.close()
Пример #8
0
def serial_handler(connect):
    if connect == '1':
        # print 'js is asking to connect serial'
        if not SerialManager.is_connected():
            try:
                global SERIAL_PORT, BITSPERSECOND, GUESS_PREFIX
                if not SERIAL_PORT:
                    SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND)
                SerialManager.connect(SERIAL_PORT, BITSPERSECOND)
                ret = "Serial connected to %s:%d." % (SERIAL_PORT, BITSPERSECOND)  + '<br>'
                time.sleep(1.0) # allow some time to receive a prompt/welcome
                SerialManager.flush_input()
                SerialManager.flush_output()
                return ret
            except serial.SerialException:
                SERIAL_PORT = None
                print "Failed to connect to serial."
                return ""
    elif connect == '0':
        # print 'js is asking to close serial'
        if SerialManager.is_connected():
            if SerialManager.close(): return "1"
            else: return ""
    elif connect == "2":
        # print 'js is asking if serial connected'
        if SerialManager.is_connected(): return "1"
        else: return ""
    else:
        print 'ambigious connect request from js: ' + connect
        return ""
Пример #9
0
def serial_handler(connect):
    if connect == '1':
        # print 'js is asking to connect serial'
        if not SerialManager.is_connected():
            try:
                global SERIAL_PORT, BITSPERSECOND, GUESS_PREFIX
                if not SERIAL_PORT:
                    SERIAL_PORT = SerialManager.match_device(
                        GUESS_PREFIX, BITSPERSECOND)
                SerialManager.connect(SERIAL_PORT, BITSPERSECOND)
                ret = "Serial connected to %s:%d." % (SERIAL_PORT,
                                                      BITSPERSECOND) + '<br>'
                time.sleep(1.0)  # allow some time to receive a prompt/welcome
                SerialManager.flush_input()
                SerialManager.flush_output()
                return ret
            except serial.SerialException:
                SERIAL_PORT = None
                print "Failed to connect to serial."
                return ""
    elif connect == '0':
        # print 'js is asking to close serial'
        if SerialManager.is_connected():
            if SerialManager.close(): return "1"
            else: return ""
    elif connect == "2":
        # print 'js is asking if serial connected'
        if SerialManager.is_connected(): return "1"
        else: return ""
    else:
        print 'ambigious connect request from js: ' + connect
        return ""
Пример #10
0
def flash_firmware_handler():
    global SERIAL_PORT, GUESS_PREFIX
    return_code = 1
    if SerialManager.is_connected():
        SerialManager.close()
    # get serial port by url argument
    # e.g: /flash_firmware?port=COM3
    if "port" in request.GET.keys():
        serial_port = request.GET["port"]
        if serial_port[:3] == "COM" or serial_port[:4] == "tty.":
            SERIAL_PORT = serial_port
    # get serial port by enumeration method
    # currenty this works on windows only for updating the firmware
    if not SERIAL_PORT:
        SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND)
    # resort to brute force methode
    # find available com ports and try them all
    if not SERIAL_PORT:
        comport_list = SerialManager.list_devices(BITSPERSECOND)
        for port in comport_list:
            print "Trying com port: " + port
            return_code = flash_upload(port, resources_dir())
            if return_code == 0:
                print "Success with com port: " + port
                SERIAL_PORT = port
                break
    else:
        return_code = flash_upload(SERIAL_PORT, resources_dir())
    ret = []
    ret.append("Using com port: %s<br>" % (SERIAL_PORT))
    if return_code == 0:
        print "SUCCESS: Arduino appears to be flashed."
        ret.append("<h2>Successfully Flashed!</h2><br>")
        ret.append('<a href="/">return</a>')
        return "".join(ret)
    else:
        SERIAL_PORT = None
        print "ERROR: Failed to flash Arduino."
        ret.append("<h2>Flashing Failed!</h2> Check Log window for possible errors. ")
        ret.append('Most likely LasaurApp could not find the right serial port.<br><a href="/">return</a><br><br>')
        if os.name != "posix":
            ret.append("If you know the COM ports the Arduino is connected to you can specifically select it here:")
            for i in range(1, 13):
                ret.append('<br><a href="/flash_firmware?port=COM%s">COM%s</a>' % (i, i))
        return "".join(ret)
Пример #11
0
def run_with_callback(host):
    """ Start a wsgiref server instance with control over the main loop.
        This is a function that I derived from the bottle.py run()
    """
    handler = default_app()
    server = wsgiref.simple_server.make_server(host, NETWORK_PORT, handler)
    server.timeout = 0.01
    server.quiet = True
    print "-----------------------------------------------------------------------------"
    print "Bottle server starting up ..."
    print "Serial is set to %d bps" % BITSPERSECOND
    print "Point your browser to: "
    print "http://%s:%d/      (local)" % ('127.0.0.1', NETWORK_PORT)
    if host == '':
        try:
            print "http://%s:%d/   (public)" % (socket.gethostbyname(
                socket.gethostname()), NETWORK_PORT)
        except socket.gaierror:
            # print "http://beaglebone.local:4444/      (public)"
            pass
    print "Use Ctrl-C to quit."
    print "-----------------------------------------------------------------------------"
    print
    try:
        webbrowser.open_new_tab('http://127.0.0.1:' + str(NETWORK_PORT))
        pass
    except webbrowser.Error:
        print "Cannot open Webbrowser, please do so manually."
    sys.stdout.flush()  # make sure everything gets flushed
    while 1:
        try:
            SerialManager.send_queue_as_ready()
            server.handle_request()
        except KeyboardInterrupt:
            break
    print "\nShutting down..."
    SerialManager.close()
Пример #12
0
ev2 = EventWrapper(
    e2,
    marm.calculate_timeout(115200) + v2dt.calculate_timeout(115200))

sertest = AutoTimer(period=1, events=[ev0, ev1, ev2])

if manager.open_port('0', 115200):
    sertest.start()

try:
    while True:

        if dataflag:
            print(marm.voltages)
            print(marm.test)
            dataflag = False

            #user_input = raw_input()
            '''
		if manager.open_port(user_input, 115200):
			sertest.start()
		else:
			if user_input == 'q' or user_input == 'quit':
				sertest.stop()
				manager.close()
				quit()'''
except KeyboardInterrupt:
    print('Closing threads, serial port, and quitting...')
    sertest.kill()
    manager.close()
    quit(0)
Пример #13
0
	alive = False

manager1 = SerialManager()
print('Ports available')
manager1.list_ports()
print('Select Dynamixel port')
user_input = utils.getch()

if manager1.open_port(user_input, 115200):
	
	serv2 = Robotis_Servo(manager1,2)
	print('Setting up Dynamixel')
	print('Dynamixel angle at ' + str(serv2.read_angle()))
	print('Dynamixel encoder at ' + str(serv2.read_encoder()))

	serv5 = Robotis_Servo(manager1,5)
	print('Setting up Dynamixel')
	print('Dynamixel angle at ' + str(serv5.read_angle()))
	print('Dynamixel encoder at ' + str(serv5.read_encoder()))

	while(alive):
		#serv.move_angle(1, 1.5, blocking=False)
		serv2.move_angle(0.5,blocking=False)
		serv5.move_angle(0.1,blocking=False)
		time.sleep(2)
		serv2.move_angle(0.1,blocking=False)
		serv5.move_angle(0.5,blocking=False)
		time.sleep(2)

manager1.close()
Пример #14
0
def run_with_callback(host, port):
    """ Start a wsgiref server instance with control over the main loop.
        This is a function that I derived from the bottle.py run()
    """
    handler = default_app()
    server = make_server(host, port, handler, handler_class=HackedWSGIRequestHandler)
    if CERTSDIR:
      server.socket = ssl.wrap_socket(server.socket,
        keyfile=KEYFILE,
        certfile=CERTSFILE,
        server_side=True,
        cert_reqs=ssl.CERT_REQUIRED,
        ca_certs=CA_CERTSFILE)
      loadAccounts()
      server.verify_request = verify_request
    server.timeout = 0.01
    server.quiet = True
    print "Persistent storage root is: " + storage_dir()
    print "-----------------------------------------------------------------------------"
    print "Bottle server starting up ..."
    print "Serial is set to %d bps" % BITSPERSECOND
    print "Point your browser to: "
    if CERTSDIR:
      print "https://%s:%d/" % (COMMON_NAME, port)
    elif COMMON_NAME:
      print "http://%s:%d/" % (COMMON_NAME, port)
    else:
      print "http://%s:%d/" % ('127.0.0.1', port)
    print "Use Ctrl-C to quit."
    print "-----------------------------------------------------------------------------"
    print
    # auto-connect on startup
    global SERIAL_PORT
    if not SERIAL_PORT:
        SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND)
    SerialManager.connect(SERIAL_PORT, BITSPERSECOND)
# I:Mega Start
    time.sleep(1.0)
# I:Mega End
#    # open web-browser
#    try:
#        webbrowser.open_new_tab('http://127.0.0.1:'+str(port))
#        pass
#    except webbrowser.Error:
#        print "Cannot open Webbrowser, please do so manually."
    sys.stdout.flush()  # make sure everything gets flushed
    server.timeout = 0
    lastPowerStatus = 0
    powerStateChange = 0
    while 1:
        try:
            serial_handler('1')
            SerialManager.send_queue_as_ready()
            server.handle_request()
            if HARDWARE == 'raspberrypi':
              powerStatus = RPiPowerControl.interval_check()
              if powerStatus != lastPowerStatus:
                powerStateChange = 1
                lastPowerStatus = powerStatus
            if powerStateChange:
              powerStateChange = checkStatus()
            time.sleep(0.0004)
        except KeyboardInterrupt:
            if HARDWARE == 'raspberrypi':
              RPiPowerControl.gpio_cleanup()
            break
        except:
            import traceback
            traceback.print_exc()
            break
    print "\nShutting down..."
    if redirect_pid:
      os.kill(redirect_pid, signal.SIGTERM)
    SerialManager.close()
Пример #15
0
ev1 = EventWrapper(e1, marm.calculate_timeout(115200))
ev2 = EventWrapper(e2, marm.calculate_timeout(115200) + v2dt.calculate_timeout(115200))

sertest = AutoTimer(period=1, events=[ev0,ev1,ev2])

if manager.open_port('0', 115200):
	sertest.start()

try:
	while True:

		if dataflag:
			print(marm.voltages)
			print(marm.test)
			dataflag = False

		#user_input = raw_input()

			'''
		if manager.open_port(user_input, 115200):
			sertest.start()
		else:
			if user_input == 'q' or user_input == 'quit':
				sertest.stop()
				manager.close()
				quit()'''
except KeyboardInterrupt:
	print('Closing threads, serial port, and quitting...')
	sertest.kill()
	manager.close()
	quit(0)
Пример #16
0
slideRenderer = SlideshowManager(config, screen)
slideRenderer.load_default_slideshow()

serialManager = SerialManager(port="/dev/ttyAMA0", baudrate=9600)

done = False

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True

    serialManager.pool()
    data = serialManager.process()
    if data:
        process_input(slideRenderer, config, data)

    slideRenderer.tick()

    time.sleep(0.05)


serialManager.close()
slideRenderer.destroy()
pygame.quit()
from Servo import *
from data_structures import mini_arm as marm

utils = Utility()
alive = True

def quit():
	global alive
	alive = False

manager1 = SerialManager()
print 'Ports available'
manager1.list_ports()
print 'Select Dynamixel port'
user_input = utils.getch()

if manager1.open_port(user_input, 57600):
	
	serv_2 = Robotis_Servo(manager1, 1)
	print 'Setting up Dynamixel 2'
	
	print 'Dynamixel 2 at ' + str(serv_2.read_angle())

	while(alive):
		serv_2.move_angle(1, 1.5, blocking=False)
		time.sleep(1)
		serv_2.move_angle(2, 1.5, blocking=False)
		time.sleep(1)

manager1.close()