Skip to content

Commit b08ba8e

Browse files
committed
publishing added
1 parent 61eec4b commit b08ba8e

File tree

9 files changed

+382
-0
lines changed

9 files changed

+382
-0
lines changed

src/rosserial_msgs/_TopicInfo.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import ustruct as struct
2+
3+
class TopicInfo(object):
4+
5+
md5sum = "0ad51f88fc44892f8c10684077646005"
6+
_type = "rosserial_msgs/TopicInfo"
7+
_has_header = False #flag to mark the presence of a Header object
8+
_full_text = """# special topic_ids
9+
uint16 ID_PUBLISHER=0
10+
uint16 ID_SUBSCRIBER=1
11+
uint16 ID_SERVICE_SERVER=2
12+
uint16 ID_SERVICE_CLIENT=4
13+
uint16 ID_PARAMETER_REQUEST=6
14+
uint16 ID_LOG=7
15+
uint16 ID_TIME=10
16+
uint16 ID_TX_STOP=11
17+
18+
# The endpoint ID for this topic
19+
uint16 topic_id
20+
21+
string topic_name
22+
string message_type
23+
24+
# MD5 checksum for this message type
25+
string md5sum
26+
27+
# size of the buffer message must fit in
28+
int32 buffer_size
29+
"""
30+
31+
ID_PUBLISHER = 0
32+
ID_SUBSCRIBER = 1
33+
ID_SERVICE_SERVER = 2
34+
ID_SERVICE_CLIENT = 4
35+
ID_PARAMETER_REQUEST = 6
36+
ID_LOG = 7
37+
ID_TIME = 10
38+
ID_TX_STOP = 11
39+
40+
def __init__(self):
41+
42+
self.topic_id=0
43+
self.topic_name=''
44+
self.message_type=''
45+
self.md5sum=''
46+
self.buffer_size=0
47+
48+
def serialize(self, buff):
49+
try:
50+
buff.write(struct.pack('<H',self.topic_id))
51+
buff.write(struct.pack('<I%ss'%len(self.topic_name),len(self.topic_name),self.topic_name))
52+
buff.write(struct.pack('<I%ss'%len(self.message_type),len(self.message_type),self.message_type))
53+
buff.write(struct.pack('<I%ss'%len(self.md5sum),len(self.md5sum),self.md5sum))
54+
buff.write(struct.pack('<i',self.buffer_size))
55+
except Exception as e:
56+
print(e)
57+
58+
def deserialize(self, str)
59+
try:
60+
end=0
61+
start=end
62+
end+=2
63+
(self.topic_id,) = struct.unpack('<H', str[start:end])
64+
start = end
65+
end += 4
66+
(length,) = struct.unpack('<I', str[start:end])
67+
start = end
68+
end += length
69+
self.topic_name = str[start:end].decode('utf-8')
70+
start = end
71+
end += 4
72+
(length,) = struct.unpack('<I',str[start:end])
73+
start = end
74+
end += length
75+
self.message_type=str[start:end].decode('utf-8')
76+
start = end
77+
end += 4
78+
(length,) = struct.unpack('<I', str[start:end])
79+
start = end
80+
end += length
81+
self.md5sum = str[start:end].decode('utf-8')
82+
start = end
83+
end += 4
84+
(self.buffer_size,) = _get_struct_i().unpack(str[start:end])
85+
return self
86+
except Exception as e:
87+
print(e)
88+
89+
90+
91+

src/std_msgs/_Bool.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import ustruct as struct
2+
3+
class Bool(object):
4+
_md5sum = "8b94c1b53db61fb6aed406028ad6332a"
5+
_type = "std_msgs/Bool"
6+
_has_header = False #flag to mark the presence of a Header object
7+
_full_text = """bool data"""
8+
__slots__ = ['data']
9+
_slot_types = ['bool']
10+
11+
def __init__(self):
12+
self.data = None
13+
14+
def serialize(self, buff):
15+
try:
16+
buff.write(struct.pack('<B', self.data))
17+
except Exception as e:
18+
print(e)
19+
20+
def deserialize(self, str):
21+
try:
22+
end = 0
23+
start = end
24+
end += 1
25+
(self.data,) = struct.unpack('<B', str[start:end])
26+
self.data = bool(self.data)
27+
return self
28+
except Exception as e:
29+
print(e)

src/std_msgs/_Byte.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import struct as ustruct
2+
3+
class Byte(object):
4+
_md5sum = "ad736a2e8818154c487bb80fe42ce43b"
5+
_type = "std_msgs/Byte"
6+
_has_header = False #flag to mark the presence of a Header object
7+
_full_text = """byte data
8+
"""
9+
10+
def __init__(self):
11+
self.data=0
12+
13+
def serialize(self, buff):
14+
try:
15+
buff.write(struct.pack('<b', self.data))
16+
except Exception as e:
17+
print(e)
18+
19+
def deserialize(self, str):
20+
try:
21+
end = 0
22+
start = end
23+
end += 1
24+
(self.data,) = struct.unpack('<b', str[start:end])
25+
return self
26+
except Exception as e:
27+
print(e)

src/std_msgs/_Int64.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import ustruct as struct
2+
3+
class Int64(object):
4+
_md5sum = "34add168574510e6e17f5d23ecc077ef"
5+
_type = "std_msgs/Int64"
6+
_has_header = False #flag to mark the presence of a Header object
7+
_full_text = """int64 data"""
8+
9+
def __init__(self):
10+
self.data=0
11+
12+
def serialize(self, buff):
13+
try:
14+
buff.write(struct.pack('<q', self.data))
15+
except Exception as e:
16+
print(e)
17+
18+
def deserialize(self, str):
19+
try:
20+
end = 0
21+
start = end
22+
end += 8
23+
(self.data,) = struct.unpack('<q', str[start:end])
24+
return self
25+
except Exception as e:
26+
print(e)

src/std_msgs/_String.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import ustruct as struct
2+
3+
class String(object):
4+
_md5sum = "992ce8a1687cec8c8bd883ec73ca41d1"
5+
_type = "std_msgs/String"
6+
_has_header = False #flag to mark the presence of a Header object
7+
_full_text = """string data
8+
"""
9+
10+
def __init__(self):
11+
self.data=''
12+
13+
def serialize(self, buff):
14+
try:
15+
buff.write(struct.pack('<I%ss'%len(self.data), len(self.data), self.data))
16+
except Exception as e:
17+
print(e)
18+
19+
def deserialize(self,str):
20+
try:
21+
end = 0
22+
start = end
23+
end += 4
24+
(length,) = struct.unpack('<I', str[start:end])
25+
start = end
26+
end += length
27+
self.data = str[start:end].decode('utf-8')
28+
return self
29+
except Exception as e:
30+
print(e)
31+
32+

src/uros/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .core import NodeHandle

src/uros/core.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import machine as m
2+
import uio
3+
import struct
4+
from time import sleep, sleep_ms, sleep_us
5+
from rosserial_msgs._TopicInfo import TopicInfo
6+
7+
header=[0xff,0xfe]
8+
9+
class NodeHandle(object):
10+
def __init__(self, serial_id, baudrate):
11+
12+
self.id=1
13+
14+
self.advertised_topics=dict()
15+
self.serial_id=serial_id
16+
self.baudrate=baudrate
17+
self.uart = m.UART(self.serial_id, self.baudrate)
18+
self.uart.init(self.baudrate, bits=8, parity=None, stop=1, txbuf=0)
19+
20+
def init_node(self):
21+
pass
22+
23+
def _advertise_topic(self,topic_name, msg):
24+
25+
register=TopicInfo()
26+
register.topic_id=self.id
27+
register.topic_name=topic_name
28+
register.message_type=msg._type
29+
register.md5sum=msg._md5sum
30+
31+
self.advertised_topics[topic_name]=self.id
32+
33+
self.id+=1
34+
35+
try:
36+
register.buffer_size=msg.buffer_size
37+
except:
38+
pass
39+
40+
#serialization
41+
packet=uio.StringIO()
42+
register.serialize(packet)
43+
44+
packet=list(packet.getvalue().encode('utf-8'))
45+
length=len(packet)
46+
47+
crclen=[checksum(le(length))]
48+
crcpack=[checksum([0,0]+packet)]
49+
50+
#final packet to be sent
51+
fpacket=header+le(length)+crclen+[0,0]+packet+crcpack
52+
53+
self.uart.write(bytearray(fpacket))
54+
55+
56+
57+
def publish(self,topic_name,msg):
58+
if topic_name not in self.advertised_topics:
59+
self._advertise_topic(topic_name, msg)
60+
61+
packet=uio.StringIO()
62+
msg.serialize(packet)
63+
64+
packet=list(packet.getvalue().encode('utf-8'))
65+
length=len(packet)
66+
67+
topic_id=le(self.advertised_topics.get(topic_name))
68+
crclen=[checksum(le(length))]
69+
crcpack=[checksum(topic_id+packet)]
70+
71+
fpacket=header+le(length)+crclen+topic_id+packet+crcpack
72+
self.uart.write(bytearray(fpacket))
73+
74+
def subscribe(self):
75+
pass
76+
77+
78+
def checksum(arr):
79+
return 255-((sum(arr))%256)
80+
81+
def le(h):
82+
h &= 0xffff
83+
return [h & 0xff, h >> 8]

