Hello,
I'm writing code to measure the serialization speed of nanopb, a protocol buffers implementation in c for embedded systems.
The code takes values from a sensor and assigns them to a protocol buffers message.
Then this message is serialized by nanopb. The serialization time is measured by Arduino Chrono library which gives me the time as an unsinged long, which works fine so far.
Since im repeating the measuring several times i want to store the time of each serialization procedure in an array, so i can compute the average time it takes.
To do this i created a global int array like this:
int serializationTime[100];
But whenever i try to assign a value to this array from any of my functions the programm stops working.
It compiles without errors, but the serial monitor only outputs some characters of the first println like this:
NQNEN⸮N⸮N⸮NENENEN⸮N⸮NENENEN⸮NENEN⸮N⸮N⸮NENENEN⸮NENENENENENEN⸮N⸮NEN⸮⸮⸮N⸮N⸮NENENENE⸮⸮NEW LOOP
This is my code:
#include <Chrono.h>
//Proto generated file
#include "./src/serializationResults.pb.h"
#include "./src/testmessages.pb.h"
//Nanopb
#include "pb_common.h"
#include "pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include "./src/generateTestmessage.h"
//Define Pin for analog Light sensor input
#define LIGHT_INPUT A5
#define SENSOR_ARRAY_SIZE 24
//Treshold for number of readings
#define TRESHOLD 100
//Chrono setup
Chrono myChronoMicros(Chrono::MICROS);
//Proto message setup
//Create testmessage for all data types
AllTypesMessage message = AllTypesMessage_init_zero;
//Create array for sensor values
//Limit array size to number of fields in message
int sensorValues[SENSOR_ARRAY_SIZE];
//Create global array to store results
int serializationTime[TRESHOLD];
bool done = false;
int getLightSensorData() {
return analogRead(LIGHT_INPUT);
}
void printResultData() {
for (int i = 0; i < TRESHOLD; i++) {
Serial.println(serializationTime[i]);
}
}
//Serialize the message
void serializeData() {
unsigned long timePassed;
int counter = 0;
while (counter < TRESHOLD) {
//uint8_t : unsigned integer of length 8 bits
uint8_t buffer[128];
//Construct output stream for writing into a memory buffer
//1st input: buffer 2nd input: maximum number of bytes to write -> size of buffer
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
//Start timer to measure serialization time
myChronoMicros.restart();
//Call pb_encode function for encoding
//1st argument: address of pb_ostream_t variable, 2nd argument: autogenerated fields description array, 3rd argument: address of TestMessage struct
//Output: boolean true=encoding successful false= encoding failed
bool status = pb_encode(&stream, AllTypesMessage_fields, &message);
//Stop timer and get result
myChronoMicros.stop();
timePassed = myChronoMicros.elapsed();
if (!status)
{
Serial.println("Failed to encode");
return;
}
//Add results to result array
serializationTime[counter] = timePassed;
Serial.println(serializationTime[counter]);
counter++;
}
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < SENSOR_ARRAY_SIZE; i++) {
sensorValues[i] = getLightSensorData();
}
message = generateTestMessage(sensorValues);
}
void loop() {
Serial.println("NEW LOOP");
if (done == false) {
serializeData();
printResultData();
done = true;
}
}
Im trying to assign the value of timePassed to the serializationTime array at the bottom of the serializeData() function.
Can anyone please tell me what I'm doing wrong, since im pretty new to arduino and the c language.
Sidenote: I get the same problem if i create a proto message for Result with a repeated serializationTime field with max_count:100 (int32_t serializationTime[100]; ) and try to assign values to this array, so I guess I'm doing something wrong with the assignment of a value to an array?
Thanks in advance for any help.