diff --git a/compile_commands.json b/compile_commands.json index 5a217c8..d517eba 100644 --- a/compile_commands.json +++ b/compile_commands.json @@ -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" } ] \ No newline at end of file diff --git a/include/Graphy/Grapher.hpp b/include/Graphy/Grapher.hpp index f84d398..305980c 100644 --- a/include/Graphy/Grapher.hpp +++ b/include/Graphy/Grapher.hpp @@ -23,6 +23,7 @@ class AsyncGrapher : public TaskWrapper { std::string title; uint refreshRate; int cnt; + bool autoZoom = true; public: /** @@ -47,7 +48,7 @@ class AsyncGrapher : public TaskWrapper { * @param name data type name * @param val updated data value */ - void update(const std::string &name, double val); + void update(const std::string &name, double val, double maxValArg = 1); /** * @brief Set the refresh rate @@ -63,6 +64,34 @@ class AsyncGrapher : public TaskWrapper { */ uint getRefreshRate(); + + /** + * @brief Get the Container of all values for the graphs + * + * @return std::map>* + */ + std::map>& 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>& 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; }; diff --git a/src/Graphy/Grapher.cpp b/src/Graphy/Grapher.cpp index 1618cc2..1fc33c2 100644 --- a/src/Graphy/Grapher.cpp +++ b/src/Graphy/Grapher.cpp @@ -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; @@ -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) { @@ -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( @@ -64,4 +97,28 @@ void AsyncGrapher::loop() { } } -} // namespace graph \ No newline at end of file +std::map>& AsyncGrapher::getContainer() { + + return container; + +} + +void AsyncGrapher::activateAutoZoom() { + autoZoom = true; +} + +void AsyncGrapher::deactivateAutoZoom() { + autoZoom = false; +} + +void AsyncGrapher::insertNewGraph(std::pair>& 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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3a81257..2c78220 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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) {