My initial code was
using n_time = std::chrono::high_resolution_clock;
n_time::time_point c_time = n_time::now();
n_time::time_point start = n_time::now();
auto gyro::duration(){
return std::chrono::duration_cast<std::chrono::milliseconds>(c_time-start).count();
}
but then I realized that I wanted to find the milliseconds from a start argument to now()
, and I wasn't sure if my initial code would give me that. So I tried
auto timeSince(n_time::time_point start) {
n_time now = n_time::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count();
}
Is this how you would find the milliseconds after start
? I heard there was a function called time_since_epoch()
and I'm not sure if that would be better to use instead?
This clock is for a specific robot function I'm trying to write, so I need to make sure that the clock doesn't cause a conversion error:
void straight(int distance, int speed) {
int time = (distance / speed) * 1000; // milliseconds
while (timeSince() < time) {
// code
}
}
The duration()
function does not give any errors in my function, but the timeSince()
gives me conversion errors such as
error: conversion from 'int' to non-scalar type 'std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point >
auto
for the type, because yourn_time now=n_time::now();
is wrong type. Besides this, your initial method is correct. – sz ppeter