예제 #1
0
    # Path
    path = path_generator.path2()
    img_path = np.ones((600,600,3))
    for i in range(path.shape[0]-1):
        cv2.line(img_path, (int(path[i,0]), int(path[i,1])), (int(path[i+1,0]), int(path[i+1,1])), (1.0,0.5,0.5), 1)

    # Initialize Car
    car = KinematicModel()
    car.init_state((50,300,0))
    start = (50,300,0)
    controller = PurePursuitControl()
    controller.set_path(path)

    while(True):
        print("\rState: "+car.state_str(), end="\t")

        # ================= Control Algorithm ================= 
        # PID Longitude Control
        end_dist = np.hypot(path[-1,0]-car.x, path[-1,1]-car.y)
        target_v = 40 if end_dist > 265 else 0
        next_a = 0.1*(target_v - car.v)

        # Pure Pursuit Lateral Control
        state = {"x":car.x, "y":car.y, "yaw":car.yaw, "v":car.v}
        next_delta, target = controller.feedback(state)
        car.control(next_a,next_delta)
        # =====================================================
        
        # Update & Render
        car.update()
예제 #2
0
def main():
    # Parameters
    N_PARTICLES = 40  # Number of Particles
    N_LANDMARKS = 80  # Number of Landmarks
    PERCEPTUAL_RANGE = 120  # Landmark Detection Range
    MOTION_NOISE = np.array([1e-5, 1e-2, 1e-5, 1e-2, 1e-5,
                             1e-2])  # Motion Noise
    Qt_sim = np.diag([4, np.deg2rad(4)])**2  # Observation Noise

    # Create landmarks
    landmarks = []
    for i in range(N_LANDMARKS):
        rx = np.random.randint(10, 490)
        ry = np.random.randint(10, 490)
        landmarks.append((rx, ry))

    # Initialize environment
    window_name = "Fast-SLAM Demo"
    cv2.namedWindow(window_name)
    img = np.ones((500, 500, 3))
    init_pos = (250, 100, 0)
    car = KinematicModel(motion_noise=MOTION_NOISE)
    car.init_state(init_pos)
    car.v = 24
    car.w = np.deg2rad(10)
    path_odometry = [(init_pos)]

    # Create particle filter
    pf = ParticleFilter((car.x, car.y, car.yaw),
                        Qt_sim,
                        MOTION_NOISE,
                        psize=N_PARTICLES)
    while (True):
        ###################################
        # Simulate Controlling
        ###################################
        u = (car.v, car.w, car.dt)
        car.update()
        # TODO(Lab-4): Remove the comment after complete the motion model.
        #'''
        pos_odometry = motion_model(path_odometry[-1], u)
        path_odometry.append(pos_odometry)
        #'''
        print("\rState: " + car.state_str() + " | Neff:" + str(pf.Neff)[0:7],
              end="\t")

        ###################################
        # Simulate Observation
        ###################################
        rvec = np.array(landmarks) - np.array((car.x, car.y))
        dist = np.hypot(rvec[:, 0], rvec[:, 1])
        landmarks_id = np.where(
            dist < PERCEPTUAL_RANGE)[0]  # Detected Landmark ids
        landmarks_detect = np.array(landmarks)[
            landmarks_id]  # Detected Landmarks
        z = []
        for i in range(landmarks_detect.shape[0]):
            lm = landmarks_detect[i]
            r = dist[landmarks_id[i]]
            phi = np.arctan2(lm[1] - car.y, lm[0] - car.x) - car.yaw
            # Add Observation Noise
            r = r + np.random.randn() * Qt_sim[0, 0]**0.5
            phi = phi + np.random.randn() * Qt_sim[1, 1]**0.5
            phi = normalize_angle(phi)
            z.append((r, phi))

        ###################################
        # SLAM Algorithm
        ###################################
        # TODO(Lab-0): Remove the comment after complete the class.
        #'''
        pf.sample(u)
        pf.update(z, landmarks_id)
        pf.resample()
        #'''

        ###################################
        # Render Canvas
        ###################################
        img_ = img.copy()
        # Draw landmark
        for lm in landmarks:
            cv2.circle(img_, lm, 3, (0.1, 0.7, 0.1), 1)
        for i in range(landmarks_detect.shape[0]):
            lm = landmarks_detect[i]
            cv2.line(img_, (int(car.x), int(car.y)), (int(lm[0]), int(lm[1])),
                     (0, 1, 0), 1)
        # Draw path
        for i in range(N_PARTICLES):
            draw_path(img_, pf.particles[i].path, 100, (1, 0.7, 0.7))
        bid = np.argmax(np.array(pf.weights))
        draw_path(img_, pf.particles[bid].path, 1000,
                  (1, 0, 0))  # Draw Best Path
        draw_path(img_, path_odometry, 1000, (0, 0, 0))  # Draw Odometry Path
        # Draw particle pose
        for i in range(N_PARTICLES):
            cv2.circle(
                img_,
                (int(pf.particles[i].pos[0]), int(pf.particles[i].pos[1])), 2,
                (1, 0, 0), 1)
            # Draw maps of particles
            '''
            for lm in pf.particles[i].landmarks:
                lx = pf.particles[i].landmarks[lm]["mu"][0,0]
                ly = pf.particles[i].landmarks[lm]["mu"][1,0]
                cv2.circle(img_, (int(lx),int(ly)), 2, (1,0,0), 1)
            '''
        # Draw map of best particle
        for lid in pf.particles[bid].landmarks:
            mean = pf.particles[bid].landmarks[lid]["mu"].reshape(2)
            cov = pf.particles[bid].landmarks[lid]["sig"]
            draw_ellipse(img_, mean, cov, (0, 0, 1))

        cv2.circle(img_, (int(car.x), int(car.y)), PERCEPTUAL_RANGE, (0, 1, 0),
                   1)  # Draw Detect Range
        img_ = car.render(img_)  # Render Car
        img_ = cv2.flip(img_, 0)
        cv2.imshow(window_name, img_)

        ###################################
        # Keyboard
        ###################################
        k = cv2.waitKey(1)  #1
        if k == ord("w"):
            car.v += 4
        elif k == ord("s"):
            car.v += -4
        if k == ord("a"):
            car.w += 5
        elif k == ord("d"):
            car.w += -5
        elif k == ord("r"):
            car.init_state(init_pos)
            pf = ParticleFilter((car.x, car.y, car.yaw),
                                Qt_sim,
                                MOTION_NOISE,
                                psize=N_PARTICLES)
            path_odometry = [(init_pos)]
            print("Reset!!")
        if k == 27:
            print()
            break
