Difference between revisions of "Getting Started with UHD and C++"

From Ettus Knowledge Base
Jump to: navigation, search
(Reading Center Frequency)
Line 71: Line 71:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Setting Motherboard Clocks==
+
==Setting the Motherboard Clocks==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
     usrp->set_clock_source(ref);
 
     usrp->set_clock_source(ref);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Selecting Sub Device==
+
==Selecting the Sub Device==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
     usrp->set_rx_subdev_spec(subdev)
 
     usrp->set_rx_subdev_spec(subdev)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Read selected Sub Device==
+
==Getting the selected Sub Device==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
     usrp->get_pp_string()
 
     usrp->get_pp_string()
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Setting Sample Rate==
+
==Setting the Sample Rate==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
     usrp->set_rx_rate(rate)
 
     usrp->set_rx_rate(rate)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Reading the Sample Rate==
+
==Getting the Sample Rate==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
     usrp->get_rx_rate()
 
     usrp->get_rx_rate()
Line 107: Line 107:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Setting RF Gain==
+
==Setting the RF Gain==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
     usrp->set_rx_gain(gain)
 
     usrp->set_rx_gain(gain)
    std::cout << boost::format("Actual RX Gain: %f dB...") % usrp->get_rx_gain() << std::endl << std::endl;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Reading RF Gain==
+
==Reading the RF Gain==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
     usrp->get_rx_gain()
 
     usrp->get_rx_gain()
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Setting IF Filter Bandwidth==
+
==Setting the IF Filter Bandwidth==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
    // set the IF filter bandwidth
+
     usrp->set_rx_bandwidth(bw)
    std::cout << boost::format("Setting RX Bandwidth: %f MHz...") % (bw / 1e6) << std::endl;
+
     usrp->set_rx_bandwidth(bw);
+
    std::cout << boost::format("Actual RX Bandwidth: %f MHz...") % (usrp->get_rx_bandwidth() / 1e6) << std::endl << std::endl;
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Selecting Antenna==
+
==Getting the IF Filter Bandwidth==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
     std::cout << boost::format("Setting RX Antenna: %s") % ant << std::endl;
+
     usrp->get_rx_bandwidth()
     usrp->set_rx_antenna(ant);
+
</syntaxhighlight>
    std::cout << boost::format("Actual RX Antenna: %s") % usrp->get_rx_antenna() << std::endl << std::endl;
+
 
 +
==Selecting the Antenna==
 +
<syntaxhighlight lang="c++">
 +
     usrp->set_rx_antenna(ant)
 +
</syntaxhighlight>
 +
 
 +
==Getting the Antenna==
 +
<syntaxhighlight lang="c++">
 +
    usrp->get_rx_antenna()
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 15:55, 15 May 2016

Application Note Number

AN-204

Revision History

Date Author Details
2016-05-01 Neel Pandeya
Nate Temple
Initial creation

Abstract

This AN explains how to write and build C++ programs that use the UHD API.

Overview

This Application Note will walk through building a basic C++ program with UHD. This program will initialize, configure the USRP device, set the sample rate, frequency, gain, bandwidth, and select the antenna.

UHD Manual

The UHD Manual is hosted at http://files.ettus.com/manual/index.html and provides information on how to use the USRP devices and how to use the UHD API to connect to them through your own software. The manual is split into two parts: The device manual, and the UHD/API manual. The first part describes details of Ettus Research devices, motherboards and daughterboards, as well as aspects of using UHD. The second is meant for developers writing UHD-based applications, and includes descriptions of the API, sorted by namespaces, classes, and files.

Including Header Files

    #include <uhd/utils/thread_priority.hpp>
    #include <uhd/utils/safe_main.hpp>
    #include <uhd/usrp/multi_usrp.hpp>
    #include <uhd/exception.hpp>
    #include <uhd/types/tune_request.hpp>
    #include <boost/program_options.hpp>
    #include <boost/format.hpp>
    #include <boost/thread.hpp>
    #include <iostream>

UHD_SAFE_MAIN()

Defines a safe wrapper that places a catch-all around main. If an exception is thrown, it prints to stderr and returns.

    int UHD_SAFE_MAIN(int argc, char *argv[]) {
        ...
    }

set_thread_priority_safe()

Set the scheduling priority on the current thread. Same as set_thread_priority but does not throw on failure.

    uhd::set_thread_priority_safe();

Create Variables

    std::string device_args("addr=192.168.10.2");
    std::string subdev("A:0");
    std::string ant("TX/RX");
    std::string ref("internal");

    double rate(1e6);
    double freq(915e6);
    double gain(10);
    double bw(1e6);

