Utility functions.
More...
|
int | clear_shared_memory (const std::string &name) |
| Clear an existing shared-memory file. More...
|
|
std::string | datetime_now_string () |
| Generate a date-time string. More...
|
|
const std::string | get_log_path (const std::string &log_dir, const std::string &spine_name) |
| Get path to a fresh log file. More...
|
|
const bool & | handle_interrupts () |
| Redirect interrupts to setting a global interrupt boolean. More...
|
|
double | low_pass_filter (double prev_output, double cutoff_period, double new_input, double dt) |
| Low-pass filter as an inline function. More...
|
|
bool | divides (uint32_t number, uint32_t divisor) |
| True if and only if divisor divides number .
|
|
std::string | random_string (unsigned length=16) |
| Generate a random string. More...
|
|
void | configure_cpu (int cpu) |
| Set the current thread to run on a given CPU core. More...
|
|
void | configure_scheduler (int priority) |
| Configure the scheduler policy to round-robin for this thread. More...
|
|
bool | lock_memory () |
| Lock all memory to RAM so that the kernel doesn't page it to swap. More...
|
|
◆ clear_shared_memory()
int upkie::cpp::utils::clear_shared_memory |
( |
const std::string & |
name | ) |
|
Clear an existing shared-memory file.
- Parameters
-
[in] | name | Name of the shared-memory file to remove if it exists. |
- Returns
- EXIT_SUCCESS if the file is cleared, EXIT_FAILURE otherwise.
◆ configure_cpu()
void upkie::cpp::utils::configure_cpu |
( |
int |
cpu | ) |
|
|
inline |
Set the current thread to run on a given CPU core.
- Parameters
-
cpu | CPU core for this thread (on the Pi, CPUID in {0, 1, 2, 3}). |
- Exceptions
-
std::runtime_error | If the operation failed. |
◆ configure_scheduler()
void upkie::cpp::utils::configure_scheduler |
( |
int |
priority | ) |
|
|
inline |
Configure the scheduler policy to round-robin for this thread.
- Parameters
-
priority | Priority of this thread for the scheduler, ranging from 1 (low) to 99 (high). See man sched . For agents, this priority should be lower than that of the spine. |
- Exceptions
-
std::runtime_error | If the operation failed. |
◆ datetime_now_string()
std::string upkie::cpp::utils::datetime_now_string |
( |
| ) |
|
|
inline |
Generate a date-time string.
- Returns
- Date-time string at the time the function is called.
Uses the same format as the Python utility function.
◆ get_log_path()
const std::string upkie::cpp::utils::get_log_path |
( |
const std::string & |
log_dir, |
|
|
const std::string & |
spine_name |
|
) |
| |
|
inline |
Get path to a fresh log file.
- Parameters
-
[in] | log_dir | Path to the logging directory. |
[in] | spine_name | Name of the spine. |
- Returns
- Path to a fresh log file.
Uses the same format as the Python utility function.
◆ handle_interrupts()
const bool & upkie::cpp::utils::handle_interrupts |
( |
| ) |
|
Redirect interrupts to setting a global interrupt boolean.
Direct interrupts (e.g.
- Returns
- Reference to the interrupt boolean.
Ctrl-C) to a boolean flag.
- Returns
- Reference to a boolean flag, which is initially false and becomes true the first time an interruption is caught.
◆ lock_memory()
bool upkie::cpp::utils::lock_memory |
( |
| ) |
|
|
inline |
Lock all memory to RAM so that the kernel doesn't page it to swap.
The Linux man pages have a great NOTES section on this. Worth a read!
◆ low_pass_filter()
double upkie::cpp::utils::low_pass_filter |
( |
double |
prev_output, |
|
|
double |
cutoff_period, |
|
|
double |
new_input, |
|
|
double |
dt |
|
) |
| |
|
inline |
Low-pass filter as an inline function.
- Parameters
-
prev_output | Previous filter output, or initial value. |
cutoff_period | Cutoff period in [s]. |
new_input | New filter input. |
dt | Sampling period in [s]. |
- Returns
- New filter output.
◆ random_string()
std::string upkie::cpp::utils::random_string |
( |
unsigned |
length = 16 | ) |
|
|
inline |
Generate a random string.
- Parameters
-
- Returns
- Random string.
The generated string contains only alphanumeric characters with no repetition.