Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MODSERIAL FATFileSystem
ConfigFileIO/ConfigFileIO.cpp
- Committer:
- tnhnrl
- Date:
- 2017-12-21
- Revision:
- 38:83d06c294807
- Parent:
- 21:38c8544db6f4
- Child:
- 43:891baf306e0a
File content as of revision 38:83d06c294807:
#include "ConfigFileIO.hpp"
#include "StaticDefs.hpp"
//configuration file io, both to read, set, and save
ConfigFileIO::ConfigFileIO() {
}
void ConfigFileIO::saveBattData(float p_gain, float i_gain, float d_gain) {
ConfigFile write_Batt_txt; //write to the config file
char PGain_buffer[128];
sprintf(PGain_buffer,"# configuration file for battery mover parameters\n\n#Gains\nPGain");
char string_pgain[128];
sprintf(string_pgain, "%f", p_gain);
write_Batt_txt.setValue(PGain_buffer, string_pgain);
char string_igain[128];
sprintf(string_igain, "%f", i_gain);
write_Batt_txt.setValue("IGain", string_igain);
char string_dgain[128];
sprintf(string_dgain, "%f", d_gain);
write_Batt_txt.setValue("DGain", string_dgain);
write_Batt_txt.setValue("\n#string pot parameters\nzeroCounts", "610");
write_Batt_txt.setValue("PistonTravelLimit", "73.0");
write_Batt_txt.setValue("slope", "0.12176");
write_Batt_txt.setValue("filterWn", "6.0");
write_Batt_txt.setValue("deadband", "0.5");
//SAVE THE DATA!
pc().printf("Saving BATTERY MOVER PID data!");
if (!write_Batt_txt.write("/local/batt.txt")) {
pc().printf("\n\rERROR: (SAVE)Failure to write batt.txt file.");
}
else {
pc().printf("\n\rFile batt.txt successful written.\n\r");
}
}
void ConfigFileIO::savePitchData(float p_gain, float i_gain, float d_gain, int batt_zeroOffset) {
ConfigFile write_pitch_txt; //write to the config file
char PGain_buffer[128];
sprintf(PGain_buffer,"# pitch outer loop parameters\n\n#Gains\nPGain");
char string_pgain[128];
sprintf(string_pgain, "%f", p_gain);
write_pitch_txt.setValue(PGain_buffer, string_pgain);
char string_igain[128];
sprintf(string_igain, "%f", i_gain);
write_pitch_txt.setValue("IGain", string_igain);
char string_dgain[128];
sprintf(string_dgain, "%f", d_gain);
write_pitch_txt.setValue("DGain", string_dgain);
write_pitch_txt.setValue("\n#Pitch sensor filter parameters\nfilterWn", "6.0");
write_pitch_txt.setValue("deadband", "0.5");
char string_zeroOffset[128];
sprintf(string_zeroOffset, "%d", batt_zeroOffset);
//bce setting was 41 mm during LASR experiments
write_pitch_txt.setValue("\n#Offset for neutral (default: 41)\nzeroOffset", string_zeroOffset);
//SAVE THE DATA!
pc().printf("Saving Buoyancy Engine Neutral Buoyancy Positions!");
if (!write_pitch_txt.write("/local/pitch.txt")) {
pc().printf("\n\rERROR: (SAVE)Failure to write depth.txt file.");
}
else {
pc().printf("\n\rFile pitch.txt successful written.\n\r");
}
}
void ConfigFileIO::saveBCEData(float p_gain, float i_gain, float d_gain) {
ConfigFile write_BCE_txt; //write to the config file
char PGain_buffer[128];
sprintf(PGain_buffer,"# configuration file for BCE parameters\n\n#Gains\nPGain");
char string_pgain[128];
sprintf(string_pgain, "%f", p_gain);
write_BCE_txt.setValue(PGain_buffer, string_pgain);
char string_igain[128];
sprintf(string_igain, "%f", i_gain);
write_BCE_txt.setValue("IGain", string_igain);
char string_dgain[128];
sprintf(string_dgain, "%f", d_gain);
write_BCE_txt.setValue("DGain", string_dgain);
write_BCE_txt.setValue("\n#string pot parameters\nzeroCounts", "253");
write_BCE_txt.setValue("PistonTravelLimit", "320.0");
write_BCE_txt.setValue("slope", "0.12176");
write_BCE_txt.setValue("filterWn", "6.0");
write_BCE_txt.setValue("deadband", "0.5");
//SAVE THE DATA!
pc().printf("Saving BCE PID data!");
if (!write_BCE_txt.write("/local/bce.txt")) {
pc().printf("\n\rERROR: (SAVE)Failure to write bce.txt file.");
}
else {
pc().printf("\n\rFile bce.txt successful written.\n\r");
}
}
void ConfigFileIO::saveDepthData(float p_gain, float i_gain, float d_gain, int bce_zeroOffset) {
ConfigFile write_depth_txt; //write to the config file
char PGain_buffer[128];
sprintf(PGain_buffer,"# depth outer loop parameters\n\n#Gains\nPGain");
char string_pgain[128];
sprintf(string_pgain, "%f", p_gain);
write_depth_txt.setValue(PGain_buffer, string_pgain);
char string_igain[128];
sprintf(string_igain, "%f", i_gain);
write_depth_txt.setValue("IGain", string_igain);
char string_dgain[128];
sprintf(string_dgain, "%f", d_gain);
write_depth_txt.setValue("DGain", string_dgain);
write_depth_txt.setValue("\n#Depth sensor filter parameters\nfilterWn", "6.0");
write_depth_txt.setValue("deadband", "0.5");
char string_zeroOffset[128];
sprintf(string_zeroOffset, "%d", bce_zeroOffset);
//bce setting was 240 mm during LASR experiments
write_depth_txt.setValue("\n#Offset for neutral (default: 240)\nzeroOffset", string_zeroOffset);
//SAVE THE DATA!
pc().printf("Saving Buoyancy Engine Neutral Buoyancy Positions!");
if (!write_depth_txt.write("/local/depth.txt")) {
pc().printf("\n\rERROR: (SAVE)Failure to write depth.txt file.");
}
else {
pc().printf("\n\rFile depth.txt successful written.\n\r");
}
}
int ConfigFileIO::load_BCE_config() {
ConfigFile cfg;
int count = 0;
if (!cfg.read("/local/bce.txt")) {
error("File Read Error");
}
char value[BUFSIZ];
if (cfg.getValue("PGain", &value[0] , sizeof(value))) {
bce().setControllerP(atof(value));
count++;
}
if (cfg.getValue("IGain", &value[0] ,sizeof(value))) {
bce().setControllerI(atof(value));
count++;
}
if (cfg.getValue("DGain", &value[0] , sizeof(value))) {
bce().setControllerD(atof(value));
count++;
}
if (cfg.getValue("zeroCounts", &value[0],sizeof(value))) {
bce().setZeroCounts(atoi(value));
count++;
}
if (cfg.getValue("PistonTravelLimit", &value[0], sizeof(value))) {
bce().setTravelLimit(atof(value));
count++;
}
if (cfg.getValue("slope", &value[0], sizeof(value))) {
bce().setPotSlope(atof(value));
count++;
}
if (cfg.getValue("filterWn", &value[0], sizeof(value))) {
bce().setFilterFrequency(atof(value));
count++;
}
if (cfg.getValue("deadband", &value[0], sizeof(value))) {
bce().setDeadband(atof(value));
count++;
}
return count;
}
int ConfigFileIO::load_BATT_config() {
ConfigFile cfg;
int count = 0;
if (!cfg.read("/local/batt.txt")) {
error("File Read Error");
}
char value[BUFSIZ];
if (cfg.getValue("PGain", &value[0] , sizeof(value))) {
batt().setControllerP(atof(value));
count++;
}
if (cfg.getValue("IGain", &value[0] ,sizeof(value))) {
batt().setControllerI(atof(value));
count++;
}
if (cfg.getValue("DGain", &value[0] , sizeof(value))) {
batt().setControllerD(atof(value));
count++;
}
if (cfg.getValue("zeroCounts", &value[0],sizeof(value))) {
batt().setZeroCounts(atoi(value));
count++;
}
if (cfg.getValue("PistonTravelLimit", &value[0], sizeof(value))) {
batt().setTravelLimit(atof(value));
count++;
}
if (cfg.getValue("slope", &value[0], sizeof(value))) {
batt().setPotSlope(atof(value));
count++;
}
if (cfg.getValue("filterWn", &value[0], sizeof(value))) {
batt().setFilterFrequency(atof(value));
count++;
}
if (cfg.getValue("deadband", &value[0], sizeof(value))) {
batt().setDeadband(atof(value));
count++;
}
return count;
}
int ConfigFileIO::load_DEPTH_config() {
ConfigFile cfg;
int count = 0;
if (!cfg.read("/local/depth.txt")) {
error("File Read Error");
}
char value[BUFSIZ];
if (cfg.getValue("PGain", &value[0] , sizeof(value))) {
depthLoop().setControllerP(atof(value));
count++;
}
if (cfg.getValue("IGain", &value[0] ,sizeof(value))) {
depthLoop().setControllerI(atof(value));
count++;
}
if (cfg.getValue("DGain", &value[0] , sizeof(value))) {
depthLoop().setControllerD(atof(value));
count++;
}
if (cfg.getValue("filterWn", &value[0], sizeof(value))) {
depthLoop().setFilterFrequency(atof(value));
count++;
}
if (cfg.getValue("deadband", &value[0], sizeof(value))) {
depthLoop().setDeadband(atof(value));
count++;
}
if (cfg.getValue("zeroOffset", &value[0], sizeof(value))) {
depthLoop().setOutputOffset(atof(value));
count++;
}
return count;
}
int ConfigFileIO::load_PITCH_config() {
ConfigFile cfg;
int count = 0;
if (!cfg.read("/local/pitch.txt")){
error("File Read Error");
}
char value[BUFSIZ];
if (cfg.getValue("PGain", &value[0] , sizeof(value))) {
pitchLoop().setControllerP(atof(value));
count++;
}
if (cfg.getValue("IGain", &value[0] ,sizeof(value))) {
pitchLoop().setControllerI(atof(value));
count++;
}
if (cfg.getValue("DGain", &value[0] , sizeof(value))) {
pitchLoop().setControllerD(atof(value));
count++;
}
if (cfg.getValue("filterWn", &value[0], sizeof(value))) {
pitchLoop().setFilterFrequency(atof(value));
count++;
}
if (cfg.getValue("deadband", &value[0], sizeof(value))) {
pitchLoop().setDeadband(atof(value));
count++;
}
if (cfg.getValue("zeroOffset", &value[0], sizeof(value))) {
pitchLoop().setOutputOffset(atof(value));
count++;
}
return count;
}