tests/protocoltest.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import machine as m
2+
import uio
3+
import struct
4+
from time import sleep, sleep_ms, sleep_us
5+
6+
#rosserial protocol header
7+
header=bytearray([0xff,0xfe])
8+
9+
#little-endian function
10+
def le(h):
11+
h &= 0xffff
12+
return [h & 0xff, h >> 8]
13+
14+
#generic checksum
15+
def checksum(arr):
16+
return 255-((sum(arr))%256)
17+
18+
#defined baudrate to use
19+
baudrate=57600
20+
21+
#uart definition for tx2 and rx2
22+
uart = m.UART(2,baudrate)
23+
uart.init(baudrate, bits=8, parity=None, stop=1, txbuf=0)
24+
25+
#values for topic negotiation
26+
topic_id=5
27+
topic_name="Greet"
28+
message_type="std_msgs/String"
29+
md5sum='992ce8a1687cec8c8bd883ec73ca41d1'
30+
buffer_size=30
31+
32+
#packet serialization for negotiation
33+
packettopic=uio.StringIO()
34+
def serialize(packet):
35+
packettopic.write(struct.pack('<H',topic_id))
36+
packettopic.write(struct.pack('<I%ss'%len(topic_name),len(topic_name),topic_name))
37+
packettopic.write(struct.pack('<I%ss'%len(message_type),len(message_type),message_type))
38+
packettopic.write(struct.pack('<I%ss'%len(md5sum),len(md5sum),md5sum))
39+
packettopic.write(struct.pack('<i',buffer_size))
40+
serialize(packettopic)
41+
packettopic=packettopic.getvalue().encode('utf-8')
42+
packetlen=len(list(packettopic))
43+
checksumlen=checksum([packetlen])
44+
checksumpack=list([checksum(list([0,0]+list(packettopic)))])
45+
46+
#packet serialization for publishing
47+
packetdata=uio.StringIO()
48+
dato="hola funpython"
49+
def serializeString(packet):
50+
packetdata.write(struct.pack('<I%ss'%len(dato),len(dato),dato))
51+
serializeString(packetdata)
52+
packetdata=packetdata.getvalue().encode('utf-8')
53+
packetlendata=len(list(packetdata))
54+
checksumlendata=checksum([packetlendata])
55+
checksumpackdata=list([checksum(list([5,0]+list(packetdata)))])
56+
57+
listo=0
58+
59+
while True:
60+
data = uart.read()
61+
if data==b'\xff\xfe\x00\x00\xff\x00\x00\xff':
62+
#negotiation is made
63+
uart.write(header)
64+
uart.write(bytearray(le(packetlen)))
65+
uart.write(bytearray([checksumlen]))
66+
uart.write(bytearray([0,0]))
67+
uart.write(packettopic)
68+
uart.write(bytearray(checksumpack))
69+
print('enviado registro de topic')
70+
listo=1
71+
elif listo==1:
72+
#publishing to the node
73+
uart.write(header)
74+
uart.write(bytearray(le(packetlendata)))
75+
uart.write(bytearray([checksumlendata]))
76+
uart.write(bytearray([5,0]))
77+
uart.write(packetdata)
78+
uart.write(bytearray(checksumpackdata))
79+
print('enviado hola')
80+
sleep(1)
81+
82+

tests/publishhola.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#uros contains NodeHandle which is reponsable to publish and subscribe
2+
import uros
3+
from std_msgs._String import String #object string imported
4+
from time import sleep
5+
node=uros.NodeHandle(2,115200) #node initialized, for tx2/rx2 and 115200 baudrate
6+
msg=String() #msg object init
7+
msg.data='Hola FunPython - Pilas esa cuarentena :v'
8+
while True:
9+
node.publish('Greet',msg) #publish data to node
10+
sleep(1)
11+

0 commit comments

Comments
 (0)