示例#1
0
def findLineCtrl(posInput, setCenter):
    if posInput:
        if posInput > (setCenter + findLineError):
            move.motorStop()
            #turnRight
            error = (posInput - 320) / 5
            outv = int(round((pid.GenOut(error)), 0))
            servo.lookright(outv)
            # servo.turnRight(coe_Genout(error, 64))
            servo.turnRight()
            X_lock = 0
        elif posInput < (setCenter - findLineError):
            move.motorStop()
            #turnLeft
            error = (320 - posInput) / 5
            outv = int(round((pid.GenOut(error)), 0))
            servo.lookleft(outv)
            # servo.turnLeft(coe_Genout(error, 64))
            servo.turnLeft()
        else:
            if CVrun:
                move.move(speed_set, 'forward')
            #forward
    else:
        if CVrun:
            move.move(speed_set, 'backward')
示例#2
0
 def left(self, sleepBeforeDrive, headAngle, wheelsTurnAngle, 
          sleepDistance):
     servo.ahead()
     servo.lookleft(headAngle)
     time.sleep(sleepBeforeDrive)
     servo.turnLeft(wheelsTurnAngle)
     self._secureMove(sleepDistance)
示例#3
0
def autoDect(speed):
    move.motorStop()
    servo.ahead()
    time.sleep(0.3)
    getMiddle = ultra.checkdist()
    print('M%f' % getMiddle)

    servo.ahead()
    servo.lookleft(100)
    time.sleep(0.3)
    getLeft = ultra.checkdist()
    print('L%f' % getLeft)

    servo.ahead()
    servo.lookright(100)
    time.sleep(0.3)
    getRight = ultra.checkdist()
    print('R%f' % getRight)

    if getMiddle >= max(getLeft, getRight):
        if getMiddle > range_min:
            move.move(speed, 'forward')
            time.sleep(0.5)
            move.motorStop()
        else:
            move.move(speed, 'backward')
            time.sleep(0.5)
    elif getLeft > max(getMiddle, getRight):
        servo.ahead()
        servo.turnLeft()
        servo.lookleft(100)
        if getLeft > range_min:
            move.move(speed, 'forward')
            time.sleep(0.5)
            move.motorStop()
        else:
            move.move(speed, 'backward')
            time.sleep(0.5)
    elif getRight > max(getMiddle, getLeft):
        servo.ahead()
        servo.turnRight()
        servo.lookright(100)
        if getRight > range_min:
            move.move(speed, 'forward')
            time.sleep(0.5)
            move.motorStop()
        else:
            move.move(speed, 'backward')
            time.sleep(0.5)
示例#4
0
def run():
    status_right = GPIO.input(line_pin_right)
    status_middle = GPIO.input(line_pin_middle)
    status_left = GPIO.input(line_pin_left)
    #print('R%d   M%d   L%d'%(status_right,status_middle,status_left))
    if status_middle == 1:
        move.move(100, 'forward')
    elif status_left == 1:
        servo.turnRight()
        move.move(100, 'forward')
    elif status_right == 1:
        servo.turnLeft()
        move.move(100, 'forward')
    else:
        move.move(100, 'backward')
示例#5
0
def set(motor, power):
    """ Execute the 'setRobotMotorPower' command. """
    # C.command_send(motor,power)
    # Set the power of left motor.
    if motor == "A":
        if power > 0:
            move.move(power, 'forward')
            #FORWARD_POWER_LEFT.ChangeDutyCycle(power)
            #BACKWARD_POWER_LEFT.ChangeDutyCycle(0)
        elif power == 0:
            move.motorStop()
            #FORWARD_POWER_LEFT.ChangeDutyCycle(0)
            #BACKWARD_POWER_LEFT.ChangeDutyCycle(0)
        else:
            move.move(-power, 'backward')
            #FORWARD_POWER_LEFT.ChangeDutyCycle(0)
            #BACKWARD_POWER_LEFT.ChangeDutyCycle(-power)

    elif motor == "B":
        if power > 0:
            move.move(power, 'backward')
            #FORWARD_POWER_RIGHT.ChangeDutyCycle(power)
            #BACKWARD_POWER_RIGHT.ChangeDutyCycle(0)
        elif power == 0:
            move.motorStop()
            #FORWARD_POWER_RIGHT.ChangeDutyCycle(0)
            #BACKWARD_POWER_RIGHT.ChangeDutyCycle(0)
        else:
            move.move(-power, 'forward')
            #FORWARD_POWER_RIGHT.ChangeDutyCycle(0)
            #BACKWARD_POWER_RIGHT.ChangeDutyCycle(-power)

    if motor == 'Left':
        servo.turnLeft(float(power / 255))
        # led.setcolor('R',power)
    elif motor == 'Right':
        servo.turnRight(float(power / 255))
        # led.setcolor('G',power)
    elif motor == 'Middle':
        servo.turnMiddle()
        # led.setcolor('B',power)

    return 0
示例#6
0
    def run(self):
        global goal_pos, servo_command, init_get, functionMode
        while self.__running.isSet():
            self.__flag.wait()
            if SR_mode:
                voice_command = SR.run()
                if voice_command == 'forward':
                    turn.turnMiddle()
                    move.move(speed_set, 'forward')
                    time.sleep(1)
                    move.motorStop()

                elif voice_command == 'backward':
                    turn.turnMiddle()
                    move.move(speed_set, 'backward')
                    time.sleep(1)
                    move.motorStop()

                elif voice_command == 'left':
                    servo.turnLeft()
                    move.move(speed_set, 'forward')
                    time.sleep(1)
                    turn.turnMiddle()
                    move.motorStop()

                elif voice_command == 'right':
                    servo.turnRight()
                    move.move(speed_set, 'forward')
                    time.sleep(1)
                    turn.turnMiddle()
                    move.motorStop()

                elif voice_command == 'stop':
                    turn.turnMiddle()
                    move.motorStop()
            else:
                self.pause()
