Ejemplo n.º 1
0
 def connect(self, parameters):
     self.stopAll()
     if parameters[0] == "WiFi":
         try:
             self.tcpClient = TCPClient(
                 (str(parameters[1]), int(parameters[2])), self.out_q,
                 self.in_q)
         except Exception as e:
             self.tcpClient = None
             Logger.getInstance().error("Cannot start WiFi connection: " +
                                        str(e))
         else:
             self.tcpClient.start()
             Logger.getInstance().info("Connected to WiFi server")
     elif parameters[0] == "COM":
         try:
             self.comClient = COMClient(str(parameters[1]),
                                        int(parameters[2]), self.out_q,
                                        self.in_q)
         except Exception as e:
             self.comClient = None
             Logger.getInstance().error("Cannot start serial connection: " +
                                        str(e))
         else:
             self.comClient.start()
             Logger.getInstance().info("Connected to COM server")
     if self.comClient or self.tcpClient:
         self.result.emit(True)
     else:
         self.result.emit(False)
Ejemplo n.º 2
0
class EmotivController(object):
    """
        Emocon
    """

    __metaclass__ = Singleton

    __socket1 = TCPClient(TARGET_IP, TARGET_PORT1)
    __spawn_socket1 = None

    def __init__(self):
        try:
            self.__socket1.connect()
            print "EmotivController instance is created"
        except IOError:
            print "IOError: Can't connect to emotiv server."

    def send_packet(self, request):
        """
            Send EEG packet to TF Server.
        """
        try:
            self.__socket1.send(request)
            response = self.__socket1.recv(1024)
            return response
        except IOError:
            return 'IOError'
    def __init__(self, id, dst_ip):
        threading.Thread.__init__(self)
        self.exitflag = False
        self.bank = 0
        self.id = id
        self.alive = True
        self.command = idle_msg
        self.dst_ip = dst_ip
        self.outputs = []
        self.is_request = False
        self.is_start = False
        self.timer1 = None

        if (id % 4) is 0:
            self.dst_port = 5001
            self.power_port = 0
            self.switch_port = 4
            self.redled_port = 8
            self.blueled_port = 9
        elif (id % 4) is 1:
            self.dst_port = 5002
            self.power_port = 1
            self.switch_port = 5
            self.redled_port = 10
            self.blueled_port = 11
        elif (id % 4) is 2:
            self.dst_port = 5003
            self.power_port = 3
            self.switch_port = 7
            self.redled_port = 14
            self.blueled_port = 15
        elif (id % 4) is 3:
            self.dst_port = 5004
            self.power_port = 2
            self.switch_port = 6
            self.redled_port = 12
            self.blueled_port = 13

        lastnumindex = dst_ip.rfind('.')
        lastnum = int(dst_ip[lastnumindex + 1:len(dst_ip)])

        self.module_ip = dst_ip[:lastnumindex + 1] + str(lastnum + id +
                                                         1) + "\r"
        # sys.stdout.write('module ip: %r\n' % self.module_ip)
        self.client = TCPClient(2, self.dst_ip, self.dst_port)
Ejemplo n.º 4
0
class DemoClass():
    tcp = TCPClient()
    default_Server_IP = "192.168.1.07"
    Camera_H_Pos = 90
    Camera_V_Pos = 90
    SERVO_MIN_ANGLE = 0
    SERVO_MAX_ANGLE = 180
    Is_Paint_Thread_On = False
    global t_Paint_Thread
    sonic_Index = 0
    sonic_buff = [0] * 20
    send_Counter = 0
    Is_tcp_Idle = True

    def __init__(self, parent=None):
        self.Camera_V_Pos = self.Camera_V_Pos + 3
        self.Camera_V_Pos = constrain(self.Camera_V_Pos, 0, 90)
        self.tcp.connectToServer(address=(self.default_Server_IP, 12345))
        self.tcp.sendData(cmd.CMD_CAMERA_UP + str(self.Camera_V_Pos))
Ejemplo n.º 5
0
def main(assist, ipv4_host, port, color, fpv, bci2000, speech, mapl, mapr):
    try:
        Console.disable_quick_edit()
        file = None
        keyboard = False
        bci = False
        listen = None
        listen_th = None
        sock = sckt.socket(sckt.AF_INET, sckt.SOCK_DGRAM)
        sock.connect(('8.8.8.8', 80))
        p_ip = sock.getsockname()[0]
        tcp_c = TCPClient(ipv4_host, port)
        if tcp_c.join() and input(MSG_READY).lower() == 'y':
            if bci2000:
                inbuff = queue.Queue()
                file = FParse(inbuff)
                file.read(bci2000)
                bci = True
            elif speech:
                listen = threading.Event()
                inbuff = queue.Queue()
                listen_th = threading.Thread(target=speech2txt,
                                             args=(inbuff, listen),
                                             daemon=True)
                listen_th.start()
            else:
                keyboard = True
                inbuff = None
            GameCtrl.init(tcp_c, color, fpv, keyboard, assist, inbuff, file,
                          speech, bci, mapl, mapr, listen)
            GameCtrl.start()
    except KeyboardInterrupt:
        pass
    if listen_th:
        listen_th.join(1)
    print(MSG_CLOSE, flush=True)
    Console.enable_quick_edit()
Ejemplo n.º 6
0
    def __init__(self, width, height, svr_addr, svr_port, host_addr,
                 host_port):  # {{{
        """ """
        self.lan = 'cn'
        self.tcp = TCPClient(4096)
        self.log = dict()

        # window
        self.width = width
        self.height = height
        self.win = tk.Tk()
        self.win.protocol('WM_DELETE_WINDOW', self.onCloseWindow)
        self.win.resizable(width=True, height=False)
        self.screenwidth = self.win.winfo_screenwidth()
        self.screenheight = self.win.winfo_screenheight()

        # homebrain address / port
        self.server_addr = tk.StringVar()
        self.server_port = tk.StringVar()
        self.server_addr.set(svr_addr)
        self.server_port.set(svr_port)

        # host address / port
        self.host_addr = tk.StringVar()
        self.host_port = tk.StringVar()
        self.host_addr.set(host_addr)
        self.host_port.set(host_port)

        self.filter_key = tk.StringVar()
        self.rule_newn_var = tk.StringVar()
        self.rule_switch_var = tk.StringVar()
        self.rule_btm_sw = 0

        self.onGlobalConfigure()
        self.createConnectView()
        self.win.mainloop()
