Statistics
| Branch: | Tag: | Revision:

root / host / examples / tx_waveforms.cpp @ 6798b4f8

History | View | Annotate | Download (6.4 kB)

1
//
2
// Copyright 2010 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#include <uhd/utils/thread_priority.hpp>
19
#include <uhd/utils/safe_main.hpp>
20
#include <uhd/usrp/simple_usrp.hpp>
21
#include <boost/program_options.hpp>
22
#include <boost/thread/thread_time.hpp> //system time
23
#include <boost/math/special_functions/round.hpp>
24
#include <boost/format.hpp>
25
#include <boost/function.hpp>
26
#include <iostream>
27
#include <complex>
28
#include <cmath>
29

    
30
namespace po = boost::program_options;
31

    
32
/***********************************************************************
33
 * Waveform generators
34
 **********************************************************************/
35
float gen_const(float){
36
    return 1;
37
}
38

    
39
float gen_square(float x){
40
    return (std::fmod(x, 1) < float(0.5))? 0 : 1;
41
}
42

    
43
float gen_ramp(float x){
44
    return std::fmod(x, 1)*2 - 1;
45
}
46

    
47
float gen_sine(float x){
48
    return std::sin(x*2*M_PI);
49
}
50

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

    
54
    //variables to be set by po
55
    std::string args, wave_type;
56
    size_t total_duration, spb;
57
    double rate, freq, wave_freq;
58
    float ampl, gain;
59

    
60
    //setup the program options
61
    po::options_description desc("Allowed options");
62
    desc.add_options()
63
        ("help", "help message")
64
        ("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args")
65
        ("duration", po::value<size_t>(&total_duration)->default_value(3), "number of seconds to transmit")
66
        ("spb", po::value<size_t>(&spb)->default_value(10000), "samples per buffer")
67
        ("rate", po::value<double>(&rate)->default_value(100e6/16), "rate of outgoing samples")
68
        ("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz")
69
        ("ampl", po::value<float>(&ampl)->default_value(float(0.3)), "amplitude of the waveform")
70
        ("gain", po::value<float>(&gain)->default_value(float(0)), "gain for the RF chain")
71
        ("wave-type", po::value<std::string>(&wave_type)->default_value("SINE"), "waveform type (CONST, SQUARE, RAMP, SINE)")
72
        ("wave-freq", po::value<double>(&wave_freq)->default_value(0), "waveform frequency in Hz")
73
    ;
74
    po::variables_map vm;
75
    po::store(po::parse_command_line(argc, argv, desc), vm);
76
    po::notify(vm);
77

    
78
    //print the help message
79
    if (vm.count("help")){
80
        std::cout << boost::format("UHD TX Waveforms %s") % desc << std::endl;
81
        return ~0;
82
    }
83

    
84
    //create a usrp device
85
    std::cout << std::endl;
86
    std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
87
    uhd::usrp::simple_usrp::sptr sdev = uhd::usrp::simple_usrp::make(args);
88
    uhd::device::sptr dev = sdev->get_device();
89
    std::cout << boost::format("Using Device: %s") % sdev->get_pp_string() << std::endl;
90

    
91
    //set the tx sample rate
92
    std::cout << boost::format("Setting TX Rate: %f Msps...") % (rate/1e6) << std::endl;
93
    sdev->set_tx_rate(rate);
94
    std::cout << boost::format("Actual TX Rate: %f Msps...") % (sdev->get_tx_rate()/1e6) << std::endl << std::endl;
95

    
96
    //set the tx center frequency
97
    std::cout << boost::format("Setting TX Freq: %f Mhz...") % (freq/1e6) << std::endl;
98
    sdev->set_tx_freq(freq);
99
    std::cout << boost::format("Actual TX Freq: %f Mhz...") % (sdev->get_tx_freq()/1e6) << std::endl << std::endl;
100

    
101
    //set the tx rf gain
102
    std::cout << boost::format("Setting TX Gain: %f dB...") % gain << std::endl;
103
    sdev->set_tx_gain(gain);
104
    std::cout << boost::format("Actual TX Gain: %f dB...") % sdev->get_tx_gain() << std::endl << std::endl;
105

    
106
    //for the const wave, set the wave freq for small samples per period
107
    if (wave_freq == 0 and wave_type == "CONST"){
108
        wave_freq = sdev->get_tx_rate()/2;
109
    }
110

    
111
    //error when the waveform is not possible to generate
112
    if (std::abs(wave_freq) > sdev->get_tx_rate()/2){
113
        throw std::runtime_error("wave freq out of Nyquist zone");
114
    }
115

    
116
    //store the generator function for the selected waveform
117
    boost::function<float(float)> wave_gen;
118
    if      (wave_type == "CONST")  wave_gen = &gen_const;
119
    else if (wave_type == "SQUARE") wave_gen = &gen_square;
120
    else if (wave_type == "RAMP")   wave_gen = &gen_ramp;
121
    else if (wave_type == "SINE")   wave_gen = &gen_sine;
122
    else throw std::runtime_error("unknown waveform type: " + wave_type);
123

    
124
    //allocate the buffer and precalculate values
125
    std::vector<std::complex<float> > buff(spb);
126
    const float cps = wave_freq/sdev->get_tx_rate();
127
    const float i_off = (wave_freq > 0)? float(0.25) : 0;
128
    const float q_off = (wave_freq < 0)? float(0.25) : 0;
129
    float theta = 0;
130

    
131
    //setup the metadata flags
132
    uhd::tx_metadata_t md;
133
    md.start_of_burst = true; //always SOB (good for continuous streaming)
134
    md.end_of_burst   = false;
135

    
136
    //send the data in multiple packets
137
    boost::system_time end_time(boost::get_system_time() + boost::posix_time::seconds(total_duration));
138
    while(end_time > boost::get_system_time()){
139

    
140
        //fill the buffer with the waveform
141
        for (size_t n = 0; n < buff.size(); n++){
142
            buff[n] = std::complex<float>(
143
                ampl*wave_gen(i_off + theta),
144
                ampl*wave_gen(q_off + theta)
145
            );
146
            theta += cps;
147
        }
148

    
149
        //bring the theta back into range [0, 1)
150
        theta = std::fmod(theta, 1);
151

    
152
        //send the entire contents of the buffer
153
        dev->send(
154
            &buff.front(), buff.size(), md,
155
            uhd::io_type_t::COMPLEX_FLOAT32,
156
            uhd::device::SEND_MODE_FULL_BUFF
157
        );
158
    }
159

    
160
    //send a mini EOB packet
161
    md.start_of_burst = false;
162
    md.end_of_burst   = true;
163
    dev->send(NULL, 0, md,
164
        uhd::io_type_t::COMPLEX_FLOAT32,
165
        uhd::device::SEND_MODE_FULL_BUFF
166
    );
167

    
168
    //finished
169
    std::cout << std::endl << "Done!" << std::endl << std::endl;
170

    
171
    return 0;
172
}