示例#7
0
def autoDect(speed):
    move.motorStop()
    servo.ahead()
    time.sleep(0.3)
    getMiddle = ultra.checkdist()
    print('M%f' % getMiddle)

    servo.ahead()
    servo.lookleft(100)
    time.sleep(0.3)
    getLeft = ultra.checkdist()
    print('L%f' % getLeft)

    servo.ahead()
    servo.lookright(100)
    time.sleep(0.3)
    getRight = ultra.checkdist()
    print('R%f' % getRight)

    if getMiddle < range_min and min(getLeft, getRight) > range_min:
        if random.randint(0, 1):
            servo.turnLeft()
        else:
            servo.turnRight()
        move.move(speed, 'forward')
        time.sleep(0.5)
        move.motorStop()
    elif getLeft < range_min and min(getMiddle, getRight) > range_min:
        servo.turnRight(0.7)
        move.move(speed, 'forward')
        time.sleep(0.5)
        move.motorStop()
    elif getRight < range_min and min(getMiddle, getLeft) > range_min:
        servo.turnLeft(0.7)
        move.move(speed, 'forward')
        time.sleep(0.5)
        move.motorStop()
    elif max(getLeft, getMiddle) < range_min and getRight > range_min:
        servo.turnRight()
        move.move(speed, 'forward')
        time.sleep(0.5)
        move.motorStop()
    elif max(getMiddle, getRight) < range_min and getLeft > range_min:
        servo.turnLeft()
        move.move(speed, 'forward')
        time.sleep(0.5)
        move.motorStop()
    elif max(getLeft, getMiddle, getRight) < range_min:
        move.move(speed, 'backward')
        time.sleep(0.5)
        move.motorStop()
    else:
        servo.turnMiddle()
        move.move(speed, 'forward')
        time.sleep(0.5)
        move.motorStop()
示例#8
0
class SR_ctrl(threading.Thread):
	def __init__(self, *args, **kwargs):
		super(SR_ctrl, self).__init__(*args, **kwargs)
		self.__flag = threading.Event()
		self.__flag.set()
		self.__running = threading.Event()
		self.__running.set()

	def run(self):
		global goal_pos, servo_command, init_get, functionMode
		while self.__running.isSet():
			self.__flag.wait()
			if SR_mode:

        voice_command = SR.run()
				if voice_command == 'forward':
					turn.turnMiddle()
					move.move(speed_set, 'forward')
					time.sleep(1)
					move.motorStop()

				elif voice_command == 'backward':
					turn.turnMiddle()
					move.move(speed_set, 'backward')
					time.sleep(1)
					move.motorStop()

				elif voice_command == 'left':
					servo.turnLeft()
					move.move(speed_set, 'forward')
					time.sleep(1)
					turn.turnMiddle()
					move.motorStop()

				elif voice_command == 'right':
					servo.turnRight()
					move.move(speed_set, 'forward')
					time.sleep(1)
					turn.turnMiddle()
					move.motorStop()

				elif voice_command == 'stop':
					turn.turnMiddle()
					move.motorStop()
