receiver.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 Use this node to establish a connection with the metraTec IPS receiver or USB stick. You have to pass the USB port the
4 stick is connected to or the IP address of the regular receiver as a command line argument or as a private parameter
5 when you are using a launch file.
6 
7 Running from command line:
8  $ rosrun ros_ips receiver.py <TYPE> <REQUIRED> <OPTIONAL>
9  For example:
10  $ rosrun ros_ips receiver.py usb /dev/ttyUSB0
11  $ rosrun ros_ips receiver.py tcp 192.168.2.223
12 
13 Subscribed topics:
14  - ips/receiver/send (std_msgs/String):
15  Message to be sent over the TCP or serial connection to the IPS receiver or USB stick
16 
17 Published topics:
18  - ips/receiver/raw (ros_ips/StringStamped):
19  Raw messages received from the receiver or USB stick
20 
21 Parameters:
22  - ~host (string, default=None):
23  IP address of the connected receiver
24  - ~port (with host: int, default=10001; without host: string, default=None):
25  Port for the TCP connection if '~host' parameter is present, otherwise specifies the USB port of the stick
26  - ~baudrate (int, default=115200):
27  Baudrate to use for serial communication with USB receiver
28 """
29 
30 from __future__ import print_function
31 import rospy
32 import sys
33 from std_msgs.msg import String
34 from ros_ips.msg import StringStamped
35 from ros_ips.communication.usb_serial import USBSerial
36 from ros_ips.communication.tcp_socket import TCPSocket
37 
38 
40  """
41  Use this class to establish a connection with the metraTec IPS receiver or USB stick and publish the received
42  messages to the ips/receiver/raw topic. The node also listens to the ips/receiver/send topic and invokes a
43  callback that forwards these messages to the connected receiver when it is updated.
44  """
45  def __init__(self, type, required, optional):
46  """
47  Initialize IPS receiver class by passing the IP address the receiver or port the USB stick is connected to.
48  :param type: String: type of the connection, either 'tcp' or 'usb'
49  :param required: String: Either HOST_IP or USB_PORT of the receiver
50  :param optional: Int: Either TCP_PORT or BAUDRATE to use for the connection
51  """
52  # type of connected to receiver. Either 'tcp' for standard receiver or 'usb' for USB stick
53  self.type = type
54  # required parameter for connection. IP address for standard receiver or USB port for USB stick
55  self.req = required
56  # optional parameter for connection. Port for standard receiver or Baudrate for USB stick
57  self.opt = optional
58  # initialize serial connection with USB stick or TCP connection with standard receiver
59  if self.type == 'usb':
60  baudrate = self.opt if self.opt is not None else 115200
61  self.con = USBSerial(self.req) if self.opt is None else USBSerial(self.req, baudrate=baudrate)
62  elif self.type == 'tcp':
63  port = self.opt if self.opt is not None else 10001
64  self.con = TCPSocket(self.req) if self.opt is None else TCPSocket(self.req, port=port)
65  else:
66  print('Connection type "{}" is invalid. Shutting down node!'.format(self.type))
67  rospy.signal_shutdown('Connection type "{}" is invalid. Shutting down node!'.format(self.type))
68 
69  # initialize subscriber that listens to ips/receiver/send to send messages to the receiver on callback
70  self.rec_send_sub = rospy.Subscriber('ips/receiver/send', String, self.callback)
71  # initialize publisher object
72  self.rec_pub = rospy.Publisher('ips/receiver/raw', StringStamped, queue_size=10)
73  # specify publishing rate
74  self.rate = rospy.Rate(30)
75 
76  def callback(self, msg):
77  """
78  Send message to connected device when /ips/receiver/send topic is updated.
79  :param msg: String: message to be sent to the connected device
80  """
81  self.con.send(msg.data)
82  print(msg.data)
83  # rospy.sleep(0.5)
84 
85  def publish(self):
86  """Publish raw receiver messages to the ips/receiver/raw topic."""
87  while not rospy.is_shutdown():
88  # get a single receiver message
89  line = self.con.get_line(b'\r')
90  # generate message
91  msg = StringStamped()
92  msg.header.stamp = rospy.Time.now()
93  msg.data = str(line)
94  # publish line as String message with timestamp
95  self.rec_pub.publish(msg)
96  # wait according to publishing rate
97  self.rate.sleep()
98 
99  def shutdown(self):
100  """Close USB connection on shutdown."""
101  self.con.close()
102 
103 
104 def get_args(args):
105  """
106  Return required and optional parameters for this node. Parameters are taken from ROS parameter server if available,
107  otherwise from command line arguments. Required arguments are either HOST_IP or USB_PORT. Depending on the required
108  parameter the optional parameter is either a TCP_PORT or the BAUDRATE, respectively.
109  :param args: CommandLineArguments: arguments passed after $rosrun receiver
110  :return: String/None: required parameter (HOST_IP for TCP/IP connection, USB_PORT for USB connection)
111  Int/None: optional parameter (TCP_PORT for TCP/IP connection, BAUDRATE for USB connection)
112  """
113  type, req, opt = None, None, None
114  if rospy.has_param('~host'):
115  type = 'tcp'
116  req = str(rospy.get_param('~host'))
117  if rospy.has_param('~port'):
118  opt = int(rospy.get_param('~port'))
119  elif rospy.has_param('~port'):
120  type = 'usb'
121  req = str(rospy.get_param('~port'))
122  if rospy.has_param('~baudrate'):
123  opt = int(rospy.get_param('~baudrate'))
124  elif len(args) > 2:
125  type = str(args[1])
126  req = str(args[2])
127  if len(args) > 3:
128  opt = int(args[3])
129 
130  return type, req, opt
131 
132 
133 if __name__ == '__main__':
134  # start node
135  rospy.init_node('receiver', anonymous=True, disable_signals=True)
136  # get parameters from command line arguments or parameter server
137  type, req, opt = get_args(sys.argv)
138  # initialize IPSReceiver class
139  ips = IPSReceiver(type, req, opt)
140  try:
141  # publish receiver messages
142  ips.publish()
143  except rospy.ROSInterruptException:
144  pass
def shutdown(self)
Definition: receiver.py:99
def callback(self, msg)
Definition: receiver.py:76
def publish(self)
Definition: receiver.py:85
def get_args(args)
Definition: receiver.py:104
def __init__(self, type, required, optional)
Definition: receiver.py:45


ros_ips
Author(s): Christian Arndt
autogenerated on Sat May 12 2018 02:24:12