|
| 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