示例#9
0
    def capture_thread(self, IPinver):
        ap = argparse.ArgumentParser()  #OpenCV initialization
        ap.add_argument("-b",
                        "--buffer",
                        type=int,
                        default=64,
                        help="max buffer size")
        args = vars(ap.parse_args())
        pts = deque(maxlen=args["buffer"])

        font = cv2.FONT_HERSHEY_SIMPLEX

        camera = picamera.PiCamera()
        camera.resolution = (640, 480)
        camera.framerate = 20
        rawCapture = PiRGBArray(camera, size=(640, 480))
        deeppicar = DeepPiCar(camera)
        context = zmq.Context()
        footage_socket = context.socket(zmq.PUB)
        print(IPinver)
        footage_socket.connect('tcp://%s:5555' % IPinver)

        avg = None
        motionCounter = 0
        lastMovtionCaptured = datetime.datetime.now()

        for frame in camera.capture_continuous(rawCapture,
                                               format="bgr",
                                               use_video_port=True):
            #time.sleep(1)
            frame_image = frame.array
            #objects,frame_image = deeppicar.process_objects_on_road(frame_image)

            #frame_image = deeppicar.follow_lane(frame_image)
            cv2.line(frame_image, (300, 240), (340, 240), (128, 255, 128), 1)
            cv2.line(frame_image, (320, 220), (320, 260), (128, 255, 128), 1)
            timestamp = datetime.datetime.now()

            if FindColorMode:
                if 0 and objects != None:
                    for obj in objects:
                        if obj.label_id == 5:
                            move.motorStop()
                        #time.sleep(1)
                ####>>>OpenCV Start<<<####
                hsv = cv2.cvtColor(frame_image, cv2.COLOR_BGR2HSV)
                mask = cv2.inRange(hsv, self.colorLower, self.colorUpper)
                mask = cv2.erode(mask, None, iterations=2)
                mask = cv2.dilate(mask, None, iterations=2)
                cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                        cv2.CHAIN_APPROX_SIMPLE)[-2]
                center = None
                move.move(80, 'forward')
                if len(cnts) > 0:
                    cv2.putText(frame_image, 'Target Detected', (40, 60), font,
                                0.5, (255, 255, 255), 1, cv2.LINE_AA)
                    c = max(cnts, key=cv2.contourArea)
                    ((x, y), radius) = cv2.minEnclosingCircle(c)
                    M = cv2.moments(c)
                    center = (int(M["m10"] / M["m00"]),
                              int(M["m01"] / M["m00"]))
                    X = int(x)
                    Y = int(y)
                    if radius > 10:
                        cv2.rectangle(frame_image,
                                      (int(x - radius), int(y + radius)),
                                      (int(x + radius), int(y - radius)),
                                      (255, 255, 255), 1)

                    if Y < (240 - tor):
                        error = (240 - Y) / 5
                        outv = int(round((pid.GenOut(error)), 0))
                        servo.up(outv)
                        Y_lock = 0
                    elif Y > (240 + tor):
                        error = (Y - 240) / 5
                        outv = int(round((pid.GenOut(error)), 0))
                        servo.down(outv)
                        Y_lock = 0
                    else:
                        Y_lock = 1

                    if X < (320 - tor * 3):
                        error = (320 - X) / 5
                        outv = int(round((pid.GenOut(error)), 0))
                        servo.lookleft(outv)
                        servo.turnLeft(coe_Genout(error, 64))
                        X_lock = 0
                    elif X > (330 + tor * 3):
                        error = (X - 240) / 5
                        outv = int(round((pid.GenOut(error)), 0))
                        servo.lookright(outv)
                        servo.turnRight(coe_Genout(error, 64))
                        X_lock = 0
                    else:
                        move.motorStop()
                        X_lock = 1

                    if X_lock == 1 and Y_lock == 1:
                        switch.switch(1, 1)
                        switch.switch(2, 1)
                        switch.switch(3, 1)
                        #moveCtrl(ultra.checkdist(), back_R, forward_R)
                    else:
                        #move.motorStop()
                        switch.switch(1, 0)
                        switch.switch(2, 0)
                        switch.switch(3, 0)

                else:
                    cv2.putText(frame_image, 'Target Detecting', (40, 60),
                                font, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
                    move.move(50, 'backward')
                    time.sleep(1)
                    move.move(70, 'forward')
                    #move.motorStop()
                ####>>>OpenCV Ends<<<####

            if WatchDogMode:
                gray = cv2.cvtColor(frame_image, cv2.COLOR_BGR2GRAY)
                gray = cv2.GaussianBlur(gray, (21, 21), 0)

                if avg is None:
                    print("[INFO] starting background model...")
                    avg = gray.copy().astype("float")
                    rawCapture.truncate(0)
                    continue

                cv2.accumulateWeighted(gray, avg, 0.5)
                frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg))

                # threshold the delta image, dilate the thresholded image to fill
                # in holes, then find contours on thresholded image
                thresh = cv2.threshold(frameDelta, 5, 255,
                                       cv2.THRESH_BINARY)[1]
                thresh = cv2.dilate(thresh, None, iterations=2)
                cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                        cv2.CHAIN_APPROX_SIMPLE)
                cnts = imutils.grab_contours(cnts)
                # print('x')
                # loop over the contours
                for c in cnts:
                    # if the contour is too small, ignore it
                    if cv2.contourArea(c) < 5000:
                        continue

                    # compute the bounding box for the contour, draw it on the frame,
                    # and update the text
                    (x, y, w, h) = cv2.boundingRect(c)
                    cv2.rectangle(frame_image, (x, y), (x + w, y + h),
                                  (128, 255, 0), 1)
                    text = "Occupied"
                    motionCounter += 1
                    #print(motionCounter)
                    #print(text)
                    LED.colorWipe(255, 16, 0)
                    lastMovtionCaptured = timestamp
                    switch.switch(1, 1)
                    switch.switch(2, 1)
                    switch.switch(3, 1)

                if (timestamp - lastMovtionCaptured).seconds >= 0.5:
                    LED.colorWipe(255, 255, 0)
                    switch.switch(1, 0)
                    switch.switch(2, 0)
                    switch.switch(3, 0)

            encoded, buffer = cv2.imencode('.jpg', frame_image)
            jpg_as_text = base64.b64encode(buffer)
            footage_socket.send(jpg_as_text)

            rawCapture.truncate(0)
示例#10
0
def run():
    global turn_status, speed, angle_rate, color_select, led, check_true_out, backing, last_turn
    status_right = GPIO.input(line_pin_right)
    status_middle = GPIO.input(line_pin_middle)
    status_left = GPIO.input(line_pin_left)
    #print('R%d   M%d   L%d'%(status_right,status_middle,status_left))

    if status_right == color_select:
        check_true_out = 0
        backing = 0
        print('left')
        led.colorWipe(0, 255, 0)
        turn_status = -1
        last_turn = -1
        servo.turnLeft(angle_rate)
        move.move(speed, 'forward')
    elif status_left == color_select:
        check_true_out = 0
        backing = 0
        print('right')
        turn_status = 1
        last_turn = 1
        led.colorWipe(0, 0, 255)
        servo.turnRight(angle_rate)
        move.move(speed, 'forward')

    elif status_middle == color_select:
        check_true_out = 0
        backing = 0
        print('middle')
        led.colorWipe(255, 255, 255)
        turn_status = 0
        servo.turnMiddle()
        move.move(speed, 'forward')
        # time.sleep(0.2)

    else:
        print('none')
        led.colorWipe(255, 0, 0)
        if not backing == 1:
            if check_true_out == 1:
                check_true_out = 0
                if turn_status == 0:
                    if last_turn == 1:
                        servo.turnRight(angle_rate)
                    else:
                        servo.turnLeft(angle_rate)
                    move.move(speed, 'backward')
                    time.sleep(0.3)
                elif turn_status == 1:
                    time.sleep(0.3)
                    servo.turnLeft(angle_rate)
                else:
                    time.sleep(0.3)
                    servo.turnRight(angle_rate)
                move.move(speed, 'backward')
                backing = 1
                # time.sleep(0.2)
            else:
                time.sleep(0.1)
                check_true_out = 1
