test/emulator/src/test_server.cpp
Go to the documentation of this file.
1 /*
2  * @brief test_server.cpp implements a simple tcp server,
3  * simulating a localization controller for unittests.
4  *
5  * Note: test_server.cpp does not implement the functions of localization controller,
6  * it just implements a simple tcp server, accepting tcp connections from clients
7  * and generating telegrams to test the ros driver for sim localization.
8  *
9  * Copyright (C) 2019 Ing.-Buero Dr. Michael Lehning, Hildesheim
10  * Copyright (C) 2019 SICK AG, Waldkirch
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  * All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions are met:
28  *
29  * * Redistributions of source code must retain the above copyright
30  * notice, this list of conditions and the following disclaimer.
31  * * Redistributions in binary form must reproduce the above copyright
32  * notice, this list of conditions and the following disclaimer in the
33  * documentation and/or other materials provided with the distribution.
34  * * Neither the name of SICK AG nor the names of its
35  * contributors may be used to endorse or promote products derived from
36  * this software without specific prior written permission
37  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
38  * contributors may be used to endorse or promote products derived from
39  * this software without specific prior written permission
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
45  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51  * POSSIBILITY OF SUCH DAMAGE.
52  *
53  * Authors:
54  * Michael Lehning <michael.lehning@lehning.de>
55  *
56  * Copyright 2019 SICK AG
57  * Copyright 2019 Ing.-Buero Dr. Michael Lehning
58  *
59  */
60 #include <string>
61 #include <vector>
62 #include <signal.h>
63 
64 #include "sick_scan/ros_wrapper.h"
66 #include "sick_scan/utils.h"
67 
68 int main(int argc, char** argv)
69 {
70  // Ros configuration and initialization
71  ROS::init(argc, argv, "sick_scan_emulator");
72  ROS::NodePtr nh = ROS::createNode("sick_scan_emulator");
73  ROS_INFO_STREAM("sick_scan_emulator started.");
74 
75  // Create a server to simulate a localization controller, incl. a listener thread to accept tcp connections
76  int tcp_port_results = 2201; // Default: The localization controller uses IP port number 2201 to send localization results
77  int tcp_port_cola = 2111; // For requests and to transmit settings to the localization controller: IP port number 2111 and 2112 to send telegrams and to request data, SOPAS CoLa-A or CoLa-B protocols
78  std::string scanner_type = "";
79  ROS::param<int>(nh, "/sick_scan/test_server/result_telegrams_tcp_port", tcp_port_results, tcp_port_results);
80  ROS::param<int>(nh, "/sick_scan/test_server/cola_telegrams_tcp_port", tcp_port_cola, tcp_port_cola);
81  bool start_scandata_immediately = false; // default (false): send scandata after switch into measurement mode (true: start send scandata thread immediately)
82  for(int n = 1; n < argc; n++)
83  {
84  ROS_INFO_STREAM("sick_scan_emulator: " << std::string(argv[n]));
85  if (strncmp(argv[n], "result_telegrams_tcp_port:=", 27) == 0)
86  tcp_port_results = atoi(argv[n] + 27);
87  if (strncmp(argv[n], "cola_telegrams_tcp_port:=", 25) == 0)
88  tcp_port_cola = atoi(argv[n] + 25);
89  if (strncmp(argv[n], "scanner_type:=", 14) == 0)
90  scanner_type = argv[n] + 14;
91  if (strncmp(argv[n], "start_scandata_immediately:=", 28) == 0)
92  start_scandata_immediately = atoi(argv[n] + 28) > 0;
93  }
94  sick_scan_xd::TestServerThread test_server_thread(nh, tcp_port_results, tcp_port_cola, scanner_type, start_scandata_immediately);
95 
96  // Subscribe to sim_loc_driver messages to monitor sim_loc_driver in error simulation mode
97  std::string result_telegrams_topic = "/sick_scan/driver/result_telegrams"; // default topic to publish result port telegram messages (type SickLocResultPortTelegramMsg)
98  ROS::param<std::string>(nh, "/sick_scan/driver/result_telegrams_topic", result_telegrams_topic, result_telegrams_topic);
99 #if defined __ROS_VERSION && __ROS_VERSION == 1
100  sick_scan_xd::SickLocResultPortTelegramMsgSubscriber result_telegram_subscriber
101  = ROS_CREATE_SUBSCRIBER(nh, sick_scan_xd::SickLocResultPortTelegramMsg, result_telegrams_topic, &sick_scan_xd::TestServerThread::messageCbResultPortTelegrams, &test_server_thread);
102 #elif defined __ROS_VERSION && __ROS_VERSION == 2
103  sick_scan_xd::SickLocResultPortTelegramMsgSubscriber result_telegram_subscriber
104  = ROS_CREATE_SUBSCRIBER(nh, sick_scan_xd::SickLocResultPortTelegramMsg, result_telegrams_topic, &sick_scan_xd::TestServerThread::messageCbResultPortTelegramsROS2, &test_server_thread);
105 #endif
106 
107  // Start simulation of a localization controller
108  test_server_thread.start();
109 
110  // Run ros event loop
111  ROS::spin(nh);
112 
113  // Cleanup and exit
114  std::cout << "sick_scan_emulator finished." << std::endl;
115  ROS_INFO_STREAM("sick_scan_emulator finished.");
116  test_server_thread.stop();
117  std::cout << "sick_scan_emulator exits." << std::endl;
118  ROS_INFO_STREAM("sick_scan_emulator exits.");
119  ROS::deleteNode(nh);
120  return 0;
121 }
roswrap::spin
ROSCPP_DECL void spin()
Enter simple event loop.
Definition: rossimu.cpp:260
sick_scan_xd::TestServerThread
Definition: test/emulator/include/sick_scan/test_server_thread.h:76
utils.h
sick_scan_xd::SickLocResultPortTelegramMsg_< std::allocator< void > >
ROS::createNode
ROS::NodePtr createNode(const std::string &node_name="sick_scan_xd")
Definition: ros_wrapper.cpp:65
sick_scan_xd::TestServerThread::stop
virtual bool stop(void)
Definition: test/emulator/src/test_server_thread.cpp:161
roswrap::param::init
void init(const M_string &remappings)
Definition: param_modi.cpp:809
ROS::deleteNode
void deleteNode(ROS::NodePtr &node)
Definition: ros_wrapper.cpp:98
ROS_INFO_STREAM
#define ROS_INFO_STREAM(...)
Definition: sick_scan_ros2_example.cpp:71
sick_scan_xd::TestServerThread::messageCbResultPortTelegrams
void messageCbResultPortTelegrams(const sick_scan_xd::SickLocResultPortTelegramMsg &msg)
Definition: test/emulator/src/test_server_thread.cpp:279
sick_scan_xd::TestServerThread::messageCbResultPortTelegramsROS2
virtual void messageCbResultPortTelegramsROS2(const std::shared_ptr< sick_scan_xd::SickLocResultPortTelegramMsg > msg)
Definition: test/emulator/include/sick_scan/test_server_thread.h:113
sick_scan_xd::TestServerThread::start
virtual bool start(void)
Definition: test/emulator/src/test_server_thread.cpp:132
main
int main(int argc, char **argv)
Definition: test/emulator/src/test_server.cpp:68
ros_wrapper.h
test_server_thread.h


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:12