|
1 | 1 | import numpy as np |
2 | 2 | import threading |
3 | | -import time |
4 | 3 | from catkit2.testbed.service import Service |
5 | 4 | from pipython import GCSDevice, pitools |
6 | 5 |
|
@@ -126,89 +125,19 @@ def _submit_positions(self): |
126 | 125 |
|
127 | 126 | def _target_positions_worker(self): |
128 | 127 | """Worker thread to handle move_to commands from the target_positions data stream.""" |
129 | | - # Benchmarking variables |
130 | | - iteration_count = 0 |
131 | | - benchmark_start = time.perf_counter() |
132 | | - total_mov_time = 0.0 |
133 | | - # total_mutex_wait_time = 0.0 |
134 | | - last_frame_time = None |
135 | | - frame_intervals = [] |
136 | | - |
137 | 128 | while not self.should_shut_down: |
138 | 129 | try: |
139 | 130 | frame = self.target_positions.get_next_frame(wait_time_in_ms=250) |
140 | | - loop_start = time.perf_counter() |
141 | | - |
142 | | - # Track time between frames |
143 | | - if last_frame_time is not None: |
144 | | - frame_intervals.append(loop_start - last_frame_time) |
145 | | - last_frame_time = loop_start |
146 | | - |
147 | 131 | data = frame.data |
148 | | - |
149 | 132 | positions = { |
150 | 133 | self.axis_num_to_name[num]: float(data[idx]) |
151 | 134 | for idx, num in enumerate(sorted(self.axis_map.values())) |
152 | 135 | } |
153 | | - |
154 | | - # Benchmark move_to (includes mutex acquisition and MOV command) |
155 | | - mov_start = time.perf_counter() |
156 | | - self._move_to_internal(positions, benchmark_times=True) |
157 | | - mov_end = time.perf_counter() |
158 | | - total_mov_time += (mov_end - mov_start) |
159 | | - |
160 | | - iteration_count += 1 |
161 | | - |
162 | | - # Log benchmark every 500 iterations |
163 | | - if iteration_count % 500 == 0: |
164 | | - elapsed = time.perf_counter() - benchmark_start |
165 | | - avg_rate = iteration_count / elapsed |
166 | | - avg_mov_time = (total_mov_time / iteration_count) * 1000 # ms |
167 | | - |
168 | | - if frame_intervals: |
169 | | - avg_interval = (sum(frame_intervals) / len(frame_intervals)) * 1000 # ms |
170 | | - min_interval = min(frame_intervals) * 1000 |
171 | | - max_interval = max(frame_intervals) * 1000 |
172 | | - else: |
173 | | - avg_interval = min_interval = max_interval = 0 |
174 | | - |
175 | | - self.log.info( |
176 | | - f'[BENCHMARK] iterations={iteration_count}, ' |
177 | | - f'avg_rate={avg_rate:.1f} Hz, ' |
178 | | - f'avg_MOV={avg_mov_time:.2f}ms, ' |
179 | | - f'frame_interval: avg={avg_interval:.2f}ms min={min_interval:.2f}ms max={max_interval:.2f}ms' |
180 | | - ) |
181 | | - |
182 | | - # Reset for next window |
183 | | - frame_intervals = [] |
184 | | - iteration_count = 0 |
185 | | - benchmark_start = time.perf_counter() |
186 | | - total_mov_time = 0.0 |
187 | | - |
| 136 | + self.move_to(positions) |
188 | 137 | self.sleep(0) |
189 | 138 | except RuntimeError: |
190 | 139 | pass |
191 | 140 |
|
192 | | - def _move_to_internal(self, positions, benchmark_times=False): |
193 | | - """ |
194 | | - Internal move_to used by worker thread with optional benchmarking. |
195 | | - Bypasses initialization check since worker only runs after init. |
196 | | - """ |
197 | | - axis_positions = {} |
198 | | - for name, value in positions.items(): |
199 | | - if name in self.axis_map: |
200 | | - axis_num = self.axis_map[name] |
201 | | - axis_positions[axis_num] = float(value) |
202 | | - |
203 | | - if not axis_positions: |
204 | | - return |
205 | | - |
206 | | - with self.mutex: |
207 | | - self.pidevice.MOV(axis_positions) |
208 | | - |
209 | | - self.is_moving = True |
210 | | - self.move_event.set() |
211 | | - |
212 | 141 | def main(self): |
213 | 142 | """Main loop - polls for move completion and updates telemetry when motion stops.""" |
214 | 143 | while not self.should_shut_down: |
|
0 commit comments