def test_road_odometry(self): RoadOdometry() rg = make_two_lane_road() lane_0 = rg.junction(0).segment(0).lane(0) lane_1 = rg.junction(0).segment(0).lane(1) lane_pos = LanePosition(s=1., r=2., h=3.) road_pos = RoadPosition(lane=lane_0, pos=lane_pos) w = [5., 7., 9.] v = [11., 13., 15.] frame_velocity = FrameVelocity(SpatialVelocity(w=w, v=v)) road_odom = RoadOdometry( road_position=road_pos, frame_velocity=frame_velocity) self.assertEqual(road_odom.lane.id().string(), lane_0.id().string()) self.assertTrue(np.allclose(road_odom.pos.srh(), lane_pos.srh())) self.assertTrue(np.allclose( frame_velocity.get_velocity().translational(), road_odom.vel.get_velocity().translational())) lane_pos_new = LanePosition(s=10., r=20., h=30.) v_new = [42., 43., 44.] frame_velocity_new = FrameVelocity(SpatialVelocity(w=w, v=v_new)) road_odom.lane = lane_1 road_odom.pos = lane_pos_new road_odom.vel = frame_velocity_new self.assertEqual(road_odom.lane.id().string(), lane_1.id().string()) self.assertTrue(np.allclose(road_odom.pos.srh(), lane_pos_new.srh())) self.assertTrue(np.allclose( frame_velocity_new.get_velocity().translational(), road_odom.vel.get_velocity().translational()))
def test_idm_controller(self): rg = make_two_lane_road() idm = IdmController( road=rg, path_or_branches=ScanStrategy.kPath, road_position_strategy=RoadPositionStrategy.kExhaustiveSearch, period_sec=0.) context = idm.CreateDefaultContext() output = idm.AllocateOutput() # Fix the inputs. pose_vector1 = PoseVector() pose_vector1.set_translation([1., 2., 3.]) ego_pose_index = idm.ego_pose_input().get_index() context.FixInputPort(ego_pose_index, pose_vector1) w = [0., 0., 0.] v = [1., 0., 0.] frame_velocity1 = FrameVelocity(SpatialVelocity(w=w, v=v)) ego_velocity_index = idm.ego_velocity_input().get_index() context.FixInputPort(ego_velocity_index, frame_velocity1) pose_vector2 = PoseVector() pose_vector2.set_translation([6., 0., 0.]) bundle = PoseBundle(num_poses=1) bundle.set_pose( 0, Isometry3(Quaternion(), pose_vector2.get_translation())) traffic_index = idm.traffic_input().get_index() context.FixInputPort(traffic_index, framework.AbstractValue.Make(bundle)) # Verify the inputs. pose_vector_eval = idm.EvalVectorInput(context, 0) self.assertTrue( np.allclose(pose_vector1.get_translation(), pose_vector_eval.get_translation())) frame_velocity_eval = idm.EvalVectorInput(context, 1) self.assertTrue( np.allclose(frame_velocity1.get_velocity().translational(), frame_velocity_eval.get_velocity().translational())) self.assertTrue( np.allclose(frame_velocity1.get_velocity().rotational(), frame_velocity_eval.get_velocity().rotational())) # Verify the outputs. idm.CalcOutput(context, output) accel_command_index = idm.acceleration_output().get_index() accel = output.get_vector_data(accel_command_index) self.assertEqual(len(accel.get_value()), 1) self.assertTrue(accel.get_value() < 0.)
def test_idm_controller(self): rg = make_two_lane_road() idm = IdmController( road=rg, path_or_branches=ScanStrategy.kPath, road_position_strategy=RoadPositionStrategy.kExhaustiveSearch, period_sec=0.) context = idm.CreateDefaultContext() output = idm.AllocateOutput() # Fix the inputs. pose_vector1 = PoseVector() pose_vector1.set_translation([1., 2., 3.]) ego_pose_index = idm.ego_pose_input().get_index() context.FixInputPort(ego_pose_index, pose_vector1) w = [0., 0., 0.] v = [1., 0., 0.] frame_velocity1 = FrameVelocity(SpatialVelocity(w=w, v=v)) ego_velocity_index = idm.ego_velocity_input().get_index() context.FixInputPort(ego_velocity_index, frame_velocity1) pose_vector2 = PoseVector() pose_vector2.set_translation([6., 0., 0.]) bundle = PoseBundle(num_poses=1) bundle.set_pose( 0, Isometry3(Quaternion(), pose_vector2.get_translation())) traffic_index = idm.traffic_input().get_index() context.FixInputPort( traffic_index, framework.AbstractValue.Make(bundle)) # Verify the inputs. pose_vector_eval = idm.EvalVectorInput(context, 0) self.assertTrue(np.allclose(pose_vector1.get_translation(), pose_vector_eval.get_translation())) frame_velocity_eval = idm.EvalVectorInput(context, 1) self.assertTrue(np.allclose( frame_velocity1.get_velocity().translational(), frame_velocity_eval.get_velocity().translational())) self.assertTrue(np.allclose( frame_velocity1.get_velocity().rotational(), frame_velocity_eval.get_velocity().rotational())) # Verify the outputs. idm.CalcOutput(context, output) accel_command_index = idm.acceleration_output().get_index() accel = output.get_vector_data(accel_command_index) self.assertEqual(len(accel.get_value()), 1) self.assertTrue(accel.get_value() < 0.)
def test_pose_bundle(self): num_poses = 7 bundle = PoseBundle(num_poses) # - Accessors. self.assertEqual(bundle.get_num_poses(), num_poses) self.assertTrue(isinstance(bundle.get_transform(0), RigidTransform)) self.assertTrue(isinstance(bundle.get_velocity(0), FrameVelocity)) # - Mutators. kIndex = 5 p = [0, 1, 2] q = Quaternion(wxyz=normalized([0.1, 0.3, 0.7, 0.9])) pose = RigidTransform(quaternion=q, p=p) bundle.set_transform(kIndex, pose) self.assertTrue((bundle.get_transform(kIndex).GetAsMatrix34() == pose.GetAsMatrix34()).all()) w = [0.1, 0.3, 0.5] v = [0., 1., 2.] frame_velocity = FrameVelocity(SpatialVelocity(w=w, v=v)) bundle.set_velocity(kIndex, frame_velocity) vel_actual = bundle.get_velocity(kIndex).get_velocity() self.assertTrue(np.allclose(vel_actual.rotational(), w)) self.assertTrue(np.allclose(vel_actual.translational(), v)) name = "Alice" bundle.set_name(kIndex, name) self.assertEqual(bundle.get_name(kIndex), name) instance_id = 42 # Supply a random instance id. bundle.set_model_instance_id(kIndex, instance_id) self.assertEqual(bundle.get_model_instance_id(kIndex), instance_id)
def test_road_odometry(self): RoadOdometry() rg = make_two_lane_road() lane_0 = rg.junction(0).segment(0).lane(0) lane_1 = rg.junction(0).segment(0).lane(1) lane_pos = LanePosition(s=1., r=2., h=3.) road_pos = RoadPosition(lane=lane_0, pos=lane_pos) w = [5., 7., 9.] v = [11., 13., 15.] frame_velocity = FrameVelocity(SpatialVelocity(w=w, v=v)) road_odom = RoadOdometry( road_position=road_pos, frame_velocity=frame_velocity) self.assertEqual(road_odom.lane.id().string(), lane_0.id().string()) self.assertTrue(np.allclose(road_odom.pos.srh(), lane_pos.srh())) self.assertTrue(np.allclose( frame_velocity.get_velocity().translational(), road_odom.vel.get_velocity().translational())) lane_pos_new = LanePosition(s=10., r=20., h=30.) v_new = [42., 43., 44.] frame_velocity_new = FrameVelocity(SpatialVelocity(w=w, v=v_new)) road_odom.lane = lane_1 road_odom.pos = lane_pos_new road_odom.vel = frame_velocity_new self.assertEqual(road_odom.lane.id().string(), lane_1.id().string()) self.assertTrue(np.allclose(road_odom.pos.srh(), lane_pos_new.srh())) self.assertTrue(np.allclose( frame_velocity_new.get_velocity().translational(), road_odom.vel.get_velocity().translational()))
def test_pose_selector(self): kScanDistance = 4. rg = make_two_lane_road() lane = rg.junction(0).segment(0).lane(0) pose0 = PoseVector() pose0.set_translation([1., 0., 0.]) pose1 = PoseVector() # N.B. Set pose1 3 meters ahead of pose0. pose1.set_translation([4., 0., 0.]) bundle = PoseBundle(num_poses=2) bundle.set_pose(0, Isometry3(Quaternion(), pose0.get_translation())) bundle.set_pose(1, Isometry3(Quaternion(), pose1.get_translation())) closest_pose = PoseSelector.FindSingleClosestPose( lane=lane, ego_pose=pose0, traffic_poses=bundle, scan_distance=kScanDistance, side=AheadOrBehind.kAhead, path_or_branches=ScanStrategy.kPath) self.assertEqual(closest_pose.odometry.lane.id().string(), lane.id().string()) self.assertTrue(closest_pose.distance == 3.) closest_pair = PoseSelector.FindClosestPair( lane=lane, ego_pose=pose0, traffic_poses=bundle, scan_distance=kScanDistance, path_or_branches=ScanStrategy.kPath) self.assertEqual( closest_pair[AheadOrBehind.kAhead].odometry.lane.id().string(), lane.id().string()) self.assertEqual(closest_pair[AheadOrBehind.kAhead].distance, 3.) self.assertEqual( closest_pair[AheadOrBehind.kBehind].odometry.lane.id().string(), lane.id().string()) self.assertEqual(closest_pair[AheadOrBehind.kBehind].distance, float('inf')) lane_pos = LanePosition(s=1., r=0., h=0.) road_pos = RoadPosition(lane=lane, pos=lane_pos) w = [1., 2., 3.] v = [-4., -5., -6.] frame_velocity = FrameVelocity(SpatialVelocity(w=w, v=v)) road_odom = RoadOdometry(road_position=road_pos, frame_velocity=frame_velocity) sigma_v = PoseSelector.GetSigmaVelocity(road_odom) self.assertEqual(sigma_v, v[0])
def test_closest_pose(self): ClosestPose() rg = make_two_lane_road() lane_0 = rg.junction(0).segment(0).lane(0) lane_1 = rg.junction(0).segment(0).lane(1) lane_pos = LanePosition(s=1., r=2., h=3.) road_pos = RoadPosition(lane=lane_0, pos=lane_pos) w = [5., 7., 9.] v = [11., 13., 15.] frame_velocity = FrameVelocity(SpatialVelocity(w=w, v=v)) road_odom = RoadOdometry( road_position=road_pos, frame_velocity=frame_velocity) closest_pose = ClosestPose(odom=road_odom, dist=12.7) self.assertEqual(closest_pose.odometry.lane.id().string(), lane_0.id().string()) self.assertEqual(closest_pose.distance, 12.7) closest_pose.odometry.lane = lane_1 closest_pose.distance = 5.9 self.assertEqual(closest_pose.odometry.lane.id().string(), lane_1.id().string()) self.assertEqual(closest_pose.distance, 5.9)
def test_frame_velocity(self): frame_velocity = FrameVelocity() self.assertTrue(isinstance(frame_velocity, BasicVector)) self.assertTrue(isinstance(copy.copy(frame_velocity), FrameVelocity)) self.assertTrue(isinstance(frame_velocity.Clone(), FrameVelocity)) self.assertEqual(frame_velocity.size(), FrameVelocity.kSize) # - Accessors. self.assertTrue( isinstance(frame_velocity.get_velocity(), SpatialVelocity)) # - Value. self.assertTrue( np.allclose(frame_velocity.get_velocity().rotational(), 3 * [0.])) self.assertTrue( np.allclose(frame_velocity.get_velocity().translational(), 3 * [0.])) # - Mutators. w = [0.1, 0.3, 0.5] v = [0., 1., 2.] frame_velocity.set_velocity(SpatialVelocity(w=w, v=v)) self.assertTrue( np.allclose(frame_velocity.get_velocity().rotational(), w)) self.assertTrue( np.allclose(frame_velocity.get_velocity().translational(), v)) # - Ensure ordering is ((wx, wy, wz), (vx, vy, vz)) velocity_expected = np.hstack((w, v)) velocity_actual = frame_velocity.get_value() self.assertTrue(np.allclose(velocity_actual, velocity_expected)) # - Fully-parameterized constructor. frame_velocity1 = FrameVelocity(SpatialVelocity(w=w, v=v)) self.assertTrue( np.allclose(frame_velocity1.get_velocity().rotational(), w)) self.assertTrue( np.allclose(frame_velocity1.get_velocity().translational(), v))
def test_pose_aggregator(self): aggregator = PoseAggregator() # - Set-up. instance_id1 = 5 # Supply a random instance id. port1 = aggregator.AddSingleInput("pose_only", instance_id1) self.assertEqual(port1.get_data_type(), PortDataType.kVectorValued) self.assertEqual(port1.size(), PoseVector.kSize) instance_id2 = 42 # Supply another random, but unique, id. ports2 = aggregator.AddSinglePoseAndVelocityInput( "pose_and_velocity", instance_id2) self.assertEqual(ports2.pose_input_port.get_data_type(), PortDataType.kVectorValued) self.assertEqual(ports2.pose_input_port.size(), PoseVector.kSize) self.assertEqual(ports2.velocity_input_port.get_data_type(), PortDataType.kVectorValued) self.assertEqual(ports2.velocity_input_port.size(), FrameVelocity.kSize) num_poses = 1 port3 = aggregator.AddBundleInput("pose_bundle", num_poses) self.assertEqual(port3.get_data_type(), PortDataType.kAbstractValued) # - CalcOutput. self.assertEqual( aggregator.get_output_port(0).get_data_type(), PortDataType.kAbstractValued) context = aggregator.CreateDefaultContext() output = aggregator.AllocateOutput() p1 = [0, 1, 2] pose1 = PoseVector() pose1.set_translation(p1) p2 = [5, 7, 9] pose2 = PoseVector() pose2.set_translation(p2) w = [0.3, 0.4, 0.5] v = [0.5, 0.6, 0.7] velocity = FrameVelocity() velocity.set_velocity(SpatialVelocity(w=w, v=v)) p3 = [50, 70, 90] q3 = Quaternion(wxyz=normalized([0.1, 0.3, 0.7, 0.9])) bundle = PoseBundle(num_poses) bundle.set_transform(0, RigidTransform(quaternion=q3, p=p3)) bundle_value = AbstractValue.Make(bundle) aggregator.get_input_port(0).FixValue(context, pose1) aggregator.get_input_port(1).FixValue(context, pose2) aggregator.get_input_port(2).FixValue(context, velocity) aggregator.get_input_port(3).FixValue(context, bundle_value) aggregator.CalcOutput(context, output) value = output.get_data(0).get_value() self.assertEqual(value.get_num_poses(), 3) pose1_actual = RigidTransform(p=p1) self.assertTrue((value.get_transform(0).GetAsMatrix34() == pose1_actual.GetAsMatrix34()).all()) pose2_actual = RigidTransform(p=p2) self.assertTrue((value.get_transform(1).GetAsMatrix34() == pose2_actual.GetAsMatrix34()).all()) vel_actual = value.get_velocity(1).get_velocity() self.assertTrue(np.allclose(vel_actual.rotational(), w)) self.assertTrue(np.allclose(vel_actual.translational(), v)) pose3_actual = RigidTransform(quaternion=q3, p=p3) self.assertTrue((value.get_transform(2).GetAsMatrix34() == pose3_actual.GetAsMatrix34()).all())
def test_frame_velocity(self): frame_velocity = FrameVelocity() self.assertTrue(isinstance(frame_velocity, BasicVector)) self.assertTrue(isinstance(copy.copy(frame_velocity), FrameVelocity)) self.assertTrue(isinstance(frame_velocity.Clone(), FrameVelocity)) self.assertEqual(frame_velocity.size(), FrameVelocity.kSize) # - Accessors. self.assertTrue(isinstance( frame_velocity.get_velocity(), SpatialVelocity)) # - Value. self.assertTrue(np.allclose( frame_velocity.get_velocity().rotational(), 3 * [0.])) self.assertTrue(np.allclose( frame_velocity.get_velocity().translational(), 3 * [0.])) # - Mutators. w = [0.1, 0.3, 0.5] v = [0., 1., 2.] frame_velocity.set_velocity(SpatialVelocity(w=w, v=v)) self.assertTrue(np.allclose( frame_velocity.get_velocity().rotational(), w)) self.assertTrue(np.allclose( frame_velocity.get_velocity().translational(), v)) # - Ensure ordering is ((wx, wy, wz), (vx, vy, vz)) velocity_expected = np.hstack((w, v)) velocity_actual = frame_velocity.get_value() self.assertTrue(np.allclose(velocity_actual, velocity_expected)) # - Fully-parameterized constructor. frame_velocity1 = FrameVelocity(SpatialVelocity(w=w, v=v)) self.assertTrue(np.allclose( frame_velocity1.get_velocity().rotational(), w)) self.assertTrue(np.allclose( frame_velocity1.get_velocity().translational(), v))
def test_pose_aggregator(self): aggregator = PoseAggregator() # - Set-up. instance_id1 = 5 # Supply a random instance id. port1 = aggregator.AddSingleInput("pose_only", instance_id1) self.assertEqual(port1.get_data_type(), PortDataType.kVectorValued) self.assertEqual(port1.size(), PoseVector.kSize) instance_id2 = 42 # Supply another random, but unique, id. ports2 = aggregator.AddSinglePoseAndVelocityInput( "pose_and_velocity", instance_id2) self.assertEqual(ports2.pose_input_port.get_data_type(), PortDataType.kVectorValued) self.assertEqual(ports2.pose_input_port.size(), PoseVector.kSize) self.assertEqual(ports2.velocity_input_port.get_data_type(), PortDataType.kVectorValued) self.assertEqual(ports2.velocity_input_port.size(), FrameVelocity.kSize) num_poses = 1 port3 = aggregator.AddBundleInput("pose_bundle", num_poses) self.assertEqual(port3.get_data_type(), PortDataType.kAbstractValued) # - CalcOutput. self.assertEqual(aggregator.get_output_port(0).get_data_type(), PortDataType.kAbstractValued) context = aggregator.CreateDefaultContext() output = aggregator.AllocateOutput() p1 = [0, 1, 2] pose1 = PoseVector() pose1.set_translation(p1) p2 = [5, 7, 9] pose2 = PoseVector() pose2.set_translation(p2) w = [0.3, 0.4, 0.5] v = [0.5, 0.6, 0.7] velocity = FrameVelocity() velocity.set_velocity(SpatialVelocity(w=w, v=v)) p3 = [50, 70, 90] q3 = Quaternion(wxyz=normalized([0.1, 0.3, 0.7, 0.9])) bundle = PoseBundle(num_poses) bundle.set_pose(0, Isometry3(q3, p3)) bundle_value = AbstractValue.Make(bundle) context.FixInputPort(0, pose1) context.FixInputPort(1, pose2) context.FixInputPort(2, velocity) context.FixInputPort(3, bundle_value) aggregator.CalcOutput(context, output) value = output.get_data(0).get_value() self.assertEqual(value.get_num_poses(), 3) isom1_actual = Isometry3() isom1_actual.set_translation(p1) self.assertTrue( (value.get_pose(0).matrix() == isom1_actual.matrix()).all()) isom2_actual = Isometry3() isom2_actual.set_translation(p2) self.assertTrue( (value.get_pose(1).matrix() == isom2_actual.matrix()).all()) vel_actual = value.get_velocity(1).get_velocity() self.assertTrue(np.allclose(vel_actual.rotational(), w)) self.assertTrue(np.allclose(vel_actual.translational(), v)) self.assertTrue( (value.get_pose(2).matrix() == Isometry3(q3, p3).matrix()).all())