2022-Robot
Robot code for 2022 FRC Season by Argos, FRC team #1756
Loading...
Searching...
No Matches
homing_storage_interface.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <fstream>
8#include <iostream>
9
10#include "units/base.h"
11#include "wpi/fs.h"
12
18template <class T>
20 public:
28 virtual bool Save(const T& homePosition) = 0;
29
36 [[nodiscard]] virtual std::optional<T> Load() = 0;
37};
38
44template <class T>
46 public:
52 explicit FSHomingStorage(const fs::path& homeFilePath) : m_homesPath{homeFilePath} {};
53
54 bool Save(const T& homePosition) override {
55 try {
56 bool success = true;
57 std::ofstream configFile(GetFilePath(), std::ios::out);
58 if (configFile.good()) {
59 configFile << homePosition.template to<double>();
60 if (!configFile.good()) {
61 std::cout << "[ERROR] Could not write to config file\n";
62 success = false;
63 }
64 } else {
65 std::cout << "[ERROR] Could not open config file\n";
66 success = false;
67 }
68 configFile.close();
69 return success;
70 } catch (...) {
71 // Error accessing file
72 std::cout << "[ERROR] Could not write to config file\n";
73 return false;
74 }
75 }
76
77 std::optional<T> Load() override {
78 try {
79 bool success = true;
80 std::ifstream configFile(GetFilePath(), std::ios::in);
81 double homePosition;
82 configFile >> homePosition;
83
84 configFile.close();
85 if (success) {
86 return units::make_unit<T>(homePosition);
87 } else {
88 return std::nullopt;
89 }
90 } catch (...) {
91 // Error accessing file
92 std::cout << "[ERROR] Could not read from config file\n";
93 return std::nullopt;
94 }
95 }
96
97 private:
98 fs::path GetFilePath() {
99 static const fs::path homeDir{"/home/lvuser"};
100 static const fs::path configFile{homeDir / m_homesPath};
101
102 std::cout << "############# Path: " << configFile << '\n';
103
104 // Create empty file if it doesn't exist yet
105 if (!fs::exists(configFile)) {
106 fs::create_directories(configFile.parent_path());
107 std::ofstream newFile(configFile);
108 newFile.close();
109 }
110
111 return configFile;
112 }
113
114 const fs::path m_homesPath;
115};
Saves and loads home positions from filesystem.
Definition: homing_storage_interface.h:45
std::optional< T > Load() override
Load home position from persistent storage.
Definition: homing_storage_interface.h:77
bool Save(const T &homePosition) override
Save home position to persistent storage.
Definition: homing_storage_interface.h:54
fs::path GetFilePath()
Definition: homing_storage_interface.h:98
const fs::path m_homesPath
Definition: homing_storage_interface.h:114
FSHomingStorage(const fs::path &homeFilePath)
Construct a new FSHomingStorage object.
Definition: homing_storage_interface.h:52
Interface capable of saving and loading home positions from persistent storage.
Definition: homing_storage_interface.h:19
virtual std::optional< T > Load()=0
Load home position from persistent storage.
virtual bool Save(const T &homePosition)=0
Save home position to persistent storage.