#include #include #include #define MAX_LINE_LENGTH 100 void scale_data(int *g_x, int *g_y, double *rpm, int num_lines) { for (int i = 0; i < num_lines; i++) { double gx_dps = g_x[i] / 16.4; // Get degrees per second x double gy_dps = g_y[i] / 16.4; // get degrees per second y double magnitude_dps = sqrt(gx_dps * gx_dps + gy_dps * gy_dps); // find the combined magnitude rpm[i] = (magnitude_dps * 60) / 360; // Divide by 360 to get degrees per minute, mulitply by 60 to get rpm } } void make_out_file(int *hours, int *minutes, int *seconds, int *milliseconds, double *rpm, int num_lines) { // open file named rpm.txt FILE *output = fopen("rpm.txt", "w"); // print each line delimeted by a comma for (int i = 0; i < num_lines; i++) { fprintf(output, "%02d:%02d:%02d:%03d,%.6f\n", hours[i], minutes[i], seconds[i], milliseconds[i], rpm[i]); } fclose(output); // close file when write is done } int main() { FILE *file; char line[MAX_LINE_LENGTH]; int num_lines = 0; // Open the file file = fopen("motion.txt", "r"); // Count the number of lines in the file to create dynamic memory arrays while (fgets(line, sizeof(line), file)) { num_lines++; } // Store with dynamic arrays due to data size int *hours = (int *)malloc(num_lines * sizeof(int)); int *minutes = (int *)malloc(num_lines * sizeof(int)); int *seconds = (int *)malloc(num_lines * sizeof(int)); int *milliseconds = (int *)malloc(num_lines * sizeof(int)); int *g_x = (int *)malloc(num_lines * sizeof(int)); int *g_y = (int *)malloc(num_lines * sizeof(int)); double *rpm = (double *)malloc(num_lines * sizeof(double)); // Go to beginning of file fseek(file, 0, SEEK_SET); // Parse the file int line_index = 0; while (fgets(line, sizeof(line), file)) { // Goes until end of file sscanf(line, "%2d:%2d:%2d:%3d %*d,%*d,%*d,%d,%d", &hours[line_index], &minutes[line_index], &seconds[line_index], &milliseconds[line_index], &g_x[line_index], &g_y[line_index]); line_index++; } fclose(file); // file isn't needed anymore, so we can close it // Scale gyroscope data scale_data(g_x, g_y, rpm, num_lines); // Create comma delimited file named rpm.txt make_out_file(hours, minutes, seconds, milliseconds, rpm, num_lines); // Free allocated memory free(hours); free(minutes); free(seconds); free(milliseconds); free(g_x); free(g_y); free(rpm); return 0; }