예제 #3
0
    l2 = Bresenham(p2[0], p3[0], p2[1], p3[1])
    l3 = Bresenham(p3[0], p4[0], p3[1], p4[1])
    l4 = Bresenham(p4[0], p1[0], p4[1], p1[1])
    check = l1+l2+l3+l4
    collision = False
    for pts in check:
        if m[int(pts[1]),int(pts[0])]<0.5:
            collision = True
            car.redo()
            car.v = -0.8*car.v
            print("gg")
            break
    if collision:
        collision_count = 1

    print("\rState: "+car.state_str(), "| Goal:", nav_pos, end="\t")
    pos = (car.x, car.y, car.yaw)
    sdata = lmodel.measure(pos)
    plist = EndPoint(pos, [sensor_size,-120,120], sdata)

    # Particle Filter
    pf.feed((car.v, car.w, car.dt), sdata)
    print(pf.neff)
    if pf.neff < particle_size/2:
        print("re")
        pf.resampling()

    pmimg = pf.particle_list[5].gmap.adaptive_get_map_prob()
    pmimg = (255*pmimg).astype(np.uint8)
    pmimg = cv2.cvtColor(pmimg, cv2.COLOR_GRAY2RGB)
    pmimg_ = cv2.flip(pmimg,0)
예제 #4
0
def main():
    window_name = "Fast-SLAM Demo"
    cv2.namedWindow(window_name)
    img = np.ones((500, 500, 3))
    init_pos = (100, 200, 0)
    lsize = 60
    detect_dist = 150
    psize = 30
    landmarks = []
    for i in range(lsize):
        rx = np.random.randint(10, 490)
        ry = np.random.randint(10, 490)
        landmarks.append((rx, ry))

    # Simulation Model
    car = KinematicModel()
    car.init_state(init_pos)
    pf = ParticleFilter((car.x, car.y, car.yaw), psize=psize)

    while (True):
        ###################################
        # Simulate Control
        ###################################
        u = (car.v, car.w, car.dt)
        # Add Control Noise
        car.v += np.random.randn() * 0.00002 * u[0]**2 + 0.00002 * u[1]**2
        car.w += np.random.randn() * 0.00002 * u[0]**2 + 0.00002 * u[1]**2
        car.update()
        print("\rState: " + car.state_str() + "| Neff:" + str(pf.Neff),
              end="\t")

        ###################################
        # Simulate Observation
        ###################################
        r = np.array(landmarks) - np.array((car.x, car.y))
        dist = np.hypot(r[:, 0], r[:, 1])
        detect_ids = np.where(dist < detect_dist)[0]
        detect_lms = np.array(landmarks)[detect_ids]
        obs = []
        for i in range(detect_lms.shape[0]):
            lm = detect_lms[i]
            r = np.sqrt((car.x - lm[0])**2 + (car.y - lm[1])**2)
            phi = np.rad2deg(np.arctan2(lm[1] - car.y,
                                        lm[0] - car.x)) - car.yaw
            # Add Observaation Noise
            r = r + np.random.randn() * R_sim[0, 0]**0.5
            phi = phi + np.random.randn() * R_sim[1, 1]**0.5
            # Normalize Angle
            phi = phi % 360
            if phi > 180:
                phi -= 360
            obs.append((r, phi))

        ###################################
        # SLAM Algorithm
        ###################################
        pf.prediction(u)
        pf.update_obs(obs, detect_ids)
        pf.resample()

        ###################################
        # Render Canvas
        ###################################
        img_ = img.copy()
        # Draw Landmark
        for lm in landmarks:
            cv2.circle(img_, lm, 3, (0.2, 0.5, 0.2), 1)
        for i in range(detect_lms.shape[0]):
            lm = detect_lms[i]
            cv2.line(img_, (int(car.x), int(car.y)), (int(lm[0]), int(lm[1])),
                     (0, 1, 0), 1)
        # Draw Trajectory
        start_cut = 100
        for j in range(psize):
            path_size = len(pf.particles[j].path)
            start = 0 if path_size < start_cut else path_size - start_cut
            for i in range(start, path_size - 1):
                cv2.line(img_, (int(pf.particles[j].path[i][0]),
                                int(pf.particles[j].path[i][1])),
                         (int(pf.particles[j].path[i + 1][0]),
                          int(pf.particles[j].path[i + 1][1])), (1, 0.7, 0.7),
                         1)
        # Draw Best Trajectory
        start_cut = 1000
        bid = np.argmax(np.array(pf.weights))
        path_size = len(pf.particles[bid].path)
        start = 0 if path_size < start_cut else path_size - start_cut
        for i in range(start, path_size - 1):
            cv2.line(img_, (int(pf.particles[bid].path[i][0]),
                            int(pf.particles[bid].path[i][1])),
                     (int(pf.particles[bid].path[i + 1][0]),
                      int(pf.particles[bid].path[i + 1][1])), (1, 0, 0), 1)
        # Draw Particle
        for j in range(psize):
            cv2.circle(
                img_,
                (int(pf.particles[j].pos[0]), int(pf.particles[j].pos[1])), 2,
                (1, 0.0, 0.0), 1)
        cv2.circle(img_, (int(car.x), int(car.y)), detect_dist, (0, 1, 0), 1)
        # Render Car
        img_ = car.render(img_)
        img_ = cv2.flip(img_, 0)
        cv2.imshow(window_name, img_)

        ###################################
        # Keyboard
        ###################################
        k = cv2.waitKey(1)
        if k == ord("w"):
            car.v += 4
        elif k == ord("s"):
            car.v += -4
        if k == ord("a"):
            car.w += 5
        elif k == ord("d"):
            car.w += -5
        elif k == ord("r"):
            car.init_state(init_pos)
            pf.init_pf(init_pos)
            print("Reset!!")
        if k == 27:
            print()
            break