示例#11
0
def run():
    global servo_move, speed_set, servo_command, functionMode, init_get, R_set, G_set, B_set, SR_mode
    servo.servo_init()
    move.setup()
    findline.setup()
    direction_command = 'no'
    turn_command = 'no'
    servo_command = 'no'
    speed_set = 100
    rad = 0.5

    info_threading = threading.Thread(
        target=info_send_client)  #Define a thread for FPV and OpenCV
    info_threading.setDaemon(
        True
    )  #'True' means it is a front thread,it would close when the mainloop() closes
    info_threading.start()  #Thread starts

    servo_move = Servo_ctrl()
    servo_move.start()
    servo_move.pause()
    findline.setup()
    while True:
        data = ''
        data = str(tcpCliSock.recv(BUFSIZ).decode())
        if not data:
            continue

        elif 'forward' == data:
            direction_command = 'forward'
            move.move(speed_set, direction_command)

        elif 'backward' == data:
            direction_command = 'backward'
            move.move(speed_set, direction_command)

        elif 'DS' in data:
            direction_command = 'no'
            move.move(speed_set, direction_command)

        elif 'left' == data:
            # turn_command = 'left'
            servo.turnLeft()

        elif 'right' == data:
            # turn_command = 'right'
            servo.turnRight()

        elif 'TS' in data:
            # turn_command = 'no'
            servo.turnMiddle()

        elif 'Switch_1_on' in data:
            fpv.Sounds(1)
            tcpCliSock.send(('Switch_1_on').encode())

        elif 'Switch_1_off' in data:
            fpv.Sounds(0)
            tcpCliSock.send(('Switch_1_off').encode())

        elif 'Switch_2_on' in data:
            #switch.switch(2,1)
            fpv.Radar(1)
            tcpCliSock.send(('Switch_2_on').encode())

        elif 'Switch_2_off' in data:
            #switch.switch(2,0)
            fpv.Radar(0)
            tcpCliSock.send(('Switch_2_off').encode())

        elif 'Switch_3_on' in data:
            switch.switch(3, 1)
            tcpCliSock.send(('Switch_3_on').encode())

        elif 'Switch_3_off' in data:
            switch.switch(3, 0)
            tcpCliSock.send(('Switch_3_off').encode())

        elif 'Switch_A_on' in data:
            tcpCliSock.send(('Switch_A_on').encode())
        elif 'Switch_A_off' in data:
            tcpCliSock.send(('Switch_A_off').encode())

        elif 'Switch_B_on' in data:
            tcpCliSock.send(('Switch_B_on').encode())
        elif 'Switch_B_off' in data:
            tcpCliSock.send(('Switch_B_off').encode())

        elif 'Switch_C_on' in data:
            fpv.HaarJJ(1)
            tcpCliSock.send(('Switch_C_on').encode())
        elif 'Switch_C_off' in data:
            fpv.HaarJJ(0)
            tcpCliSock.send(('Switch_C_off').encode())

        elif 'Face_Track_on' in data:
            # functionMode = 3
            fpv.FaceTrack(1)
            tcpCliSock.send(('Face_Track_on').encode())

        elif 'Face_Track_off' in data:
            # functionMode = 0
            fpv.FaceTrack(0)
            tcpCliSock.send(('Face_Track_off').encode())

        elif 'function_1_on' in data:
            servo.ahead()
            time.sleep(0.2)
            tcpCliSock.send(('function_1_on').encode())
            radar_send = servo.radar_scan()
            tcpCliSock.sendall(radar_send.encode())
            print(radar_send)
            time.sleep(0.3)
            tcpCliSock.send(('function_1_off').encode())

        elif 'function_2_on' in data:
            functionMode = 2
            fpv.FindColor(1)
            tcpCliSock.send(('function_2_on').encode())

        elif 'function_2_off' in data:
            functionMode = 0
            fpv.FindColor(0)
            switch.switch(1, 0)
            switch.switch(2, 0)
            switch.switch(3, 0)
            tcpCliSock.send(('function_2_off').encode())

        elif 'function_3_on' in data:
            functionMode = 3
            fpv.WatchDog(1)
            tcpCliSock.send(('function_3_on').encode())

        elif 'function_3_off' in data:
            functionMode = 0
            fpv.WatchDog(0)
            tcpCliSock.send(('function_3_off').encode())

        elif 'function_4_on' in data:
            functionMode = 4
            servo_move.resume()
            tcpCliSock.send(('function_4_on').encode())

        elif 'function_4_off' in data:
            functionMode = 0
            servo_move.pause()
            move.motorStop()
            tcpCliSock.send(('function_4_off').encode())

        elif 'function_5_on' in data:
            functionMode = 5
            servo_move.resume()
            tcpCliSock.send(('function_5_on').encode())

        elif 'function_5_off' in data:
            functionMode = 0
            servo_move.pause()
            move.motorStop()
            tcpCliSock.send(('function_5_off').encode())

        elif 'function_6_on' in data:
            if MPU_connection:
                functionMode = 6
                servo_move.resume()
                tcpCliSock.send(('function_6_on').encode())

        elif 'function_6_off' in data:
            functionMode = 0
            servo_move.pause()
            move.motorStop()
            init_get = 0
            tcpCliSock.send(('function_6_off').encode())

            #elif 'function_1_off' in data:
        #   tcpCliSock.send(('function_1_off').encode())

        elif 'lookleft' == data:
            servo_command = 'lookleft'
            servo_move.resume()

        elif 'lookright' == data:
            servo_command = 'lookright'
            servo_move.resume()

        elif 'up' == data:
            servo_command = 'up'
            servo_move.resume()

        elif 'down' == data:
            servo_command = 'down'
            servo_move.resume()

        elif 'stop' == data:
            if not functionMode:
                servo_move.pause()
            servo_command = 'no'
            pass

        elif 'home' == data:
            servo.ahead()

        elif 'CVrun' == data:
            if not FPV.CVrun:
                FPV.CVrun = 1
                tcpCliSock.send(('CVrun_on').encode())
            else:
                FPV.CVrun = 0
                tcpCliSock.send(('CVrun_off').encode())

        elif 'wsR' in data:
            try:
                set_R = data.split()
                R_set = int(set_R[1])
                led.colorWipe(R_set, G_set, B_set)
            except:
                pass

        elif 'wsG' in data:
            try:
                set_G = data.split()
                G_set = int(set_G[1])
                led.colorWipe(R_set, G_set, B_set)
            except:
                pass

        elif 'wsB' in data:
            try:
                set_B = data.split()
                B_set = int(set_B[1])
                led.colorWipe(R_set, G_set, B_set)
            except:
                pass

        elif 'pwm0' in data:
            try:
                set_pwm0 = data.split()
                pwm0_set = int(set_pwm0[1])
                servo.setPWM(0, pwm0_set)
            except:
                pass

        elif 'pwm1' in data:
            try:
                set_pwm1 = data.split()
                pwm1_set = int(set_pwm1[1])
                servo.setPWM(1, pwm1_set)
            except:
                pass

        elif 'pwm2' in data:
            try:
                set_pwm2 = data.split()
                pwm2_set = int(set_pwm2[1])
                servo.setPWM(2, pwm2_set)
            except:
                pass

        elif 'Speed' in data:
            try:
                set_speed = data.split()
                speed_set = int(set_speed[1])
            except:
                pass

        elif 'Save' in data:
            try:
                servo.saveConfig()
            except:
                pass

        elif 'CVFL' in data:
            if not FPV.FindLineMode:
                FPV.FindLineMode = 1
                tcpCliSock.send(('CVFL_on').encode())
            else:
                move.motorStop()
                FPV.FindLineMode = 0
                tcpCliSock.send(('CVFL_off').encode())

        elif 'Render' in data:
            if FPV.frameRender:
                FPV.frameRender = 0
            else:
                FPV.frameRender = 1

        elif 'WBswitch' in data:
            if FPV.lineColorSet == 255:
                FPV.lineColorSet = 0
            else:
                FPV.lineColorSet = 255

        elif 'lip1' in data:
            try:
                set_lip1 = data.split()
                lip1_set = int(set_lip1[1])
                FPV.linePos_1 = lip1_set
            except:
                pass

        elif 'lip2' in data:
            try:
                set_lip2 = data.split()
                lip2_set = int(set_lip2[1])
                FPV.linePos_2 = lip2_set
            except:
                pass

        elif 'err' in data:
            try:
                set_err = data.split()
                err_set = int(set_err[1])
                FPV.findLineError = err_set
            except:
                pass

        elif 'FCSET' in data:
            FCSET = data.split()
            fpv.colorFindSet(int(FCSET[1]), int(FCSET[2]), int(FCSET[3]))

        elif 'setEC' in data:  #Z
            ECset = data.split()
            try:
                fpv.setExpCom(int(ECset[1]))
            except:
                pass

        elif 'defEC' in data:  #Z
            fpv.defaultExpCom()

        elif 'police' in data:
            if LED.ledfunc != 'police':
                tcpCliSock.send(('rainbow_off').encode())
                LED.ledfunc = 'police'
                ledthread.resume()
                tcpCliSock.send(('police_on').encode())
            elif LED.ledfunc == 'police':
                LED.ledfunc = ''
                ledthread.pause()
                tcpCliSock.send(('police_off').encode())

        elif 'rainbow' in data:
            if LED.ledfunc != 'rainbow':
                tcpCliSock.send(('police_off').encode())
                LED.ledfunc = 'rainbow'
                ledthread.resume()
                tcpCliSock.send(('rainbow_on').encode())
            elif LED.ledfunc == 'rainbow':
                LED.ledfunc = ''
                ledthread.pause()
                tcpCliSock.send(('rainbow_off').encode())

        elif 'sr' in data:
            if not SR_mode:
                if SR_dect:
                    SR_mode = 1
                    tcpCliSock.send(('sr_on').encode())
                    sr.resume()

            elif SR_mode:
                SR_mode = 0
                sr.pause()
                move.motorStop()
                tcpCliSock.send(('sr_off').encode())

        else:
            pass

        print(data)
