Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

decode - NanoPB Help! encoding nested repeated items and not sure I have the schema defined properly

I am defining a breakdown for a battery pack. Packs are made of one or more Modules. Modules are made of 6 Cells.

I have been able to get the pack information minus the repeated modules. Would love the help with understanding encode/decode and verifying that my schema is laid out correctly. Thanks in advance!!!

syntax = "proto2";

package TeslaBMS;

message Pack { 
  optional string packName = 1
  required int32 numberOfModules = 2;
  required float currentVoltage = 3;
  required float averagePacktemp = 4;
  repeated Module modules = 5;

    message Module {
      required int32 id = 1;
      required float moduleVoltage = 2;
      required float moduleTemp = 3;
      required float lowestCellVolt = 4;
      required float highestCellVolt = 5;
      repeated Cell cells = 6;

    message Cell{
         required int32 cellId = 1;
         required float cellVolt = 2;
         required string balanceState = 3;
    }
  }
}

Which produces these Struct defs

/* Struct definitions */
typedef struct _TeslaBMS_Pack {
    pb_callback_t packName;
    int32_t numberOfModules;
    float currentVoltage;
    float averagePacktemp;
    pb_callback_t modules;
} TeslaBMS_Pack;

typedef struct _TeslaBMS_Pack_Cell {
    int32_t cellId;
    float cellVolt;
    pb_callback_t balanceState;
} TeslaBMS_Pack_Cell;

typedef struct _TeslaBMS_Pack_Module {
    int32_t id;
    float moduleVoltage;
    float moduleTemp;
    float lowestCellVolt;
    float highestCellVolt;
    pb_callback_t cells;
} TeslaBMS_Pack_Module;

Here is what I have for my encode/decode based on examples I could find.

Encode:

void encoder(){
    // Setup pack message
    TeslaBMS_Pack mypack = TeslaBMS_Pack_init_zero;
    // Setup module message
    TeslaBMS_Pack_Module myModule = TeslaBMS_Pack_Module_init_zero;
    // Setup cell message
    TeslaBMS_Pack_Module_Cell myCells = TeslaBMS_Pack_Module_Cell_init_zero;
    // stream to write buffer
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    
  

    // mypack.packName = userinput yet to be defined 
    mypack.averagePacktemp = bms.getAvgTemperature();
    mypack.currentVoltage = bms.getPackVoltage();
    printf("Pack Voltage: 
");
    printf(" %.3f 
", mypack.currentVoltage);
    printf("Average Temp: 
");
    printf(" %.3f 
", mypack.averagePacktemp);
    mypack.numberOfModules = bms.getNumOfModules();
    



    // get module val?
    // for(int i = 0; i < mypack.numberOfModules; i++){
    //     BMSModule currentMod = bms.getModules(i);
    //     myModule.id = i;
    //     myModule.moduleVoltage = currentMod.getAverageV();
    //     myModule.highestCellVolt = currentMod.getHighCellV();
    //     myModule.lowestCellVolt = currentMod.getLowCellV();
    //     myModule.moduleTemp = currentMod.getAvgTemp();
    // }


    
    //encode
        // modules
    if (!(pb_encode_submessage(&stream, TeslaBMS_Pack_Module_fields, &mypack.modules))){
        printf("pb_encode_submessage Failed!
");
        return;
    }
        // cells
    // (! pb_encode_submessage(&ostream, TeslaBMS_Pack_Module_Cell_fields, &mypack.modules.cells)){
    //     printf("pb_encode_submessage Failed!
");
    //     return;
    // }

    if (!(status = pb_encode(&stream, TeslaBMS_Pack_fields, &mypack))){
        // message_length = ostream.bytes_written;

        /* Then just check for any errors.. */
        printf("Encoding failed: %s
", PB_GET_ERROR(&stream));
    }
            
}

Decode:

TeslaBMS_Pack mypack = TeslaBMS_Pack_init_default;
            /* Create a stream that reads from the buffer. */
            pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
            
            /* Now we are ready to decode the message. */
            status = pb_decode(&stream, TeslaBMS_Pack_fields, &mypack);
            
            /* Check for errors... */
            if (!status)
            {
                printf(" Decoding failed: %s
", PB_GET_ERROR(&stream));
            
            }
            
            /* Print the data contained in the message. */
            printf("********MESSAGE FROM NANOPB!*********
");
            // printf("Pack Name: ", myPack.packName);
            printf("Pack Voltage: 
");
            printf(" %.3f 
", mypack.currentVoltage);
            printf("Average Temp: 
");
            printf(" %.3f 
", mypack.averagePacktemp);
            printf("Number of Modules: 
");
           
           //TODO: add a conditional to display the number of modules only if more than one.
            printf(" %.3f 
", mypack.numberOfModules);
            
            // for (TeslaBMS_Pack_Module mod : TeslaBMS_Pack_Module_fields.){
            //     printf("Pack Voltage: 
");
            //     printf(" %.3f 
", mypack.currentVoltage);
            //     printf("Average Temp: 
");
            //     printf(" %.3f 
", mypack.averagePacktemp);
            //     printf("Number of Modules: 
");
            //     printf(" %.3f 
", mypack.numberOfModules);
            // // }
            printf("************************************
" );

        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...