예제 #5
0
def main():
    window_name = "EKF SLAM Demo"
    cv2.namedWindow(window_name)
    img = np.ones((500, 500, 3))
    init_pos = (100, 200, 0)
    landmarks = [(100, 80), (400, 100), (300, 450)]
    # Simulation Model
    car = KinematicModel()
    car.init_state(init_pos)

    #
    slam = EkfSLAM()
    slam.init_pos(init_pos)

    pos_now = init_pos
    path = [pos_now]
    while (True):
        car.update()
        img_ = img.copy()
        print("\rState: " + car.state_str(), end="\t")

        # Simulate Observation
        obs = []
        for lm in landmarks:
            r = np.sqrt((car.x - lm[0])**2 + (car.y - lm[1])**2)
            phi = np.rad2deg(np.arctan2(lm[1] - car.y,
                                        lm[0] - car.x)) - car.yaw
            obs.append((r, phi))

        slam.ekf_prediction(car.v, car.w, car.dt)
        print(slam.x[0:3])

        # Draw Landmark
        for lm in landmarks:
            cv2.circle(img_, lm, 3, (0.2, 0.2, 0.8), 2)
            cv2.line(img_, (int(car.x), int(car.y)), lm, (0, 1, 0), 1)
        # Draw Predict Path
        for i in range(len(path) - 1):
            cv2.line(img_, (int(path[i][0]), int(path[i][1])),
                     (int(path[i + 1][0]), int(path[i + 1][1])), (1, 0.5, 0.5),
                     1)

        img_ = car.render(img_)
        img_ = cv2.flip(img_, 0)
        cv2.imshow(window_name, img_)

        # Noise
        x_noise = np.random.randn() * Q_sim[0, 0]**0.5
        y_noise = np.random.randn() * Q_sim[1, 1]**0.5
        yaw_noise = np.random.randn() * Q_sim[2, 2]**0.5
        # Keyboard
        k = cv2.waitKey(1)
        car.x += x_noise
        car.y += y_noise
        car.yaw += yaw_noise
        if k == ord("w"):
            car.v += 4
        elif k == ord("s"):
            car.v += -4
        if k == ord("a"):
            car.w += 5
        elif k == ord("d"):
            car.w += -5
        elif k == ord("r"):
            car.init_state(init_pos)
            nav_pos = None
            path = None
            print("Reset!!")
        if k == 27:
            print()
            break
        pos_now = velocity_motion_model(pos_now, car.v, car.w, car.dt)
        path.append(pos_now)
예제 #6
0
    # Path
    path = path_generator.path2()
    img_path = np.ones((600, 600, 3))
    for i in range(path.shape[0] - 1):
        cv2.line(img_path, (int(path[i, 0]), int(path[i, 1])),
                 (int(path[i + 1, 0]), int(path[i + 1, 1])), (1.0, 0.5, 0.5),
                 1)

    # Initialize Car
    car = KinematicModel()
    car.init_state((50, 300, 0))
    controller = PurePursuitControl()
    controller.set_path(path)

    while (True):
        print("\rState: " + car.state_str() + "\t")

        # ================= Control Algorithm =================
        # PID Longitude Control
        end_dist = np.hypot(path[-1, 0] - car.x, path[-1, 1] - car.y)
        target_v = 40 if end_dist > 265 else 0
        next_a = 0.1 * (target_v - car.v)

        # Pure Pursuit Lateral Control
        state = {"x": car.x, "y": car.y, "yaw": car.yaw, "v": car.v}
        next_delta = controller.feedback(target)
        car.control(next_a, next_delta)
        # =====================================================

        # Update & Render
        car.update()