# Using the UHD API for Python Programming

**URL:** <https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867>\
**Category:** GNU Radio\
**Created:** [June 22, 2012, 4:35pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867 "2012-06-22T16:35:07Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 22, 2012, 4:35pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/1 "2012-06-22T16:35:07Z")

</div>

Hi all,  
I’m having some trouble using the C++ UHD API for writing Python code.  
I understand that the code has been SWIG’d from C++ to Python.

## Specifically, the C++ code I want to convert to Python is:

uhd::stream\_cmd\_t  
stream\_cmd(uhd::stream\_cmd\_t::STREAM\_MODE\_NUM\_SAMPS\_AND\_DONE);  
stream\_cmd.num\_samps = samps\_to\_recv;  
stream\_cmd.stream\_now = false;  
stream\_cmd.time\_spec = time\_to\_recv;  
usrp-\>issue\_stream\_cmd(stream\_cmd);

## uhd::time\_spec\_t cmd\_time = usrp-\>get\_time\_now() + uhd::time\_spec\_t(0.1); usrp-\>set\_command\_time(cmd\_time); usrp-\>set\_rx\_freq(1.03e9, 0/_ch0_/); usrp-\>set\_rx\_freq(1.03e9, 1/_ch1_/); usrp-\>set\_clear\_time();

## With my attempt at Python:

self.stream\_cmd =  
uhd.stream\_cmd\_t(uhd.stream\_cmd\_t.stream\_mode\_t.STREAM\_MODE\_NUM\_SAMPS\_AND\_DONE)  
self.stream\_cmd.num\_samps = samps\_to\_recv;  
self.stream\_cmd.stream\_now = false;  
self.stream\_cmd.time\_spec = time\_to\_recv;  
self.uhd\_usrp\_source\_0.issue\_stream\_cmd(self.stream\_cmd)

## cmd\_time = self.uhd\_usrp\_source\_0.get\_time\_now() + uhd.time\_spec\_t(0.1) self.uhd\_usrp\_source\_0.set\_command\_time(cmd\_time) self.uhd\_usrp\_source\_0.set\_rx\_freq(1.03e9, 0) self.uhd\_usrp\_source\_0.set\_rx\_freq(1.03e9, 1) self.uhd\_usrp\_source\_0.set\_clear\_time()

Running this, I get the error:  
AttributeError: ‘module’ object has no attribute ‘stream\_cmd\_t’

What is this struct called after it has been swigged into Python, and  
how  
do I access it?  
As a follow up, does anyone have any general guidelines to follow when  
writing code from the C++ UHD API into Python?  
I’ve checked the gr-uhd source tree examples, but they didn’t provide  
the  
insight needed to access the full C++ API.

-Alex

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [June 22, 2012, 6:20pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/2 "2012-06-22T18:20:53Z")

</div>

On 06/22/2012 07:33 AM, Alexander O. wrote:

> stream\_cmd.stream\_now = false;  
> stream\_cmd.time\_spec = time\_to\_recv;  
> usrp-\>issue\_stream\_cmd(stream\_cmd);

Stream command is not swigged because the control of streaming is part  
of gnuradio’s scheduler. So start() and stop() get automatically called  
by the scheduler. For convenience this is a set\_command\_time() to  
control stream\_cmd.time\_spec.

Basically, gnuradio is handling the streaming for you. Think block-based  
streaming model.

> uhd::time\_spec\_t cmd\_time = usrp-\>get\_time\_now() + uhd::time\_spec\_t(0.1);  
> usrp-\>set\_command\_time(cmd\_time);  
> usrp-\>set\_rx\_freq(1.03e9, 0/_ch0_/);  
> usrp-\>set\_rx\_freq(1.03e9, 1/_ch1_/);  
> usrp-\>set\_clear\_time();

This part is swigged.

-josh

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [June 22, 2012, 7:19pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/3 "2012-06-22T19:19:21Z")

</div>

For get\_time\_now(), would I need to swig multi\_usrp.hpp?

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [June 22, 2012, 7:36pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/4 "2012-06-22T19:36:53Z")

</div>

On 06/22/2012 10:18 AM, Alexander O. wrote:

> For get\_time\_now(), would I need to swig multi\_usrp.hpp?

Everything you see in these headers should be available in python:

> **[include « gr-uhd - gnuradio.git - GNU Radio](https://www.gnuradio.org/cgit/gnuradio.git/tree/gr-uhd/include)**

-josh

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [June 22, 2012, 9:20pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/5 "2012-06-22T21:20:52Z")

</div>

So to tune two USRP N210s connected by a MIMO cable, I should set the  
command time to be some time spec, and then re-tune both channels to the  
same frequency, thus giving them the same phase offset?

i.e. My code is looking more like:  
self.cmd\_time = self.uhd\_usrp\_source\_0.uhd.time\_spec\_t(.1)  
self.uhd\_usrp\_source\_0.set\_command\_time(self.cmd\_time)  
self.uhd\_usrp\_source\_0.set\_center\_freq(freq, 0)  
self.uhd\_usrp\_source\_0.set\_center\_freq(freq, 1)  
self.uhd\_usrp\_source\_0.clear\_command\_time()

The phases are still off… Is my method incorrect?

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [June 22, 2012, 9:25pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/6 "2012-06-22T21:25:33Z")

</div>

On 06/22/2012 12:19 PM, Alexander O. wrote:

> So to tune two USRP N210s connected by a MIMO cable, I should set the  
> command time to be some time spec, and then re-tune both channels to the  
> same frequency, thus giving them the same phase offset?
> 
> i.e. My code is looking more like:

make sure you you tell the board to get its reference of clock and time  
source from the mimo cable

> self.cmd\_time = self.uhd\_usrp\_source\_0.uhd.time\_spec\_t(.1)

careful, make sure that cmd time is a time in the future

> self.uhd\_usrp\_source\_0.set\_command\_time(self.cmd\_time)  
> self.uhd\_usrp\_source\_0.set\_center\_freq(freq, 0)  
> self.uhd\_usrp\_source\_0.set\_center\_freq(freq, 1)  
> self.uhd\_usrp\_source\_0.clear\_command\_time()
> 
> The phases are still off… Is my method incorrect?

Synchronization of device time is separate from synchronizing the phase  
of the RF frontend. This may be helpful, with code examples for both:

[http://files.ettus.com/uhd\_docs/manual/html/sync.html](http://files.ettus.com/uhd_docs/manual/html/sync.html)

-Josh

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [June 22, 2012, 7:26pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/7 "2012-06-22T19:26:22Z")

</div>

There’s already a “get\_time\_now” method swigged in the  
gr\_uhd\_usrp\_{source,sink} block, along with a “get\_time\_last\_pps” method  
if you need to synchronize to an absolute time.

Sean

From: [discuss-gnuradio-bounces+sean.nowlan=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid)  
[[mailto:discuss-gnuradio-bounces+sean.nowlan=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid)] On  
Behalf Of Alexander O.  
Sent: Friday, June 22, 2012 1:18 PM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Cc: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Discuss-gnuradio] Using the UHD API for Python Programming

For get\_time\_now(), would I need to swig multi\_usrp.hpp?  
On Fri, Jun 22, 2012 at 12:19 PM, Josh B.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

On 06/22/2012 07:33 AM, Alexander O. wrote:

> stream\_cmd.stream\_now = false;  
> stream\_cmd.time\_spec = time\_to\_recv;  
> usrp-\>issue\_stream\_cmd(stream\_cmd);

Stream command is not swigged because the control of streaming is part  
of gnuradio’s scheduler. So start() and stop() get automatically called  
by the scheduler. For convenience this is a set\_command\_time() to  
control stream\_cmd.time\_spec.

Basically, gnuradio is handling the streaming for you. Think block-based  
streaming model.

> uhd::time\_spec\_t cmd\_time = usrp-\>get\_time\_now() + uhd::time\_spec\_t(0.1);  
> usrp-\>set\_command\_time(cmd\_time);  
> usrp-\>set\_rx\_freq(1.03e9, 0/_ch0_/);  
> usrp-\>set\_rx\_freq(1.03e9, 1/_ch1_/);  
> usrp-\>set\_clear\_time();  
> This part is swigged.

-josh

* * *

Discuss-gnuradio mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

–  
Alex Olihovik  
University of Virginia 2013  
BS Electrical Engineering  
BS Computer Engineering  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [August 31, 2015, 6:04pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/8 "2015-08-31T18:04:35Z")

</div>

Hi people.

I’m using the same code to synchronize channel phase. I’m using MIMO  
cable, and the next lines are used to do that

```
    self.uhd_usrp_source_0.set_clock_source("mimo", 1)
    self.uhd_usrp_source_0.set_time_source("mimo", 1)
    self.uhd_usrp_source_0.set_samp_rate(samp_rate)
    self.uhd_usrp_source_0.set_center_freq(1.2e9, 0)
    self.uhd_usrp_source_0.set_gain(10, 0)
    self.uhd_usrp_source_0.set_antenna("RX2", 0)
    self.uhd_usrp_source_0.set_center_freq(1.2e9, 1)
    self.uhd_usrp_source_0.set_gain(10, 1)
    self.uhd_usrp_source_0.set_antenna("RX2", 1)

```

To synch the phase I used

```
    self.cmd_time = self.uhd_usrp_source_0.uhd_time_spect_t(.1)

```

self.uhd\_usrp\_source\_0.set\_command\_time(self.cmd\_time)  
self.uhd\_usrp\_source\_0.clear\_command\_time()

When I run the python file, I have an error, it says: " File  
“sync\_mimo\_nophase\_grc.py”, line 73, in **init**  
self.cmd\_time = self.uhd\_usrp\_source\_0.uhd\_time\_spect\_t(.1)  
AttributeError: ‘usrp\_source\_sptr’ object has no attribute  
‘uhd\_time\_spect\_t’"

Anyone can help me with my problem.

Thanks

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [August 31, 2015, 6:34pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/9 "2015-08-31T18:34:47Z")

</div>

Actually, it dindn’t work if I use uhd.time\_spec\_t(.1)

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [August 31, 2015, 6:27pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/10 "2015-08-31T18:27:51Z")

</div>

On 08/31/2015 12:04 PM, Tibisay Sanchez wrote:

> ```
> self.uhd_usrp_source_0.set_antenna("RX2", 0)
> 
> ```
> 
> When I run the python file, I have an error, it says: " File  
> “sync\_mimo\_nophase\_grc.py”, line 73, in **init**  
> self.cmd\_time = self.uhd\_usrp\_source\_0.uhd\_time\_spect\_t(.1)  
> AttributeError: ‘usrp\_source\_sptr’ object has no attribute  
> ‘uhd\_time\_spect\_t’"
> 
> Anyone can help me with my problem.
> 
> Thanks

It’s uhd\_time\_spec\_t, not uhd\_time\_spect\_t

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [August 31, 2015, 9:31pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/11 "2015-08-31T21:31:14Z")

</div>

my line is

self.cmd\_time = self.uhd\_usrp\_source\_0.uhd.time\_spec\_t(.1)

but it shows an error when it calls uhd.time\_spec\_t(.1)

it says usrp\_source\_sptr’ object has no attribute ‘uhd\_time\_spect\_t’.

I need both LO are align to synch the phase

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [August 31, 2015, 9:42pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/12 "2015-08-31T21:42:24Z")

</div>

time\_spec\_t() is a helper function in class “uhd”, it is not a method in  
a uhd.usrp\_source/sink.

self.cmd\_time = uhd.time\_spec\_t(0.1)

Would be the correct idiom.

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [August 31, 2015, 6:44pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/13 "2015-08-31T18:44:13Z")

</div>

On 08/31/2015 12:34 PM, Tibisay Sanchez wrote:

> Actually, it dindn’t work if I use uhd.time\_spec\_t(.1)

I should have looked closer:

you want:

cmd\_time = uhd.time\_spec\_t(0.1)

---

<div class="post-metadata">

**Author:** ![grandison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@grandison](https://www.ruby-forum.com/u/grandison)\
**Post date:** [August 31, 2015, 9:50pm UTC](https://www.ruby-forum.com/t/using-the-uhd-api-for-python-programming/220867/14 "2015-08-31T21:50:17Z")

</div>

Thanks a lot.

It works!!

At least it doesn’t show an error in the code.

Now, I’m going to test the system.

thanks for your help
