Exemple #1
0
def main():
    robot = movements.robot()
    camera = PiCamera()
    camera.resolution = (640, 480)
    camera.framerate = 50
    camera.hflip = True

    rawCapture = PiRGBArray(camera, size=(640, 480))

    time.sleep(0.1)

    for frame in camera.capture_continuous(rawCapture,
                                           format="bgr",
                                           use_video_port=True):
        image = frame.array

        blur = cv2.GaussianBlur(image, (3, 3), 0)

        lower = np.array([3, 9, 114], dtype="uint8")
        upper = np.array([43, 49, 154], dtype="uint8")

        thresh = cv2.inRange(blur, lower, upper)
        thresh2 = thresh.copy()

        image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST,
                                                      cv2.CHAIN_APPROX_SIMPLE)

        max_area = 0
        best_cent = 1
        detected = False
        for cent in contours:
            area = cv2.contourArea(cent)
            if area > max_area:
                max_area = area
                best_cent = cent
                detected = True

        M = cv2.moments(best_cent)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])

        direction, distance_from_cent = find_dir(cx)

        if detected == True:
            scan_dist = robot.scan_for_obstacles()

            if scan_dist >= 25:
                if direction == 'left':
                    robot.left_forward()

                elif direction == 'right':
                    robot.right_forward()

            elif scan_dist >= 10:
                if direction == 'left':
                    robot.left()

                elif direction == 'right':
                    robot.right()

            else:
                robot.pause()

        else:
            robot.pause()

        rawCapture.truncate(0)
Exemple #2
0
import RPi.GPIO as GPIO
import movements
import time
import curses

robot = movements.robot()


def move(char):
    if char == curses.KEY_UP:
        screen.addstr(0, 0, 'up   ')
        robot.forward()

    elif char == curses.KEY_DOWN:
        screen.addstr(0, 0, 'down ')
        robot.backward()

    elif char == curses.KEY_LEFT:
        screen.addstr(0, 0, 'left ')
        robot.right()

    elif char == curses.KEY_RIGHT:
        screen.addstr(0, 0, 'right')
        robot.left()

    elif char == 32:
        screen.addstr(0, 0, 'stop ')
        robot.stop()


screen = curses.initscr()
Exemple #3
0
def main():
    robot = movements.robot()
    camera = PiCamera()
    camera.resolution = (640, 480)
    camera.framerate = 50
    camera.hflip = True
    camera.vflip = True

    rawCapture = PiRGBArray(camera, size=(640, 480))

    time.sleep(0.1)

    for frame in camera.capture_continuous(rawCapture,
                                           format="bgr",
                                           use_video_port=True):
        image = frame.array

        blur = cv2.GaussianBlur(image, (3, 3), 0)

        lower = np.array([3, 9, 114], dtype="uint8")
        upper = np.array([43, 49, 154], dtype="uint8")

        thresh = cv2.inRange(blur, lower, upper)
        thresh2 = thresh.copy()

        image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST,
                                                      cv2.CHAIN_APPROX_SIMPLE)
        max_area = 0
        best_cent = 1
        detected = False
        for cent in contours:
            area = cv2.contourArea(cent)
            if area > max_area:
                max_area = area
                best_cent = cent
                detected = True

        M = cv2.moments(best_cent)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.circle(blur, (cx, cy), 10, (0, 0, 255), -1)

        cv2.imshow('Tracking', blur)

        direction = find_pos(cx)
        if detected == True:
            #print '{}'.format(direction)
            dist = int(robot.scan_for_obstacles())

            if direction == 'left':
                if dist < 30:
                    robot.left()

                else:
                    robot.left_forward()

            else:
                if dist < 30:
                    robot.right()

                else:
                    robot.right_forward()

        else:
            #print 'nuttin'
            robot.pause()

        blah = cv2.waitKey(1) & 0xFF

        rawCapture.truncate(0)

        if blah == ord("q"):
            break