Skip to content

Instantly share code, notes, and snippets.

@andrepuschmann
Last active October 17, 2020 13:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrepuschmann/fe6fa8917f7898855e401221015a2b6c to your computer and use it in GitHub Desktop.
Save andrepuschmann/fe6fa8917f7898855e401221015a2b6c to your computer and use it in GitHub Desktop.
Function to set USRP time to GPS time
static uhd_error set_time_to_gps_time(rf_uhd_handler_t* handler)
{
const char sensor_name[] = "gps_time";
uhd_string_vector_handle sensors;
uhd_string_vector_make(&sensors);
uhd_error error = uhd_usrp_get_mboard_sensor_names(handler->usrp, 0, &sensors);
#if PRINT_SENSOR_NAMES
size_t sensors_len;
uhd_string_vector_size(sensors, &sensors_len);
for (int i = 0; i < sensors_len; i++) {
char buff[256];
uhd_string_vector_at(sensors, i, buff, 128);
printf("sensor %s present\n", buff);
}
#endif
if (error == UHD_ERROR_NONE && find_string(sensors, sensor_name)) {
uhd_sensor_value_handle value_h;
uhd_sensor_value_make_from_string(&value_h, "w", "t", "f");
error = uhd_usrp_get_mboard_sensor(handler->usrp, sensor_name, 0, &value_h);
int full_secs = 0;
if (error == UHD_ERROR_NONE) {
uhd_sensor_value_data_type_t sensor_dtype;
uhd_sensor_value_data_type(value_h, &sensor_dtype);
full_secs = uhd_sensor_value_to_int(value_h, &full_secs);
printf("gps full_secs=%d\n", full_secs);
double frac_secs;
uhd_sensor_value_data_type(value_h, &sensor_dtype);
full_secs = uhd_sensor_value_to_realnum(value_h, &frac_secs);
printf("gps frac_secs=%f\n", frac_secs);
full_secs = frac_secs;
printf("gps after rounding full_secs=%d\n", full_secs);
// seems to work
error = uhd_usrp_set_time_unknown_pps(handler->usrp, full_secs + 1, 0.0);
// doesn't work
// error = uhd_usrp_set_time_next_pps(handler->usrp, full_secs+1, 0.0, 0);
if (error != UHD_ERROR_NONE) {
char err_msg[256];
uhd_usrp_last_error(handler->usrp, err_msg, 256);
fprintf(stderr, "Error code %d: %s\n", error, err_msg);
}
// sleep a second to make sure values are set correctly
sleep(1);
}
uhd_sensor_value_free(&value_h);
}
if (error != UHD_ERROR_NONE) {
fprintf(stderr, "USRP has no sensor \"%s\", or reading failed. UHD error: %i\n", sensor_name, error);
}
uhd_string_vector_free(&sensors);
return error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment