Editing GRAndWalkthroughUSRP

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 232: Line 232:
 
04-22 11:30:01.626 14537 14537 D GrTemplateUSRP: UsbInterface[mId=4,mAlternateSetting=0,mName=USRP B200,mClass=255,mSubclass=0,mProtocol=0,mEndpoints=[
 
04-22 11:30:01.626 14537 14537 D GrTemplateUSRP: UsbInterface[mId=4,mAlternateSetting=0,mName=USRP B200,mClass=255,mSubclass=0,mProtocol=0,mEndpoints=[
 
04-22 11:30:01.626 14537 14537 D GrTemplateUSRP: UsbEndpoint[mAddress=136,mAttributes=2,mMaxPacketSize=512,mInterval=0]]]]
 
04-22 11:30:01.626 14537 14537 D GrTemplateUSRP: UsbEndpoint[mAddress=136,mAttributes=2,mMaxPacketSize=512,mInterval=0]]]]
04-22 11:30:01.626 14537 14537 D GrTemplateUSRP: Called Request Permission
+
04-22 11:30:01.626 14537 14537 D GrTemplateUSRP:
04-22 11:30:01.627 14537 14537 D GrTemplateUSRP: Found fd: 22  usbfs_path: /dev/bus/usb
 
04-22 11:30:01.627 14537 14537 D GrTemplateUSRP: Found vid: 9472  pid: 34</pre>
 
Since we're launching our flowgraph from [[GRAndWalkthroughCP]], we'll also see the flowgraph start messages, though we still haven't done anything with the USRP, yet.
 
 
 
== Creating a UHD USRP Source ==
 
 
 
Now we have the permissions and all of the information we need to talk to the USRP. Now its time to change the flowgraph to use a UHD USRP Source block as the source instead of the sig_source_f.
 
 
 
First, we need to pass the '''fd''' and '''usrpfs_path''' values to the flowgraph. Change the function signature in '''fg.cpp''' to this:
 
 
 
<syntaxhighlight lang="Java">JNIEXPORT void JNICALL
 
Java_org_gnuradio_grtemplateusrp_MainActivity_FgInit(JNIEnv* env,
 
                                                    jobject thiz,
 
                                                    int fd, jstring devname)</syntaxhighlight>
 
And in '''MainActivity.java''' where we declare the JNI functions, we need to add the two arguments:
 
 
 
<syntaxhighlight lang="Java">public native void FgInit(int fd, String usbfs_path);</syntaxhighlight>
 
And in '''SetupRadio''', make sure to pass this information as "FgInit(fd, usbfs_path)".
 
 
 
We then extract the information and format a device argument string for the usrp_source block:
 
 
 
<syntaxhighlight lang="Java">
 
#include <gnuradio/uhd/usrp_source.h>
 
 
 
....
 
 
 
  const char *usbfs_path = env->GetStringUTFChars(devname, NULL);
 
  std::stringstream args;
 
  args << "uhd,fd=" << fd << ",usbfs_path=" << usbfs_path;
 
  GR_INFO("fg", boost::str(boost::format("Using UHD args=%1%") % args.str()));
 
 
 
  uhd::stream_args_t stream_args;
 
  stream_args.cpu_format = "fc32";
 
  stream_args.otw_format = "sc16";
 
 
 
  ....
 
 
 
  gr::uhd::usrp_source::sptr src;
 
 
 
  ....
 
 
 
  src = gr::uhd::usrp_source::make(args.str(), stream_args);
 
 
 
  src->set_samp_rate(200e3);
 
  src->set_center_freq(101.1e6);
 
  src->set_gain(20); // adjust as needed</syntaxhighlight>
 
The '''src''' is now a complex source. So what I'll do is create a simple flowgraph that looks like:
 
 
 
<syntaxhighlight lang="Java">usrp_source -> complex_to_real -> multiply_const_ff -> opensl_sink</syntaxhighlight>
 
The full flowgraph '''FgInit''' looks like this
 
 
 
<syntaxhighlight lang="Java">// Get any GNU Radio headers
 
#include <gnuradio/top_block.h>
 
#include <gnuradio/uhd/usrp_source.h>
 
#include <gnuradio/blocks/complex_to_real.h>
 
#include <gnuradio/blocks/multiply_const_ff.h>
 
#include <grand/opensl_sink.h>
 
 
 
// Declare the global virtual machine and top-block objects
 
JavaVM *vm;
 
gr::top_block_sptr tb;
 
 
 
extern "C" {
 
 
 
JNIEXPORT void JNICALL
 
Java_org_gnuradio_grtemplateusrp_MainActivity_FgInit(JNIEnv* env,
 
                                                    jobject thiz,
 
                                                    int fd, jstring devname)
 
{
 
  GR_INFO("fg", "FgInit Called");
 
 
 
  const char *usbfs_path = env->GetStringUTFChars(devname, NULL);
 
  std::stringstream args;
 
  args << "uhd,fd=" << fd << ",usbfs_path=" << usbfs_path;
 
  GR_INFO("fg", boost::str(boost::format("Using UHD args=%1%") % args.str()));
 
 
 
  uhd::stream_args_t stream_args;
 
  stream_args.cpu_format = "fc32";
 
  stream_args.otw_format = "sc16";
 
 
 
  float samp_rate = 48e3;  // 48 kHz
 
 
 
  // Declare our GNU Radio blocks
 
  gr::uhd::usrp_source::sptr src;
 
  gr::blocks::complex_to_real::sptr c2r;
 
  gr::blocks::multiply_const_ff::sptr mult;
 
  gr::grand::opensl_sink::sptr snk;
 
 
 
  // Construct the objects for every block in the flowgraph
 
  tb = gr::make_top_block("fg");
 
  src = gr::uhd::usrp_source::make(args.str(), stream_args);
 
  c2r = gr::blocks::complex_to_real::make();
 
  mult = gr::blocks::multiply_const_ff::make(0.0);
 
  snk = gr::grand::opensl_sink::make(int(samp_rate));
 
 
 
  src->set_samp_rate(200e3);
 
  src->set_center_freq(101.1e6);
 
  src->set_gain(20); // adjust as needed
 
 
 
  // Connect up the flowgraph
 
  tb->connect(src, 0, c2r, 0);
 
  tb->connect(c2r, 0, mult, 0);
 
  tb->connect(mult, 0, snk, 0);
 
}
 
 
 
....</syntaxhighlight>
 
This flowgraph does nothing particularly useful with the USRP, but we should be able to listen to static. We are not event setting up the sample rate, frequency, or gain values, so the incoming samples have no connection to anything real.
 
 
 
For more complex usage of a USRP app, take a look at the [https://github.com/trondeau/GrRxFM GrRxFM], which uses a USRP to receive and demodulate broadcast FM signals. It sets up sliders to adjust the frequency and gain as well as handles sample rates and rate changes within the flowgraph.
 

Please note that all contributions to GNU Radio are considered to be released under the Creative Commons Attribution-ShareAlike (see GNU Radio:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel | Editing help (opens in new window)