Using the UHD API for Python Programming

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

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

For get_time_now(), would I need to swig multi_usrp.hpp?

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:
http://gnuradio.org/cgit/gnuradio.git/tree/gr-uhd/include

-josh

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?

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

-Josh

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: [email protected]n.invalid
[mailto:[email protected]n.invalid] On
Behalf Of Alexander O.
Sent: Friday, June 22, 2012 1:18 PM
To: [email protected]
Cc: [email protected]
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.
<[email protected]mailto:[email protected]> 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
[email protected]mailto:[email protected]
https://lists.gnu.org/mailman/listinfo/discuss-gnuradio


Alex Olihovik
University of Virginia 2013
BS Electrical Engineering
BS Computer Engineering
[email protected]mailto:[email protected]

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

Actually, it dindn’t work if I use uhd.time_spec_t(.1)

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

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

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.

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)

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs