serial_read.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Robotical Ltd. 2016 - Apache License
3 import signal
4 import sys
5 import time
6 import serial
7 import StringIO
8 from rosserial_msgs.msg import TopicInfo
9 from std_msgs.msg import String, Int8
10 from marty_msgs.msg import ServoMsg, ServoMsgArray
11 ROS_ACK = "\xff\xfe\x00\x00\xff\x00\x00\xff"
12 # ROS_STOP = "\xff\xfe\x00\x00\xff\x0b\x00\xf4" # TX stop request
13 ROS_RESP = "\xff\xfe" # Sync Flag / Protocol version
14 
15 # ROS msgs md5 checksums
16 MD5_String = "992ce8a1687cec8c8bd883ec73ca41d1"
17 MD5_Int8 = "27ffa0c9c4b8fb8492252bcad9e5c57b"
18 MD5_Int32 = "da5909fbe378aeaf85e547e830cc1bb7"
19 MD5_ServoMsg = "1bdf7d827c361b33ed3b4360c70ce4c0"
20 MD5_ServoMsgArray = "320c8def1674d51423fb942c56a6619b"
21 
22 freq = 1.0
23 
24 # Catch Ctrl-c
25 def signal_handler(signal, frame):
26  print "EXITING!"
27  sys.exit(0)
28 signal.signal(signal.SIGINT, signal_handler)
29 
30 # Byte/Hex Manipulation Methods
31 #-------------------------------------------------------------------------------
32 
33 def ByteToHex( byteStr ):
34  return ''.join( [ "%02X " % ord( j ) for j in byteStr ] ).strip()
35 
36 #-------------------------------------------------------------------------------
37 
38 def ByteToInt( byteStr ):
39  test = (''.join( [ "%02X " % ord( j ) for j in byteStr ] ).strip()).split()
40  for i in xrange(len(test)):
41  test[i] = int(test[i], 16)
42  return sum(test)
43 
44 #-------------------------------------------------------------------------------
45 
46 def HexToByte( hexStr ):
47  byte_array = []
48  hexStr = ''.join( hexStr.split(" ") )
49  for i in range(0, len(hexStr), 2):
50  byte_array.append( chr( int (hexStr[i:i+2], 16 ) ) )
51  return ''.join( byte_array )
52 
53 #-------------------------------------------------------------------------------
54 
55 # SerialMsg Length Checksum
56 def LCheckSum(data):
57  checksum = len(data)
58  return " {:02X}".format(255 - (checksum % 256))
59 
60 # TopicID Checksum
61 def TCheckSum(topic_id, data):
62  checksum = topic_id + ByteToInt(data)
63  return " {:02X}".format(255 - (checksum % 256))
64 
65 # Length as 2 HexBytes
66 def HexMsgLen(data):
67  LEN_HIGH = " {:02X}".format(len(data) / 256)
68  LEN_LOW = " {:02X}".format(len(data) % 256)
69  return LEN_LOW, LEN_HIGH
70 
71 # TopicNum as 2 HexBytes
72 def HexTopID(topic_num):
73  TID_HIGH = " {:02X}".format(topic_num / 256)
74  TID_LOW = " {:02X}".format(topic_num % 256)
75  print "LOW/HIGH: ", TID_LOW, TID_HIGH
76  return TID_LOW, TID_HIGH
77 
78 
79 #-------------------------------------------------------------------------------
80 # MAIN FUNCTION
81 #-------------------------------------------------------------------------------
82 
83 # Init Serial
84 ser = serial.Serial(
85  port='/dev/ttyUSB0',
86  baudrate = 115200,
87  parity=serial.PARITY_NONE,
88  stopbits=serial.STOPBITS_ONE,
89  bytesize=serial.EIGHTBITS,
90  timeout=0
91 )
92 
93 # Setup Publisher Info
94 pub_topic_info = TopicInfo()
95 pub_topic_info.topic_id = 101
96 pub_topic_info.topic_name = "/marty/chatter"
97 pub_topic_info.message_type = "std_msgs/String"
98 pub_topic_info.md5sum = MD5_String
99 pub_topic_info.buffer_size = 256
100 # Initialise Pub Message
101 pub_msg = String()
102 pub_msg.data = "Testing!"
103 
104 # Setup Subscriber Info
105 sub_topic_info = TopicInfo()
106 sub_topic_info.topic_id = 103
107 sub_topic_info.topic_name = "/marty/servo_array"
108 # sub_topic_info.message_type = "std_msgs/Int8"
109 # sub_topic_info.md5sum = MD5_Int8
110 sub_topic_info.message_type = "marty_msgs/ServoMsgArray"
111 sub_topic_info.md5sum = MD5_ServoMsgArray
112 # sub_topic_info.message_type = "marty_msgs/ServoMsgArray"
113 # sub_topic_info.md5sum = MD5_ServoMsgArray
114 sub_topic_info.buffer_size = 32
115 
116 # Initialise Serial Publisher Buffer
117 pub_buffer = StringIO.StringIO()
118 pub_topic_info.serialize(pub_buffer)
119 pub_topic_data = pub_buffer.getvalue()
120 
121 # Initialise Serial Message Buffer
122 pub_msg_buffer = StringIO.StringIO()
123 pub_msg.serialize(pub_msg_buffer)
124 msg_data = pub_msg_buffer.getvalue()
125 
126 # Initialise Serial Subscriber Buffer
127 sub_buffer = StringIO.StringIO()
128 sub_topic_info.serialize(sub_buffer)
129 sub_topic_data = sub_buffer.getvalue()
130 
131 # Flags
132 ready = False
133 pub_init = False
134 sub_init = False
135 
136 test_pub_id = 0
137 test_sub_id = 1
138 
139 # Wait for ROS Serial ACK, then send info
140 while 1:
141  # Wait for ROSSerial Acknowledgement
142  x=ser.readline()
143  print "Received: ", ByteToHex(x)
144  # if (ready == False):
145  # print "Expecting: ", ByteToHex(ROS_ACK)
146  # if (x==ROS_ACK):
147  # print "GOT ROS ACK!"
148  # ready = True
149  # else:
150  # print "No ROS ACK :("
151  # else:
152  # Send Publisher Setup
153  if (sub_init == False):
154  len_low, len_high = HexMsgLen(sub_topic_data)
155  # tid_low, tid_high = HexTopID(1)
156  tid_low, tid_high = HexTopID(TopicInfo.ID_SUBSCRIBER)
157  l_checksum = LCheckSum(sub_topic_data)
158  t_checksum = TCheckSum(TopicInfo.ID_SUBSCRIBER, sub_topic_data)
159  s_header = ByteToHex(ROS_RESP) + len_low + len_high + l_checksum + tid_low + tid_high
160  print "SerialHeader: ", s_header
161  print "TopicInfo: ", sub_buffer.getvalue()
162  ser.write(HexToByte(s_header) + sub_buffer.getvalue() + HexToByte(t_checksum))
163  sub_init = True
164  time.sleep(1)
165  # Send Message
166  else:
167  print "Waiting for Sub Msg!"
168  # sub_msg_buffer = StringIO.StringIO()
169  # print "Length: ", len(x)
170  # if (len(x) <= 9):
171  # # print "Received: ", ByteToHex(x)
172  # sub_msg_buffer.write(x)
173  # # print "RecBuff: ", ByteToHex(sub_msg_buffer.getvalue())
174  # # # Initialise Sub Message
175  # sub_msg = Int8()
176  # sub_msg.deserialize(sub_msg_buffer.getvalue())
177  # print "GOT: ", sub_msg.data
178 
179  # Send Publisher Setup
180  # if (pub_init == False):
181  # len_low, len_high = HexMsgLen(pub_topic_data)
182  # tid_low, tid_high = HexTopID(TopicInfo.ID_PUBLISHER)
183  # # tid_low, tid_high = HexTopID(pub_topic_info.topic_id)
184  # l_checksum = LCheckSum(pub_topic_data)
185  # t_checksum = TCheckSum(TopicInfo.ID_PUBLISHER, pub_topic_data)
186  # s_header = ByteToHex(ROS_RESP) + len_low + len_high + l_checksum + tid_low + tid_high
187  # print "SerialHeader: ", s_header
188  # print "TopicInfo: ", pub_buffer.getvalue()
189  # ser.write(HexToByte(s_header) + pub_buffer.getvalue() + HexToByte(t_checksum))
190  # pub_init = True
191  # time.sleep(1)
192  # # Send Message
193  # else:
194  # len_low, len_high = HexMsgLen(msg_data)
195  # tid_low, tid_high = HexTopID(pub_topic_info.topic_id)
196  # l_checksum = LCheckSum(msg_data)
197  # t_checksum = TCheckSum(pub_topic_info.topic_id, msg_data)
198  # s_header = ByteToHex(ROS_RESP) + len_low + len_high + l_checksum + tid_low + tid_high
199  # ser.write(HexToByte(s_header) + pub_msg_buffer.getvalue() + HexToByte(t_checksum))
200  time.sleep(1.0/freq)
def ByteToHex(byteStr)
Definition: serial_read.py:33
def HexMsgLen(data)
Definition: serial_read.py:66
def TCheckSum(topic_id, data)
Definition: serial_read.py:61
def signal_handler(signal, frame)
Definition: serial_read.py:25
def LCheckSum(data)
Definition: serial_read.py:56
def HexToByte(hexStr)
Definition: serial_read.py:46
def HexTopID(topic_num)
Definition: serial_read.py:72
def ByteToInt(byteStr)
Definition: serial_read.py:38


ros_marty
Author(s): Alejandro Bordallo
autogenerated on Fri Jul 21 2017 14:51:45