def run():
    global memory
    camera.sendImageTo(reciever)

    actOnProcess = Thread(target=actOn.move, args=(memory, ))
    actOnProcess.start()
    input('Press enter when ready: ')

    try:
        camera.start(memory)
    except Exception as e:
        print(e)
        traceback.print_exc()
    print('Program stopped')
    stop_thread(actOnProcess)
    memory['speed'] = 1500
    memory['angle'] = 90
    memory['running'] = True
    actOn.move(memory)
    memory['running'] = False
    try:
        camera.start(memory)
    except Exception as e:
        print(e)
        traceback.print_exc()
    print('Program stopped')
    stop_thread(actOnProcess)
    memory['speed'] = 1500
    memory['angle'] = 90
    memory['running'] = True
    actOn.move(memory)
    memory['running'] = False


def stop_thread(actOnProcess):
    global memory
    memory['running'] = False
    actOnProcess.join()


if __name__ == '__main__':
    try:
        while not memory['true_stop']:
            run()
            memory['speed'] = 1500
            memory['angle'] = 90
            memory['running'] = True
            actOn.move(memory)
    finally:
        stop_thread(actOnProcess)
Exemple #3
0
def reciever(helper):
    global memory
    helper['speed'] = 1590
    helper['correction'] = 90
    helper['debug'] = memory['debug']
    image = helper['image']
    if helper['debug']:
        helper['draw_image'] = image.copy()
    fps = 1 / (time.time() - memory['time'])
    print("FPS:{0}".format(fps))
    if fps < memory['minfps']:
        memory['minfps'] = fps
    print("minFPS:{0}".format(memory['minfps']))
    memory['itercount'] += 1
    memory['totfps'] += fps
    print("avgfps:{0}".format(memory['totfps'] / memory['itercount']))
    memory['time'] = time.time()
    localiser.getOurLocation(helper)

    # Get Contours
    getContours.get_c(helper)
    print("gcnt t:{0}".format(time.time()))
    #cc.clean(helper)
    if memory['reverse'] < 0:
        memory['reverse'] += 1
    elif memory['reverse'] > 0:
        helper['speed'] = 1400
        helper['correction'] = memory['lastAngle']
        actOn.move(helper)
        memory['reverse'] -= 1
        return
    if helper['main_y_contour'] is None and helper['main_b_contour'] is None:
        # Be careful
        helper['midpoints'] = None
        helper['speed'] = 1420
        memory['reverse'] -= 5
        if memory['reverse'] < -20:
            memory['reverse'] = 20
    elif helper['main_y_contour'] is None:
        helper['midpoints'] = np.array([[0, image.shape[1]]])
        #followLine.follow(helper, 'blue')
        print('Can\'t see yellow')
    elif helper['main_b_contour'] is None:
        helper['midpoints'] = np.array([[0, 0]])
        #followLine.follow(helper, 'yellow')
        print('Can\'t see blue')
    else:
        # determine path to be followed in our coordinate frame
        pathfinder.getPathToFollow(helper)
        print("pf t:{0}".format(time.time()))

        print('Normal operation')

    if not helper['midpoints'] is None:
        print("everything is ok")

        # determine a new path to follow taking into account obstacles
        obstacleDetector.amendPath(helper)
        print("od t:{0}".format(time.time()))

        # determine our location in our coordinate frame

        # calculate any corrections
        gc.getCorrection(helper)
        print("gc t:{0}".format(time.time()))

        # detecting corner: gives which direction we are headed in and prints the angle
        if helper['main_y_contour'] is not None and helper[
                'main_b_contour'] is not None:
            print(detectCorner.detectCorner(helper))

        # physically adjust course, speed etc
        # gCorner.get_corner(helper)          # draws a white line
        actOn.move(helper)
        # Draw things for debug purposes
        if helper['debug']:
            for e in helper['midpoints']:
                cv2.circle(helper['draw_image'], (int(e[0]), int(e[1])), 4,
                           (0, 0, 255))

    else:
        actOn.move(helper)
    print("ao t:{0}".format(time.time()))

    memory['lastAngle'] = helper['correction']
    if (memory['debug']):
        # display on the image
        cv2.imshow("uneditted", image)
        cv2.imshow("drawn", helper['draw_image'])
        videowriter.writeToFile(helper)

        # exit image (doesnt work)
        if cv2.waitKey(1) == 'q':
            return -1
        else:
            return 0
        return 0