Ejemplo n.º 7
0
        print '[ImageSource] Sending name:"%s" and size:"%s"' % (imgname,
                                                                 str(size))

        # ********************************************************
        #Initialize UDP socket
        udpClient = UDPClient(BUFFER_SIZE)
        # ********************************************************
        #Send Image Information via UDP packet in the format "IMAGE_NAME,IMAGE_SIZE"
        udpClient.sendUDPmsg(imgname + ',' + str(size), RPI_IP, UDP_PORT)
        #Assert that message with Image Name and Image Size was received
        ret = udpClient.receiveUDPmsg(delay)
        print "[ImageSource] Mensagem recebida do MidiSource", ret
        # ********************************************************
        #Connect to RPI TCP server
        if (ret == "OK"):
            tcpClient = TCPClient(RPI_IP, TCP_PORT, BUFFER_SIZE)
            tcpClient.connectTCP()
            print '[ImageSource] TCP Client ready to send the image'
            #tcpClient.sendFileByNameAndClose(imgname)
            # ********************************************************
            #Send Image
            tcpClient.sendDataBySize(img.read(), size)
            # ********************************************************
            #Close TCP socket
            tcpClient.closeTCPclient()
            print '[ImageSource] TCPClient closed'

            #**************************MIDI PART******************************************
            # ********************************************************
            #Wait for UDP Packet Flag saying MIDI is ready to be sent
            ret = udpClient.receiveUDPmsg(delay)
Ejemplo n.º 8
0
    def run(self):
        SOCK_CLOSE_STATE = 1
        SOCK_OPENTRY_STATE = 2
        SOCK_OPEN_STATE = 3
        SOCK_CONNECTTRY_STATE = 4
        SOCK_CONNECT_STATE = 5

        idle_state = 1
        datasent_state = 2

        sys.stdout.write('thread for %r ' % self.serverip)
        sys.stdout.write('is starting\r\n')

        # TCPClient instance creation

        client = TCPClient(2, self.serverip, self.serverport)
        filename = self.serverip + '_log.txt'

        #print(filename)
        IsTimeout = 0

        self.f = open(filename, 'w+')

        while True:
            if client.state is SOCK_CLOSE_STATE:
                cur_state = client.state
                client.state = client.open()
                if client.state != cur_state:
                    sys.stdout.write('client.state is %r\r\n' % client.state)
                time.sleep(1)

            elif client.state is SOCK_OPEN_STATE:
                cur_state = client.state
                client.state = client.connect()
                if client.state != cur_state:
                    sys.stdout.write('client.state is %r\r\n' % client.state)
                time.sleep(1)

            elif client.state is SOCK_CONNECT_STATE:
                if client.working_state == idle_state:
                    try:
                        client.write(msg)
                        client.working_state = datasent_state
                        self.istimeout = 0
                        self.totaltrycount += 1

                        self.timer1 = threading.Timer(5.0, self.myTimer)
                        self.timer1.start()
                    except Exception as e:
                        sys.stdout.write('%r\r\n' % e)
                elif client.working_state == datasent_state:
                    response = client.readline()
                    if (response != ""):
                        sys.stdout.write(response)
                        sys.stdout.write('\n')
                        sys.stdout.flush()
                        self.timer1.cancel()
                        self.istimeout = 0

                        if (msg in response):
                            logstr = '[' + self.serverip + ']' + strftime(
                                "%d %b %Y %H:%M:%S",
                                localtime()) + ': success\r\n'
                            sys.stdout.write(logstr)
                            self.successcount += 1
                            time.sleep(10)
                            client.working_state = idle_state
                        else:
                            logstr = '[' + self.serverip + ']' + strftime(
                                "%d %b %Y %H:%M:%S",
                                localtime()) + ': fail by broken data\r\n'
                            sys.stdout.write(logstr)
                            self.failcount += 1
                            self.f.write(logstr)
                            self.f.write("\r\n")
                            time.sleep(10)
                            client.working_state = idle_state

                    if self.istimeout is 1:
                        self.timer1.cancel()
                        self.istimeout = 0
                        logstr = '[' + self.serverip + ']' + strftime(
                            "%d %b %Y %H:%M:%S",
                            localtime()) + ': fail by timeout\r\n'
                        sys.stdout.write(logstr)
                        self.failcount += 1
                        self.f.write(logstr)
                        self.f.write("\r\n")
                        time.sleep(5)
                        client.working_state = idle_state

                    response = ""
Ejemplo n.º 9
0
# # # # # # # # # # # # # # #
#      Socket Helpers       #
# # # # # # # # # # # # # # #

from sys import argv
from TCPClient import TCPClient

if len(argv) == 3:
    ip = argv[1]
    port = int(argv[2])
else:
    ip = '127.0.0.1'
    port = 1337

TCP = TCPClient(ip, port)


def readString():
    str = TCP.readline()
    print '<', str
    return str


def readArray():
    return readString().split(' ')


def write(str):
    if isinstance(str, (list, tuple)):
        str = ' '.join(str)
    print '>', str
class ScratchServer(BaseHTTPRequestHandler):
    tcp = TCPClient()
    Camera_H_Pos = 90
    Camera_V_Pos = 90
    SERVO_MIN_ANGLE = 0
    SERVO_MAX_ANGLE = 180
    Is_Paint_Thread_On = False
    global t_Paint_Thread
    sonic_Index = 0
    sonic_buff = [0]*20
    send_Counter = 0
    Is_tcp_Idle = True

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(self.parsePath(self.path))

    def parsePath(self, path):
        pathParts = path.split('/')
        if len(pathParts) < 2:
            ScratchServer.lastError = "ERROR: No Command detected"
            return ScratchServer.lastError

        cmd = pathParts[1]

        if cmd == "favicon.ico": return
        
        if (cmd != "poll" and cmd != "lastError"): ScratchServer.lastError = ""

        if cmd == "crossdomain.xml":
            return self.getXDomainResponse()
        elif cmd == "connect":
            server_ip = pathParts[2]
            return self.connect2Car(server_ip)
        elif cmd == "disconnect":
            return self.disconnectFromCar()
        elif cmd == "cUp":
            return self.cameraUp(pathParts[2])
        elif cmd == "cDown":
            return self.cameraDown(pathParts[2])
        elif cmd == "cLeft":
            return self.cameraLeft(pathParts[2])
        elif cmd == "cRight":
            return self.cameraRight(pathParts[2])
        elif cmd == "centerCamera":
            return self.centerCamera()
        elif cmd == "moveForward":
            return self.moveForward(pathParts[2])
        elif cmd == "moveBackward":
            return self.moveBackward(pathParts[2])
        elif cmd == "stop":
            return self.stop()
        elif cmd == "stepForward":
            return self.stepForward(pathParts[2], pathParts[3])
        elif cmd == "stepBackward":
            return self.stepBackward(pathParts[2], pathParts[3])
        elif cmd == "turnLeft":
            return self.turnLeft(pathParts[2])
        elif cmd == "turnRight":
            return self.turnRight(pathParts[2])
        elif cmd == "center":
            return self.center();
        elif cmd == "lightRed":
            return self.lightRed()
        elif cmd == "lightGreen":
            return self.lightGreen()
        elif cmd == "lightBlue":
            return self.lightBlue()
        elif cmd == "buzz":
            return self.buzz(pathParts[2])
        elif cmd == "distance":
            return self.distance()
        elif cmd == "lastError":
            return self.getLastError()
        elif cmd == "lastMessage":
            return self.lastMessage()
        elif cmd == "poll":
            return self.poll()
        elif cmd == "reset_all":
            return self.resetAll()

        ScratchServer.lastError = "ERROR: '" + pathParts[1] + "' is not a valid command"
        return ScratchServer.lastError

    def lastMessage(self):
        print ScratchServer.msg
        return ScratchServer.msg

    def getLastError(self):
        print ScratchServer.lastError
        return ScratchServer.lastError

    def getXDomainResponse(self):
        resp = "<cross-domain-policy>\n"
        resp += "<allow-access-from domain=\"*\" to-ports=\"8085\"/>\n"
        resp += "</cross-domain-policy>\x00"
        return resp

    def poll(self):
        ScratchServer.msg = "sonic " + str(ScratchServer.iSonic)
        if ScratchServer.lastError != "": ScratchServer.msg += "\nERROR: " + ScratchServer.lastError
        print ScratchServer.msg
        return ScratchServer.msg

    
    def centerCamera(self):
        try:
            ScratchServer.Camera_V_Pos = 90
            ScratchServer.Camera_H_Pos = 90
            self.tcp.sendData(cmd.CMD_CAMERA_UP + str(ScratchServer.Camera_V_Pos))
            self.tcp.sendData(cmd.CMD_CAMERA_LEFT + str(ScratchServer.Camera_H_Pos))
        except Exception, e:
            ScratchServer.lastError = "Error: " + e.message
            print ScratchServer.lastError
            return ScratchServer.lastError

        return ""
