Add UDP
This commit is contained in:
parent
c9155b1450
commit
9cd23555a6
|
@ -3,6 +3,8 @@
|
|||
#include <future>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/array.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -94,6 +96,8 @@ ECmd cmd = ENone;
|
|||
|
||||
int counter = 0;
|
||||
|
||||
float targetH = 10.;
|
||||
|
||||
float getHZadFromUser()
|
||||
{
|
||||
return 10.;
|
||||
|
@ -237,7 +241,7 @@ int model()
|
|||
while (cmd != EExit)
|
||||
{
|
||||
|
||||
hZad = getHZadFromUser(); // Неблокирующая функция для получения высоты
|
||||
hZad = targetH; // Неблокирующая функция для получения высоты
|
||||
|
||||
if (abs(superpooh.getH() - hZad) < 1 && abs(t - lastT) > 5) {
|
||||
superpooh.eat(0.1);
|
||||
|
@ -249,28 +253,58 @@ int model()
|
|||
|
||||
t = t + dt;
|
||||
|
||||
std::cout << "Weight = " << superpooh.getM() << std::endl;
|
||||
std::cout << "Velocity = " << superpooh.getV() << std::endl;
|
||||
std::cout << "Acceleration = " << superpooh.getA() << std::endl;
|
||||
std::cout << "===========" << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
if (t < 200) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
else {
|
||||
std::cout << "Weight = " << superpooh.getM() << std::endl;
|
||||
std::cout << "Velocity = " << superpooh.getV() << std::endl;
|
||||
std::cout << "Acceleration = " << superpooh.getA() << std::endl;
|
||||
std::cout << "Time = " << t << std::endl;
|
||||
std::cout << "===========" << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::thread th(model);
|
||||
char ch;
|
||||
while (cmd != EExit)
|
||||
try
|
||||
{
|
||||
std::cout << "Enter command (q to exit)" << std::endl;
|
||||
std::cin >> ch;
|
||||
if (ch == 'q') {
|
||||
cmd = EExit;
|
||||
th.join();
|
||||
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);
|
||||
char ch;
|
||||
while (cmd != EExit)
|
||||
{
|
||||
boost::array<char, 1> recv_b;
|
||||
boost::asio::ip::udp::endpoint remote_endpoint;
|
||||
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;
|
||||
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;
|
||||
|
||||
}
|
Loading…
Reference in a new issue