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). 23 from std_msgs.msg
import Float64
24 from controller
import Robot
31 message =
'Received velocity value: ' + str(data.data)
36 timeStep = int(robot.getBasicTimeStep())
37 left = robot.getMotor(
'motor.left')
38 right = robot.getMotor(
'motor.right')
39 sensor = robot.getDistanceSensor(
'prox.horizontal.2')
40 sensor.enable(timeStep)
41 left.setPosition(float(
'inf'))
42 right.setPosition(float(
'inf'))
44 left.setVelocity(velocity)
45 right.setVelocity(velocity)
47 print(
'Initializing ROS: connecting to ' + os.environ[
'ROS_MASTER_URI'])
49 rospy.init_node(
'listener', anonymous=
True)
50 print(
'Subscribing to "motor" topic')
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())
61 left.setVelocity(velocity)
62 right.setVelocity(velocity)