From 2f970c06849c42978e471651d950008f3fde4bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E9=9B=AA=E9=93=AD?= <37106995+Xueming10wu@users.noreply.github.com> Date: Mon, 28 Sep 2020 16:27:31 +0800 Subject: [PATCH 1/2] Add files via upload --- SimpleRobot/CMakeLists.txt | 49 ++++++ SimpleRobot/README.md | 7 + SimpleRobot/include/Client.h | 38 +++++ SimpleRobot/include/Protocol.h | 103 +++++++++++ SimpleRobot/include/Server.h | 44 +++++ SimpleRobot/include/common.h | 29 ++++ SimpleRobot/src/Client.cpp | 82 +++++++++ SimpleRobot/src/Protocol.cpp | 292 ++++++++++++++++++++++++++++++++ SimpleRobot/src/Server.cpp | 127 ++++++++++++++ SimpleRobot/src/client_main.cpp | 13 ++ SimpleRobot/src/common.cpp | 18 ++ SimpleRobot/src/server_main.cpp | 11 ++ 12 files changed, 813 insertions(+) create mode 100644 SimpleRobot/CMakeLists.txt create mode 100644 SimpleRobot/README.md create mode 100644 SimpleRobot/include/Client.h create mode 100644 SimpleRobot/include/Protocol.h create mode 100644 SimpleRobot/include/Server.h create mode 100644 SimpleRobot/include/common.h create mode 100644 SimpleRobot/src/Client.cpp create mode 100644 SimpleRobot/src/Protocol.cpp create mode 100644 SimpleRobot/src/Server.cpp create mode 100644 SimpleRobot/src/client_main.cpp create mode 100644 SimpleRobot/src/common.cpp create mode 100644 SimpleRobot/src/server_main.cpp diff --git a/SimpleRobot/CMakeLists.txt b/SimpleRobot/CMakeLists.txt new file mode 100644 index 0000000..103a13f --- /dev/null +++ b/SimpleRobot/CMakeLists.txt @@ -0,0 +1,49 @@ +cmake_minimum_required(VERSION 2.8.3) +project(simple_robot) + +include_directories( + include include +) + +add_library(common STATIC + include/common.h + src/common.cpp +) + +add_library(Protocol STATIC + include/Protocol.h + src/Protocol.cpp +) + +add_library(Client STATIC + include/Client.h + src/Client.cpp +) + +add_library(Server STATIC + include/Server.h + src/Server.cpp +) + + +add_executable(client + src/client_main.cpp + include/common.h + src/common.cpp + include/Protocol.h + src/Protocol.cpp + include/Client.h + src/Client.cpp +) +target_link_libraries(client common Protocol Client) + +add_executable (server + src/server_main.cpp + include/common.h + src/common.cpp + include/Protocol.h + src/Protocol.cpp + include/Server.h + src/Server.cpp +) +target_link_libraries(server common Protocol Server) diff --git a/SimpleRobot/README.md b/SimpleRobot/README.md new file mode 100644 index 0000000..3d08a3c --- /dev/null +++ b/SimpleRobot/README.md @@ -0,0 +1,7 @@ +# SimpleRobot +机器人教学 第二讲 + +在build文件夹下 +cmake .. +make +编译完成 \ No newline at end of file diff --git a/SimpleRobot/include/Client.h b/SimpleRobot/include/Client.h new file mode 100644 index 0000000..4ef4497 --- /dev/null +++ b/SimpleRobot/include/Client.h @@ -0,0 +1,38 @@ +#ifndef CLIENT_H +#define CLIENT_H + +#include "Protocol.h" + +class Client +{ +private: + int client_fd; //tcp对象实例 + struct sockaddr_in serverAddr; //协议族 + const int serverPort; //服务程序端口号 + + bool isConnected; //是否连接 true已连接 false未连接 + + const int bufferMaxSize; //缓存区大小设置 + //char *buffer; //tcp通讯缓存区 + + + //timer + int period; + int count; + + const int axies; //机械臂轴数 + +public: + Client(const char * serverIP = "127.0.0.1", int serverPort_ = 8000, int axies_ = 6, int buffer_size_ = 0x2000); //默认8192 + + ~Client(); + + void listening(); //启动服务器,并接收数据 + + void timerRegister(int period = 100000); //定时器初始化 us + + Trajectory goal; //目标轨迹 + ComplexPoint feedback; //反馈当前点 +}; + +#endif //CLIENT_H \ No newline at end of file diff --git a/SimpleRobot/include/Protocol.h b/SimpleRobot/include/Protocol.h new file mode 100644 index 0000000..12af63b --- /dev/null +++ b/SimpleRobot/include/Protocol.h @@ -0,0 +1,103 @@ +#ifndef PROTOCOL_H +#define PROTOCOL_H + +#include "common.h" + +//空间点 +struct Point +{ + int duration; //运动的持续时间 单位us + int * postions; //位置,脉冲 + //std::vector postions; //位置,脉冲 + //std::vector electric; //电流,mA ,可选项 +}; + + +//轨迹 +struct Trajectory +{ + uint8_t state; //状态值,执行或取消 + int axies; //关节数 + + int pointSize; //单点缓存所占长度 + + int pointsMaxCount; //点的最大数量 + + int pointsCount; //点的数量 + + int bufferMaxSize; //流缓存区最大长度 + int bufferSize; //已使用的缓存区长度,用于通讯发送接收数据 + + //std::vector points; //路径点集合,用于对接应用程序 + Point * points; + uint8_t * buffer; //流缓存区,用于对接socket +}; + +//原本Trajectory就可以完成数据的反馈,但为了程序的可读性,添加并使用ComplexPoint +//复合空间点 +struct ComplexPoint +{ + uint8_t state; //状态值,执行或取消 + int axies; //关节数 + + int pointSize; //单点缓存所占长度 + + int bufferMaxSize; //流缓存区最大长度 + int bufferSize; //已使用的缓存区长度,用于通讯发送接收数据 + + Point point; //点 + uint8_t * buffer; //流缓存区,用于对接socket +}; + + + + +//状态值 +enum STATE +{ + PENDING = (uint8_t)'?', //挂起态 + RUNING = (uint8_t)',', //执行态 + STOPPED = (uint8_t)'!' //静止态 +}; + +// 符号位 0xf0 : "+" , 0x0f :"-" + +// 单点数据结构 point +// | duration | postion_0 | postion_1 | ...... | postion_n | +// | 2 | 3 | 3 | ...... | 3 | +// 无符号 符号 + 2 符号 + 2 ...... 符号 + 2 +// 2 + (1+2) x axies = 2 + 3 x Axies +// Axies = 6 2 + 3 x 6 = 20 + +// 轨迹存储结构 +// | state | point_0 | point_1 | ...... | point_n | +// | 1 | 20 | 20 | ...... | 20 | +// 无符号 +// 1 + Points x ( 2 + 3 x Axies ) +// Axies = 6 1 + 20 x Points + +//应用数据结构转为流结构 +void trajectory2buffer(Trajectory &trajectory_); +void point2buffer(ComplexPoint & point_); + +//流结构转为应用数据结构 +void buffer2trajectory(Trajectory &trajectory_); +void buffer2point(ComplexPoint & point_); + +//初始化缓存区 +void initTrajectoryBuffer(Trajectory &trajectory_); +void initComplexPointBuffer(ComplexPoint & point_); + +//打印基本信息 +void printInfo(Trajectory &trajectory_); +void printInfo(ComplexPoint &point_); + +//打印路径 +void printTrajectory(Trajectory &trajectory_); +void printComplexPoint(ComplexPoint &point_); + +//打印缓存区 +void printBuffer(Trajectory &trajectory_); +void printBuffer(ComplexPoint &point_); + +#endif //PROTOCOL_H \ No newline at end of file diff --git a/SimpleRobot/include/Server.h b/SimpleRobot/include/Server.h new file mode 100644 index 0000000..13677da --- /dev/null +++ b/SimpleRobot/include/Server.h @@ -0,0 +1,44 @@ +#ifndef SERVER_H +#define SERVER_H + +#include "Protocol.h" + +class Server +{ +private: + int listenfd; //tcp对象实例 + int connection_fd; //连接对象 + bool isConnected; //是否连接 true已连接 false未连接 + + const int port; //本程序(服务端)端口号 + + char * serverIP; //本程序(服务端)IP + struct sockaddr_in serverAddr; //协议族 + socklen_t serverAddrLen; //地址长度 + + + char * clientIP; //客户端IP + struct sockaddr_in clientAddr; //客户地址端口信息 + socklen_t clientAddrLen; //地址长度 + + const int bufferMaxSize; //缓存区大小设置 + //char *buffer; //tcp通讯缓存区 + + //timer + int period; + int count; + + const int axies; //机械臂轴数 + +public: + Server(int port_ = 8000, int bufferMaxSize_ = 0x2000, int axies_ = 6); //默认8192 + + ~Server(); + + void listening(); //启动服务器,并接收数据 + + Trajectory goal; //目标轨迹 + ComplexPoint feedback; //反馈当前位置 +}; + +#endif //SERVER_H \ No newline at end of file diff --git a/SimpleRobot/include/common.h b/SimpleRobot/include/common.h new file mode 100644 index 0000000..58e231c --- /dev/null +++ b/SimpleRobot/include/common.h @@ -0,0 +1,29 @@ +#ifndef COMMON_H +#define COMMON_H + +//所以平时需要用到的头文件都放在这里 +#include +#include +#include +#include +#include +#include +#include + +//tcp +#include +#include +#include +#include +#include + +using namespace std; + +namespace MyFunctions +{ + extern bool condition; //循环判断条件 false为不符合条件 true为符合条件 + extern void stop(int sign); //捕获信号量 + extern const bool ok(); //反馈condition变量 +} // namespace MyFunctions + +#endif // COMMON_H \ No newline at end of file diff --git a/SimpleRobot/src/Client.cpp b/SimpleRobot/src/Client.cpp new file mode 100644 index 0000000..b4eece1 --- /dev/null +++ b/SimpleRobot/src/Client.cpp @@ -0,0 +1,82 @@ +#include "Client.h" + +Client::Client(const char *serverIP, int serverPort_, int axies_, int buffer_size_) : serverPort(serverPort_), axies(axies_), bufferMaxSize(buffer_size_) +{ + + std::cout << "客户端启动,尝试连接服务端 " << serverIP << ":" << serverPort_ << std::endl; + // socket + client_fd = socket(AF_INET, SOCK_STREAM, 0); + if (client_fd == -1) + { + std::cout << "Error: socket" << std::endl; + exit(0); + } + + //服务端 ip及程序端口号 + serverAddr.sin_family = AF_INET; //tcp IPv4 + serverAddr.sin_port = htons(serverPort); + serverAddr.sin_addr.s_addr = inet_addr(serverIP); + + //尝试连接服务器 + if (connect(client_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) + { + std::cout << "Error: connect" << std::endl; + exit(0); + } + + //反馈数据结构 + feedback.state = STATE::STOPPED; //静止态 + feedback.axies = axies; + feedback.pointSize = 2 + axies * 3; + feedback.bufferMaxSize = 1 + feedback.pointSize; + initComplexPointBuffer(feedback); + + + //目标数据结构 + goal.state = STATE::STOPPED; //静止态 + goal.axies = axies; + goal.pointSize = 2 + axies * 3; + goal.pointsMaxCount = 400; + goal.bufferMaxSize = bufferMaxSize; + initTrajectoryBuffer(goal); +} + +Client::~Client() +{ +} + +void Client::listening() +{ + while (MyFunctions::ok()) + { + //路径点赋值 + goal.state = STATE::PENDING; //挂起态 + std::cout << "\n\nduration : "; + cin >> goal.points[0].duration; + for (int i = 0; i < goal.axies; i ++) + { + std::cout << "postions[" << i <<"] "; + cin >> goal.points[0].postions[i]; + } + goal.pointsCount = 1; + trajectory2buffer(goal); + printInfo(goal); + printBuffer(goal); + + //发数据 + send(client_fd, goal.buffer, goal.bufferSize, 0); + if (strcmp((char * )goal.buffer, "exit") == 0) + { + std::cout << "...disconnect" << std::endl; + break; + } + usleep(period); + } + close(client_fd); +} + +void Client::timerRegister(int period) //定时器初始化 us +{ + count = 0; + this->period = period; +} \ No newline at end of file diff --git a/SimpleRobot/src/Protocol.cpp b/SimpleRobot/src/Protocol.cpp new file mode 100644 index 0000000..6ba0c3f --- /dev/null +++ b/SimpleRobot/src/Protocol.cpp @@ -0,0 +1,292 @@ +//参考 https://www.jianshu.com/p/3b233facd6bb +//函数手册(外网) https://man7.org/linux/man-pages/man2/open.2.html + +#include "Protocol.h" + +//应用数据结构转为流结构 +void trajectory2buffer(Trajectory &trajectory_) +{ + //std::cout << "trajectory2buffer " << std::endl; + //trajectory_.bufferSize = 1 + trajectory_.points.size() * trajectory_.pointSize; + + //记录缓存区使用长度 + trajectory_.bufferSize = 1 + trajectory_.pointSize * trajectory_.pointsCount; + + //设置轨迹状态 + trajectory_.buffer[0] = trajectory_.state; + + //对每个路径点进行访问 + for (int pointsSeq = 0; pointsSeq < trajectory_.pointsCount; pointsSeq++) + { + //std::cout <<"对每个路径点进行访问" << std::endl; + //路径点执行所用时间高位 + trajectory_.buffer[1 + pointsSeq * trajectory_.pointSize] = + (abs(trajectory_.points[pointsSeq].duration) >> 8) & 0xff; + //路径点执行所用时间低位 + trajectory_.buffer[2 + pointsSeq * trajectory_.pointSize] = + abs(trajectory_.points[pointsSeq].duration) & 0xff; + + //对路径点中的每个轴进行访问 + for (int axiesSeq = 0; axiesSeq < trajectory_.axies; axiesSeq++) + { + //std::cout <<"对路径点中的每个轴进行访问" << std::endl; + //轴的符号位设置 + if (trajectory_.points[pointsSeq].postions[axiesSeq] < 0) + { + trajectory_.buffer[3 + pointsSeq * trajectory_.pointSize + axiesSeq * 3] = 0x0f; + } + else + { + trajectory_.buffer[3 + pointsSeq * trajectory_.pointSize + axiesSeq * 3] = 0xf0; + } + + //数值位高位数据 + trajectory_.buffer[4 + pointsSeq * trajectory_.pointSize + axiesSeq * 3] = + (abs(trajectory_.points[pointsSeq].postions[axiesSeq]) >> 8) & 0xff; + + //数值位低位数据 + trajectory_.buffer[5 + pointsSeq * trajectory_.pointSize + axiesSeq * 3] = + abs(trajectory_.points[pointsSeq].postions[axiesSeq]) & 0xff; + } + } +} + +void point2buffer(ComplexPoint & point_) +{ + //记录缓存区使用长度 + point_.bufferSize = 1 + point_.pointSize; + + //设置状态 + point_.buffer[0] = point_.state; + + //路径点执行所用时间高位 + point_.buffer[1] = (abs(point_.point.duration) >> 8) & 0xff; + //路径点执行所用时间低位 + point_.buffer[2] = abs(point_.point.duration) & 0xff; + + //对路径点中的每个轴进行访问 + for (int axiesSeq = 0; axiesSeq < point_.axies; axiesSeq++) + { + //std::cout <<"对路径点中的每个轴进行访问" << std::endl; + //轴的符号位设置 + if (point_.point.postions[axiesSeq] < 0) + { + point_.buffer[3 + axiesSeq * 3] = 0x0f; + } + else + { + point_.buffer[3 + + axiesSeq * 3] = 0xf0; + } + + //数值位高位数据 + point_.buffer[4 + axiesSeq * 3] = (abs(point_.point.postions[axiesSeq]) >> 8) & 0xff; + + //数值位低位数据 + point_.buffer[5 + axiesSeq * 3] = abs(point_.point.postions[axiesSeq]) & 0xff; + } +} + + +//流结构转为应用数据结构 +void buffer2trajectory(Trajectory &trajectory_) +{ + //确定路径点的数量 + trajectory_.pointsCount = floor((trajectory_.bufferSize - 1) / trajectory_.pointSize); + + //获得轨迹状态 + trajectory_.state = trajectory_.buffer[0]; + + //获取每个路径点 + for (int pointsSeq = 0; pointsSeq < trajectory_.pointsCount; pointsSeq++) + { + //获得每个路径点运动持续时间 + trajectory_.points[pointsSeq].duration = + abs((int)((trajectory_.buffer[1 + pointsSeq * trajectory_.pointSize] << 8) | + trajectory_.buffer[2 + pointsSeq * trajectory_.pointSize])); + + //获取每个轴 + for (int axiesSeq = 0; axiesSeq < trajectory_.axies; axiesSeq ++) + { + //获得轴的数值 + trajectory_.points[pointsSeq].postions[axiesSeq] = + abs((int)((trajectory_.buffer[4 + pointsSeq * trajectory_.pointSize + axiesSeq * 3] << 8) | + trajectory_.buffer[5 + pointsSeq * trajectory_.pointSize + axiesSeq * 3])); + + //获得轴的符号 + if (trajectory_.buffer[3 + pointsSeq * trajectory_.pointSize + axiesSeq * 3] == 0x0f) + { + trajectory_.points[pointsSeq].postions[axiesSeq] *= -1; + } + } + } +} + + +void buffer2point(ComplexPoint & point_) +{ + //获得轨迹状态 + point_.state = point_.buffer[0]; + + //获得运动持续时间 + point_.point.duration = abs((int)((point_.buffer[1] << 8) | point_.buffer[2])); + + //获取每个轴 + for (int axiesSeq = 0; axiesSeq < point_.axies; axiesSeq ++) + { + //获得轴的数值 + point_.point.postions[axiesSeq] = + abs((int)((point_.buffer[4 + axiesSeq * 3] << 8) | point_.buffer[5 + axiesSeq * 3])); + + //获得轴的符号 + if (point_.buffer[3 + axiesSeq * 3] == 0x0f) + { + point_.point.postions[axiesSeq] *= -1; + } + } +} + + +/* + uint8_t state; //状态值,执行或取消 + int axies; //关节数 + + int pointSize; //单点缓存所占长度 + + int bufferMaxSize; //流缓存区最大长度 + int bufferSize; //已使用的缓存区长度,用于通讯发送接收数据 + + Point point; //点 + uint8_t * buffer; //流缓存区,用于对接socket +*/ + + +//初始化缓存区 +void initTrajectoryBuffer(Trajectory &trajectory_) +{ + //轨迹内存分配 + trajectory_.points = new Point[trajectory_.pointsMaxCount]; + + //每个点都进行空间内存分配 + for (int i = 0; i < trajectory_.pointsMaxCount; i ++) + { + trajectory_.points[i].duration = 0; + trajectory_.points[i].postions = new int[trajectory_.axies]; + } + + //通讯缓存区内存分配 + trajectory_.buffer = new uint8_t[trajectory_.bufferMaxSize]; + + //已使用点数和缓存区数量 + trajectory_.pointsCount = 0; + trajectory_.bufferSize = 0; +} + +void initComplexPointBuffer(ComplexPoint & point_) +{ + //点内存分配 + point_.point.duration = 0; + point_.point.postions = new int[point_.axies]; + + //通讯缓存区内存分配 + point_.buffer = new uint8_t[point_.bufferMaxSize]; + + //已使用缓存区数量 + point_.bufferSize = 0; +} + + + +void printInfo(Trajectory &trajectory_) +{ + std::cout << "轨迹信息 : " << std::endl; + std::cout << "状态 : " << trajectory_.state << std::endl; + std::cout << "自由度 : " << trajectory_.axies << std::endl; + std::cout << "单点缓存尺寸 : " << trajectory_.pointSize << std::endl; + + std::cout << "最大点数 : " << trajectory_.pointsMaxCount << std::endl; + std::cout << "最大缓存区 : " << trajectory_.bufferMaxSize << " B"<< std::endl; + + std::cout << "已用点数 : " << trajectory_.pointsCount << std::endl; + std::cout << "已使用的缓存区 : " << trajectory_.bufferSize << std::endl; +} + + +void printInfo(ComplexPoint &point_) +{ + std::cout << "当前点信息 : " << std::endl; + std::cout << "状态 : " << point_.state << std::endl; + std::cout << "自由度 : " << point_.axies << std::endl; + std::cout << "单点缓存尺寸 : " << point_.pointSize << std::endl; + + std::cout << "最大缓存区 : " << point_.bufferMaxSize << "B"<< std::endl; + std::cout << "已使用的缓存区 : " << point_.bufferSize << std::endl; +} + + + +//打印轨迹信息 +void printTrajectory(Trajectory &trajectory_) +{ + std::cout << "state : " << trajectory_.state << std::endl; + for (int pointsSeq = 0; pointsSeq < trajectory_.pointsCount; pointsSeq++) + { + std::cout << "duration : " << trajectory_.points[pointsSeq].duration << ", axies : "; + for (int axiesSeq = 0; axiesSeq < trajectory_.axies; axiesSeq++) + { + std::cout << trajectory_.points[pointsSeq].postions[axiesSeq] << " "; + } + std::cout << dec << std::endl; + } +} + +//打印单点信息 +void printComplexPoint(ComplexPoint &point_) +{ + std::cout << "duration : " << point_.point.duration << ", axies : "; + for (int axiesSeq = 0; axiesSeq < point_.axies; axiesSeq++) + { + std::cout << point_.point.postions[axiesSeq] << " "; + } + std::cout << dec << std::endl; +} + + +//打印缓存区数据 +void printBuffer(Trajectory &trajectory_) +{ + std::cout << "Trajectory printBuffer : "; + for (int i = 0; i < trajectory_.bufferSize; i++) + { + if(i == 0) + { + std::cout << "STATE : " << (char)trajectory_.buffer[i] << " "; + continue; + } + if (i % 20 == 1) + { + std::cout << std::endl; + } + std::cout << hex << (int)trajectory_.buffer[i] << " "; + } + std::cout << dec << std::endl; +} + + +void printBuffer(ComplexPoint &point_) +{ + std::cout << "printBuffer : "; + for (int i = 0; i < point_.bufferSize; i++) + { + if(i == 0) + { + std::cout << "STATE : " << (char)point_.buffer[i] << " "; + continue; + } + if (i % 20 == 1) + { + std::cout << std::endl; + } + std::cout << hex << (int)point_.buffer[i] << " "; + } + std::cout << dec << std::endl; +} \ No newline at end of file diff --git a/SimpleRobot/src/Server.cpp b/SimpleRobot/src/Server.cpp new file mode 100644 index 0000000..261b318 --- /dev/null +++ b/SimpleRobot/src/Server.cpp @@ -0,0 +1,127 @@ +#include "Server.h" + +Server::Server(int port_, int bufferMaxSize_, int axies_) : port(port_), bufferMaxSize(bufferMaxSize_), axies(axies_) +{ + serverIP = new char [INET_ADDRSTRLEN]; + serverAddrLen = (socklen_t)sizeof(serverAddr); + clientIP = new char[INET_ADDRSTRLEN]; + clientAddrLen = (socklen_t)sizeof(clientAddr); + inet_ntop(AF_INET, &(serverAddr.sin_addr), serverIP, INET_ADDRSTRLEN); + std::cout << "信息 : 服务端信息 " << serverIP << ":" << port << std::endl; + std::cout << "信息 : 服务器已启动" << std::endl; + + isConnected = false; + // socket + listenfd = socket(AF_INET, SOCK_STREAM, 0); + + if (listenfd == -1) + { + std::cout << "Error: socket" << std::endl; + exit(0); + } + + // bind + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(port); + serverAddr.sin_addr.s_addr = INADDR_ANY; + + + + if (bind(listenfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) + { + std::cout << "Error: bind, 服务端口已被占用" << std::endl; + exit(0); + } + + // listen + if (listen(listenfd, 5) == -1) + { + std::cout << "Error: listen" << std::endl; + exit(0); + } + else + { + std::cout << "信息 : 监听设置完毕" << std::endl; + } + + + + + //反馈数据结构 + feedback.state = STATE::STOPPED; //静止态 + feedback.axies = axies; + feedback.pointSize = 2 + axies * 3; + feedback.bufferMaxSize = 1 + feedback.pointSize; + initComplexPointBuffer(feedback); + + + //目标数据结构 + goal.state = STATE::STOPPED; //静止态 + goal.axies = axies; + goal.pointSize = 2 + axies * 3; + goal.pointsMaxCount = 400; + goal.bufferMaxSize = bufferMaxSize; + initTrajectoryBuffer(goal); +} + +Server::~Server() +{ +} + +void Server::listening() +{ + while (MyFunctions::ok()) + { + std::cout << "信息 : 服务器等待连接" << std::endl; + + connection_fd = accept(listenfd, (struct sockaddr *)&clientAddr, &clientAddrLen); + + if (connection_fd < 0) + { + if (MyFunctions::ok()) + { + std::cout << "Error: 服务器端口连接启动失败" << std::endl; + continue; + } + else + { + break; + } + } + else + { + isConnected = true; + } + if (isConnected) + { + inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN); + std::cout << "信息 : 客户端信息 " << clientIP << ":" << ntohs(clientAddr.sin_port) << std::endl; + } + + while (MyFunctions::ok()) + { + //memset(buffer, 0, bufferMaxSize); + + //收数据 + int len = recv(connection_fd, goal.buffer, goal.bufferMaxSize, 0); //阻塞态 + + //退出连接 + if (strcmp((char *)goal.buffer, "exit") == 0 || len == 0) + { + std::cout << "信息 : 客户端断开连接 " << clientIP << ":" << ntohs(clientAddr.sin_port) << std::endl; + break; + } + + //获取缓存区长度 + goal.bufferSize = len; + + buffer2trajectory(goal); + printTrajectory(goal); + } + + close(connection_fd); + isConnected = false; + usleep(period); + } + close(listenfd); +} \ No newline at end of file diff --git a/SimpleRobot/src/client_main.cpp b/SimpleRobot/src/client_main.cpp new file mode 100644 index 0000000..91b65f4 --- /dev/null +++ b/SimpleRobot/src/client_main.cpp @@ -0,0 +1,13 @@ +#include "Client.h" + +using namespace std; + + +int main(int argc, char *argv[]) +{ + signal(SIGINT,MyFunctions::stop); + Client *client = new Client("127.0.0.1", 8000, 6 ,8192); + client->timerRegister(100000); + client->listening(); + return 0; +} \ No newline at end of file diff --git a/SimpleRobot/src/common.cpp b/SimpleRobot/src/common.cpp new file mode 100644 index 0000000..c4821dc --- /dev/null +++ b/SimpleRobot/src/common.cpp @@ -0,0 +1,18 @@ +#include "common.h" + +//自定义函数 + +bool MyFunctions::condition = true; + +void MyFunctions::stop(int sign) +{ + MyFunctions::condition = false; + std::cout << "\nUser's Interrupt" << std::endl; + //exit(0); +} + +const bool MyFunctions::ok() +{ + //std::cout << "MyFunctions::ok()" << std::endl; + return MyFunctions::condition; +} \ No newline at end of file diff --git a/SimpleRobot/src/server_main.cpp b/SimpleRobot/src/server_main.cpp new file mode 100644 index 0000000..2a5718b --- /dev/null +++ b/SimpleRobot/src/server_main.cpp @@ -0,0 +1,11 @@ +#include "Server.h" + +using namespace std; + +int main(int argc, char *argv[]) +{ + signal(SIGINT, MyFunctions::stop); + Server *server = new Server(); + server->listening(); + return 0; +} \ No newline at end of file From d338d94c6bd3aa18e68be441ffa917886ceb293a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E9=9B=AA=E9=93=AD?= <37106995+Xueming10wu@users.noreply.github.com> Date: Fri, 13 Nov 2020 19:58:30 +0800 Subject: [PATCH 2/2] Delete sketch_sep17a.ino --- .../arduino/sketch_sep17a/sketch_sep17a.ino" | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 "\347\254\2541\350\212\202/arduino/sketch_sep17a/sketch_sep17a.ino" diff --git "a/\347\254\2541\350\212\202/arduino/sketch_sep17a/sketch_sep17a.ino" "b/\347\254\2541\350\212\202/arduino/sketch_sep17a/sketch_sep17a.ino" deleted file mode 100644 index e1e8d2e..0000000 --- "a/\347\254\2541\350\212\202/arduino/sketch_sep17a/sketch_sep17a.ino" +++ /dev/null @@ -1,63 +0,0 @@ -#include - - -//stm32duino -//步进电机驱动 需要 3个信号线进行控制 -const int ena_pin = PA4; -const int dir_pin = PA5; -const int plu_pin = PA6; - -void setup() -{ - //设置引脚工作,模式 - pinMode(ena_pin, OUTPUT); - pinMode(dir_pin, OUTPUT); - pinMode(plu_pin, OUTPUT); - - //使能端开启 - digitalWrite(ena_pin, LOW); -} - -void driver_run(int plu, int duration_us) -{ - //判断电机旋转方向 - if (plu > 0) - { - digitalWrite(dir_pin, HIGH); - } - else - { - digitalWrite(dir_pin, LOW); - } - - //输出脉冲信号 - for (int i = 0 ; i < abs(plu); i ++) - { - digitalWrite(plu_pin, HIGH); - delayMicroseconds(duration_us); - digitalWrite(plu_pin, LOW); - delayMicroseconds(duration_us); - } -} - -int main() -{ - init(); - setup(); - - while (1) - { - //旋转180° - driver_run(6400 / 2, 50); - delay(1000); - //旋转-90° - driver_run(-6400 / 4, 100); - delay(1000); - } - return 0; -} - - - -//串口烧录失败 sudo chmod 777 /dev/tty* -// sudo usermod -aG dialout 用户名