2023-Robot
Robot code for 2023 FRC season by Argos, FRC team #1756
Loading...
Searching...
No Matches
fs_homing.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <fstream>
8#include <iostream>
9
12#include "units/base.h"
13#include "wpi/fs.h"
14
15namespace argos_lib {
16
18 public:
24 explicit SwerveFSHomingStorage(const fs::path& swerveHomesPath);
31 bool Save(const argos_lib::swerve::SwerveModulePositions& homePosition) override;
37 std::optional<argos_lib::swerve::SwerveModulePositions> Load() override;
38
39 private:
45 fs::path GetFilePath();
46 const fs::path m_swerveHomesPath;
47 };
48
54 template <class T>
56 public:
62 explicit FSHomingStorage(const fs::path& homeFilePath) : m_homesPath{homeFilePath} {};
63
64 bool Save(const T& homePosition) override {
65 try {
66 bool success = true;
67 std::ofstream configFile(GetFilePath(), std::ios::out);
68 if (configFile.good()) {
69 configFile << homePosition.template to<double>();
70 if (!configFile.good()) {
71 std::cout << "[ERROR] Could not write to config file\n";
72 success = false;
73 }
74 } else {
75 std::cout << "[ERROR] Could not open config file\n";
76 success = false;
77 }
78 configFile.close();
79 return success;
80 } catch (...) {
81 // Error accessing file
82 std::cout << "[ERROR] Could not write to config file\n";
83 return false;
84 }
85 }
86
87 std::optional<T> Load() override {
88 try {
89 bool success = true;
90 std::ifstream configFile(GetFilePath(), std::ios::in);
91
92 if (configFile.peek() == std::ifstream::traits_type::eof()) {
93 return std::nullopt;
94 }
95
96 double homePosition;
97 configFile >> homePosition;
98
99 configFile.close();
100 if (success) {
101 return units::make_unit<T>(homePosition);
102 } else {
103 return std::nullopt;
104 }
105 } catch (...) {
106 // Error accessing file
107 std::cout << "[ERROR] Could not read from config file\n";
108 return std::nullopt;
109 }
110 }
111
112 private:
113 fs::path GetFilePath() {
114 static const fs::path homeDir{"/home/lvuser"};
115 const fs::path configFile{homeDir / m_homesPath};
116
117 std::cout << "############# Path: " << configFile << '\n';
118
119 // Create empty file if it doesn't exist yet
120 if (!fs::exists(configFile)) {
121 fs::create_directories(configFile.parent_path());
122 std::ofstream newFile(configFile);
123 newFile.close();
124 }
125
126 return configFile;
127 }
128
129 const fs::path m_homesPath;
130 };
131} // namespace argos_lib
Saves and loads home positions from filesystem.
Definition fs_homing.h:55
bool Save(const T &homePosition) override
Save home position to persistent storage.
Definition fs_homing.h:64
fs::path GetFilePath()
Definition fs_homing.h:113
FSHomingStorage(const fs::path &homeFilePath)
Construct a new FSHomingStorage object.
Definition fs_homing.h:62
const fs::path m_homesPath
Definition fs_homing.h:129
std::optional< T > Load() override
Load home position from persistent storage.
Definition fs_homing.h:87
Interface capable of saving and loading home positions from persistent storage.
Definition homing_interface.h:39
Definition fs_homing.h:17
bool Save(const argos_lib::swerve::SwerveModulePositions &homePosition) override
Save positions as new homes.
Definition fs_homing.cpp:17
fs::path GetFilePath()
Get the path of the file to load from and save to.
Definition fs_homing.cpp:49
const fs::path m_swerveHomesPath
Path of persistent storage file relative to home directory.
Definition fs_homing.h:46
std::optional< argos_lib::swerve::SwerveModulePositions > Load() override
Load absolute positions that represent 0 degree module orientations.
Definition fs_homing.cpp:31
Interface capable of saving and loading module home positions from persistent storage.
Definition homing_interface.h:13
Definition swap_controllers_command.h:12
Representation of the absolute encoder position of each module at home position.
Definition swerve_utils.h:38