Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReceivedDataStructure - can't seem to figure out how to make it work #4

Closed
mjs513 opened this issue Aug 11, 2018 · 7 comments
Closed

Comments

@mjs513
Copy link

mjs513 commented Aug 11, 2018

@fabolhak
I changed the subscriber sketch to print the message out using ReceivedDataStructure as in the example on the Subscriber tutorial on the UAVcan website. While I got the subscriber started and the sketch runs I can't seem to get the message read an printed using ReceivedDataStructure: here is new subscriber I am using:

#ifndef	SUBSCRIBER_HPP
#define	SUBSCRIBER_HPP

#include <uavcan/uavcan.hpp>
#include <uavcan/dsdlc/equipment/gnss/Fix.hpp>
#include <uavcan/helpers/ostream.hpp>

using namespace uavcan;
using namespace equipment;
using namespace gnss;



void initSubscriber(Node<NodeMemoryPoolSize> *node)
{
  // create a subscriber
    Subscriber<Fix> Fix_data_Subscriber(*node);

    const int res = Fix_data_Subscriber.start(
        [&](const ReceivedDataStructure<Fix>& msg)
        {
            /*
             * The message will be streamed in YAML format.
             */
            //std::cout << msg << std::endl;
            /*
             * If the standard iostreams are not available (they rarely available in embedded environments),
             * use the helper class uavcan::OStream defined in the header file <uavcan/helpers/ostream.hpp>.
             */
            OStream::instance() << msg << OStream::endl;
        });

  if(res < 0)
  {
    Serial.print("Unable to start subscriber!  ");
    Serial.println(res);
  } else {
    Serial.println("Subscriber Started");
  }
}

#endif

Any suggestions would be appreciated. I am going to keep playing with it just in case I missed something.

BTW: only spinOnce works if I try and use something in the form of const int res = node.spin(uavcan::MonotonicDuration::getInfinite()); it gives me an error code of 4.
Thanks
Mike

@pavel-kirienko
Copy link

I changed the subscriber sketch to print the message out using ReceivedDataStructure as in the example on the Subscriber tutorial on the UAVcan website.

ReceivedDataStructure<> provides some additional metadata about the data structure, such as the source node ID, the interface index, the timestamp, and so on; if you don't need that, you can print the message itself directly ([&](const Fix& msg)).

Your code doesn't work because the subscriber instance is destroyed when your initSubscriber() returns.

only spinOnce works if I try and use something in the form of const int res = node.spin(uavcan::MonotonicDuration::getInfinite()); it gives me an error code of 4.

Error code 4 means that the driver has returned an error: https://github.com/UAVCAN/libuavcan/blob/0dc5751ffd905f63096fb4d15485e1a1510635e5/libuavcan/include/uavcan/error.hpp#L28
Could be related to timeout handling.

@mjs513
Copy link
Author

mjs513 commented Aug 12, 2018

Hi Pavel
Thanks for getting back to me.

ReceivedDataStructure<> provides some additional metadata about the data structure, such as the source node ID, the interface index, the timestamp, and so on;

Read that and wanted to see if I could print it out using the ostream method that I saw in the subscriber example I was looking at: https://uavcan.org/Implementations/Libuavcan/Tutorials/3._Publishers_and_subscribers/. Running the subscriber from callback works great.

Your code doesn't work because the subscriber instance is destroyed when your initSubscriber() returns.

So if I put // create a subscriber Subscriber<Fix> Fix_data_Subscriber(*node); outside the function that should work? I will give it a try.

Could be related to timeout handling.

Will check that one out.

@pavel-kirienko
Copy link

So if I put // create a subscriber Subscriber Fix_data_Subscriber(*node); outside the function that should work? I will give it a try.

Yes, or just make it static.

@mjs513
Copy link
Author

mjs513 commented Aug 12, 2018

Ok. After a couple of tries I got it working with this layout for initSubscriber::

   static Subscriber<Fix> Fix_data_Subscriber(*node);

    const int res = Fix_data_Subscriber.start(
        [&](const ReceivedDataStructure<Fix>& msg)
        {
            /*
             * The message will be streamed in YAML format.
             */
            //std::cout << msg << std::endl;
            /*
             * If the standard iostreams are not available (they rarely available in embedded environments),
             * use the helper class uavcan::OStream defined in the header file <uavcan/helpers/ostream.hpp>.
             */
            Serial.println(msg.longitude_deg_1e8/1e8); 
            OStream::instance() << msg << OStream::endl;
        });

It does printout the longitude piece of the msg. But ostream is not printing. But I think that is a function of the Teensy compiler and printf function not implemented the way it should be.

I will work the timeout issue after I get this working.

@fabolhak
Copy link
Contributor

Hey,
for debugging on embedded devices like Arduino and Teensy I typically use Serial.println(). I'm not sure if std::out or printf() also work, but I never saw that before. I guess someone tries to implement it here. But I don't really see a point in doing that. Serial.println() works pretty fine.

@fabolhak
Copy link
Contributor

Since this is not really an issue related to this repository I will close it for now (you may reopen if you think it is valid here).

@mjs513
Copy link
Author

mjs513 commented Aug 12, 2018

@fabolhak
Well maybe it is an issue with the repsository - don't know. The fix was to remove the following lines of code from the phoenix_can_shield.h:

// this is important for UAVCAN to compile
extern "C"{
  int _getpid(){ return -1;}
  int _kill(int pid, int sig){ return -1; }
  int _write(){return -1;}
}

It compiles fine without it. Also had to add #define printf Serial.printf to the main sketch or in the subscriber.h file as well as deleting the code. As for the time out issue still working that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants