|
| 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 | + |
0 commit comments