From df3f9f9dbfded8fde919f700eeb3fdaead6c5a32 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Tue, 11 May 2021 16:16:28 +0000 Subject: [PATCH] bug fixes --- UML1/superpoohREST.cpp | 188 +++++++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 82 deletions(-) diff --git a/UML1/superpoohREST.cpp b/UML1/superpoohREST.cpp index ffa740a..eeccbd9 100644 --- a/UML1/superpoohREST.cpp +++ b/UML1/superpoohREST.cpp @@ -11,15 +11,16 @@ #include #include -using namespace utility; // Common utilities like string conversions -using namespace web; // Common features like URIs. -using namespace web::http; // Common HTTP functionality +using namespace utility; // Common utilities like string conversions +using namespace web; // Common features like URIs. +using namespace web::http; // Common HTTP functionality using namespace web::http::experimental::listener; // HTTP listener -using namespace concurrency::streams; // Asynchronous streams +using namespace concurrency::streams; // Asynchronous streams using namespace std; -class PIDImpl { +class PIDImpl +{ public: // Kp - proportional gain // Ki - Integral gain @@ -47,14 +48,7 @@ private: * Implementation */ PIDImpl::PIDImpl(double dt, double max, double min, double Kp, double Kd, double Ki) - : _dt(dt) - , _max(max) - , _min(min) - , _Kp(Kp) - , _Kd(Kd) - , _Ki(Ki) - , _pre_error(0) - , _integral(0) + : _dt(dt), _max(max), _min(min), _Kp(Kp), _Kd(Kd), _Ki(Ki), _pre_error(0), _integral(0) { } @@ -97,7 +91,8 @@ PIDImpl::~PIDImpl() { } -enum ECmd { +enum ECmd +{ ENone, EExit }; @@ -115,7 +110,8 @@ float getHZadFromUser() return 10.; } -class Bear { +class Bear +{ float h; float m; float v; @@ -124,10 +120,10 @@ class Bear { public: Bear() { - v = 0; // m/s - a = 0; // m/s2 + v = 0; // m/s + a = 0; // m/s2 m = 10; // kg - h = 0; // Текущая высота + h = 0; // Текущая высота } float getH() { @@ -153,7 +149,8 @@ public: h = h + dh; a = (extrnForce - 9.8 * m) / m; // m * a = mg - F - if (h < 0) { // Stay on the ground + if (h < 0) + { // Stay on the ground h = 0.; v = 0.; a = 0.; @@ -171,11 +168,10 @@ public: } }; -class Engine; - -class Engine { +class Engine +{ float enginePower = 0; // n 0..500 n - float maxPower; // n + float maxPower; // n public: Engine(float imaxPower) { @@ -199,16 +195,15 @@ public: } }; -class SmartFlyingBear : public Bear { - PIDImpl* pid; - Engine* pengine; +class SmartFlyingBear : public Bear +{ + unique_ptr pid; + unique_ptr pengine; float hZad; public: - SmartFlyingBear(PIDImpl* ppid, Engine* ppengine) + SmartFlyingBear(PIDImpl *ppid, Engine *ppengine) : pid(ppid), pengine(ppengine) { - pid = ppid; - pengine = ppengine; hZad = 0; } @@ -220,11 +215,13 @@ public: virtual void calc(float dt, float extrnForce) { float enginePowerPercent = 0; - if (pid != nullptr) { + if (pid != nullptr) + { enginePowerPercent = pid->calculate(hZad, getH()); } - if (pengine != nullptr) { + if (pengine != nullptr) + { pengine->setPower(enginePowerPercent); Bear::calc(dt, extrnForce + pengine->getForce()); } @@ -238,17 +235,19 @@ int model() //Bear pooh; SmartFlyingBear superpooh(new PIDImpl(0.1, 100, -100, 0.15, 0.001, 0.01), new Engine(500.)); - float t = 0; // Время моделирования + float t = 0; // Время моделирования float dt = 0.1; // s float lastT = -5; float hZad = 0; // Измененение высоты за шаг моделирования - while (cmd != EExit) { + while (cmd != EExit) + { mtx.lock(); hZad = targetH; // Неблокирующая функция для получения высоты mtx.unlock(); - if (abs(superpooh.getH() - hZad) < 1 && abs(t - lastT) > 5) { + if (abs(superpooh.getH() - hZad) < 1 && abs(t - lastT) > 5) + { superpooh.eat(0.1); lastT = t; } @@ -258,9 +257,12 @@ int model() t = t + dt; - if (t < 200) { + if (t < 200) + { std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } else { + } + else + { std::cout << "Weight = " << superpooh.getM() << std::endl; std::cout << "Velocity = " << superpooh.getV() << std::endl; std::cout << "Acceleration = " << superpooh.getA() << std::endl; @@ -278,74 +280,93 @@ void handle_signal(int s) cmd = EExit; } -bool is_authentificated(http_request message) { +bool is_authentificated(http_request message) +{ auto headers = message.headers(); - if (message.headers().find("Authorization") == headers.end()) return false; + if (message.headers().find("Authorization") == headers.end()) + return false; auto authHeader = headers["Authorization"]; auto credsPos = authHeader.find("Basic"); - if (credsPos == std::string::npos) + if (credsPos == std::string::npos) return false; - + auto base64 = authHeader.substr(credsPos + std::string("Basic").length() + 1); - if (base64.empty()) return false; + if (base64.empty()) + return false; auto bytes = utility::conversions::from_base64(base64); std::string creds(bytes.begin(), bytes.end()); auto colonPos = creds.find(":"); - if (colonPos == std::string::npos) return false; + if (colonPos == std::string::npos) + return false; auto user = creds.substr(0, colonPos); - auto password = creds.substr(colonPos + 1, creds.size() - colonPos - 1); + auto password = creds.substr(colonPos + 1, creds.size() - colonPos - 1); - if (user == "pooh" && password == "honey") { + if (user == "pooh" && password == "honey") + { return true; - } else { + } + else + { return false; } } -void handle_get(http_request message){ - cout<<"Handle get: "< lock(mtx); targetH = jsonObject.at(U("target_height")).as_number().to_double(); - cout<<"Val:"<