# Triggering USRP at given GPS time from GNU Radio

**URL:** <https://www.ruby-forum.com/t/triggering-usrp-at-given-gps-time-from-gnu-radio/234317>\
**Category:** GNU Radio\
**Created:** [February 23, 2015, 9:35pm UTC](https://www.ruby-forum.com/t/triggering-usrp-at-given-gps-time-from-gnu-radio/234317 "2015-02-23T21:35:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [February 23, 2015, 9:35pm UTC](https://www.ruby-forum.com/t/triggering-usrp-at-given-gps-time-from-gnu-radio/234317/1 "2015-02-23T21:35:07Z")

</div>

Hi all,

Whenever I want to trigger USRP with at given time with use of a GPSDO  
I’m adding something like this at the end of a constructor of a topblock  
generated by gnuradio-companion:

```
    if start_time!="":
        self.start_time_epoch =

```

time.mktime(time.strptime(start\_time,’%d.%m.%Y %H:%M:%S’))

self.uhd\_usrp\_source\_0.set\_start\_time(uhd.time\_spec\_t(self.start\_time\_epoch))

where start\_time is a string parameter containing the time at which the  
device should start reception.

The snippet might be useful for others trying to do the same thing.

I want to ask you if there is a way to do better than that and generate  
directly in gnuradio-companion a flowgraph that use GPSDO of USRP to  
trigger it without having to edit anything manually?

Best Regards,  
Piotr K.

---

<div class="post-metadata">

**Author:** ![hibernaculus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@hibernaculus](https://www.ruby-forum.com/u/hibernaculus)\
**Post date:** [February 24, 2015, 11:01am UTC](https://www.ruby-forum.com/t/triggering-usrp-at-given-gps-time-from-gnu-radio/234317/2 "2015-02-24T11:01:44Z")

</div>

On 23-02-15 21:33, Piotr K. wrote:

> self.uhd\_usrp\_source\_0.set\_start\_time(uhd.time\_spec\_t(self.start\_time\_epoch))
> 
> where start\_time is a string parameter containing the time at which the  
> device should start reception.
> 
> The snippet might be useful for others trying to do the same thing.  
> This is usefull to me indeed. Thanks.
> 
> I want to ask you if there is a way to do better than that and generate  
> directly in gnuradio-companion a flowgraph that use GPSDO of USRP to  
> trigger it without having to edit anything manually?  
> I have not found a way yet.  
> I always edit manually and add a few lines of code, like you seem to do.  
> (See the end of this mail)

For this application it would be helpfull if you could set the  
start\_time from grc.

In general it would be very helpfull if you could add your own python  
code to grc without having to edit it manually after generating.

A “codeblock” in a similar way the import block is implemented would be  
very helpful. Just a block where you can add code. You should probably  
be able to choose where the code is added.

at the top of the file after all imports  
at the beginning of the constructor of the flowgraph  
at the end of the constructor of the flow\_graph  
as a seperate method of the flowgraph  
at the start of main  
at the end of main.

We could make it a feature request.  
Or maybe I should just implement it.

Here follows my code snippet to set the start of capturing in the near  
future. This also works if you do not have a GPSDO  
You need to set a start\_time in the near future which I do like this:

```
     # In grc add a parameter delay (in seconds) with type float and

```

default 0.5  
# at the end of def **init** () of the generated flowgrapg  
add the following code:  
#  
self.init\_timed\_streaming(self.delay)

```
 def init_timed_streaming(self, delay):
     # You can optionally set the time in the usrp to zero or to

```

something else  
# with set\_time\_now() or if you have a GPSDO with  
set\_time\_unknown\_pps()  
# or set\_time\_next\_pps()  
#self.uhd\_usrp\_source.set\_time\_next\_pps(uhd.time\_spec\_t(0))  
#self.uhd\_usrp\_source.set\_time\_unknown\_pps(uhd.time\_spec\_t(0))  
#time.sleep(2)

```
     # Now get the time from the usrp before streaming has started .
     # You can use either uhd_usrp_source or uhd_usrp_sink to get

```

the time now.  
tnow = self.uhd\_usrp\_source.get\_time\_now()

```
     # If you have a GPSDO installed tnow is the absolute time
     # locked to the GPS time it received.
     # If you do not have a GPSDO time then this time is just a
     # meaningless number which started somewhere in the past.
     # tnow is of type uhd.time_spec_t

     self.reasonable_delay = delay
     # delay in seconds into the future, this should be at least
     # an order of magnitude larger then the round trip time to USRP

     # capture and transmit will start at t0
     self.t0 = tnow + uhd.time_spec_t(self.reasonable_delay)
     self.t0_secs = self.t0.get_real_secs() # the time in secs (full

```

- fractional) as a floating point number  
print "tnow secs = ", tnow.get\_real\_secs()  
print "t0\_secs = ", self.t0\_secs

of the uhd\_usrp\_sink  
# If you set the start\_time of both then you can build a system  
# where source and sink start at the exact same moment. This  
# can be usefull in several situations.  
self.uhd\_usrp\_source.set\_start\_time (self.t0 )  
self.uhd\_usrp\_sink.set\_start\_time (self.t0 )

Best regards,

Martin Dudok van Heel

---

<div class="post-metadata">

**Author:** ![hibernaculus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@hibernaculus](https://www.ruby-forum.com/u/hibernaculus)\
**Post date:** [February 24, 2015, 2:58pm UTC](https://www.ruby-forum.com/t/triggering-usrp-at-given-gps-time-from-gnu-radio/234317/3 "2015-02-24T14:58:20Z")

</div>

W dniu 24.02.2015 o 10:59, Martin pisze:

> at the top of the file after all imports  
> at the beginning of the constructor of the flowgraph  
> at the end of the constructor of the flow\_graph  
> as a seperate method of the flowgraph  
> at the start of main  
> at the end of main.
> 
> We could make it a feature request.  
> Or maybe I should just implement it.

Martin,

The solution proposed by you would be very good to give to the user  
ability to do something more sophisticated than gnuradio-companion  
supports at any point in the future.

However in my opinion ability to trigger USRP at a specified time is  
quite basic feature of the device. Maybe it would be better to add the  
start\_time parameter in gnuradio-companion UHD blocks (source and sink)  
that would be parsed into appropriate python code. To keep it simple to  
implement the value given could be as a float in seconds that would be  
directly converted into USRP’s clock value like this:

```
uhd.time_spec_t(start_time)

```

and it would be user task to assign proper value like:

start\_time: time.mktime(time.strptime(time\_string,‘%d.%m.%Y  
%H:%M:%S’)) #if he uses GPSDO

or

```
start_time: self.uhd_usrp_source.get_time_now()+4 #if he just to

```

start 4 seconds from now

The examples of how to use this parameter could be documented in the  
description of the blocks so each user wouldn’t have to find this out on  
his own.

The other advantage of this approach - it would also give a simple way  
to synchronize UHD source and sink from gnuradio-companion.

Best Regards,  
Piotr K.
