This commit is contained in:
Inex Code 2021-03-30 16:37:24 +00:00
parent c9155b1450
commit 9cd23555a6

View file

@ -3,6 +3,8 @@
#include <future> #include <future>
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include <boost/asio.hpp>
#include <boost/array.hpp>
using namespace std; using namespace std;
@ -94,6 +96,8 @@ ECmd cmd = ENone;
int counter = 0; int counter = 0;
float targetH = 10.;
float getHZadFromUser() float getHZadFromUser()
{ {
return 10.; return 10.;
@ -237,7 +241,7 @@ int model()
while (cmd != EExit) while (cmd != EExit)
{ {
hZad = getHZadFromUser(); // Неблокирующая функция для получения высоты hZad = targetH; // Неблокирующая функция для получения высоты
if (abs(superpooh.getH() - hZad) < 1 && abs(t - lastT) > 5) { if (abs(superpooh.getH() - hZad) < 1 && abs(t - lastT) > 5) {
superpooh.eat(0.1); superpooh.eat(0.1);
@ -249,27 +253,57 @@ int model()
t = t + dt; t = t + dt;
if (t < 200) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
else {
std::cout << "Weight = " << superpooh.getM() << std::endl; std::cout << "Weight = " << superpooh.getM() << std::endl;
std::cout << "Velocity = " << superpooh.getV() << std::endl; std::cout << "Velocity = " << superpooh.getV() << std::endl;
std::cout << "Acceleration = " << superpooh.getA() << std::endl; std::cout << "Acceleration = " << superpooh.getA() << std::endl;
std::cout << "Time = " << t << std::endl;
std::cout << "===========" << std::endl; std::cout << "===========" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50)); std::this_thread::sleep_for(std::chrono::milliseconds(50));
} }
}
return 0; return 0;
} }
int main() { int main() {
try
{
boost::asio::io_context io_context;
boost::asio::ip::udp::endpoint reciever(boost::asio::ip::udp::v4(), 1337);
boost::asio::ip::udp::socket socket(io_context, reciever);
std::thread th(model); std::thread th(model);
char ch; char ch;
while (cmd != EExit) while (cmd != EExit)
{ {
std::cout << "Enter command (q to exit)" << std::endl; boost::array<char, 1> recv_b;
std::cin >> ch; boost::asio::ip::udp::endpoint remote_endpoint;
if (ch == 'q') { size_t len = socket.receive_from(boost::asio::buffer(recv_b), remote_endpoint);
ch = *(recv_b.data());
std::cout.write(recv_b.data(), len);
std::cout << ch << std::endl;
if (*recv_b.data() == 'q') {
std::cout << "Exiting!" << std::endl;
cmd = EExit; cmd = EExit;
th.join(); th.join();
} }
if (*recv_b.data() == 'd') {
std::cout << "Going down!" << std::endl;
targetH = 0.;
}
if (*recv_b.data() == 'u') {
std::cout << "Going up to 30 meters!" << std::endl;
targetH = 30.;
}
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
} }
return 0; return 0;