ethierlab / Motopya

MotoTrak python Version
MIT License
0 stars 0 forks source link

RAM Optimization #3

Closed siamakhaz closed 1 month ago

siamakhaz commented 3 months ago

I suggest using fixed-size buffers to avoid consuming all the RAM and potentially crashing the program. Instead of a single buffer, I recommend creating two buffers. While one buffer is being filled, the other can be read and processed. where: https://github.com/ethierlab/Motopya/blob/main/RatTaskPyArduino/rat_task_arduinozero/rat_task_arduinozero.ino#L101C1-L102C67

std::vector<std::vector> tmp_value_buffer; // [time, value], first row is the oldest data std::vector<std::vector> trial_value_buffer; // [time, value]

siamakhaz commented 3 months ago

here are some suggestions :

#define BUFFER_SIZE 2000
int tmp_value_buffer[BUFFER_SIZE][2];
int trial_value_buffer[BUFFER_SIZE][2];
int tmp_index = 0;
int trial_index = 0;

// Reset buffer indices to reuse them
void resetBuffers() {
  tmp_index = 0;
  trial_index = 0;
}
inline int getTimerDuration(unsigned long start) {
  return millis() - start;
}
void recordCurrentValue() {
  trial_time = session_t - trial_start_time;
  trial_value_buffer[trial_index][0] = trial_time;
  trial_value_buffer[trial_index][1] = moduleValue_now;
  if (++trial_index >= BUFFER_SIZE) {
    sendTrialData2Python(false);
    trial_index = 0;
  }
  peak_moduleValue = max(peak_moduleValue, moduleValue_now);
}

Reuse Buffers:

Clear and reuse buffers instead of allocating new memory. Example:

trial_value_buffer.clear();
rudy-saal commented 1 month ago

We are no longer using arduino or C++ with it's limited RAM; we no longer need this issue.