|
| 1 | +# ---------------------------------------------------------------------------- |
| 2 | +# SymForce - Copyright 2025, Skydio, Inc. |
| 3 | +# This source code is under the Apache 2.0 license found in the LICENSE file. |
| 4 | +# ---------------------------------------------------------------------------- |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +import symforce |
| 11 | + |
| 12 | +symforce.set_epsilon_to_number(float(10 * np.finfo(np.float32).eps)) |
| 13 | +import torch |
| 14 | + |
| 15 | +import symforce.symbolic as sf |
| 16 | +from symforce import typing as T |
| 17 | +from symforce.experimental.caspar import CasparLibrary |
| 18 | +from symforce.experimental.caspar import memory as mem |
| 19 | + |
| 20 | +""" |
| 21 | +We need unique classes to distinguish between values and expectations |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +class Landmark(sf.V2): ... |
| 26 | + |
| 27 | + |
| 28 | +class Odometry(sf.V3): ... |
| 29 | + |
| 30 | + |
| 31 | +class Lidar(sf.V2): ... |
| 32 | + |
| 33 | + |
| 34 | +class Gnss(sf.V2): ... |
| 35 | + |
| 36 | + |
| 37 | +caslib = CasparLibrary() |
| 38 | + |
| 39 | + |
| 40 | +@caslib.add_kernel |
| 41 | +def make_poses( |
| 42 | + angle: T.Annotated[sf.Symbol, mem.ReadSequential], |
| 43 | +) -> T.Annotated[sf.Pose2, mem.WriteSequential]: |
| 44 | + x = sf.cos(angle) * 10 |
| 45 | + y = sf.sin(angle) * 10 |
| 46 | + |
| 47 | + return sf.Pose2.from_tangent([angle, x, y]) |
| 48 | + |
| 49 | + |
| 50 | +@caslib.add_kernel |
| 51 | +def get_odometry( |
| 52 | + pose_k: T.Annotated[sf.Pose2, mem.ReadIndexed], |
| 53 | + pose_kp1: T.Annotated[sf.Pose2, mem.ReadIndexed], |
| 54 | +) -> T.Annotated[Odometry, mem.WriteSequential]: |
| 55 | + return Odometry(pose_k.local_coordinates(pose_kp1)) |
| 56 | + |
| 57 | + |
| 58 | +@caslib.add_kernel |
| 59 | +def get_lidar( |
| 60 | + pose: T.Annotated[sf.Pose2, mem.ReadShared], |
| 61 | + landmark: T.Annotated[Landmark, mem.ReadShared], |
| 62 | +) -> T.Annotated[Lidar, mem.WriteSequential]: |
| 63 | + landmark_body = pose.inverse() * landmark |
| 64 | + return Lidar(landmark_body) |
| 65 | + |
| 66 | + |
| 67 | +@caslib.add_kernel |
| 68 | +def get_gnss( |
| 69 | + pose: T.Annotated[sf.Pose2, mem.ReadIndexed], |
| 70 | +) -> T.Annotated[Gnss, mem.WriteSequential]: |
| 71 | + return Gnss(pose.t) |
| 72 | + |
| 73 | + |
| 74 | +@caslib.add_factor |
| 75 | +def fac_lidar( |
| 76 | + pose: T.Annotated[sf.Pose2, mem.Tunable], |
| 77 | + landmark: T.Annotated[Landmark, mem.Tunable], |
| 78 | + observed_lidar: T.Annotated[Lidar, mem.Constant], |
| 79 | +) -> sf.V2: |
| 80 | + return pose.inverse() * landmark - observed_lidar |
| 81 | + |
| 82 | + |
| 83 | +@caslib.add_factor |
| 84 | +def fac_position( |
| 85 | + pose: T.Annotated[sf.Pose2, mem.Tunable], |
| 86 | + observed_gnss: T.Annotated[Lidar, mem.Constant], |
| 87 | +) -> sf.V2: |
| 88 | + return pose.t - observed_gnss |
| 89 | + |
| 90 | + |
| 91 | +@caslib.add_factor |
| 92 | +def fac_odometry( |
| 93 | + pose_k: T.Annotated[sf.Pose2, mem.Tunable], |
| 94 | + pose_kp1: T.Annotated[sf.Pose2, mem.Tunable], |
| 95 | + observed_odom: T.Annotated[Odometry, mem.Constant], |
| 96 | +) -> sf.V3: |
| 97 | + return sf.V3(pose_k.local_coordinates(pose_kp1)) - observed_odom |
| 98 | + |
| 99 | + |
| 100 | +out_dir = Path(__file__).resolve().parent / "generated" |
| 101 | +caslib.generate(out_dir) |
| 102 | +caslib.compile(out_dir) |
| 103 | + |
| 104 | + |
| 105 | +# Can also be imported using: lib = caslib.import_lib(out_dir) |
| 106 | +from generated import caspar_lib as lib # type: ignore[import-not-found] |
| 107 | + |
| 108 | +torch.set_default_device("cuda") |
| 109 | +N_POSE = 1024 |
| 110 | +N_LANDMARK = 300 |
| 111 | +INTERVAL_GNSS = 1 |
| 112 | +MATCH_PER_LIDAR = 8 |
| 113 | + |
| 114 | +arange_pose = torch.arange(N_POSE, dtype=torch.int32) |
| 115 | + |
| 116 | +angles = torch.linspace(0, 2 * np.pi, N_POSE, device="cuda")[None, :] |
| 117 | +pose_caspar = torch.empty(mem.caspar_size(sf.Pose2), N_POSE) |
| 118 | +lib.make_poses(angles, pose_caspar, N_POSE) |
| 119 | + |
| 120 | +landmarks_caspar = (torch.rand(mem.caspar_size(Landmark), N_LANDMARK) - 0.5) * 10 |
| 121 | + |
| 122 | +odometry_caspar = torch.empty(mem.caspar_size(Odometry), N_POSE - 1) |
| 123 | +odom_k_indices = arange_pose[:-1] |
| 124 | +odom_kp1_indices = arange_pose[1:] |
| 125 | +lib.get_odometry( |
| 126 | + pose_caspar, odom_k_indices, pose_caspar, odom_kp1_indices, odometry_caspar, N_POSE - 1 |
| 127 | +) |
| 128 | + |
| 129 | +gnss_caspar = torch.empty(mem.caspar_size(Gnss), N_POSE // INTERVAL_GNSS) |
| 130 | +gnss_indices = torch.arange(0, N_POSE, INTERVAL_GNSS, dtype=torch.int32) |
| 131 | +lib.get_gnss(pose_caspar, gnss_indices, gnss_caspar, N_POSE // INTERVAL_GNSS) |
| 132 | + |
| 133 | + |
| 134 | +lidar_caspar = torch.empty(mem.caspar_size(Lidar), N_POSE * MATCH_PER_LIDAR) |
| 135 | +lidar_pose_indices = arange_pose.repeat_interleave(MATCH_PER_LIDAR) |
| 136 | +lidar_pose_indices_shared = torch.empty(N_POSE * MATCH_PER_LIDAR, 2, dtype=torch.int32) |
| 137 | +lib.shared_indices(lidar_pose_indices, lidar_pose_indices_shared) |
| 138 | + |
| 139 | +_indices_tmp = torch.arange(MATCH_PER_LIDAR)[None:] + torch.arange(N_POSE)[:, None] |
| 140 | +lidar_landmark_indices = _indices_tmp.to(torch.int32).ravel() % N_LANDMARK |
| 141 | +lidar_landmark_indices_shared = torch.empty(N_POSE * MATCH_PER_LIDAR, 2, dtype=torch.int32) |
| 142 | +lib.shared_indices(lidar_landmark_indices, lidar_landmark_indices_shared) |
| 143 | +lib.get_lidar( |
| 144 | + pose_caspar, |
| 145 | + lidar_pose_indices_shared, |
| 146 | + landmarks_caspar, |
| 147 | + lidar_landmark_indices_shared, |
| 148 | + lidar_caspar, |
| 149 | + N_POSE * MATCH_PER_LIDAR, |
| 150 | +) |
| 151 | + |
| 152 | +pose_stacked = torch.empty(N_POSE, mem.stacked_size(sf.Pose2)) |
| 153 | +landmarks_stacked = torch.empty(N_LANDMARK, mem.stacked_size(Landmark)) |
| 154 | +odometry_stacked = torch.empty(N_POSE - 1, mem.stacked_size(Odometry)) |
| 155 | +gnss_stacked = torch.empty(N_POSE // INTERVAL_GNSS, mem.stacked_size(Gnss)) |
| 156 | +lidar_stacked = torch.empty(N_POSE * MATCH_PER_LIDAR, mem.stacked_size(Lidar)) |
| 157 | +lib.Pose2_caspar_to_stacked(pose_caspar, pose_stacked) |
| 158 | +lib.Landmark_caspar_to_stacked(landmarks_caspar, landmarks_stacked) |
| 159 | +lib.Odometry_caspar_to_stacked(odometry_caspar, odometry_stacked) |
| 160 | +lib.Gnss_caspar_to_stacked(gnss_caspar, gnss_stacked) |
| 161 | +lib.Lidar_caspar_to_stacked(lidar_caspar, lidar_stacked) |
| 162 | + |
| 163 | + |
| 164 | +params = lib.SolverParams() |
| 165 | +params.diag_init = 1.0 |
| 166 | +params.solver_iter_max = 200 |
| 167 | +params.pcg_iter_max = 50 |
| 168 | +params.pcg_rel_error_exit = 1e-6 |
| 169 | + |
| 170 | +solver = lib.GraphSolver( |
| 171 | + params, |
| 172 | + Pose2_num_max=N_POSE, |
| 173 | + Landmark_num_max=N_LANDMARK, |
| 174 | + fac_lidar_num_max=N_POSE * MATCH_PER_LIDAR, |
| 175 | + fac_position_num_max=N_POSE // INTERVAL_GNSS, |
| 176 | + fac_odometry_num_max=N_POSE - 1, |
| 177 | +) |
| 178 | + |
| 179 | +pose_stacked_noisy = pose_stacked.clone() |
| 180 | +pose_stacked_noisy[:, 2:4] += torch.randn_like(pose_stacked_noisy[:, 2:4]) |
| 181 | +landmarks_stacked_noisy = landmarks_stacked + torch.randn_like(landmarks_stacked) |
| 182 | +solver.set_Pose2_nodes_from_stacked_device(pose_stacked_noisy) |
| 183 | +solver.set_Landmark_nodes_from_stacked_device(landmarks_stacked_noisy) |
| 184 | + |
| 185 | +solver.set_fac_lidar_observed_lidar_data_from_stacked_device(lidar_stacked) |
| 186 | +solver.set_fac_lidar_pose_indices_from_device(lidar_pose_indices) |
| 187 | +solver.set_fac_lidar_landmark_indices_from_device(lidar_landmark_indices) |
| 188 | + |
| 189 | +solver.set_fac_position_observed_gnss_data_from_stacked_device(gnss_stacked) |
| 190 | +solver.set_fac_position_pose_indices_from_device(gnss_indices) |
| 191 | + |
| 192 | +solver.set_fac_odometry_observed_odom_data_from_stacked_device(odometry_stacked) |
| 193 | +solver.set_fac_odometry_pose_k_indices_from_device(odom_k_indices) |
| 194 | +solver.set_fac_odometry_pose_kp1_indices_from_device(odom_kp1_indices) |
| 195 | + |
| 196 | +solver.finish_indices() |
| 197 | +solver.solve(print_progress=True) |
0 commit comments