Ejemplo n.º 11
0
from TCPClient import TCPClient
import Constants


def read_cmd_code():
    try:
        return int(input('Execute: '))
    except ValueError:
        print("ERROR: Not a number")
        print("Example: 1")
        return read_cmd_code()


if __name__ == '__main__':
    client = TCPClient("localhost", 5001)
    if client.fail:
        print("Contolla il server")
        exit(1)
    cmd = ""
    while True:
        print("Available actions")
        cn = 1
        for cmd in Constants.actions:
            print(cn.__str__() + " - " + cmd)
            cn += 1
        cmd = read_cmd_code() - 1
        client.send_message(Constants.actions[cmd])
        if Constants.actions[cmd] == "exit":
            break
    client.close_socket()
    exit(0)
Ejemplo n.º 12
0
from TCPClient import TCPClient
import time

app = TCPClient()  # Create new class object
app.address = ('localhost', 5000)  # Set up ip and port for connection
app.start()  # Start connection and running forever
time.sleep(5)  # Delay 5 secs
app.send("test1")  # Send message
time.sleep(2)  # Delay 2 secs
print(app.rcvmsg)  # Print out to see receive message
time.sleep(1)  # Delay 2 secs
app.stop()  # Stop running
class MainWindow(QMainWindow, Ui_MainWindow):
    tcp = TCPClient()
    default_Server_IP = "192.168.1.108"
    Camera_H_Pos = 90
    Camera_V_Pos = 90
    SERVO_MIN_ANGLE = 0
    SERVO_MAX_ANGLE = 180
    Is_Paint_Thread_On = False
    global t_Paint_Thread
    sonic_Index = 0
    sonic_buff = [0] * 20
    send_Counter = 0
    Is_tcp_Idle = True

    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.setFixedSize(self.width(), self.height())
        self.msgDlg = Messgae_Dialog()
        self.loadLogo()
        self.webView.setZoomFactor(1.2)
        #self.webView.setUrl(QUrl().fromLocalFile("/html/car_photo.html"))
        #self.webView.setUrl(QtCore.QUrl("file:/html/car_photo.html"))
        photo_Path = os.getcwd() + '/html/car_photo.html'
        self.webView.load(QtCore.QUrl.fromUserInput(photo_Path))
        #self.webView.page().settings().setAttribute(QtWebEngineWidgets.QWebEngineSettings.ShowScrollBars, False)
        self.webView.page().mainFrame().setScrollBarPolicy(
            Qt.Horizontal, Qt.ScrollBarAlwaysOff)
        self.webView.page().mainFrame().setScrollBarPolicy(
            Qt.Vertical, Qt.ScrollBarAlwaysOff)
        try:
            file_Config = open("Config.txt", "r")
            self.default_Server_IP = file_Config.read()
        except (Exception, e):
            print((
                "Config.txt is not exist,If the Program is the first executed, To ignore this information",
                e))
        finally:
            file_Config.close()
        self.lineEdit_IP_Addr.setText(self.default_Server_IP)

        self.wgt_Drawing = PaintArea(self)
        self.wgt_Drawing.setGeometry(10, 10, 400, 300)
        self.wgt_Drawing.setVisible(False)
        self.mutex = threading.Lock()
        #self.t_Recv_Sonic_Thread = Recv_Sonic_Thread(self)
        #self.t_Scan_Sonic_Thread = Scan_Sonic_Thread(self)
        #self.t_Paint_Thread = Piant_Thread(self.wgt_Drawing)
        #self.t_Paint_Thread.start()

    def loadLogo(self):
        scene = QGraphicsScene(self)
        #scene.setSceneRect(-600, -600, 1200, 1200)
        #pic = QPixmap("imgs/logo02.png")
        pic = QPixmap(":/imgs/logo_Nomal")
        scene.addPixmap(pic)
        view = self.logo
        view.setStyleSheet("background:transparent")
        view.setScene(scene)
        view.setRenderHint(QPainter.Antialiasing)
        view.show()

    @pyqtSlot()
    def on_btn_Up_pressed(self):
        self.Camera_V_Pos = self.Camera_V_Pos + self.slider_Camera.value()
        self.Camera_V_Pos = constrain(self.Camera_V_Pos, self.SERVO_MIN_ANGLE,
                                      self.SERVO_MAX_ANGLE)
        self.slider_Camera_V.setValue(self.Camera_V_Pos)
        self.tcp.sendData(cmd.CMD_CAMERA_UP + str(self.Camera_V_Pos))

    @pyqtSlot()
    def on_btn_Up_released(self):
        self.tcp.sendData(cmd.CMD_CAMERA_STOP)

    @pyqtSlot()
    def on_btn_Left_pressed(self):
        self.Camera_H_Pos = self.Camera_H_Pos + self.slider_Camera.value()
        self.Camera_H_Pos = constrain(self.Camera_H_Pos, self.SERVO_MIN_ANGLE,
                                      self.SERVO_MAX_ANGLE)
        self.slider_Camera_H.setValue(self.Camera_H_Pos)
        self.tcp.sendData(cmd.CMD_CAMERA_LEFT + str(self.Camera_H_Pos))

    @pyqtSlot()
    def on_btn_Left_released(self):
        self.tcp.sendData(cmd.CMD_CAMERA_STOP)

    @pyqtSlot()
    def on_btn_Down_pressed(self):
        self.Camera_V_Pos = self.Camera_V_Pos - self.slider_Camera.value()
        self.Camera_V_Pos = constrain(self.Camera_V_Pos, 80,
                                      self.SERVO_MAX_ANGLE)
        self.slider_Camera_V.setValue(self.Camera_V_Pos)
        self.tcp.sendData(cmd.CMD_CAMERA_DOWN + str(self.Camera_V_Pos))

    @pyqtSlot()
    def on_btn_Down_released(self):
        self.tcp.sendData(cmd.CMD_CAMERA_STOP)

    @pyqtSlot()
    def on_btn_Right_pressed(self):
        self.Camera_H_Pos = self.Camera_H_Pos - self.slider_Camera.value()
        self.Camera_H_Pos = constrain(self.Camera_H_Pos, self.SERVO_MIN_ANGLE,
                                      self.SERVO_MAX_ANGLE)
        self.slider_Camera_H.setValue(self.Camera_H_Pos)
        self.tcp.sendData(cmd.CMD_CAMERA_LEFT + str(self.Camera_H_Pos))

    @pyqtSlot()
    def on_btn_Right_released(self):
        self.tcp.sendData(cmd.CMD_CAMERA_STOP)

    @pyqtSlot()
    def on_btn_Home_clicked(self):
        self.Camera_H_Pos = 90
        self.Camera_V_Pos = 90
        self.slider_Camera_H.setValue(self.Camera_H_Pos)
        self.slider_Camera_V.setValue(self.Camera_V_Pos)
        self.tcp.sendData(cmd.CMD_CAMERA_LEFT +
                          str(90 + self.slider_FineServo2.value()))
        self.tcp.sendData(cmd.CMD_CAMERA_UP +
                          str(90 + self.slider_FineServo2.value()))

    @pyqtSlot()
    def on_btn_Forward_pressed(self):
        #self.tcp.sendData(cmd.CMD_FORWARD + str(self.slider_Speed.value()))
        self.setMoveSpeed(cmd.CMD_FORWARD, self.slider_Speed.value())

    @pyqtSlot()
    def on_btn_Forward_released(self):
        self.tcp.sendData(cmd.CMD_STOP)

    @pyqtSlot()
    def on_btn_TurnLeft_pressed(self):
        self.tcp.sendData(cmd.CMD_TURN_LEFT +
                          str(self.slider_Direction.value() +
                              self.slider_FineServo1.value()))

    @pyqtSlot()
    def on_btn_TurnLeft_released(self):
        self.tcp.sendData(cmd.CMD_TURN_CENTER +
                          str(90 + self.slider_FineServo1.value()))

    @pyqtSlot()
    def on_btn_TurnRight_pressed(self):
        self.tcp.sendData(cmd.CMD_TURN_RIGHT +
                          str(self.slider_Direction.value() +
                              self.slider_FineServo1.value()))

    @pyqtSlot()
    def on_btn_TurnRight_released(self):
        self.tcp.sendData(cmd.CMD_TURN_CENTER +
                          str(90 + self.slider_FineServo1.value()))

    @pyqtSlot()
    def on_btn_Backward_pressed(self):
        #self.tcp.sendData(cmd.CMD_BACKWARD + str(self.slider_Speed.value()))
        self.setMoveSpeed(cmd.CMD_BACKWARD, self.slider_Speed.value())

    @pyqtSlot()
    def on_btn_Backward_released(self):
        self.tcp.sendData(cmd.CMD_STOP)

    @pyqtSlot()
    def on_btn_Mode_clicked(self):
        if self.btn_Mode.text() == "VIDEO":
            self.webView.setVisible(False)
            self.wgt_Drawing.setVisible(True)
            if self.btn_Connect.text() == "DisConnect":
                self.t_Paint_Thread = Piant_Thread(self)
                self.t_Paint_Thread.start()
                self.t_Recv_Sonic_Thread = Recv_Sonic_Thread(self)
                self.t_Recv_Sonic_Thread.start()
                self.t_Scan_Sonic_Thread = Scan_Sonic_Thread(self)
                self.t_Scan_Sonic_Thread.start()
                self.Is_Paint_Thread_On = True
            self.btn_Mode.setText("RADAR")
        elif self.btn_Mode.text() == "RADAR":
            self.wgt_Drawing.setVisible(False)
            self.webView.setVisible(True)
            if self.Is_Paint_Thread_On == True:
                if self.t_Paint_Thread.is_alive():
                    #stop_thread(self.t_Paint_Thread)
                    self.t_Paint_Thread.isRun = False
                    print(("Stop_thread ... -> t_Paint_Thread",
                           self.t_Paint_Thread.getName()))
                if self.t_Recv_Sonic_Thread.is_alive():
                    #stop_thread(self.t_Recv_Sonic_Thread)
                    self.t_Recv_Sonic_Thread.isRun = False
                    print(("Stop_thread ... -> t_Recv_Sonic_Thread",
                           self.t_Recv_Sonic_Thread.getName()))
                if self.t_Scan_Sonic_Thread.is_alive():
                    #stop_thread(self.t_Scan_Sonic_Thread)
                    self.t_Scan_Sonic_Thread.isRun = False
                    print(("Stop_thread ... -> t_Scan_Sonic_Thread",
                           self.t_Scan_Sonic_Thread.getName()))
                self.Is_Paint_Thread_On = False
            self.btn_Mode.setText("VIDEO")

    @pyqtSlot()
    def on_btn_Connect_clicked(self):
        if self.btn_Connect.text() == "Connect":
            server_ip = self.lineEdit_IP_Addr.text()
            print(("Connecting......", server_ip))
            try:
                self.tcp.connectToServer(address=(server_ip, 12345))
            except Exception as e:
                print((
                    "Connect to server Faild!: Server IP is right? Server is opend?",
                    e))
                self.msgDlg.showMessage(
                    "Connect to server Faild! \n\t1. Server IP is right? \n\t2. Server is opend?"
                )
                return
            print("Connecttion Successful !")
            if self.default_Server_IP != server_ip:
                file_Config = open("Config.txt", "w")
                file_Config.write(server_ip)
                file_Config.close()
                print("default_Server_IP is Changed!")
            self.webView.setUrl(
                QUrl("http://" + server_ip + ":8090/javascript_simple.html"))

            self.lineEdit_IP_Addr.setEnabled(False)
            self.btn_Connect.setText("DisConnect")
        elif self.btn_Connect.text() == "DisConnect":
            self.tcp.disConnect()
            self.webView.stop()
            self.webView.setUrl(QUrl().fromLocalFile("/html/car_photo.html"))
            #self.webView.setUrl(QUrl("file:/html/car_photo.html"))
            self.lineEdit_IP_Addr.setEnabled(True)
            self.btn_Connect.setText("Connect")

    @pyqtSlot(bool)
    def on_webView_loadFinished(self, p0):
        self.webView.setZoomFactor(1.2)

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Up:
            self.Camera_V_Pos = self.Camera_V_Pos + self.slider_Camera.value(
            ) + self.slider_FineServo3.value()
            self.Camera_V_Pos = constrain(self.Camera_V_Pos,
                                          self.SERVO_MIN_ANGLE,
                                          self.SERVO_MAX_ANGLE)
            self.slider_Camera_V.setValue(self.Camera_V_Pos)
            self.tcp.sendData(cmd.CMD_CAMERA_UP + str(self.Camera_V_Pos))

            self.wgt_Drawing.max_range = self.Camera_V_Pos
            self.wgt_Drawing.repaint()
        elif event.key() == Qt.Key_Down:
            self.Camera_V_Pos = self.Camera_V_Pos - self.slider_Camera.value(
            ) + self.slider_FineServo3.value()
            self.Camera_V_Pos = constrain(self.Camera_V_Pos, 80,
                                          self.SERVO_MAX_ANGLE)
            self.slider_Camera_V.setValue(self.Camera_V_Pos)
            self.tcp.sendData(cmd.CMD_CAMERA_DOWN + str(self.Camera_V_Pos))

            self.wgt_Drawing.max_range = self.Camera_V_Pos
            self.wgt_Drawing.repaint()
        elif event.key() == Qt.Key_Left:
            self.Camera_H_Pos = self.Camera_H_Pos + self.slider_Camera.value(
            ) + self.slider_FineServo2.value()
            self.Camera_H_Pos = constrain(self.Camera_H_Pos,
                                          self.SERVO_MIN_ANGLE,
                                          self.SERVO_MAX_ANGLE)
            self.slider_Camera_H.setValue(self.Camera_H_Pos)
            self.tcp.sendData(cmd.CMD_CAMERA_LEFT + str(self.Camera_H_Pos))
        elif event.key() == Qt.Key_Right:
            self.Camera_H_Pos = self.Camera_H_Pos - self.slider_Camera.value(
            ) + self.slider_FineServo2.value()
            self.Camera_H_Pos = constrain(self.Camera_H_Pos,
                                          self.SERVO_MIN_ANGLE,
                                          self.SERVO_MAX_ANGLE)
            self.slider_Camera_H.setValue(self.Camera_H_Pos)
            self.tcp.sendData(cmd.CMD_CAMERA_LEFT + str(self.Camera_H_Pos))
        elif event.key() == Qt.Key_R:
            self.tcp.sendData(cmd.CMD_RGB_R)
        elif event.key() == Qt.Key_G:
            self.tcp.sendData(cmd.CMD_RGB_G)
        elif event.key() == Qt.Key_B:
            self.tcp.sendData(cmd.CMD_RGB_B)
        elif event.key() == Qt.Key_H:
            self.Camera_H_Pos = 90
            self.Camera_V_Pos = 90
            self.slider_Camera_H.setValue(self.Camera_H_Pos)
            self.slider_Camera_V.setValue(self.Camera_V_Pos)
            self.tcp.sendData(cmd.CMD_CAMERA_LEFT +
                              str(90 + self.slider_FineServo2.value()))
            self.tcp.sendData(cmd.CMD_CAMERA_UP +
                              str(90 + self.slider_FineServo2.value()))

        if event.isAutoRepeat():
            pass
        else:
            #print "You Pressed Key : ", event.key(), event.text()
            if event.key() == Qt.Key_W:
                self.setMoveSpeed(cmd.CMD_FORWARD, self.slider_Speed.value())
            elif event.key() == Qt.Key_S:
                self.setMoveSpeed(cmd.CMD_BACKWARD, self.slider_Speed.value())
            elif event.key() == Qt.Key_A:
                self.tcp.sendData(cmd.CMD_TURN_LEFT +
                                  str(self.slider_Direction.value() +
                                      self.slider_FineServo1.value()))
            elif event.key() == Qt.Key_D:
                self.tcp.sendData(cmd.CMD_TURN_RIGHT +
                                  str(self.slider_Direction.value() +
                                      self.slider_FineServo1.value()))
            elif event.key() == Qt.Key_V:
                self.tcp.sendData(cmd.CMD_BUZZER_ALARM)

    def keyReleaseEvent(self, event):
        if event.isAutoRepeat():
            pass
        else:
            #print "You Released Key : ", event.key()
            if event.key() == Qt.Key_W or event.key() == Qt.Key_S:
                self.tcp.sendData(cmd.CMD_STOP)
            elif event.key() == Qt.Key_A or event.key() == Qt.Key_D:
                self.tcp.sendData(cmd.CMD_TURN_CENTER +
                                  str(90 + self.slider_FineServo1.value()))
            elif event.key() == Qt.Key_V:
                self.tcp.sendData(cmd.CMD_BUZZER_ALARM)
        if event.key() == Qt.Key_Up or event.key() == Qt.Key_Down or event.key(
        ) == Qt.Key_Left or event.key() == Qt.Key_Right:
            self.tcp.sendData(cmd.CMD_CAMERA_STOP)

    def setMoveSpeed(self, CMD, spd):
        self.tcp.sendData(CMD + str(int(spd // 3)))
        self.tcp.sendData(CMD)
        time.sleep(0.07)
        self.tcp.sendData(CMD + str(int(spd // 3 * 2)))
        time.sleep(0.07)
        self.tcp.sendData(CMD + str(int(spd)))

    @pyqtSlot("int")
    def on_slider_Camera_valueChanged(self, value):
        self.tcp.sendData(cmd.CMD_CAMERA_SLIDER + str(value))

    @pyqtSlot("int")
    def on_slider_Speed_valueChanged(self, value):
        pass
        #self.tcp.sendData(cmd.CMD_SPEED_SLIDER + str(value))

    @pyqtSlot("int")
    def on_slider_Direction_valueChanged(self, value):
        pass
        #self.tcp.sendData(cmd.CMD_DIR_SLIDER + str(value))
    @pyqtSlot()
    def on_btn_RGB_R_clicked(self):
        self.tcp.sendData(cmd.CMD_RGB_R)

    @pyqtSlot()
    def on_btn_RGB_G_clicked(self):
        self.tcp.sendData(cmd.CMD_RGB_G)

    @pyqtSlot()
    def on_btn_RGB_B_clicked(self):
        self.tcp.sendData(cmd.CMD_RGB_B)

    @pyqtSlot()
    def on_btn_Buzzer_clicked(self):
        pass
        #self.tcp.sendData(cmd.CMD_BUZZER_ALARM)
    @pyqtSlot()
    def on_btn_Buzzer_pressed(self):
        self.tcp.sendData(cmd.CMD_BUZZER_ALARM + "1")

    @pyqtSlot()
    def on_btn_Buzzer_released(self):
        self.tcp.sendData(cmd.CMD_BUZZER_ALARM)
Ejemplo n.º 14
0
         midi = open(midiname,'r')
         # f is a file-like object. 
         old_file_position = midi.tell()
         midi.seek(0, os.SEEK_END)
         size = midi.tell()
         midi.seek(old_file_position, os.SEEK_SET)
         #size = os.path.getsize(midiname)
         print '[MidiSource] Sending name:"%s" and size:"%s",' % (midiname,str(size))
         udpServer.sendUDPmsgToLastClient(midiname+','+str(size)+',')
         # ********************************************************
         ret = udpServer.receiveUDPmsg(delay)
         print "[MidiSource] Mensagem recebida do ImageSource " , ret
         # ********************************************************
         #Connect to Android TCP server on Android_IP and TCP_PORT
         if(ret=="OK"):
             tcpClient = TCPClient(imageSource_addr[0],TCP_PORT,BUFFER_SIZE)
             tcpClient.connectTCP()
             print '[MidiSource] TCP Client ready to send the MIDI'
             #tcpClient.sendFileByNameAndClose(imgname)
             # ********************************************************
             #Send Image
             tcpClient.sendDataBySize(midi.read(), size)
             # ********************************************************
             #Close TCP socket
             tcpClient.closeTCPclient()
             print '[MidiSource] TCPClient closed'
         # ********************************************************
         #Send MIDI
         # ********************************************************
         #If transfer has finished, Close TCP socket
 
Ejemplo n.º 15
0
def main():

    
    # Open TCP connection to robot
    client = TCPClient()

    # Startup realsense pipeline
    pipeline = rs.pipeline()

    #Create a config and configure the pipeline to stream
    #  different resolutions of color and depth streams
    
    config = rs.config()
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 60)
    config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 60)

    # Start streaming
    profile = pipeline.start(config)

    # Getting the depth sensor's depth scale (see rs-align example for explanation)
    depth_sensor = profile.get_device().first_depth_sensor()
    depth_scale = depth_sensor.get_depth_scale()
    print("Depth Scale is: " , depth_scale)

    # Create an align object
    # rs.align allows us to perform alignment of depth frames to others frames
    # The "align_to" is the stream type to which we plan to align depth frames.
    align_to = rs.stream.color
    align = rs.align(align_to)

    # Declare depth filters
    dec_filter = rs.decimation_filter()  # Decimation - reduces depth frame density
    spat_filter = rs.spatial_filter()  # Spatial    - edge-preserving spatial smoothing
    hole_filling = rs.hole_filling_filter()
    temp_filter = rs.temporal_filter()  # Temporal   - reduces temporal noise

    depth_to_disparity = rs.disparity_transform(True)
    disparity_to_depth = rs.disparity_transform(False)

    # initialize variables for trajectory calculation
    buffer_len = 30 #number of points that will be taken into account for trajectory calculation
    pts = deque(maxlen=buffer_len)
    camera_pts = deque(maxlen=buffer_len)
    time_vec = deque(maxlen=buffer_len)
    tic = time.time()
    tic_frame = None
    none_count = 0

    preditcion_store = []
    points_store = []
    catch_point = []

    # loop for video
    while True:
        # Wait for frames from realsense
        frames = pipeline.wait_for_frames()
        # Align the depth frame to color frame
        aligned_frames = align.process(frames)

        # Get aligned frames
        aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image
        color_frame = aligned_frames.get_color_frame()

        # Filter aligned depth frame
        #aligned_depth_frame = dec_filter.process(aligned_depth_frame)
        aligned_depth_frame = depth_to_disparity.process(aligned_depth_frame)
        aligned_depth_frame = spat_filter.process(aligned_depth_frame)
        aligned_depth_frame = temp_filter.process(aligned_depth_frame)
        aligned_depth_frame = disparity_to_depth.process(aligned_depth_frame)
        aligned_depth_frame = hole_filling.process(aligned_depth_frame)

        # Get images to work on
        depth_image = np.asanyarray(aligned_depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())

        # Validate that both frames are valid
        if not aligned_depth_frame or not color_frame:
            print('Frame is none')
            continue

        depth_image = np.asanyarray(aligned_depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())
        
        # Detect the orange ball and return image with results
        center,radius = bd.detect_ball(color_image)

        # update the points queue
        if center is not None :
            
            #get depth from depth_image and append to center, append the current center to the points list
            depth = depth_image[center[1],center[0]]
            if not tic_frame:
                tic_frame = time.time()
            center.append(depth)
            camera_pts.append(center)

            # Transform point from camera coordinates to robot coordinate frame
            center_world = bte.transform_to_world(center)
            pts.append(center_world)
            points_store.append(center_world)

            #append current time to time vector
            toc = time.time()
            time_vec.append(toc-tic)

            

        else:
            none_count = none_count+1


        #if no points were detected for some time (10 frames), reset the point vector and polynomial calculation
        if none_count >10:
            pts.clear()
            camera_pts.clear()
            time_vec.clear()
            none_count = 0

        # if more then x ball positions were detected, calculate the trajectory estimation
        if(len(pts) > 7):
            toce = time.time()
            params_x,params_y,params_z = bte.estimate_trajectory(np.asarray(pts), np.asarray(time_vec))

            catch_point = cpc.get_catching_point(params_x,params_y,params_z)


            #Send catching point to robot
            if catch_point is not None:
                client.send_message(np.round(catch_point,2))
                print("Processing time:",(time.time()-toce))
                print("Sent point: ",np.round(catch_point,2))
                catch_point_camera = bte.transform_to_camera(catch_point)
                cv2.drawMarker(color_image, tuple(catch_point_camera.astype(int)[:2]), (0, 255, 0) ,cv2.MARKER_CROSS,10)

            # calculate future points for ball from the estimated polynomial parameters and draw them
            print("Tic frame: ", tic_frame)
            print("Time now: ", time.time)
            future_points = bte.get_future_points_3D(params_x,params_y,params_z,tic,time.time(),2)
            
            for point in future_points.transpose():
                preditcion_store.append(point)
                camera_point = bte.transform_to_camera(point)
                cv2.drawMarker(color_image, tuple(camera_point.astype(int)[:2]), (255, 0, 0) ,cv2.MARKER_CROSS,5)


            # loop over the set of tracked points to draw the balls past movement
            print("cam points: ", camera_pts)
            for i in range(1, len(camera_pts)):

                # compute the thickness of the line and
                # draw the connecting lines
                thickness = int(np.sqrt(buffer_len / float(i + 1)) * 2.5)
                cv2.drawMarker(color_image, (camera_pts[i][0],camera_pts[i][1]), (0, 0, 255), cv2.MARKER_CROSS,10)
        
            break
        # Display results
        cv2.imshow("Result image", color_image)
        out.write(color_image)  # uncomment to save video
        key = cv2.waitKey(1) & 0xFF

        # if the 'q' key is pressed, stop the loop
        if key == ord("q"):
            break

    cv2.imshow("Result image", color_image)

    del points_store[0]    
    points_store = np.asarray(points_store)
    preditcion_store = np.asarray(preditcion_store)
    print("Catching point: ", catch_point)
    print("points: ", points_store)
    print('first: ', points_store[:,0])
    print('prediction: ',preditcion_store)
    
    visualization(points_store[:,0],points_store[:,1],points_store[:,2],preditcion_store[:,0],preditcion_store[:,1] , preditcion_store[:,2],catch_point)
    # close all windows
    cv2.destroyAllWindows()
   
    client.close()
 def __init__(self):
     super(TCPLocust, self).__init__()
     self.client = TCPClient()
Ejemplo n.º 17
0
class ScratchServer(BaseHTTPRequestHandler):
    tcp = TCPClient()
    Camera_H_Pos = 90
    Camera_V_Pos = 90
    SERVO_MIN_ANGLE = 0
    SERVO_MAX_ANGLE = 180
    Is_Paint_Thread_On = False
    global t_Paint_Thread
    sonic_Index = 0
    sonic_buff = [0] * 20
    send_Counter = 0
    Is_tcp_Idle = True

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(self.parsePath(self.path).encode('utf-8'))

    def parsePath(self, path):
        pathParts = path.split('/')
        if len(pathParts) < 2:
            ScratchServer.lastError = "ERROR: No Command detected"
            return ScratchServer.lastError

        cmd = pathParts[1]

        if cmd == "favicon.ico": return

        if (cmd != "poll" and cmd != "lastError"): ScratchServer.lastError = ""

        if cmd == "crossdomain.xml":
            return self.getXDomainResponse()
        elif cmd == "connect":
            server_ip = pathParts[2]
            return self.connect2Car(server_ip)
        elif cmd == "disconnect":
            return self.disconnectFromCar()
        elif cmd == "cUp":
            return self.cameraUp(pathParts[2])
        elif cmd == "cDown":
            return self.cameraDown(pathParts[2])
        elif cmd == "cLeft":
            return self.cameraLeft(pathParts[2])
        elif cmd == "cRight":
            return self.cameraRight(pathParts[2])
        elif cmd == "centerCamera":
            return self.centerCamera()
        elif cmd == "moveForward":
            return self.moveForward(pathParts[2])
        elif cmd == "moveBackward":
            return self.moveBackward(pathParts[2])
        elif cmd == "stop":
            return self.stop()
        elif cmd == "stepForward":
            return self.stepForward(pathParts[2], pathParts[3])
        elif cmd == "stepBackward":
            return self.stepBackward(pathParts[2], pathParts[3])
        elif cmd == "turnLeft":
            return self.turnLeft(pathParts[2])
        elif cmd == "turnRight":
            return self.turnRight(pathParts[2])
        elif cmd == "center":
            return self.center()
        elif cmd == "lightRed":
            return self.lightRed()
        elif cmd == "lightGreen":
            return self.lightGreen()
        elif cmd == "lightBlue":
            return self.lightBlue()
        elif cmd == "buzz":
            return self.buzz(pathParts[2])
        elif cmd == "distance":
            return self.distance()
        elif cmd == "lastError":
            return self.getLastError()
        elif cmd == "lastMessage":
            return self.lastMessage()
        elif cmd == "poll":
            return self.poll()
        elif cmd == "reset_all":
            return self.resetAll()

        ScratchServer.lastError = "ERROR: '" + pathParts[
            1] + "' is not a valid command"
        return ScratchServer.lastError

    def lastMessage(self):
        print(ScratchServer.msg)
        return ScratchServer.msg

    def getLastError(self):
        print(ScratchServer.lastError)
        return ScratchServer.lastError

    def getXDomainResponse(self):
        resp = "<cross-domain-policy>\n"
        resp += "<allow-access-from domain=\"*\" to-ports=\"8085\"/>\n"
        resp += "</cross-domain-policy>\x00"
        return resp

    def poll(self):
        ScratchServer.msg = "sonic " + str(ScratchServer.iSonic)
        if ScratchServer.lastError != "":
            ScratchServer.msg += "\nERROR: " + ScratchServer.lastError
        print(ScratchServer.msg)
        return ScratchServer.msg

    def centerCamera(self):
        try:
            ScratchServer.Camera_V_Pos = 90
            ScratchServer.Camera_H_Pos = 90
            self.tcp.sendData(cmd.CMD_CAMERA_UP +
                              str(ScratchServer.Camera_V_Pos))
            self.tcp.sendData(cmd.CMD_CAMERA_LEFT +
                              str(ScratchServer.Camera_H_Pos))
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        return ""

    def resetAll(self):
        try:
            print("returning crossdomain.xml")
            self.centerCamera(0)
            self.stop()
            self.center(0)
            self.buzz(1000)
            return "Reset Complete"
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

    def connect2Car(self, server_ip):
        print("Connecting......", server_ip)
        try:
            self.tcp.connectToServer(address=(str(server_ip), 12345))
        except Exception as e:
            #print(type(server_ip),type(e))
            ScratchServer.lastError = e  #"Connect to server " + server_ip + " Failed!: " +
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Connection Successful !"
        print(ScratchServer.msg)
        return ScratchServer.msg

    def disconnectFromCar(self):
        try:
            self.tcp.disConnect()
            print("Disconnection Successful !")
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Disconnected"
        print(ScratchServer.msg)
        return ScratchServer.msg

    def distance(self):
        try:
            sonic = 0
            iTry = 0
            while sonic == 0 and iTry < 3:
                self.tcp.sendData(cmd.CMD_ULTRASONIC)
                sonic = self.tcp.recvData()
            ScratchServer.iSonic = float(sonic)
        except Exception as e:
            ScratchServer.msg = "Sonic Data error :\n" + e
            ScratchServer.lastError = ScratchServer.msg
            print(ScratchServer.msg)
            ScratchServer.iSonic = 0
            return ScratchServer.msg

        ScratchServer.msg = "distance received: " + str(ScratchServer.iSonic)
        print(ScratchServer.msg)
        return str(ScratchServer.iSonic)

    def cameraUp(self, angle):
        try:
            angle = int(angle)
            ScratchServer.Camera_V_Pos = 90 + angle
            ScratchServer.Camera_V_Pos = constrain(ScratchServer.Camera_V_Pos,
                                                   self.SERVO_MIN_ANGLE,
                                                   self.SERVO_MAX_ANGLE)
            self.tcp.sendData(cmd.CMD_CAMERA_UP +
                              str(ScratchServer.Camera_V_Pos))
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Camera V moved up to " + str(
            ScratchServer.Camera_V_Pos)
        print(ScratchServer.msg)
        return ScratchServer.msg

    def cameraDown(self, angle):
        try:
            angle = int(angle)
            ScratchServer.Camera_V_Pos = 90 - angle
            ScratchServer.Camera_V_Pos = constrain(ScratchServer.Camera_V_Pos,
                                                   80, self.SERVO_MAX_ANGLE)
            self.tcp.sendData(cmd.CMD_CAMERA_DOWN +
                              str(ScratchServer.Camera_V_Pos))
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Camera V moved down to " + str(
            ScratchServer.Camera_V_Pos)
        print(ScratchServer.msg)
        return ScratchServer.msg

    def cameraLeft(self, angle):
        try:
            angle = int(angle)
            ScratchServer.Camera_H_Pos = 90 + angle
            ScratchServer.Camera_H_Pos = constrain(ScratchServer.Camera_H_Pos,
                                                   self.SERVO_MIN_ANGLE,
                                                   self.SERVO_MAX_ANGLE)
            self.tcp.sendData(cmd.CMD_CAMERA_LEFT +
                              str(ScratchServer.Camera_H_Pos))
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Camera h moved LEFT to " + str(
            ScratchServer.Camera_H_Pos)
        print(ScratchServer.msg)
        return ScratchServer.msg

    def cameraRight(self, angle):
        try:
            angle = int(angle)
            ScratchServer.Camera_H_Pos = 90 - angle
            ScratchServer.Camera_H_Pos = constrain(ScratchServer.Camera_H_Pos,
                                                   self.SERVO_MIN_ANGLE,
                                                   self.SERVO_MAX_ANGLE)
            self.tcp.sendData(cmd.CMD_CAMERA_RIGHT +
                              str(ScratchServer.Camera_H_Pos))
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Camera h moved RIGHT to " + str(
            ScratchServer.Camera_H_Pos)
        print(ScratchServer.msg)
        return ScratchServer.msg

    def lightRed(self):
        try:
            self.tcp.sendData(cmd.CMD_RGB_R)
            ScratchServer.msg = "Light to Red"
            print(ScratchServer.msg)
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        return ScratchServer.msg

    def lightGreen(self):
        try:
            self.tcp.sendData(cmd.CMD_RGB_G)
            ScratchServer.msg = "Light to Green"
            print(ScratchServer.msg)
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        return ScratchServer.msg

    def lightBlue(self):
        try:
            self.tcp.sendData(cmd.CMD_RGB_B)
            ScratchServer.msg = "Light to Blue"
            print(ScratchServer.msg)
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        return ScratchServer.msg

    def buzz(self, duration):
        try:
            duration = int(duration)
            print("Buzzing started")
            self.tcp.sendData(cmd.CMD_BUZZER_ALARM + "1")
            time.sleep(float(duration) / 1000)
            self.tcp.sendData(cmd.CMD_BUZZER_ALARM + "0")
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Buzzed for " + str(duration) + "ms"
        print(ScratchServer.msg)
        return ScratchServer.msg

    def moveForward(self, speed):
        try:
            speed = int(speed)
            print("Moving Forward")
            self.setMoveSpeed(cmd.CMD_FORWARD, speed)
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Moving forward at speed " + str(speed)
        print(ScratchServer.msg)
        return ScratchServer.msg

    def moveBackward(self, speed):
        try:
            speed = int(speed)
            print("Moving Backward")
            self.setMoveSpeed(cmd.CMD_BACKWARD, speed)
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Moving Backward at speed " + str(speed)
        print(ScratchServer.msg)
        return ScratchServer.msg

    def stop(self):
        try:
            print("Stopping")
            self.tcp.sendData(cmd.CMD_STOP)
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Stopped"
        print(ScratchServer.msg)
        return ScratchServer.msg

    def stepForward(self, speed, duration):
        try:
            speed = int(speed)
            duration = int(duration)

            print("Stepping Forward")
            self.setMoveSpeed(cmd.CMD_FORWARD, speed)
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Stepping forward at speed " + str(speed)
        print(ScratchServer.msg)
        time.sleep(float(duration) / 1000)
        self.tcp.sendData(cmd.CMD_STOP)
        ScratchServer.msg2 = "Stopped moving after " + str(duration) + "ms"

        return ScratchServer.msg + "\n" + ScratchServer.msg2

    def stepBackward(self, speed, duration):
        try:
            speed = int(speed)
            duration = int(duration)
            print("Stepping Backward")
            self.setMoveSpeed(cmd.CMD_BACKWARD, speed)
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Stepping Backward at speed " + str(speed)
        print(ScratchServer.msg)
        time.sleep(float(duration) / 1000)
        self.tcp.sendData(cmd.CMD_STOP)
        ScratchServer.msg2 = "Stopped moving after " + str(duration) + "ms"

        return ScratchServer.msg + "\n" + ScratchServer.msg2

    def turnLeft(self, angle):
        try:
            angle = int(angle)
            print("Turning Left")
            self.tcp.sendData(cmd.CMD_TURN_LEFT + str(angle))
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Turned Left to " + str(angle)
        print(ScratchServer.msg)
        return ScratchServer.msg

    def center(self):
        try:
            print("Centering")
            self.tcp.sendData(cmd.CMD_TURN_CENTER + str(90))
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Centered"
        print(ScratchServer.msg)
        return ScratchServer.msg

    def turnRight(self, angle):
        try:
            angle = int(angle)
            print("Turning Right")
            self.tcp.sendData(cmd.CMD_TURN_RIGHT + str(angle))
        except Exception as e:
            ScratchServer.lastError = "Error: " + e
            print(ScratchServer.lastError)
            return ScratchServer.lastError

        ScratchServer.msg = "Turned Right to " + str(angle)
        print(ScratchServer.msg)
        return ScratchServer.msg

    def setMoveSpeed(self, CMD, spd):
        self.tcp.sendData(CMD + str(float(spd) / 3))
        time.sleep(0.07)
        self.tcp.sendData(CMD + str(float(spd) / 3 * 2))
        time.sleep(0.07)
        self.tcp.sendData(CMD + str(spd))
