PROS opcontrol not running code after the while loop

So I am using PROS for my robot. I am creating a logging function that will write a text file on the sd card. However the only ways I have been able to write and keep the changes on the file were closing the file. This would not be a problem however i am running the “write” function once per op control loop. I have seen other solutions about adding a hotkey to break out of the loop to run fclose, but i am wondering about the viability for this to work during a match. do i just inform my drivers that during the last ten seconds to press a button so that the file is saved. idk what to do lol.

Could you post a screenshot or code block of what you’re trying to do? That’ll help us understand your problem better :slight_smile:

	int32_t leftSpeed = 0;
	int32_t rightSpeed = 0;
	int32_t avgSpeed = 0;

	fstream operatorLogFile = createLogFile(uniqueLogName(controlMode::OPERATOR, &ruleFile));

	uint32_t startTime = millis();

	while (true) {
		rightSpeed = accelerate(deadzone(ANALOG_RIGHT_X, ANALOG_RIGHT_Y), rightSpeed);
		leftSpeed = accelerate(deadzone(ANALOG_LEFT_X, ANALOG_LEFT_Y), leftSpeed);

		if(master.get_analog(ANALOG_RIGHT_Y) != 0 || master.get_analog(ANALOG_LEFT_Y) != 0){
			switch(closeEnough(leftSpeed, rightSpeed)){
				case true:
					avgSpeed = (leftSpeed + rightSpeed)/2;
					leftMotorGroup = avgSpeed;
					rightMotorGroup = avgSpeed;
					break;

				case false:
					leftMotorGroup = leftSpeed;
					rightMotorGroup = rightSpeed;
					break;
			}
		} else {
			leftMotorGroup.brake();
			rightMotorGroup.brake();
		}

		if(master.get_digital(DIGITAL_Y) != 0){ // current solution to get the log to save
			closeAndUpdateRuleFile(&ruleFile); // used to call break; but this allows us to keep driving after saving the log.
			operatorLogFile.close();
		}

		level logLevel = determineLevel({leftMotorGroup[0], leftMotorGroup[1], leftMotorGroup[2]}, {rightMotorGroup[0], rightMotorGroup[1], rightMotorGroup[2]}, master);
		ostringstream payload = formulateDataString({leftMotorGroup[0], leftMotorGroup[1], leftMotorGroup[2]}, {rightMotorGroup[0], rightMotorGroup[1], rightMotorGroup[2]}, master, millis() - startTime);
		writeLine(&operatorLogFile, &payload, logLevel);

		pros::delay(20);
	}

so basically my current solution is to have a “keybind” for saving the file but ideally i would want it so that it save automatically when i close the program during a match or when testing

Here is a method my team wrote to save the value of the left joystick. It runs inside the main opcontrol loop. It creates an ofstream and then directs the data to the file and closes it. If your opcontrol loop has a 10ms delay, this will record data every 10 ms. I do not recommend logging data any more frequent than that, partially because it is not necessary and partially because I haven’t tested it, and I am afraid it may try to write to much information in too short of a period of time.

void logData(double leftJoy){
	//saves all data to sd card
	std::ofstream dataFile;
	dataFile.open("/usd/data/data.csv", std::ios_base::app);
	dataFile << float(leftJoy) << std::endl;
	dataFile.close();
}

In the code, the folder data is accessed on the sd card in append mode, meaning it adds to any existing data in the file, or creates the file if it does not exist. The file is then given the left joystick value and the end line character, moving to the next line. If you want to log multiple values in a csv, something my team often did, you could modify the code to something like

dataFile << item1 << ", " << item2 << std:endl;

and so on. This removes the need for a keybind of any sort and will always save the data to the sd card. The method can also be run in any loop of code, such as autonomous or any while/for loop you would wish to have.

Ah I see what you mean. Unfortunately, I believe that there isn’t a way to run something off of when you close the program because iirc that literally just kills all running processes. Instead, you should research a way to have it save in a different way from completely closing the file (I’ve never used SD cards before so I’m not an expert there by any means). Good luck!

Yea I am going to stick with my current hotkey solution. I wish it wouldn’t kill all processes when stopping. i mean basically so that code after the while loop can run. that would be nice imo. I might look into async stuff but idk i have a working solution so im gonna stick with it. if it aint broke dont fix it kinda stuff

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.