Statistics
| Branch: | Tag: | Revision:

root / host / examples / tx_waveforms.cpp @ 8673ee72

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 float((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
    static const float two_pi = 2*std::acos(float(-1));
49
    return std::sin(x*two_pi);
50
}
51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
172
    return 0;
173
}