bug fixes

This commit is contained in:
Inex Code 2021-05-11 16:16:28 +00:00
parent 6478ccacb7
commit df3f9f9dbf
1 changed files with 106 additions and 82 deletions

View File

@ -11,15 +11,16 @@
#include <cpprest/uri.h>
#include <cpprest/json.h>
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<PIDImpl> pid;
unique_ptr<Engine> 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: "<<message.to_string()<<endl;
if (is_authentificated(message)) {
void handle_get(http_request message)
{
cout << "Handle get: " << message.to_string() << endl;
if (is_authentificated(message))
{
json::value jsonObject;
jsonObject[U("target_height")] = json::value::number(targetH);
message.reply(status_codes::OK,jsonObject);
} else {
message.reply(status_codes::OK, jsonObject);
}
else
{
message.reply(status_codes::Forbidden);
}
}
void handle_post(http_request message){
cout<<"Handle post: "<<message.to_string()<<endl;
if (is_authentificated(message)) {
void handle_post(http_request message)
{
cout << "Handle post: " << message.to_string() << endl;
if (is_authentificated(message))
{
json::value jsonObject;
try{
try
{
message.extract_json()
.then([&jsonObject](json::value jo){
cout<<"Val:"<<jo.serialize() << endl;
.then([&jsonObject](json::value jo) {
cout << "Val:" << jo.serialize() << endl;
jsonObject = jo;
mtx.lock();
const lock_guard<mutex> lock(mtx);
targetH = jsonObject.at(U("target_height")).as_number().to_double();
cout<<"Val:"<<targetH<< endl;
mtx.unlock();
cout << "Val:" << targetH << endl;
})
.wait();
}
catch (const std::exception & e) {
catch (const exception &e)
{
printf("Error exception:%s\n", e.what());
}
message.reply(status_codes::OK,jsonObject);
} else {
message.reply(status_codes::OK, jsonObject);
}
else
{
message.reply(status_codes::Forbidden);
}
}
void handle_quit(http_request message){
cout<<"Handle quit: "<<message.to_string()<<endl;
if (is_authentificated(message)) {
void handle_quit(http_request message)
{
cout << "Handle quit: " << message.to_string() << endl;
if (is_authentificated(message))
{
cmd = EExit;
message.reply(status_codes::OK);
} else {
}
else
{
message.reply(status_codes::Forbidden);
}
}
@ -355,23 +376,26 @@ int main()
int rc = 0;
web::http::experimental::listener::http_listener
listener(U("http://localhost:8080/"));
listener.support(methods::GET,handle_get);
listener.support(methods::POST,handle_post);
listener.support(methods::DEL,handle_quit);
web::http::experimental::listener::http_listener
listener(U("http://localhost:8080/"));
listener.support(methods::GET, handle_get);
listener.support(methods::POST, handle_post);
listener.support(methods::DEL, handle_quit);
try{
try
{
std::thread th(model);
listener.open()
.then([&listener](){printf("\nStarting svrRest\n");})
.wait();
while(cmd != EExit);
listener.open()
.then([&listener]() { printf("\nStarting svrRest\n"); })
.wait();
while (cmd != EExit)
;
th.join();
}
catch (const std::exception & e) {
printf("Error exception:%s\n", e.what());
}
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);