-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented terminal command processing, data post processing, data filters, and refresh rate tracking #66
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
Changes from all commits
97249cd
0bd849c
047401d
0a2b78e
7a04ff6
72385fc
6cd21d5
5ede9a2
ee8c27c
8eac292
8ef93a6
91c3252
191c86b
08b4aa6
f579dd3
c816848
0c97d4d
327f2b3
4e4c0c8
50eafc5
1ee6c86
c378e66
019277f
18f5214
c01e706
b2adc45
2f2e07c
75e4cfa
404d914
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #pragma once | ||
|
|
||
| #include "client/DataBuffer.hpp" | ||
| #include "client/post_processing.hpp" | ||
|
|
||
| #include <string> | ||
| #include <iostream> | ||
| #include <atomic> | ||
| #include <memory> | ||
|
|
||
| class CommandProcessor { | ||
| public: | ||
| CommandProcessor(std::shared_ptr<DataBuffer> dataBuffer, std::shared_ptr<PostProcessing> postProcessor); | ||
| void start(); //run command loop | ||
| void stop(); | ||
|
|
||
| private: | ||
| std::shared_ptr<DataBuffer> dataBuffer_; | ||
| std::shared_ptr<PostProcessing> postProcessor_; | ||
| std::string command; | ||
| std::atomic<bool> running_{true}; | ||
|
|
||
| void processCommand(const std::string& command); | ||
| }; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #pragma once | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
| #include <deque> | ||
|
|
||
| class DataFilters { | ||
| public: | ||
| DataFilters(); | ||
|
|
||
| int KalmanFilter(double input); | ||
| double MovingAverageFilter(double input); | ||
|
|
||
| private: | ||
| size_t MAX_KALMAN_SIZE = 10; | ||
| std::vector<double> kalmanList; | ||
|
|
||
| size_t MAX_MOVINGAVERAGE = 10; | ||
| std::deque<double> movingAverageList; | ||
|
|
||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #pragma once | ||
|
|
||
| #include <mutex> | ||
| #include <iostream> | ||
| #include <list> | ||
|
|
||
| class PostProcessing { | ||
| public: | ||
| PostProcessing(); | ||
| float processData(float data); | ||
| void reset(); | ||
| void addOffset(float offset); | ||
| void addScaling(float scaleFactor); | ||
| void updateDataBase(); | ||
|
|
||
| private: | ||
| float currentOffset = 0.0; | ||
| float currentScaleFactor = 1.0; | ||
| std::mutex mutex_; | ||
|
|
||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "client/command_processor.hpp" | ||
| #include "client/DataBuffer.hpp" | ||
|
|
||
| /* | ||
| TO DO: add stop mechanism for command processor thread, currently it runs indefinitely and can only be stopped by exiting the program | ||
| TO DO: add command_processor in onExit() in main.cpp | ||
| TO DO: handle race conditions between command processor and tcp client both accessing data buffer using mutex | ||
| */ | ||
|
|
||
| CommandProcessor::CommandProcessor(std::shared_ptr<DataBuffer> dataBuffer, std::shared_ptr<PostProcessing> postProcessor) | ||
| : dataBuffer_(dataBuffer), postProcessor_(postProcessor) { | ||
|
|
||
| } | ||
|
|
||
| void CommandProcessor::start() { | ||
| while(running_) { | ||
| std::cout << "Enter command (type 'exit' to quit): "; | ||
| std::getline(std::cin, command); | ||
|
|
||
| if (command == "exit") { | ||
| break; | ||
| } | ||
|
|
||
|
|
||
| processCommand(command); | ||
| } | ||
| } | ||
|
|
||
| void CommandProcessor::stop() { | ||
| running_ = false; | ||
| } | ||
|
|
||
| void CommandProcessor::processCommand(const std::string& command) { | ||
| /* | ||
| command: | ||
| <function> <parameter1> <parameter2> | ||
| */ | ||
| std::string cmdType = command.substr(0, command.find(' ')); | ||
|
|
||
| //get the parameters from command | ||
| size_t firstSpace = command.find(' '); | ||
| size_t secondSpace = command.find(' ', firstSpace + 1); | ||
| std::string firstParameter = command.substr(firstSpace + 1, secondSpace - firstSpace - 1); | ||
| std::string secondParameter = command.substr(secondSpace + 1); | ||
|
|
||
|
Comment on lines
+40
to
+45
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just adding a note here: |
||
| if(cmdType == "printAll") { | ||
| dataBuffer_->printAll(); | ||
|
|
||
| } else if (cmdType == "clear") { | ||
| dataBuffer_->clear(); | ||
|
|
||
| } else if (cmdType == "size"){ | ||
| std::cout << "Buffer size: " << dataBuffer_->size() << std::endl; | ||
|
|
||
| } else if (cmdType == "reset"){ | ||
| //reset all post processing parameters to default values | ||
| postProcessor_->reset(); | ||
|
|
||
| } else if (cmdType == "setOffset"){ | ||
| float offset = std::stof(firstParameter); | ||
| postProcessor_->addOffset(offset); | ||
|
|
||
| } else if (cmdType == "setScale"){ | ||
| float scaleFactor = std::stof(firstParameter); | ||
| postProcessor_->addScaling(scaleFactor); | ||
|
|
||
| } else { | ||
| std::cout << "Unknown command: " << command << std::endl; | ||
| } | ||
|
|
||
| //add more commands as needed | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #include "client/data_filters.hpp"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
|
|
||
| DataFilters::DataFilters() { | ||
| } | ||
|
|
||
|
|
||
| int DataFilters::KalmanFilter(double input) { | ||
|
|
||
| // QUEUE DATA TYPE, FIRST IN FIRST OUT | ||
| return 0.0; | ||
| } | ||
|
|
||
| double DataFilters::MovingAverageFilter(double input) { | ||
| movingAverageList.push_front(input); | ||
| if (MAX_MOVINGAVERAGE <= movingAverageList.size()) { | ||
| movingAverageList.pop_back(); | ||
| } | ||
|
|
||
| double sum = 0; | ||
| int i = 0; | ||
| while (i < movingAverageList.size()) { | ||
| sum += movingAverageList[i]; | ||
| i++; | ||
| } | ||
|
|
||
| return sum / i; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to use
std::chrono::steady_clockandduration_cast<milliseconds>for rate measurements - leaving this as a reference for potential future improvement :)