ros_python.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright 1996-2020 Cyberbotics Ltd.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 """
18 This is a simple example of a Webots controller running a Python ROS node thanks to rospy.
19 The robot is publishing the value of its front distance sensor and receving motor commands (velocity).
20 """
21 
22 import rospy
23 from std_msgs.msg import Float64
24 from controller import Robot
25 import os
26 
27 
28 def callback(data):
29  global velocity
30  global message
31  message = 'Received velocity value: ' + str(data.data)
32  velocity = data.data
33 
34 
35 robot = Robot()
36 timeStep = int(robot.getBasicTimeStep())
37 left = robot.getMotor('motor.left')
38 right = robot.getMotor('motor.right')
39 sensor = robot.getDistanceSensor('prox.horizontal.2') # front central proximity sensor
40 sensor.enable(timeStep)
41 left.setPosition(float('inf')) # turn on velocity control for both motors
42 right.setPosition(float('inf'))
43 velocity = 0
44 left.setVelocity(velocity)
45 right.setVelocity(velocity)
46 message = ''
47 print('Initializing ROS: connecting to ' + os.environ['ROS_MASTER_URI'])
48 robot.step(timeStep)
49 rospy.init_node('listener', anonymous=True)
50 print('Subscribing to "motor" topic')
51 robot.step(timeStep)
52 rospy.Subscriber('motor', Float64, callback)
53 pub = rospy.Publisher('sensor', Float64, queue_size=10)
54 print('Running the control loop')
55 while robot.step(timeStep) != -1 and not rospy.is_shutdown():
56  pub.publish(sensor.getValue())
57  print('Published sensor value: ', sensor.getValue())
58  if message:
59  print(message)
60  message = ''
61  left.setVelocity(velocity)
62  right.setVelocity(velocity)
def callback(data)
Definition: ros_python.py:28


webots_ros
Author(s): Cyberbotics
autogenerated on Fri Sep 4 2020 03:55:03