Skip to content

Commit 89f3350

Browse files
committed
server and client test ok
1 parent a6ce38a commit 89f3350

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ catkin_package(
132132
# include_directories(include)
133133
include_directories(
134134
${catkin_INCLUDE_DIRS}
135+
server/include
135136
)
136137

137138
## Declare a C++ library
@@ -150,6 +151,9 @@ add_executable(m100_x3_node
150151
src/djicam_utils.cpp
151152
)
152153

154+
add_executable(m100_x3_client_node
155+
src/m100_x3_client_node.cpp
156+
)
153157
## Add cmake target dependencies of the executable
154158
## same as for the library above
155159
# add_dependencies(m100_x3_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
@@ -160,6 +164,10 @@ target_link_libraries(m100_x3_node
160164
${catkin_LIBRARIES}
161165
)
162166

167+
target_link_libraries(m100_x3_client_node
168+
${catkin_LIBRARIES}
169+
)
170+
163171
#############
164172
## Install ##
165173
#############

server/include/msgdef.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ static const uint32_t IMAGE_W = 1280;
99
static const uint32_t IMAGE_H = 720;
1010
static const uint32_t IMAGE_CH = 1;
1111
static const uint32_t HEADER_LEN = 1 * sizeof(uint32_t) + 3 * sizeof(uint32_t) + 2 * sizeof(uint32_t);
12-
static const uint32_t TOTAL_LEN = HEADER_LEN+IMAGE_W*IMAGE_H*IMAGE_CH;
12+
static const uint32_t IMG_DATA_LEN = IMAGE_W*IMAGE_H*IMAGE_CH;
13+
static const uint32_t TOTAL_LEN = HEADER_LEN+IMG_DATA_LEN;
1314

1415
class ImageMsgHeader {
1516
public:

src/m100_x3_client_node.cpp

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <ros/ros.h>
2+
#include <image_transport/image_transport.h>
3+
#include <cv_bridge/cv_bridge.h>
4+
#include <sensor_msgs/image_encodings.h>
5+
#include <sensor_msgs/CameraInfo.h>
6+
#include "cv.h"
7+
8+
#include <iostream>
9+
#include <boost/array.hpp>
10+
#include <boost/asio.hpp>
11+
#include <boost/format.hpp>
12+
#include <sysexits.h>
13+
#include "msgdef.h"
14+
15+
using boost::asio::ip::udp;
16+
using boost::asio::ip::tcp;
17+
using bfmt = boost::format;
18+
19+
using std::cout;
20+
using std::endl;
21+
22+
int main(int argc, char** argv) {
23+
ros::init(argc, argv, "m100_x3_client");
24+
ros::NodeHandle nh("~");
25+
26+
image_transport::ImageTransport transport(nh);
27+
image_transport::Publisher image_pub;
28+
ros::Publisher caminfo_pub;
29+
sensor_msgs::Image im;
30+
sensor_msgs::CameraInfo cam_info;
31+
cv_bridge::CvImage cvi;
32+
33+
cvi.header.frame_id = "image";
34+
cvi.encoding = "mono8";
35+
36+
image_pub = transport.advertise("image_raw", 1);
37+
38+
IplImage* pImg = cvCreateImage(cvSize(IMAGE_W, IMAGE_H), IPL_DEPTH_8U, 1);
39+
40+
try {
41+
boost::asio::io_service io_service;
42+
43+
tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), PORT_NUMBER);
44+
45+
// udp::socket socket(io_service);
46+
// socket.open(udp::v4());
47+
tcp::socket socket(io_service);
48+
boost::system::error_code error = boost::asio::error::host_not_found;
49+
ROS_INFO_STREAM(
50+
"Wait for acceptance... \n(Blocked here means server busy. Another x3client is "
51+
"running?)");
52+
socket.connect(endpoint, error);
53+
if (error) {
54+
ROS_ERROR_STREAM("Server offline, exit...");
55+
return EXIT_FAILURE;
56+
}
57+
58+
boost::array<char, TOTAL_LEN> img;
59+
size_t imgidx = 0;
60+
size_t tfcnt = 0;
61+
ImageMsgHeader h;
62+
ROS_INFO("Connect successfully.");
63+
while (ros::ok()) {
64+
boost::array<char, TOTAL_LEN> buf;
65+
boost::system::error_code error;
66+
67+
size_t len = socket.read_some(boost::asio::buffer(buf), error);
68+
69+
if (error == boost::asio::error::eof) {
70+
ROS_ERROR_STREAM("Server down");
71+
return EX_PROTOCOL; // Connection closed cleanly by peer.
72+
} else if (error)
73+
throw boost::system::system_error(error); // Some other error.
74+
75+
std::copy(buf.begin(), buf.begin() + len, img.begin() + imgidx);
76+
imgidx += len;
77+
tfcnt++;
78+
if (imgidx == TOTAL_LEN) {
79+
h.parse((uint8_t*)img.c_array());
80+
// struct timeval tm;
81+
// gettimeofday(&tm, NULL);
82+
ROS_DEBUG_STREAM(bfmt("[%X] %dx%dx%d @ %.6f / %4d") % h.header % h.image_w %
83+
h.image_h % h.image_ch %
84+
((double)h.secs + 1e-6 * (double)h.usecs) %
85+
// ((int)(tm.tv_sec-h.secs)*1000000+(int)(tm.tv_usec-h.usecs))
86+
tfcnt);
87+
imgidx = 0;
88+
tfcnt = 0;
89+
if ((h.header == 0XFEDCBA98) && (h.image_w == IMAGE_W) && (h.image_h == IMAGE_H) &&
90+
(h.image_ch == IMAGE_CH)) {
91+
memcpy(pImg->imageData, (uint8_t*)img.c_array() + HEADER_LEN, IMG_DATA_LEN);
92+
cvi.image = pImg;
93+
cvi.header.stamp = ros::Time(h.secs, h.usecs);
94+
cvi.toImageMsg(im);
95+
image_pub.publish(im);
96+
} else {
97+
std::cerr << "Header not correct!!!!" << std::endl;
98+
return EX_IOERR;
99+
}
100+
101+
} else if (imgidx > TOTAL_LEN) {
102+
std::cerr << "receive overflow!!!!" << std::endl;
103+
return EX_IOERR;
104+
} else {
105+
// std::cout << std::endl;
106+
}
107+
}
108+
} catch (std::exception& e) {
109+
std::cout << "Big error" << std::endl;
110+
std::cerr << e.what() << std::endl;
111+
}
112+
113+
return 0;
114+
}

0 commit comments

Comments
 (0)