示例#12
0
def run():
    global servo_move, speed_set, servo_command, functionMode, init_get, R_set, G_set, B_set, SR_mode
    servo.servo_init()
    move.setup()
    findline.setup()
    direction_command = "no"
    turn_command = "no"
    servo_command = "no"
    speed_set = 100
    rad = 0.5

    info_threading = threading.Thread(
        target=info_send_client
    )  # Define a thread for FPV and OpenCV
    info_threading.setDaemon(
        True
    )  #'True' means it is a front thread,it would close when the mainloop() closes
    info_threading.start()  # Thread starts

    servo_move = Servo_ctrl()
    servo_move.start()
    servo_move.pause()
    findline.setup()
    while True:
        data = ""
        data = str(tcpCliSock.recv(BUFSIZ).decode())
        if not data:
            continue

        elif "forward" == data:
            direction_command = "forward"
            move.move(speed_set, direction_command)

        elif "backward" == data:
            direction_command = "backward"
            move.move(speed_set, direction_command)

        elif "DS" in data:
            direction_command = "no"
            move.move(speed_set, direction_command)

        elif "left" == data:
            # turn_command = 'left'
            servo.turnLeft()

        elif "right" == data:
            # turn_command = 'right'
            servo.turnRight()

        elif "TS" in data:
            # turn_command = 'no'
            servo.turnMiddle()

        elif "Switch_1_on" in data:
            switch.switch(1, 1)
            tcpCliSock.send(("Switch_1_on").encode())

        elif "Switch_1_off" in data:
            switch.switch(1, 0)
            tcpCliSock.send(("Switch_1_off").encode())

        elif "Switch_2_on" in data:
            switch.switch(2, 1)
            tcpCliSock.send(("Switch_2_on").encode())

        elif "Switch_2_off" in data:
            switch.switch(2, 0)
            tcpCliSock.send(("Switch_2_off").encode())

        elif "Switch_3_on" in data:
            switch.switch(3, 1)
            tcpCliSock.send(("Switch_3_on").encode())

        elif "Switch_3_off" in data:
            switch.switch(3, 0)
            tcpCliSock.send(("Switch_3_off").encode())

        elif "function_1_on" in data:
            servo.ahead()
            time.sleep(0.2)
            tcpCliSock.send(("function_1_on").encode())
            radar_send = servo.radar_scan()
            tcpCliSock.sendall(radar_send.encode())
            print(radar_send)
            time.sleep(0.3)
            tcpCliSock.send(("function_1_off").encode())

        elif "function_2_on" in data:
            functionMode = 2
            fpv.FindColor(1)
            tcpCliSock.send(("function_2_on").encode())

        elif "function_3_on" in data:
            functionMode = 3
            fpv.WatchDog(1)
            tcpCliSock.send(("function_3_on").encode())

        elif "function_4_on" in data:
            functionMode = 4
            servo_move.resume()
            tcpCliSock.send(("function_4_on").encode())

        elif "function_5_on" in data:
            functionMode = 5
            servo_move.resume()
            tcpCliSock.send(("function_5_on").encode())

        elif "function_6_on" in data:
            if MPU_connection:
                functionMode = 6
                servo_move.resume()
                tcpCliSock.send(("function_6_on").encode())

        # elif 'function_1_off' in data:
        # 	tcpCliSock.send(('function_1_off').encode())

        elif "function_2_off" in data:
            functionMode = 0
            fpv.FindColor(0)
            switch.switch(1, 0)
            switch.switch(2, 0)
            switch.switch(3, 0)
            tcpCliSock.send(("function_2_off").encode())

        elif "function_3_off" in data:
            functionMode = 0
            fpv.WatchDog(0)
            tcpCliSock.send(("function_3_off").encode())

        elif "function_4_off" in data:
            functionMode = 0
            servo_move.pause()
            move.motorStop()
            tcpCliSock.send(("function_4_off").encode())

        elif "function_5_off" in data:
            functionMode = 0
            servo_move.pause()
            move.motorStop()
            tcpCliSock.send(("function_5_off").encode())

        elif "function_6_off" in data:
            functionMode = 0
            servo_move.pause()
            move.motorStop()
            init_get = 0
            tcpCliSock.send(("function_6_off").encode())

        elif "lookleft" == data:
            servo_command = "lookleft"
            servo_move.resume()

        elif "lookright" == data:
            servo_command = "lookright"
            servo_move.resume()

        elif "up" == data:
            servo_command = "up"
            servo_move.resume()

        elif "down" == data:
            servo_command = "down"
            servo_move.resume()

        elif "stop" == data:
            if not functionMode:
                servo_move.pause()
            servo_command = "no"
            pass

        elif "home" == data:
            servo.ahead()

        elif "CVrun" == data:
            if not FPV.CVrun:
                FPV.CVrun = 1
                tcpCliSock.send(("CVrun_on").encode())
            else:
                FPV.CVrun = 0
                tcpCliSock.send(("CVrun_off").encode())

        elif "wsR" in data:
            try:
                set_R = data.split()
                R_set = int(set_R[1])
                led.colorWipe(R_set, G_set, B_set)
            except:
                pass

        elif "wsG" in data:
            try:
                set_G = data.split()
                G_set = int(set_G[1])
                led.colorWipe(R_set, G_set, B_set)
            except:
                pass

        elif "wsB" in data:
            try:
                set_B = data.split()
                B_set = int(set_B[1])
                led.colorWipe(R_set, G_set, B_set)
            except:
                pass

        elif "pwm0" in data:
            try:
                set_pwm0 = data.split()
                pwm0_set = int(set_pwm0[1])
                servo.setPWM(0, pwm0_set)
            except:
                pass

        elif "pwm1" in data:
            try:
                set_pwm1 = data.split()
                pwm1_set = int(set_pwm1[1])
                servo.setPWM(1, pwm1_set)
            except:
                pass

        elif "pwm2" in data:
            try:
                set_pwm2 = data.split()
                pwm2_set = int(set_pwm2[1])
                servo.setPWM(2, pwm2_set)
            except:
                pass

        elif "Speed" in data:
            try:
                set_speed = data.split()
                speed_set = int(set_speed[1])
            except:
                pass

        elif "Save" in data:
            try:
                servo.saveConfig()
            except:
                pass

        elif "CVFL" in data:
            if not FPV.FindLineMode:
                FPV.FindLineMode = 1
                tcpCliSock.send(("CVFL_on").encode())
            else:
                move.motorStop()
                FPV.FindLineMode = 0
                tcpCliSock.send(("CVFL_off").encode())

        elif "Render" in data:
            if FPV.frameRender:
                FPV.frameRender = 0
            else:
                FPV.frameRender = 1

        elif "WBswitch" in data:
            if FPV.lineColorSet == 255:
                FPV.lineColorSet = 0
            else:
                FPV.lineColorSet = 255

        elif "lip1" in data:
            try:
                set_lip1 = data.split()
                lip1_set = int(set_lip1[1])
                FPV.linePos_1 = lip1_set
            except:
                pass

        elif "lip2" in data:
            try:
                set_lip2 = data.split()
                lip2_set = int(set_lip2[1])
                FPV.linePos_2 = lip2_set
            except:
                pass

        elif "err" in data:
            try:
                set_err = data.split()
                err_set = int(set_err[1])
                FPV.findLineError = err_set
            except:
                pass

        elif "FCSET" in data:
            FCSET = data.split()
            fpv.colorFindSet(int(FCSET[1]), int(FCSET[2]), int(FCSET[3]))

        elif "setEC" in data:  # Z
            ECset = data.split()
            try:
                fpv.setExpCom(int(ECset[1]))
            except:
                pass

        elif "defEC" in data:  # Z
            fpv.defaultExpCom()

        elif "police" in data:
            if LED.ledfunc != "police":
                tcpCliSock.send(("rainbow_off").encode())
                LED.ledfunc = "police"
                ledthread.resume()
                tcpCliSock.send(("police_on").encode())
            elif LED.ledfunc == "police":
                LED.ledfunc = ""
                ledthread.pause()
                tcpCliSock.send(("police_off").encode())

        elif "rainbow" in data:
            if LED.ledfunc != "rainbow":
                tcpCliSock.send(("police_off").encode())
                LED.ledfunc = "rainbow"
                ledthread.resume()
                tcpCliSock.send(("rainbow_on").encode())
            elif LED.ledfunc == "rainbow":
                LED.ledfunc = ""
                ledthread.pause()
                tcpCliSock.send(("rainbow_off").encode())

        elif "sr" in data:
            if not SR_mode:
                if SR_dect:
                    SR_mode = 1
                    tcpCliSock.send(("sr_on").encode())
                    sr.resume()

            elif SR_mode:
                SR_mode = 0
                sr.pause()
                move.motorStop()
                tcpCliSock.send(("sr_off").encode())

        else:
            pass

        print(data)
