-
Notifications
You must be signed in to change notification settings - Fork 12
subt: add rospy+zmq based communication with cloudsim #523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a912dd7
subt: add rospy+zmq based communication with cloudsim
zbynekwinkler dca11c3
Update base docker with msgpack and pyzmq for python2.
zbynekwinkler b1ac06c
subt.cloudsim2osgar: introduce acc, rot and orientation outputs.
zbynekwinkler 35feb57
subt/ros/proxy/ros_proxy_node.cc: remove imu-related messages
zbynekwinkler 5bfb8a2
subt.cloudsim2osgar: remove combined imu message
zbynekwinkler 0f44a12
Merge remote-tracking branch 'origin/master' into feature/cloudsim2osgar
zbynekwinkler 3e204d6
subt/zmq-subt-x2.json: remove the "imu" output (not used yet)
zbynekwinkler e885071
subt.cloudsim2osgar: remove 'imu' output
zbynekwinkler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #!/usr/bin/env python2 | ||
|
|
||
| import errno | ||
| import math | ||
| import socket | ||
| import sys | ||
| import threading | ||
| import time | ||
|
|
||
| import rospy | ||
| import zmq | ||
| import msgpack | ||
|
|
||
| from sensor_msgs.msg import Imu | ||
|
|
||
|
|
||
| def py3round(f): | ||
| if abs(round(f) - f) == 0.5: | ||
| return int(2.0 * round(f / 2.0)) | ||
| return int(round(f)) | ||
|
|
||
|
|
||
| def wait_for_master(): | ||
| """Return when /use_sim_time is set in rosparams. If rospy.init_node is called before /use_sim_time | ||
| is set, it uses real time.""" | ||
| print("Waiting for rosmaster") | ||
| start = time.time() | ||
| last_params = [] | ||
| while not rospy.is_shutdown(): | ||
| try: | ||
| params = rospy.get_param_names() | ||
| if params != last_params: | ||
| print(time.time()-start, params) | ||
| if '/use_sim_time' in params: | ||
| return True | ||
| last_params = params | ||
| time.sleep(0.01) | ||
| except socket.error as serr: | ||
| if serr.errno != errno.ECONNREFUSED: | ||
| raise serr # re-raise error if its not the one we want | ||
| time.sleep(0.5) | ||
|
|
||
| class Bus: | ||
| def __init__(self): | ||
| self.lock = threading.Lock() | ||
| context = zmq.Context.instance() | ||
| self.push = context.socket(zmq.PUSH) | ||
| self.push.setsockopt(zmq.LINGER, 100) # milliseconds | ||
| self.push.bind('tcp://*:5565') | ||
|
|
||
| def register(self, *outputs): | ||
| pass | ||
|
|
||
| def publish(self, channel, data): | ||
| raw = msgpack.packb(data, use_bin_type=True) | ||
| with self.lock: | ||
| self.push.send_multipart([channel, raw]) | ||
|
|
||
|
|
||
| class main: | ||
| def __init__(self): | ||
| # get cloudsim ready | ||
| robot_name = sys.argv[1] | ||
| imu_name = '/'+robot_name+'/imu/data' | ||
| wait_for_master() | ||
| rospy.init_node('imu2osgar', log_level=rospy.DEBUG) | ||
| rospy.loginfo("waiting for {}".format(imu_name)) | ||
| rospy.wait_for_message(imu_name, Imu) | ||
| rospy.sleep(2) | ||
| self.imu_count = 0 | ||
|
|
||
| # start | ||
| self.bus = Bus() | ||
| self.bus.register('rot', 'acc', 'orientation') | ||
| rospy.Subscriber(imu_name, Imu, self.imu) | ||
| rospy.spin() | ||
|
|
||
| def imu(self, msg): | ||
| self.imu_count += 1 | ||
| rospy.loginfo_throttle(10, "imu callback: {}".format(self.imu_count)) | ||
| acc = [msg.linear_acceleration.x, msg.linear_acceleration.y, msg.linear_acceleration.z] | ||
| orientation = [msg.orientation.x, msg.orientation.y, msg.orientation.z, msg.orientation.w] | ||
|
|
||
| # copy & paste from rosmsg | ||
| q0, q1, q2, q3 = orientation # quaternion | ||
| x = math.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2)) | ||
| y = math.asin(2 * (q0 * q2 - q3 * q1)) | ||
| z = math.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3)) | ||
| rot = [x, y, z] | ||
| # end of copy & paste from rosmsg | ||
|
|
||
| self.bus.publish('rot', [py3round(math.degrees(angle) * 100) for angle in rot]) | ||
| self.bus.publish('acc', [py3round(x * 1000) for x in acc]) | ||
| self.bus.publish('orientation', orientation) | ||
| # preliminary suggestion for combined message | ||
| #angular_velocity = [msg.angular_velocity.x, msg.angular_velocity.y, msg.angular_velocity.z] | ||
| #data = [ | ||
| # msg.header.stamp.to_nsec()/1000000, # time in milliseconds | ||
| # orientation, | ||
| # angular_velocity, | ||
| # acc, | ||
| #] | ||
| #self.bus.publish('imu', data) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| if len(sys.argv) < 2: | ||
| print("need robot name as argument") | ||
| raise SystemExit(1) | ||
| main() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import contextlib | ||
|
|
||
| from threading import Thread | ||
|
|
||
| import zmq | ||
|
|
||
| from osgar.bus import BusShutdownException | ||
| import osgar.lib.serialize | ||
|
|
||
| class Pull: | ||
|
|
||
| def __init__(self, config, bus): | ||
| bus.register(*config['outputs']) | ||
| self.endpoint = config.get('endpoint', 'tcp://127.0.0.1:5565') | ||
| self.timeout = config.get('timeout', 1) # default recv timeout 1s | ||
| self.thread = Thread(target=self.run) | ||
| self.thread.name = bus.name | ||
| self.bus = bus | ||
|
|
||
| def start(self): | ||
| self.thread.start() | ||
|
|
||
| def join(self, timeout=None): | ||
| self.thread.join(timeout=timeout) | ||
|
|
||
| def run(self): | ||
| context = zmq.Context.instance() | ||
| socket = context.socket(zmq.PULL) | ||
| # https://stackoverflow.com/questions/7538988/zeromq-how-to-prevent-infinite-wait | ||
| socket.RCVTIMEO = int(self.timeout * 1000) # convert to milliseconds | ||
| socket.connect(self.endpoint) | ||
|
|
||
| with contextlib.closing(socket): | ||
| while self.bus.is_alive(): | ||
| try: | ||
| channel, raw = socket.recv_multipart() | ||
| message = osgar.lib.serialize.deserialize(raw) | ||
| self.bus.publish(channel.decode('ascii'), message) | ||
| except zmq.error.Again: | ||
| pass | ||
|
|
||
| def request_stop(self): | ||
| self.bus.shutdown() | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.