Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for External Clock #4

Open
jgoppert opened this issue May 20, 2024 · 1 comment
Open

Support for External Clock #4

jgoppert opened this issue May 20, 2024 · 1 comment

Comments

@jgoppert
Copy link

jgoppert commented May 20, 2024

Feature Request

Current Approach:

I'm running zephyr native sim on lockstep with a robotics simulator "Gazebo". Currently I'm just disabling the realtime feature and then polling on on ringbuf is_empty using nanosleep to sleep the thread every millisecond.

Requested Feature:

It would be great if there was an external clock api where the zephyr cpu could be paused until I tell it to run until it reaches a given timestamp. Something like:

#ifdef CLOCK_EXTERNAL
nsi_run_until(struct timespec tv) {
...
}
#endif

Usecase:

  1. simulator sends current clock time and data via UDP to zephyr (zephyr model cpu is paused)
  2. posix native thread receives clock and data and buffers data to send to zephyr thread (ring buf, msg queue)
  3. (zephyr model cpu is unpaused) posix native thread runs scheduler until the boot time matches the sim time
  4. process repeats when next external clock message from UDP is received
@aescolar
Copy link
Contributor

@jgoppert
If I understand what you are describing, what you would like to do, should already be possible today without any modification to the native_simulator runner or Zephyr code.
(If you are using Zephyr's native_sim and its system tick timer driver you should indeed run with its real time mode disabled)

Note that in the native_simulator you can have your own HW model components, and this can register their own event timers:
https://github.com/BabbleSim/native_simulator/blob/main/docs/Design.md#the-hw-scheduling
https://github.com/BabbleSim/native_simulator/blob/main/common/src/include/nsi_hws_models_if.h#L25-L36
When a HW model event callback is executing the rest of the native simulator and Zephyr is paused.
So, one option may be to have your own HW model component, which registers a timer.

static uint64_t my_event_time;
static void my_event_callback(void) {
...
}
NSI_HW_EVENT(my_event_time, my_event_callback, 100)

In that event timer callback (my_event_callback()), block waiting for your external simulator next time update (ext_Time), when you receive it, advance that event timer time to new external simulator time in the future (my_event_time=ext_Time;) and return from the callback.
The native simulator will then advance time until ext_Time and call your callback (my_event_callback) again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants