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

Extra Functions #5

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
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"
}
]
31 changes: 30 additions & 1 deletion 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 @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the comment block to include documentation for maxValArg


/**
* @brief Set the refresh rate
Expand All @@ -63,6 +64,34 @@ 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;
};
Expand Down
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
Loading