//Returns the most frequently occuring element //or -1 if there is no such unique element int64_t get_mode(int64_t* vector) { if (g_existCalcs[arg1].modeFlag == 1) return g_existCalcs[arg1].mode; g_existCalcs[arg1].modeFlag = 1; //Mode of a uniform vector is any element within it if (g_length == 1 || g_vec_properties[arg1][0] == UNIFORM) { g_existCalcs[arg1].mode = vector[0]; return vector[0]; } //All vectors with unique values have no mode else if (g_vec_properties[arg1][0] == SEQUP || g_vec_properties[arg1][0] == SEQDOWN || g_vec_properties[arg1][0] == PRIME) { g_existCalcs[arg1].mode = -1; return -1; } int64_t max = get_maximum(vector); int64_t min = get_minimum(vector); if ((max - min) < 2147483648) //16gb can hold 2147483648 int64s //12gb can hold 1610612736 int64s //8gb can hold 1073741824 int64s //4gb can hold 536870912 int64s return fast_mode(vector, max, min); int64_t* result = cloned(vector); qsort(result, g_length, sizeof(int64_t), int64Ascend); int64_t curSpanCount = 0, hiSpanCount = 0, currentCheck = 0, repeatCount = 0, mode = 0; for (int64_t i = 0; i < g_length; i++) { if (currentCheck != result[i]) { currentCheck = result[i]; curSpanCount = 0; } curSpanCount++; if (curSpanCount > hiSpanCount) { repeatCount = 0; hiSpanCount = curSpanCount; mode = currentCheck; } else if (currentCheck != result[i+1] && curSpanCount == hiSpanCount) { repeatCount = 1; } } if (repeatCount > 0) { g_existCalcs[arg1].mode = -1; return -1; } g_existCalcs[arg1].mode = mode; return mode; }
int main(void) { setup(); uart_init(); TWI_Init(); input = getchar(); while((input != 's'))//wait for user input to begin program { input = getchar(); } sei();//global interrupts on IMU_setup(); printf("\nLet's Begin\n\nChoose an option:\n\n space bar - PID (loops forever)\n 'm' - manual control (loops forever)\n 'i' - check IMU\n 'x' - get x acceleration\n 'y' - get y acceleration\n 'z' - get z acceleration\n 'f' - bluetooth fast mode\n 't' - test IMU write\n 'a' - enter acceleration mode\n"); while((1)) { input = getchar(); if ((input == ' '))//PID algorithm { output_timer_on(); while((1)) { PID(); } } else if ((input == 'm'))//manual control { printf("\nManual Mode:\n\nInstructions:\n\n 'n' - forwards\n 'v' - reverse\n 'b' - stop\n '1-9' - use the number keys to select the power level\n"); while((1)) { manual(); } } else if ((input == 'i'))//check WHO_AM_I register { check_imu(); } else if ((input == 'x'))//get x direction acceleration { acceleration = get_accel('x'); print_float(acceleration); printf("\n"); } else if ((input == 'y'))//get y direction acceleration { acceleration = get_accel('y'); print_float(acceleration); printf("\n"); } else if ((input == 'z'))//get z direction acceleration { acceleration = get_accel('z'); print_float(acceleration); printf("\n"); } else if ((input == 'f'))//place RN-42 HID into fast mode { fast_mode(); } else if ((input == 't'))//write a regsiter of the IMU and check it worked { TWI_WRITEBYTE(MPU6050_PWR_MGMT_1, 0x02); if ((TWI_READBYTE(MPU6050_PWR_MGMT_1) == 0x02)) printf("\nSuccess\n"); else printf("\nFailure\n"); } else if ((input == 'a'))//acceleration mode for acquiring data { acceleration_mode(); } } }