Ejemplo n.º 18
0
                           str(mode) + "] [bus:" + str(bus) + "] [device:" +
                           str(device) + "]")
            spi_thread = SPIThread(q_spi_write, mode, bus, device)
            Logger.log_spi("Initialized spi-server with: " + "[mode:" +
                           str(mode) + "] [bus:" + str(bus) + "] [device:" +
                           str(device) + "]")

        # GAME
        Logger.log_game("Initializing game")
        game_thread = GameThread()
        Logger.log_game("Initialized game")

        # TCP-SERVER
        Logger.log_tcp("Initializing tcp-server with: " + "[host:" +
                       str(host) + "] [port:" + str(port) + "]")
        tcp_thread = TCPClient(host, port, q_tcp_read, q_tcp_write)
        Logger.log_tcp("Initialized tcp-server with:" + "[host:" + str(host) +
                       "] [port:" + str(port) + "]")

        # START

        # MOTION TRACKING
        if useMotion:
            Logger.log_game("Starting Motion Tracking")
            motion_thread.start()
            Logger.log_game("Started Motion Tracking")

        # SPI-SERVER
        if useSPI:
            Logger.log_spi("Starting spi-server")
            spi_thread.start()
Ejemplo n.º 19
0
if __name__ == '__main__':
    import cv2
    from TCPClient import TCPClient
    from cmdline import command
    
    #cap = cv2.VideoCapture('http://192.168.1.157:8090/?action=stream/frame.mjpg')
    cap = cv2.VideoCapture(0) ## to use your webcam instead of stream    
    tcp = TCPClient()
    cmd = command()
    bConnected = False
    #### uncomment the below lines to interact with the car over TCP
    #### these have been commented out in the absence of the car itself to test its interactino with a TF NN
    #### Note this needs testing with py3.7!!
#    tcp.sendData(cmd.LED_BLUE.encode())
#    tcp.sendData(cmd.LED_RED.encode())
#    tcp.sendData(cmd.LED_GREEN.encode())
    
#    try:
#        tcp.connectToServer(address = ("192.168.1.157", 12345))
    bConnected = True
#        #tcp.sendData(">RGB Blue".encode())
#    except Exception:
#        print("Connect to server Failed!: Check the Server IP is correct and open!")
    
    while bConnected:
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        faces = cv2.CascadeClassifier('haarcascade_frontalface_default.xml').detectMultiScale(gray, 1.3,5)
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 0), 2)
            roi_gray = gray[y:y+h, x:x+w]