bug fixes

This commit is contained in:
Inex Code 2021-05-11 16:16:28 +00:00
parent 6478ccacb7
commit df3f9f9dbf

View file

@ -19,7 +19,8 @@ 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;
@ -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,9 +168,8 @@ public:
}
};
class Engine;
class Engine {
class Engine
{
float enginePower = 0; // n 0..500 n
float maxPower; // n
public:
@ -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());
}
@ -244,11 +241,13 @@ int model()
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)
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);
if (user == "pooh" && password == "honey") {
if (user == "pooh" && password == "honey")
{
return true;
} else {
}
else
{
return false;
}
}
void handle_get(http_request message){
void handle_get(http_request message)
{
cout << "Handle get: " << message.to_string() << endl;
if (is_authentificated(message)) {
if (is_authentificated(message))
{
json::value jsonObject;
jsonObject[U("target_height")] = json::value::number(targetH);
message.reply(status_codes::OK, jsonObject);
} else {
}
else
{
message.reply(status_codes::Forbidden);
}
}
void handle_post(http_request message){
void handle_post(http_request message)
{
cout << "Handle post: " << message.to_string() << endl;
if (is_authentificated(message)) {
if (is_authentificated(message))
{
json::value jsonObject;
try{
try
{
message.extract_json()
.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();
})
.wait();
}
catch (const std::exception & e) {
catch (const exception &e)
{
printf("Error exception:%s\n", e.what());
}
message.reply(status_codes::OK, jsonObject);
} else {
}
else
{
message.reply(status_codes::Forbidden);
}
}
void handle_quit(http_request message){
void handle_quit(http_request message)
{
cout << "Handle quit: " << message.to_string() << endl;
if (is_authentificated(message)) {
if (is_authentificated(message))
{
cmd = EExit;
message.reply(status_codes::OK);
} else {
}
else
{
message.reply(status_codes::Forbidden);
}
}
@ -361,15 +382,18 @@ int main()
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);
while (cmd != EExit)
;
th.join();
}
catch (const std::exception & e) {
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}