def demo_step_find_object(ip,
                          speed=20,
                          dis=1,
                          object='cup',
                          vAngle=30,
                          hAngle=90):
    """
    移动,一步一步的寻找物体

     Parameter
     -------
     *ip:string
         -树莓派的
     *object:string
         -要寻找的物体
     *vAngle:int
         -垂直方向的角度
     *hAngle:int
         -水平方向的角度
     *speed:int
         -移动的速度
     *dis:float
         -时间间隔
    """
    # 初始化
    car = Car(ip)
    # 相机初始化
    camera = Camera()
    camera.connect_server(ip)
    camera.start_receive()
    camera.thread_play()

    ai = Ai(classes="./aiLib/coco/coco.names",
            config="./aiLib/coco/yolov3.cfg",
            weight="./aiLib/coco/yolov3.weights")

    car.turn_servo_camera_vertical(vAngle)
    car.turn_servo_camera_horizental(hAngle)

    global STOP_FLAGE
    while True:

        if STOP_FLAGE is True:
            print('over .............................................')
            return

        status = get_status_with_camera(car, camera, ai, object)
        car.turn_servo_camera_horizental(90)
        print(status)
        if status == 'status_move':
            car.run_forward(speed, dis)
        if status == 'status_turn_right':
            car.spin_right(10, 0.4)
        if status == 'status_turn_left':
            car.spin_left(10, 0.4)
        if status == 'status_stop':
            return
def move_step_find_object1_thread(ip, camera, ai, object, vAngle, hAngle):
    """
    小车移动寻找目标
     Parameter
     -------
     *ip:string
         -树莓派的地址
     *camera:class
         -相机控制
     *ai:class
         -人工智能类
     *object:string
         --目标名
     *vAngle:int
         --相机垂直角度
     *hAngle:int
         --相机水平角度
     Return
     ---------
     None
    """
    global CRUSING_FLOG
    global STOP_FLAGE
    global CAMERA_FLAGE
    car = Car(ip)
    car.turn_servo_camera_vertical(vAngle)
    car.turn_servo_camera_horizental(hAngle)  #运动到相机指定方向

    while not STOP_FLAGE:
        Cruising(car, 5)  # 以7的速度进行漫游
        car.turn_servo_camera_vertical(vAngle - 2)  #抬起相机查看
        car.turn_servo_camera_horizental(hAngle)
        ret = check_object_step(camera, car, ai, object)  #旋转小车确认
        if ret == 'find':
            break
        if ret == 'nothing':  #没有找到,从新找
            mainThread_ = threading.Thread(target=find_object,
                                           args=(
                                               camera,
                                               ai,
                                               object,
                                           ))
            mainThread_.start()
            CRUSING_FLOG = True

    car.spin_left(2, 0.1)  # 回转,减少误差

    start_sensor_check = False

    while True:
        if STOP_FLAGE == True:
            car.stop_all_wheels()
            print(
                'Cruising over .............................................')
            return

        pic = camera.take_picture()
        y = []
        x = []
        x1 = 320
        y1 = 0

        for i in range(1):  #多张图寻找
            ret, names, box = ai.find_object(pic)
            for item in names:
                if item == object:
                    id = names.index(item)
                    print(box[id])
                    y.append(box[id][1])
                    x.append(box[id][0])

        #多张图求平均
        ysum = 0
        for d in y:
            ysum = ysum + d
        if len(y) > 0:
            y1 = ysum / len(y)
            print(y1)

        #多张图,求取x平均
        xsum = 0
        for d in x:
            xsum = xsum + d
        if len(x) > 0:
            x1 = xsum / len(x)
            print(x1)

        if x1 > 360:  #右转
            print('turn right')
            car.turn_right(4, 0.1)
        elif x1 < 200:  #左转
            print('turn left')
            car.turn_left(4, 0.1)
        else:  #直行
            car.run_forward(4, 0.2)
        if y1 > 300:  #说明车子靠近目标,启动超声波,相机向下移动一点
            start_sensor_check = True
            car.turn_servo_camera_vertical(vAngle + 2)

        #超声波检查
        dis1 = 1000
        dis2 = 1000
        if start_sensor_check:
            car.turn_servo_ultrasonic_angle(50)  #左
            time.sleep(0.2)
            dis1 = car.distance_from_obstacle()
            car.turn_servo_ultrasonic_angle(130)  #右
            time.sleep(0.2)
            dis2 = car.distance_from_obstacle()
            car.turn_servo_ultrasonic_angle(90)  #中
            time.sleep(0.2)

        dis = car.distance_from_obstacle()
        distance_to_obstacle = min(min(dis1, dis2), dis)  #选区最小值

        print("distance", distance_to_obstacle)
        if (0 < distance_to_obstacle
                and distance_to_obstacle < 30):  #检查范围,满足条件说明,车找到杯子了
            print("find cup")
            return
def demo_move_step_find_object(ip,
                               speed=20,
                               dis=1,
                               object='cup',
                               vAngle=40,
                               hAngle=80):
    """
    连续移动寻找物体

     Parameter
     -------
     *ip:string
         -树莓派的Ip
     *object:string
         -要寻找的物体
     *vAngle:int
         -垂直方向的角度
     *hAngle:int
         -水平方向的角度
    """
    car = Car(ip)

    camera = Camera()
    camera.connect_server(ip)
    camera.start_receive()
    camera.thread_play()

    ai = Ai(classes="./aiLib/coco/coco.names",
            config="./aiLib/coco/yolov3.cfg",
            weight="./aiLib/coco/yolov3.weights")

    car.turn_servo_camera_vertical(vAngle)
    car.turn_servo_camera_horizental(hAngle)

    mainThread_ = threading.Thread(target=find_object,
                                   args=(
                                       camera,
                                       ai,
                                       object,
                                   ))
    mainThread_.start()

    Cruising(car, 4)  # 以4的速度进行漫游

    car.turn_servo_camera_vertical(vAngle)
    car.turn_servo_camera_horizental(hAngle)

    global STOP_FLAGE
    while CAMERA_FLAGE:

        if STOP_FLAGE == True:
            car.stop_all_wheels()
            print('over .............................................')
            return

        status = get_status_with_camera(car, camera, ai, object)
        car.turn_servo_camera_horizental(90)
        print(status)
        if status == 'status_move':
            car.run_forward(speed, dis)
        if status == 'status_turn_right':
            car.spin_right(10, 0.4)
        if status == 'status_turn_left':
            car.spin_left(10, 0.4)
        if status == 'status_stop':
            return