Skip to content

Commit

Permalink
Merge pull request #5 from UvuvDev/graph-extensions
Browse files Browse the repository at this point in the history
Extra Functions
  • Loading branch information
jazonshou authored Oct 16, 2024
2 parents 60989f5 + 81ab835 commit 617ef6c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 7 deletions.
40 changes: 40 additions & 0 deletions compile_commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,45 @@
],
"directory": "C:\\Users\\Mavericks\\Desktop\\Graphy",
"file": "src\\Graphy\\TaskWrapper.cpp"
},
{
"arguments": [
"clang++",
"-c",
"-target",
"armv7ar-none-none-eabi",
"-fno-ms-extensions",
"-fno-ms-compatibility",
"-fno-delayed-template-parsing",
"-isystemc:\\program files\\pros\\toolchain\\usr\\bin\\../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/include/c++/10.2.1",
"-isystemc:\\program files\\pros\\toolchain\\usr\\bin\\../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/include/c++/10.2.1/arm-none-eabi/thumb/v7-a+simd/softfp",
"-isystemc:\\program files\\pros\\toolchain\\usr\\bin\\../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/include/c++/10.2.1/backward",
"-isystemc:\\program files\\pros\\toolchain\\usr\\bin\\../lib/gcc/arm-none-eabi/10.2.1/include",
"-isystemc:\\program files\\pros\\toolchain\\usr\\bin\\../lib/gcc/arm-none-eabi/10.2.1/include-fixed",
"-isystemc:\\program files\\pros\\toolchain\\usr\\bin\\../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/include",
"-iquote./include",
"-iquote./include/okapi/squiggles",
"-iquote./include/./",
"-mcpu=cortex-a9",
"-mfpu=neon-fp16",
"-mfloat-abi=softfp",
"-Os",
"-g",
"-D_POSIX_THREADS",
"-D_UNIX98_THREAD_MUTEX_ATTRIBUTES",
"-D_POSIX_TIMERS",
"-D_POSIX_MONOTONIC_CLOCK",
"-Wno-psabi",
"-ffunction-sections",
"-fdata-sections",
"-fdiagnostics-color",
"-funwind-tables",
"--std=gnu++17",
"-o",
"bin/main.cpp.o",
"src\\main.cpp"
],
"directory": "C:\\Users\\Mavericks\\Desktop\\Graphy",
"file": "src\\main.cpp"
}
]
34 changes: 32 additions & 2 deletions include/Graphy/Grapher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AsyncGrapher : public TaskWrapper {
std::string title;
uint refreshRate;
int cnt;
bool autoZoom = true;

public:
/**
Expand All @@ -46,8 +47,9 @@ class AsyncGrapher : public TaskWrapper {
*
* @param name data type name
* @param val updated data value
* @param maxValArg Value that the "val" gets divided by for placement on the graph.
*/
void update(const std::string &name, double val);
void update(const std::string &name, double val, double maxValArg = 1);

/**
* @brief Set the refresh rate
Expand All @@ -63,8 +65,36 @@ class AsyncGrapher : public TaskWrapper {
*/
uint getRefreshRate();


/**
* @brief Get the Container of all values for the graphs
*
* @return std::map<std::string, std::vector<double>>*
*/
std::map<std::string, std::vector<double>>& getContainer();

/**
* @brief If you have data points you want to plot, you can directly input them
* with this function.
*
* @param An std::pair to a string and then your data points
*/
void insertNewGraph(std::pair<std::string, std::vector<double>>& newGraph, uint32_t color = COLOR_CORNFLOWER_BLUE);

/**
* @brief Make the graph automatically zoom in. Max value of the graph is at the top.
*
*/
void activateAutoZoom();

/**
* @brief Turns off auto zoom. All updates have to be divided by the max value.
*
*/
void deactivateAutoZoom();

protected:
void loop() override;
};

} // namespace graph
} // namespace graph
67 changes: 62 additions & 5 deletions src/Graphy/Grapher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace graphy {

AsyncGrapher::AsyncGrapher(const std::string &title, const uint rate) {
AsyncGrapher::AsyncGrapher(const std::string &title, const uint rate) {
this->title = title;
this->refreshRate = rate;
cnt = 0;
Expand All @@ -18,11 +18,44 @@ void AsyncGrapher::addDataType(const std::string &name, const uint32_t color) {
}
}

void AsyncGrapher::update(const std::string &name, double val) {
container[name].push_back(val);
void AsyncGrapher::update(const std::string &name, double val, double maxValArg) {

static int maxVal = 1;

static int updateSize = 0;

if (val > maxVal && autoZoom) {
maxVal = val;
container[name].push_back(val / maxVal);
updateSize++;

// If it's gone through half the length of the graph (doesn't run constantly to save resources)
if (updateSize > MAX_CACHE_SIZE / 2) {
// Resets the variables.
maxVal = 1;
updateSize = 0;

// Find the max value in the container, then continue graphing at that max value.
for (const auto &item : container) {
for (int i = 0; i < item.second.size(); i++) {
double val1 = item.second[i];
if (val1 > maxVal) {
maxVal = val1;
}
}
}
}
}
else {
container[name].push_back(val / maxValArg);
}

if (container[name].size() > MAX_CACHE_SIZE) {
container[name].erase(container[name].begin());
}



}

void AsyncGrapher::setRefreshRate(const uint rate) {
Expand Down Expand Up @@ -52,7 +85,7 @@ void AsyncGrapher::loop() {
GRAPH_LEFT + MAX_CACHE_SIZE,
(++indexLine) * 14 + 30,
item.first.c_str());
for (int i = 0; i < item.second.size()-1; i++) {
for (int i = 0; i < item.second.size() - 1; i++) {
double val1 = item.second[i] * (GRAPH_BOTTOM - GRAPH_TOP);
double val2 = item.second[i + 1] * (GRAPH_BOTTOM - GRAPH_TOP);
pros::screen::draw_line(
Expand All @@ -64,4 +97,28 @@ void AsyncGrapher::loop() {
}
}

} // namespace graph
std::map<std::string, std::vector<double>>& AsyncGrapher::getContainer() {

return container;

}

void AsyncGrapher::activateAutoZoom() {
autoZoom = true;
}

void AsyncGrapher::deactivateAutoZoom() {
autoZoom = false;
}

void AsyncGrapher::insertNewGraph(std::pair<std::string, std::vector<double>>& newGraph, uint32_t color) {
if (cnt > MAX_DATA) {
std::runtime_error("Error: max number of data is 14");
} else {
cnt++;
container.insert({newGraph.first, newGraph.second});
colors.insert({newGraph.first, color});
}
}

} // namespace graphy
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ void opcontrol() {
grapher->addDataType("Ema Vel", COLOR_ORANGE);
grapher->addDataType("Desired Vel", COLOR_AQUAMARINE);
grapher->addDataType("Kalman Vel", COLOR_RED);

grapher->activateAutoZoom();

grapher->startTask();
pros::ADIAnalogIn pot('A');
while(true) {
Expand Down

0 comments on commit 617ef6c

Please sign in to comment.