Results 1 to 7 of 7

Thread: Running C code

  1. #1
    Join Date
    Feb 2010
    Posts
    6

    Default Running C code

    A few questions about the JNA:

    (1) Is there any comprehensive tutorial/explanation of it, aside from the API?
    (2) What's the difference between a BlockingFunction and a Function?
    (3) How do you pass a double to a native function that takes in 6 arguments? Do you simply do this:

    Pointer my_double=new Pointer(8);
    my_double.setDouble(0,0.8);
    native_func.call6(blah,blah,blah,blah,blah,my_doub le.address().toUWord().toPrimitive());

  2. #2
    Join Date
    Jul 2009
    Posts
    25

    Default Re: Running C code

    (1) There is not a comprehensive tutorial, but there is a lot of examples in WPILibJ itself. You can search for calls to various methods, or look in the fpga, image, and communication packages.

    The JNA api is roughly based on the JNA project at https://jna.dev.java.net/javadoc/overview-summary.html. But the version there uses Java SE features such as reflection that take care of a lot of issues that we currently have to write "boiler plate" code for.

    (2) Use BlockingFunction if you are calling a C function that may take a long time (a long computation), or may block waiting for IO, user input, other threads, etc. If you know that the C function is reasonably quick, you can use a plain Function. But all java threads will pause until the Function call returns.

    (3) It turns out that passing doubles on the cRIO/PPC is more complicated than we'd like. The ABI (the parameter passing conventions) for floating point values uses different registers than for integers/pointers.

    The example code you showed would work IF the function was expecting a pointer to a double.

    If you wanted to pass doubles by value, you'd have to create a wrapper function in C that expects a long (really a long long), then converts the long long bits to double bits and calls the correct C function.

    So the dance is something like:

    [in Java]
    void foo (double bar) {
    long dbits = Double.doubleToLongBits(bar);
    int var1 = (int)(dbits >>> 32);
    int var2 = (int)(dbits);
    fooWrapperPtr.call2(var1, var2);
    }

    [in C]
    union uu2 { long long l; unsigned int lParts[2]; double d; };
    INLINE double lb2d(long long l) { union uu2 x; x.l = l; return x.d; }

    void foo(double bar) {
    ...
    }

    void fooWrapper(long long bar) {
    foo(lb2d(bar));
    }


    Not pretty :-(

  3. #3
    Join Date
    Feb 2010
    Posts
    6

    Default Re: Running C code

    Thank you SO MUCH for that response. How do I run C code on a robot imaged for Java? I've been trying to do this for weeks; if I knew how, I wouldn't be fiddling with JNA in the first place.

    If I don't need the robot to run C code, how do I do what you suggested (write a C wrapper function)?

    Please respond as soon as possible; I'm kind of desperate right now!
    Last edited by ideasrule; 02-16-2010 at 06:57 PM.

  4. #4
    Join Date
    Jul 2009
    Posts
    25

    Default Re: Running C code

    I'm a bit confused as to what you are trying to do.

    If you simply want to run C++ code, you'd have probably already re-imaged it and be done with it.

    If you really want to run a C robot, then I'm not sure what to say.

    If you want to run some C code and some Java code, then the JNA path is the right one. THIS IS NOT A WELL TRAVELED PATH - ISSUES MAY TURN UP THAT PREVENT SUCCESS! You would need to compile some C code using wind river, and create a .out file. Then ftp that file over to the crio. You can then load that code by editing the RIO file ni-rt.ini, and adding your .out file to the list of StartupDLLs. Note that re-imaging the cRIO will probably overwrite this file so you'll have to re-edit it.

    That's a rough outline - I can't help with the details though. You can ask on the C++ forums, and/or bug Brad Miller.

  5. #5
    Join Date
    Feb 2010
    Posts
    6

    Default Re: Running C code

    Quote Originally Posted by derekwhite View Post
    I'm a bit confused as to what you are trying to do.
    Long story short, I want to use the vision libraries, which are written in C, but we want to use Java for most of the programming. One of the vision functions--namely, MatchShape--requires a double as one of its parameters.
    If you want to run some C code and some Java code, then the JNA path is the right one. THIS IS NOT A WELL TRAVELED PATH - ISSUES MAY TURN UP THAT PREVENT SUCCESS! You would need to compile some C code using wind river, and create a .out file. Then ftp that file over to the crio. You can then load that code by editing the RIO file ni-rt.ini, and adding your .out file to the list of StartupDLLs. Note that re-imaging the cRIO will probably overwrite this file so you'll have to re-edit it.
    Thanks! After I do that, do I simply write in the Java code:

    BlockingFunction myFunction = NativeLibrary.getDefaultInstance().getBlockingFunc tion("myfunc");

    where myfunc could be whatever function I had in the .out file?

  6. #6
    Join Date
    Jul 2009
    Posts
    25

    Default Re: Running C code

    Yes, that should do it.

  7. #7
    Join Date
    Feb 2010
    Posts
    6

    Default Re: Running C code

    Sorry to bug you again. Can you take a look at my question over at the C/C++ forums? I've done something very similar to what I described, but it didn't work.

    I'm pretty desperate, as tomorrow is the last day we have to work on the robot.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Compressor Code Problem
    By mws7067 in forum C/C++
    Replies: 3
    Last Post: 02-08-2010, 11:54 PM
  2. No Robot Code error
    By rjh in forum C/C++
    Replies: 1
    Last Post: 01-25-2010, 11:12 PM
  3. vision code
    By 365Lucie in forum C/C++
    Replies: 0
    Last Post: 01-22-2010, 01:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •