Teeny 3.5 and nanopb

Hello everyone,

I'm working to implement nanopb into my project to easily share IMU data from the board over the USB serial connection (for now). After reading the documentation and finding other example I've created this

Code:
#include "MPU9250.h"

#include "imu.h"
#include "eigen_teensy.h"
#include "utility.h"

#include "simple.pb.h"

#include "pb_decode.h"
#include "pb_encode.h"

#include <Print.h>
#include <Stream.h>

class Print;
class Stream;

static bool pb_print_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count) {
    Print* p = reinterpret_cast<Print*>(stream->state);
    size_t written = p->write(buf, count);
    return written == count;
}

pb_ostream_s as_pb_ostream(Print& p) {
    return {&pb_print_write, &p, SIZE_MAX, 0};
}

static bool pb_stream_read(pb_istream_t *stream, pb_byte_t *buf, size_t count) {
    Stream* s = reinterpret_cast<Stream*>(stream->state);
    size_t written = s->readBytes(buf, count);
    return written == count;
}

pb_istream_s as_pb_istream(Stream& s) {
    #ifndef PB_NO_ERRMSG
        return {&pb_stream_read, &s, SIZE_MAX, 0};
    #else
        return {&pb_stream_read, &s, SIZE_MAX};
    #endif
}

/* extern "C" int main(void) */
int main(void) {
    
    AHRS::IMU imu;
    usb_serial_class serial_usb;
    serial_usb.begin(115200);

    AHRS_IMUMeasurement imu_msg = AHRS_IMUMeasurement_init_zero;

    pb_ostream_s pb_out = as_pb_ostream(serial_usb);
    serial_usb.println("starting");
    
    // loop forever
    while (1) {
        imu.imu.readSensor();
        imu_msg.accel_meas[0] = imu.imu.getAccelX_mss();
        imu_msg.accel_meas[1] = imu.imu.getAccelY_mss();
        imu_msg.accel_meas[2] = imu.imu.getAccelZ_mss();

        bool status = pb_encode(&pb_out, AHRS_IMUMeasurement_fields, &imu_msg);
        //serial_usb.println(imu.imu.getAccelX_mss(), 9);
        delay(1000);
    }
}

The idea seems to be to [bind a stream to an output device](https://jpa.kapsi.fi/nanopb/docs/concepts.html#streams) but I must not be applying it correctly. Here when I call `pb_encode` it returns without error and a true status flag, but there is no output on the Serial port.

I don't believe it's an issue with the IMU or serial connection as I can send the data out correctly by simply printing the string directly. There is an issue with how I'm calling the protobuf functions and defining my callback functions.

Can anyone provide some guidance on what I'm doing incorrectly here or suggestions on how to best utilize nanopb to send my own messages over serial?

Thank you
 
I know this post is two years old but I thought I would reply with what I've learned about nanopb.

It seems like OP didn't include the .pb.h and ph.c files that nanopb creates after the build. Nanopb expects you to create a proto file that has your message structure in it. That structure file is the proto file. You'll need to build nanopb with that file in the generator-bin folder. For windows I use cmd window to cd into that directory then use: protoc --nanopb_out=. messages.proto to build the .pb.h and .pb.c files. You can then copy those files into your project working directory.

If you're using platform PlatformIO like me it's really easy to add nanopb or better yet nanopb-ardunio into your project. Hope this helps someone.
 
Back
Top