示例#13
0
    def appCommand(data_input):
        global direction_command, turn_command, servo_command
        if data_input == 'forwardStart\n':
            direction_command = 'forward'
            move.move(speed_set, direction_command)

        elif data_input == 'backwardStart\n':
            direction_command = 'backward'
            move.move(speed_set, direction_command)

        elif data_input == 'leftStart\n':
            turn_command = 'left'
            servo.turnLeft()
            move.move(speed_set, direction_command)

        elif data_input == 'rightStart\n':
            turn_command = 'right'
            servo.turnRight()
            move.move(speed_set, direction_command)

        elif 'forwardStop' in data_input:
            if turn_command == 'no':
                move.motorStop()

        elif 'backwardStop' in data_input:
            if turn_command == 'no':
                move.motorStop()

        elif 'leftStop' in data_input:
            turn_command = 'no'
            servo.turnMiddle()
            move.motorStop()

        elif 'rightStop' in data_input:
            turn_command = 'no'
            servo.turnMiddle()
            move.motorStop()

        if data_input == 'lookLeftStart\n':
            servo_command = 'lookleft'
            servo_move.resume()

        elif data_input == 'lookRightStart\n':
            servo_command = 'lookright'
            servo_move.resume()

        elif data_input == 'downStart\n':
            servo_command = 'down'
            servo_move.resume()

        elif data_input == 'upStart\n':
            servo_command = 'up'
            servo_move.resume()

        elif 'lookLeftStop' in data_input:
            servo_move.pause()
            servo_command = 'no'
        elif 'lookRightStop' in data_input:
            servo_move.pause()
            servo_command = 'no'
        elif 'downStop' in data_input:
            servo_move.pause()
            servo_command = 'no'
        elif 'upStop' in data_input:
            servo_move.pause()
            servo_command = 'no'

        if data_input == 'aStart\n':
            if LED.ledfunc != 'police':
                LED.ledfunc = 'police'
                ledthread.resume()
            elif LED.ledfunc == 'police':
                LED.ledfunc = ''
                ledthread.pause()

        elif data_input == 'bStart\n':
            if LED.ledfunc != 'rainbow':
                LED.ledfunc = 'rainbow'
                ledthread.resume()
            elif LED.ledfunc == 'rainbow':
                LED.ledfunc = ''
                ledthread.pause()

        elif data_input == 'cStart\n':
            switch.switch(1, 1)
            switch.switch(2, 1)
            switch.switch(3, 1)

        elif data_input == 'dStart\n':
            switch.switch(1, 0)
            switch.switch(2, 0)
            switch.switch(3, 0)

        elif 'aStop' in data_input:
            pass
        elif 'bStop' in data_input:
            pass
        elif 'cStop' in data_input:
            pass
        elif 'dStop' in data_input:
            pass

        print(data_input)
