MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / main

Function main

examples/Protobuf Example/sensor_simulator.py:165–216  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

163
164
165def main() -> None:
166 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
167 period = 1.0 / RATE_HZ
168
169 sys.stdout.write(
170 f"\nProtobuf Sensor Simulator\n"
171 f" target : udp://{HOST}:{PORT}\n"
172 f" rate : {RATE_HZ:.0f} Hz\n"
173 f" mix : Sensor (default) + bare Vec3 every {VEC3_EVERY_N_FRAMES} frames\n"
174 f" stop : Ctrl+C\n\n"
175 )
176 sys.stdout.flush()
177
178 t0 = time.monotonic()
179 counter = 0
180 sensor_count = 0
181 vec3_count = 0
182 next_t = t0
183
184 try:
185 while True:
186 t = time.monotonic() - t0
187 counter += 1
188
189 if counter % VEC3_EVERY_N_FRAMES == 0:
190 frame = make_vec3_frame(t, counter)
191 kind = "Vec3"
192 vec3_count += 1
193 else:
194 frame = make_sensor_frame(t, counter)
195 kind = "Sensor"
196 sensor_count += 1
197
198 sock.sendto(frame, (HOST, PORT))
199
200 # Throttle.
201 next_t += period
202 sleep_for = next_t - time.monotonic()
203 if sleep_for > 0:
204 time.sleep(sleep_for)
205 elif sleep_for < -0.5:
206 next_t = time.monotonic() # we fell badly behind, resync
207
208 # Status line (refreshes in place).
209 sys.stdout.write(
210 render_status(t, counter, sensor_count, vec3_count, kind, len(frame))
211 )
212 sys.stdout.flush()
213
214 finally:
215 sys.stdout.write("\n\nstopped.\n")
216 sys.stdout.flush()
217
218
219if __name__ == "__main__":

Callers 1

Calls 5

make_vec3_frameFunction · 0.85
make_sensor_frameFunction · 0.85
render_statusFunction · 0.85
writeMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected