/** * If we set some values using the Preferences class, test that they show up * in networktables.ini */ TEST(PreferencesTest, WritePreferencesToFile) { NetworkTable::Shutdown(); NetworkTable::GlobalDeleteAll(); // persistent keys don't get deleted normally, so make remaining keys // non-persistent and delete them too for (const auto& info : nt::GetEntryInfo("", 0)) { nt::SetEntryFlags(info.name, 0); } NetworkTable::GlobalDeleteAll(); std::remove(kFileName); NetworkTable::Initialize(); Preferences* preferences = Preferences::GetInstance(); preferences->PutString("testFilePutString", "Hello, preferences file"); preferences->PutInt("testFilePutInt", 1); preferences->PutDouble("testFilePutDouble", 0.5); preferences->PutFloat("testFilePutFloat", 0.25f); preferences->PutBoolean("testFilePutBoolean", true); preferences->PutLong("testFilePutLong", 1000000000000000000ll); preferences->Save(); Wait(kSaveTime); static char const* kExpectedFileContents[] = { "[NetworkTables Storage 3.0]", "boolean \"/Preferences/testFilePutBoolean\"=true", "double \"/Preferences/testFilePutDouble\"=0.5", "double \"/Preferences/testFilePutFloat\"=0.25", "double \"/Preferences/testFilePutInt\"=1", "double \"/Preferences/testFilePutLong\"=1e+18", "string \"/Preferences/testFilePutString\"=\"Hello, preferences file\""}; std::ifstream preferencesFile(kFileName); for (auto& kExpectedFileContent : kExpectedFileContents) { ASSERT_FALSE(preferencesFile.eof()) << "Preferences file prematurely reached EOF"; std::string line; std::getline(preferencesFile, line); ASSERT_EQ(kExpectedFileContent, line) << "A line in networktables.ini was not correct"; } }
Shooter::Shooter( MyRobot& theRobot, int bottom_motor_channel, int top_motor_channel, int bottom_geartooth_channel, int top_geartooth_channel, int injector_channel ) : m_robot(theRobot), motor_bottom(bottom_motor_channel), motor_top(top_motor_channel), geartooth_bottom(bottom_geartooth_channel), geartooth_top(top_geartooth_channel), injector(injector_channel), pid_p(SHOOTER_P), pid_i(SHOOTER_I), pid_d(SHOOTER_D), drive_ratio(DRIVE_RATIO), tolerance(TOLERANCE), shot_time(SHOT_TIME), release_time(RELEASE_TIME), pid_bottom( pid_p, pid_i, pid_d, &geartooth_bottom, &motor_bottom ), pid_top( pid_p, pid_i, pid_d, &geartooth_top, &motor_top ), m_auto(false), m_speed(0.0F), speed_bottom(0.0F), speed_top(0.0F), running(false), shooting(kIdle) { Preferences *pref = Preferences::GetInstance(); bool saveNeeded = false; printf("In Shooter constructor, pref = 0x%p\n", pref); if (!pref->ContainsKey( "Shooter.pid_p" )) { pref->PutDouble( "Shooter.pid_p", SHOOTER_P ); printf("Preferences: save P\n"); saveNeeded = true; } if (!pref->ContainsKey( "Shooter.pid_i" )) { pref->PutDouble( "Shooter.pid_i", SHOOTER_I ); printf("Preferences: save I\n"); saveNeeded = true; } if (!pref->ContainsKey( "Shooter.pid_d" )) { pref->PutDouble( "Shooter.pid_d", SHOOTER_D ); printf("Preferences: save D\n"); saveNeeded = true; } if (!pref->ContainsKey( "Shooter.drive_ratio" )) { pref->PutDouble( "Shooter.drive_ratio", DRIVE_RATIO ); printf("Preferences: save drive_ratio\n"); saveNeeded = true; } if (!pref->ContainsKey( "Shooter.tolerance" )) { pref->PutDouble( "Shooter.tolerance", TOLERANCE ); printf("Preferences: save tolerance\n"); saveNeeded = true; } if (!pref->ContainsKey( "Shooter.shot_time" )) { pref->PutDouble( "Shooter.shot_time", SHOT_TIME ); printf("Preferences: save shot_time\n"); saveNeeded = true; } if (!pref->ContainsKey( "Shooter.release_time" )) { pref->PutDouble( "Shooter.release_time", RELEASE_TIME ); printf("Preferences: save release_time\n"); saveNeeded = true; } if (saveNeeded) { pref->Save(); printf("Preferences: saved\n"); } InitShooter(); }