Skip to content

Commit 4b85508

Browse files
committed
comments and doc
1 parent 14008af commit 4b85508

File tree

5 files changed

+101
-15
lines changed

5 files changed

+101
-15
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
11
# uPy-rosserial
2-
This is an implementation of rosserial for MicroPython, here everything needed will be found.
2+
`rosserial` a method used by ROS in order to establish communication via serial , mostly this is used with microcontrollers, which in this case are the ones responsible in some ROS applications for actuators and sensors usage.
3+
4+
Since there is no rosserial package for uPy as there is for Arduino, this repo has been created where every needed script to establish rosserial with uPy will be found.
5+
6+
## Features
7+
- [x] Advertising Topics
8+
- [x] Publishing
9+
- [ ] Subscribing
10+
- [ ] Actions
11+
- [ ] Services
12+
13+
**To Do: Subscribing testing and implementation.**
14+
15+
## Installation
16+
In theory every board with the kind of generic `UART` class for ESP32 is capable of using this library, but it must be known exactly which `UART ID` is being used, this means for example, for ESP32 defined pins correspond to TX0 and RX0 for UART0, and so on. In the examples below, UART2 is used.
17+
18+
In order to use ros node communication, have in mind a python class for each message must be available. this means a dependency of this library is [uPy Genpy](https://github.com/FunPythonEC/uPy-genpy), used to create Python classes for messages from `*.msg` files. Follow the installation from `ugenpy` before proceeding.
19+
20+
Once `ugenpy` is inside, the packages `uros` and `rosserial_msgs` from `src` folder from this repository must be copied to the flash memory. I strongly recommend using [rshell](https://github.com/dhylands/rshell).
21+
>Note: Soon this will be available to be installed with upip.
22+
23+
**Have in mind before publishing or subscribing to a topic, the message class must be generated with `ugenpy`**
24+
25+
## Usage
26+
### Publish example
27+
28+
Suppose `ColorRGBA.py` has been created using `ugenpy` and `ColorRGBA.msg` file:
29+
```
30+
float32 r
31+
float32 g
32+
float32 b
33+
float32 a
34+
```
35+
36+
And then running the following e.g.:
37+
38+
``` python
39+
import uros
40+
from std_msgs._ColorRGBA import ColorRGBA #message object ColorRGBA
41+
from time import sleep
42+
node=uros.NodeHandle(2,115200) #node initialized, for tx2/rx2 and 115200 baudrate
43+
msg=ColorRGBA() #msg object init
44+
msg.r=1 #values to variables assigned
45+
msg.g=3
46+
msg.b=4
47+
msg.a=1
48+
while True:
49+
node.publish('Colorsh',msg) #publish data to node Colorsh
50+
sleep(1)
51+
```

src/rosserial_msgs/_TopicInfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def deserialize(self, str):
8181
self.md5sum = str[start:end].decode('utf-8')
8282
start = end
8383
end += 4
84-
(self.buffer_size,) = _get_struct_i().unpack(str[start:end])
84+
(self.buffer_size,) = struct.unpack('<i',str[start:end])
8585
return self
8686
except Exception as e:
8787
print(e)

src/uros/core.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
1+
#libraries needed
2+
# machine: for uart usage
3+
# uio: packet buffer
4+
# struct: serlization
5+
# _TopicInfo: for topic negotiation
6+
17
import machine as m
28
import uio
3-
import struct
9+
import ustruct as struct
410
from time import sleep, sleep_ms, sleep_us
511
from rosserial_msgs._TopicInfo import TopicInfo
612

13+
#rosserial protocol header
714
header=[0xff,0xfe]
815

16+
#class to manage publish and subscribe
17+
#COULD BE CHANGED AFTERWARDS
918
class NodeHandle(object):
1019
def __init__(self, serial_id, baudrate):
1120

21+
"""
22+
id: used for topics id (negotiation)
23+
advertised_topics: manage alreade negotiated topics
24+
serial_id: uart id
25+
baudrate: baudrate used for serial comm
26+
"""
1227
self.id=1
13-
1428
self.advertised_topics=dict()
1529
self.serial_id=serial_id
1630
self.baudrate=baudrate
1731
self.uart = m.UART(self.serial_id, self.baudrate)
1832
self.uart.init(self.baudrate, bits=8, parity=None, stop=1, txbuf=0)
1933

20-
def init_node(self):
21-
pass
2234

35+
#method to manage and advertise topic
36+
#before publishing or subscribing
2337
def _advertise_topic(self,topic_name, msg):
2438

39+
"""
40+
topic_name: eg. (/std_msgs/Greet)
41+
msg: message object
42+
"""
2543
register=TopicInfo()
2644
register.topic_id=self.id
2745
register.topic_name=topic_name
@@ -30,34 +48,37 @@ def _advertise_topic(self,topic_name, msg):
3048

3149
self.advertised_topics[topic_name]=self.id
3250

51+
#id are summed by one
3352
self.id+=1
3453

3554
try:
3655
register.buffer_size=msg.buffer_size
37-
except:
38-
pass
56+
except Exception as e:
57+
print(e)
3958

4059
#serialization
4160
packet=uio.StringIO()
4261
register.serialize(packet)
4362

63+
#already serialized (packet)
4464
packet=list(packet.getvalue().encode('utf-8'))
4565
length=len(packet)
4666

67+
#both checksums
4768
crclen=[checksum(le(length))]
4869
crcpack=[checksum([0,0]+packet)]
4970

5071
#final packet to be sent
5172
fpacket=header+le(length)+crclen+[0,0]+packet+crcpack
52-
5373
self.uart.write(bytearray(fpacket))
5474

5575

56-
5776
def publish(self,topic_name,msg):
77+
5878
if topic_name not in self.advertised_topics:
5979
self._advertise_topic(topic_name, msg)
6080

81+
#same as advertise
6182
packet=uio.StringIO()
6283
msg.serialize(packet)
6384

@@ -74,10 +95,10 @@ def publish(self,topic_name,msg):
7495
def subscribe(self):
7596
pass
7697

77-
98+
#checksum method, receives array
7899
def checksum(arr):
79100
return 255-((sum(arr))%256)
80-
101+
#little-endian method
81102
def le(h):
82103
h &= 0xffff
83104
return [h & 0xff, h >> 8]

tests/colorrgba.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import uros
2+
from std_msgs._ColorRGBA import ColorRGBA #message object ColorRGBA
3+
from time import sleep
4+
node=uros.NodeHandle(2,115200) #node initialized, for tx2/rx2 and 115200 baudrate
5+
msg=ColorRGBA() #msg object init
6+
msg.r=1 #values to variables assigned
7+
msg.g=3
8+
msg.b=4
9+
msg.a=1
10+
while True:
11+
node.publish('Colorsh',msg) #publish data to node Colorsh
12+
sleep(1)

tests/protocoltest.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
This is a script to prove
3+
rosserial topic negotiation
4+
and publish
5+
"""
6+
17
import machine as m
28
import uio
39
import struct
@@ -77,6 +83,4 @@ def serializeString(packet):
7783
uart.write(packetdata)
7884
uart.write(bytearray(checksumpackdata))
7985
print('enviado hola')
80-
sleep(1)
81-
82-
86+
sleep(1)

0 commit comments

Comments
 (0)