示例#14
0
    def appCommand(data_input):
        global direction_command, turn_command, servo_command
        if data_input == "forwardStart\n":
            direction_command = "forward"
            move.move(speed_set, direction_command)

        elif data_input == "backwardStart\n":
            direction_command = "backward"
            move.move(speed_set, direction_command)

        elif data_input == "leftStart\n":
            turn_command = "left"
            servo.turnLeft()
            move.move(speed_set, direction_command)

        elif data_input == "rightStart\n":
            turn_command = "right"
            servo.turnRight()
            move.move(speed_set, direction_command)

        elif "forwardStop" in data_input:
            if turn_command == "no":
                move.motorStop()

        elif "backwardStop" in data_input:
            if turn_command == "no":
                move.motorStop()

        elif "leftStop" in data_input:
            turn_command = "no"
            servo.turnMiddle()
            move.motorStop()

        elif "rightStop" in data_input:
            turn_command = "no"
            servo.turnMiddle()
            move.motorStop()

        if data_input == "lookLeftStart\n":
            servo_command = "lookleft"
            servo_move.resume()

        elif data_input == "lookRightStart\n":
            servo_command = "lookright"
            servo_move.resume()

        elif data_input == "downStart\n":
            servo_command = "down"
            servo_move.resume()

        elif data_input == "upStart\n":
            servo_command = "up"
            servo_move.resume()

        elif "lookLeftStop" in data_input:
            servo_move.pause()
            servo_command = "no"
        elif "lookRightStop" in data_input:
            servo_move.pause()
            servo_command = "no"
        elif "downStop" in data_input:
            servo_move.pause()
            servo_command = "no"
        elif "upStop" in data_input:
            servo_move.pause()
            servo_command = "no"

        if data_input == "aStart\n":
            if LED.ledfunc != "police":
                LED.ledfunc = "police"
                ledthread.resume()
            elif LED.ledfunc == "police":
                LED.ledfunc = ""
                ledthread.pause()

        elif data_input == "bStart\n":
            if LED.ledfunc != "rainbow":
                LED.ledfunc = "rainbow"
                ledthread.resume()
            elif LED.ledfunc == "rainbow":
                LED.ledfunc = ""
                ledthread.pause()

        elif data_input == "cStart\n":
            switch.switch(1, 1)
            switch.switch(2, 1)
            switch.switch(3, 1)

        elif data_input == "dStart\n":
            switch.switch(1, 0)
            switch.switch(2, 0)
            switch.switch(3, 0)

        elif "aStop" in data_input:
            pass
        elif "bStop" in data_input:
            pass
        elif "cStop" in data_input:
            pass
        elif "dStop" in data_input:
            pass

        print(data_input)