Creating a USRP Object

Make a new multi usrp from the device address.

    uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(device_args);

Setting the Motherboard Clocks

    usrp->set_clock_source(ref);

Selecting the Sub Device

    usrp->set_rx_subdev_spec(subdev)

Getting the selected Sub Device

    usrp->get_pp_string()

Setting the Sample Rate

    usrp->set_rx_rate(rate)

Getting the Sample Rate

    usrp->get_rx_rate()

Setting Center Frequency

    uhd::tune_request_t tune_request(freq)
    usrp->set_rx_freq(tune_request);

Getting the Center Frequency

    usrp->get_rx_freq()

Setting the RF Gain

    usrp->set_rx_gain(gain)

Reading the RF Gain

    usrp->get_rx_gain()

Setting the IF Filter Bandwidth

    usrp->set_rx_bandwidth(bw)

Getting the IF Filter Bandwidth

    usrp->get_rx_bandwidth()

Selecting the Antenna

    usrp->set_rx_antenna(ant)

Getting the Antenna

    usrp->get_rx_antenna()

Exiting

    return EXIT_SUCCESS;

Full Example

#include <uhd/utils/thread_priority.hpp>
#include <uhd/utils/safe_main.hpp>
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/exception.hpp>
#include <uhd/types/tune_request.hpp>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <iostream>

int UHD_SAFE_MAIN(int argc, char *argv[]) {
    uhd::set_thread_priority_safe();

    std::string device_args("addr=192.168.10.2");
    std::string subdev("A:0");
    std::string ant("TX/RX");
    std::string ref("internal");

    double rate(1e6);
    double freq(915e6);
    double gain(10);
    double bw(1e6);

    //create a usrp device
    std::cout << std::endl;
    std::cout << boost::format("Creating the usrp device with: %s...") % device_args << std::endl;
    uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(device_args);

    // Lock mboard clocks
    std::cout << boost::format("Lock mboard clocks: %f") % ref << std::endl;
    usrp->set_clock_source(ref);
    
    //always select the subdevice first, the channel mapping affects the other settings
    std::cout << boost::format("subdev set to: %f") % subdev << std::endl;
    usrp->set_rx_subdev_spec(subdev);
    std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;

    //set the sample rate
    if (rate <= 0.0) {
        std::cerr << "Please specify a valid sample rate" << std::endl;
        return ~0;
    }

    // set sample rate
    std::cout << boost::format("Setting RX Rate: %f Msps...") % (rate / 1e6) << std::endl;
    usrp->set_rx_rate(rate);
    std::cout << boost::format("Actual RX Rate: %f Msps...") % (usrp->get_rx_rate() / 1e6) << std::endl << std::endl;

    // set freq
    std::cout << boost::format("Setting RX Freq: %f MHz...") % (freq / 1e6) << std::endl;
    uhd::tune_request_t tune_request(freq);
    usrp->set_rx_freq(tune_request);
    std::cout << boost::format("Actual RX Freq: %f MHz...") % (usrp->get_rx_freq() / 1e6) << std::endl << std::endl;

    // set the rf gain
    std::cout << boost::format("Setting RX Gain: %f dB...") % gain << std::endl;
    usrp->set_rx_gain(gain);
    std::cout << boost::format("Actual RX Gain: %f dB...") % usrp->get_rx_gain() << std::endl << std::endl;

    // set the IF filter bandwidth
    std::cout << boost::format("Setting RX Bandwidth: %f MHz...") % (bw / 1e6) << std::endl;
    usrp->set_rx_bandwidth(bw);
    std::cout << boost::format("Actual RX Bandwidth: %f MHz...") % (usrp->get_rx_bandwidth() / 1e6) << std::endl << std::endl;

    // set the antenna
    std::cout << boost::format("Setting RX Antenna: %s") % ant << std::endl;
    usrp->set_rx_antenna(ant);
    std::cout << boost::format("Actual RX Antenna: %s") % usrp->get_rx_antenna() << std::endl << std::endl;

    return EXIT_SUCCESS;
}


CMake

Use the uhd/host/examples/init_usrp/CMakeLists.txt file as template - Add the names of your C++ source files to the add_executable(...) section - Put both modified CMakeLists.txt file and C++ file into an empty folder


Compile and Install

- Create a “build” folder and invoke CMake the usual way:

   mkdir build
   cd build
   cmake ../
   make


Running the Application

[example output]

Additional Example Programs

- Github UHD