Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions SimpleRobot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions SimpleRobot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SimpleRobot
机器人教学 第二讲

在build文件夹下
cmake ..
make
编译完成
38 changes: 38 additions & 0 deletions SimpleRobot/include/Client.h
Original file line number Diff line number Diff line change
@@ -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
103 changes: 103 additions & 0 deletions SimpleRobot/include/Protocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef PROTOCOL_H
#define PROTOCOL_H

#include "common.h"

//空间点
struct Point
{
int duration; //运动的持续时间 单位us
int * postions; //位置,脉冲
//std::vector<int> postions; //位置,脉冲
//std::vector<int> electric; //电流,mA ,可选项
};


//轨迹
struct Trajectory
{
uint8_t state; //状态值,执行或取消
int axies; //关节数

int pointSize; //单点缓存所占长度

int pointsMaxCount; //点的最大数量

int pointsCount; //点的数量

int bufferMaxSize; //流缓存区最大长度
int bufferSize; //已使用的缓存区长度,用于通讯发送接收数据

//std::vector<Point> 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
44 changes: 44 additions & 0 deletions SimpleRobot/include/Server.h
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions SimpleRobot/include/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef COMMON_H
#define COMMON_H

//所以平时需要用到的头文件都放在这里
#include <iostream>
#include <stdio.h>
#include <inttypes.h>
#include <cstring>
#include <cmath>
#include <signal.h>
#include <vector>

//tcp
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>

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
82 changes: 82 additions & 0 deletions SimpleRobot/src/Client.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
Loading