Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to wrap and log PIDSource and PIDOutput #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Commands/Drive/DriveStraight.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Subsystems/Drive.h"
#include <PIDSource.h>
#include <PIDOutput.h>
#include "Common/PIDLogger.h"

namespace tator {

Expand All @@ -26,8 +27,9 @@ class DriveStraight: public CommandBase, public PIDSource, public PIDOutput {
speed = config["speed"].as<double>();
distance = config["distance"].as<double>();
YAML::Node pid_ = config["PID"];
PIDLogger* pidLogger = new PIDLogger(this, this, name);
pid = new PIDController(pid_["P"].as<double>(), pid_["I"].as<double>(),
pid_["D"].as<double>(), pid_["F"].as<double>(), this, this);
pid_["D"].as<double>(), pid_["F"].as<double>(), pidLogger, pidLogger);
}

static std::string GetBaseName() {
Expand Down
72 changes: 72 additions & 0 deletions src/Common/PIDLogger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* PIDLogger.cpp
*
* Created on: Jun 20, 2015
* Author: lee
*/

#include <Common/PIDLogger.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>

namespace tator {

PIDLogger::PIDLogger(PIDSource* in, PIDOutput* out, std::string name) {
// Stolen from USBManager
if (log == nullptr)
log = new Logger("PIDLogger");
log->Info("Opening PID log directory...");
DIR* dir;
if ((dir = opendir(kLogDirectory)) == nullptr) {
if (errno == ENOENT) {
if (!mkdir(kLogDirectory, 0755)) {
log->Error("Creating PID log directory: %s", strerror(errno));
return;
} else {
log->Info("Created PID log directory");
}
} else {
log->Error("Opening PID log directory: %s", strerror(errno));
return;
}
} else {
closedir(dir);
}
this->in = in;
this->out = out;
time_t rawtime;
struct tm* timeinfo;
char buf[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buf, sizeof(buf), ("PIDlog_" + name + " %C %a %b %d %H.%M.%S.txt").c_str(), timeinfo);
char fname[120];
strcpy(fname, kLogDirectory);
strcat(fname, buf);
log->Info("Opening log file: \"%s\"", fname);
file = fopen(fname, "w");
if (file == nullptr) {
log->Error("Opening log file: %s", strerror(errno));
return;
}
}

PIDLogger::~PIDLogger() {
}


double PIDLogger::PIDGet() {
double get = in->PIDGet();
fprintf(file, "g%f\n", get);
return get;
}

void PIDLogger::PIDWrite(float output) {
fprintf(file, "g%f\n", output);
return out->PIDWrite(output);
}

}
35 changes: 35 additions & 0 deletions src/Common/PIDLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* PIDLogger.h
*
* Created on: Jun 20, 2015
* Author: lee
*/

#ifndef PIDLOGGER_H_
#define PIDLOGGER_H_

#include <PIDOutput.h>
#include <PIDSource.h>
#include <cstdio>
#include <string>
#include "Logger.h"

namespace tator {

class PIDLogger: public PIDSource, public PIDOutput {
public:
PIDLogger(PIDSource* in, PIDOutput* out, std::string name);
virtual ~PIDLogger();
virtual double PIDGet() override;
virtual void PIDWrite(float output) override;
private:
PIDSource* in;
PIDOutput* out;
constexpr const static char* kLogDirectory = "/media/sda1/PIDlogs/";
Logger* log;
FILE* file;
};

}

#endif /* PIDLOGGER_H_ */
7 changes: 5 additions & 2 deletions src/Subsystems/Drive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Drive.h"
#include "Common/Tester/ManualTester.h"
#include "Common/PIDLogger.h"

namespace tator {

Expand All @@ -19,10 +20,12 @@ Drive::Drive(YAML::Node config) :
encoderR = new Encoder(conEncR[0].as<int>(), conEncR[1].as<int>());
YAML::Node pidl = config["PID"]["L"];
YAML::Node pidr = config["PID"]["R"];
PIDLogger* pidLoggerL = new PIDLogger(encoderL, driveL, "DriveLeft");
pidL = new PIDController(pidl["P"].as<double>(), pidl["I"].as<double>(),
pidl["D"].as<double>(), pidl["F"].as<double>(), encoderL, driveL);
pidl["D"].as<double>(), pidl["F"].as<double>(), pidLoggerL, pidLoggerL);
PIDLogger* pidLoggerR = new PIDLogger(encoderR, driveR, "DriveRight");
pidR = new PIDController(pidr["P"].as<double>(), pidr["I"].as<double>(),
pidr["D"].as<double>(), pidr["F"].as<double>(), encoderR, driveR);
pidr["D"].as<double>(), pidr["F"].as<double>(), pidLoggerR, pidLoggerR);
maxRPS = config["maxRPS"].as<double>();

encoderL->SetDistancePerPulse(1.0 / 360.0);
Expand Down