Skip to content

Instantly share code, notes, and snippets.

@ronekko
Last active October 17, 2016 12:01
Show Gist options
  • Select an option

  • Save ronekko/1693ae5317bb6e3bca3d511a4bab5a56 to your computer and use it in GitHub Desktop.

Select an option

Save ronekko/1693ae5317bb6e3bca3d511a4bab5a56 to your computer and use it in GitHub Desktop.
rosbridge_turtlesim_tcp_client
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 14 20:46:03 2016
@author: sakurai
------------------------------------------------------
This script is an example of TCP client for rosbridge.
Install rosbridge on the remote machine.
$ sudo apt-get install ros-indigo-rosbridge-suite
Run the turtlesim and the rosbridge TCP server.
$ roscore
$ rosrun turtlesim turtlesim_node
$ roslaunch rosbridge_server rosbridge_tcp.launch
Then run this script, and you can control the turtle.
"""
import socket
import json
def make_message(twist):
return json.dumps(dict(op='publish',
topic='/turtle1/cmd_vel',
msg=twist))
def make_twist(x_lin, y_lin, z_lin, x_ang, y_ang, z_ang):
return dict(linear=dict(x=x_lin, y=y_lin, z=z_lin),
angular=dict(x=x_ang, y=y_ang, z=z_ang))
if __name__ == '__main__':
host = '192.168.101.11'
port = 9090
sock = socket.socket()
sock.connect((host, port))
advertise = json.dumps(dict(op='advertise',
topic='/turtle1/cmd_vel',
type='geometry_msgs/Twist'))
sock.send(advertise)
print "Use WASD keys (& Enter) to move the turtle."
while True:
x_lin = z_ang = 0.0
key = raw_input()
if key == 'w':
x_lin = 2.0
elif key == 'a':
z_ang = 2.0
elif key == 's':
x_lin = -2.0
elif key == 'd':
z_ang = -2.0
else:
continue
twist = make_twist(x_lin, 0.0, 0.0, 0.0, 0.0, z_ang)
message = make_message(twist)
sock.send(message)
sock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment