cs354_ws
instead of catkin_ws
, and the
project is named labs
instead
of beginner_tutorials
.
Create a Python file named bumper.py
that moves the robot
forward until it bumps into something, and then stops. You can place
your Python file in the same scripts
directory you
created in the tutorials above.
Some advice: The organization of your python program will differ a bit from the talker and listener examples from the tutorial. In the tutorial, one node was a responsible for publishing messages, and a different node subscribed to those messages. In this case, a single node will act as both a publisher and a subscriber: your node must subscribe to a bumper topic and publish robot commands.
This raises the question of how to share information between the sensor callback and the loop that is publishing control commands. A simple approach is to create a global variable that is modified by the callback and then accessed in the main loop. Something like this:
#!/usr/bin/env python CALLBACK_DATA = None # Create a global variable def callback(data): global CALLBACK_DATA # 'global' keyword prevents Python from # creating a new local (temporary) variable. CALLBACK_DATA = data def main_loop(): for i in range(10 span>): print CALLBACK_DATA if __name__ == "__main__": callback("Howdy!") main_loop()
A more elegant approach involves creating a Python class to represent your node. You can then use an instance variable to store data received from the callback:
#!/usr/bin/env python class CallBacker(object): def __init__(self): self.callback_data = None def callback(self, data): self.callback_data = data def main_loop(self): for i in range(10): print self.callback_data if __name__ == "__main__": cb = CallBacker() cb.callback("Howdy!") cb.main_loop()
You are welcome to use either approach for this assignment.
rosrun labs bumper.pyin a terminal window.
If you have extra time, improve your node so that when an obstacle is hit the robot:
Submit your completed bumper.py
file through Canvas. Each
group should submit only one file. Comments in the file should
include the names of all group members.