示例#15
0
    def appCommand(data_input):
        global direction_command, turn_command, servo_command

        # 0riv3r:
        # The following code fixes the un-synced app buttons with their respective functions
        # ==================================================================================  
        #
        # The commands that arrive from the phone are not sync with the phone app buttons
        # original:
        # full-arrows on the right side of the app's pannel
        # fixing the arrows by changing the arrived data
        #   arrow drawing   |   arrived-text                    |   text-changed-to
        #   -------------   |   ------------                        ---------------
        #        <          |   downStart, downStop             |   lookLeftStart, lookLeftStop
        #        >          |   upStart, upStop                 |   lookRightStart, lookRightStop
        #        ^          |   lookLeftStart, lookLeftStop     |   upStart, upStop
        #        v          |   lookRightStart, lookRightStop   |   downStart, downStop
        # -----------------------------------------------------------------------------------------
        #     letters       |   arrived-text                    |   command-changed-to
        #     -------       |   ------------                        ---------------
        #        A          |   aStart, aStop - begin police/end police
        #        B          |   bStart, bStop - start changing/illuminating back lights        | servo.ahead()
        #        C          |   cStart, cStop - turn off some lights on the raspberry-pi head
        #        D          |   dStart, dStop - turn on some lights on the raspberry-pi head

        if data_input == 'lookLeftStart\n':
            data_input = 'upStart\n'
        elif data_input == 'lookRightStart\n':
            data_input = 'downStart\n'
        elif data_input == 'upStart\n':
            data_input = 'lookRightStart\n'
        elif data_input == 'downStart\n':
            data_input = 'lookLeftStart\n'

        elif 'lookLeftStop' in data_input:
            data_input = 'upStop\n'
        elif 'lookRightStop' in data_input:
            data_input = 'downStop\n'
        elif 'downStop' in data_input:
            data_input = 'lookLeftStop\n'
        elif 'upStop' in data_input:
            data_input = 'lookRightStop\n'

        # ================================================================================== 

        if data_input == 'forwardStart\n':
            direction_command = 'forward'
            move.move(app.speedControl(), direction_command)

        elif data_input == 'backwardStart\n':
            direction_command = 'backward'
            move.move(app.speedControl(), direction_command)

        elif data_input == 'leftStart\n':
            turn_command = 'left'
            servo.turnLeft()
            move.move(app.speedControl(), direction_command)

        elif data_input == 'rightStart\n':
            turn_command = 'right'
            servo.turnRight()
            move.move(app.speedControl(), direction_command)

        elif 'forwardStop' in data_input:
            if turn_command == 'no':
                move.motorStop()

        elif 'backwardStop' in data_input:
            if turn_command == 'no':
                move.motorStop()

        elif 'leftStop' in data_input:
            turn_command = 'no'
            servo.turnMiddle()
            move.motorStop()

        elif 'rightStop' in data_input:
            turn_command = 'no'
            servo.turnMiddle()
            move.motorStop()

        if data_input == 'lookLeftStart\n':
            servo_command = 'lookleft'
            servo_move.resume()

        elif data_input == 'lookRightStart\n': 
            servo_command = 'lookright'
            servo_move.resume()

        elif data_input == 'downStart\n':
            servo_command = 'down'
            servo_move.resume()

        elif data_input == 'upStart\n':
            servo_command = 'up'
            servo_move.resume()

        elif 'lookLeftStop' in data_input:
            servo_move.pause()
            servo_command = 'no'
        elif 'lookRightStop' in data_input:
            servo_move.pause()
            servo_command = 'no'
        elif 'downStop' in data_input:
            servo_move.pause()
            servo_command = 'no'
        elif 'upStop' in data_input:
            servo_move.pause()
            servo_command = 'no'


        if data_input == 'aStart\n':
            app.btn_A()

        elif data_input == 'bStart\n':
            app.btn_B()

        elif data_input == 'cStart\n':

            switch.switch(1,1)
            switch.switch(2,1)
            switch.switch(3,1)

        elif data_input == 'dStart\n':

            switch.switch(1,0)
            switch.switch(2,0)
            switch.switch(3,0)

        elif 'aStop' in data_input:
            pass
        elif 'bStop' in data_input:
            pass
        elif 'cStop' in data_input:
            pass
        elif 'dStop' in data_input:
            pass

        print(data_input)