Save and load text file onto/from SD card

Hm, ok, sorry I asked that. There are way too many false starts in that code for me to get into it tonight, it’s hard to see what the latest is with 1500 lines of code, however, I think it’s worth your while to take a step back and try to understand what you code is actually trying to do.

It’s basic rerun code where you are recording the controller values (and perhaps some others). You record everything into a big array during driver, and then play back the contents of the array during autonomous, so far so good. The piece you are struggling with is saving/loading. You can go the C++ std::ostream way if you want, but it’s really overkill, all you need to do is have functionality that saves and loads that big RAM array to the SD card.

Have a look at this demo, see what I’m doing, it’s a very simple 4 motor rerun (no clever stuff here at all).

We record into an array (RAM) during driver, only 15 seconds of data is recorded. Then it saves to SD card. When auton triggers, the data is loaded from SD card and used as playback, essentially what you are trying to do.

code
/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       james                                                     */
/*    Created:      Mon Feb 21 2022                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/
#include "vex.h"

using namespace vex;

// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain       Brain;
// A global instance of vex::competition
vex::competition Competition;

vex::controller  Controller1;

vex::motor       m1(PORT1);
vex::motor       m2(PORT2);
vex::motor       m3(PORT3);
vex::motor       m4(PORT5);

#define         LOOP_DELAY    20          // 20mS delay in each rec/play loop
#define         MAX_SAMPLES  ((1000/LOOP_DELAY) * 15)

// storage for the rerun data
int8_t          auton_data[4][MAX_SAMPLES];  // 15 seconds at 50Hz, 4 axis

void autonomous( void ) {
    // Load from SD card
    int nRead = Brain.SDcard.loadfile( "auton.dat", (uint8_t *)auton_data, sizeof(auton_data));

    if( nRead == sizeof(auton_data) ) {
      // we are good

      for(int index =0;index<MAX_SAMPLES;index++ ){
        Brain.Screen.printAt(10, 60, "Play... %d", index );
    
        int8_t a1 = auton_data[0][index];
        int8_t a2 = auton_data[1][index];
        int8_t a3 = auton_data[2][index];
        int8_t a4 = auton_data[3][index];

        m1.spin(forward, a1, rpm );
        m2.spin(forward, a2, rpm );
        m3.spin(forward, a3, rpm );
        m4.spin(forward, a4, rpm );

        // same delay as before
        vex::task::sleep(LOOP_DELAY); 
      }
    }

    // we are finished
    m1.stop();
    m2.stop();
    m3.stop();
    m4.stop();
}

void usercontrol( void ) {
  int index = 0;

  // User control code here, inside the loop
  while (1) {
    Brain.Screen.printAt(10, 20, "Recording... %d", index );
    int8_t  a1 = Controller1.Axis1.value();
    int8_t  a2 = Controller1.Axis2.value();
    int8_t  a3 = Controller1.Axis3.value();
    int8_t  a4 = Controller1.Axis4.value();

    m1.spin(forward, a1, rpm );
    m2.spin(forward, a2, rpm );
    m3.spin(forward, a3, rpm );
    m4.spin(forward, a4, rpm );

    if( index < MAX_SAMPLES ) {
      auton_data[0][index] = a1;
      auton_data[1][index] = a2;
      auton_data[2][index] = a3;
      auton_data[3][index] = a4;
      index++;
    }
    else {
      // we are finished
      m1.stop();
      m2.stop();
      m3.stop();
      m4.stop();
      // save to SD card
      // this should probably be on a button
      Brain.SDcard.savefile( "auton.dat", (uint8_t *)auton_data, sizeof(auton_data));
      Brain.Screen.printAt(10, 40, "Done..." );
      return;
    }

    vex::task::sleep(LOOP_DELAY); 
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
    //Set up callbacks for autonomous and driver control periods.
    Competition.autonomous( autonomous );
    Competition.drivercontrol( usercontrol );
    
    //Prevent main from exiting with an infinite loop.                        
    while(1) {
      vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
    }           
}

I’ll try and decipher your program tomorrow, too late today.