Skip to content

Instantly share code, notes, and snippets.

@eborghi10
Created April 22, 2019 04:25
Show Gist options
  • Save eborghi10/20d2bc13f181e351dd4196b05a5b8ba6 to your computer and use it in GitHub Desktop.
Save eborghi10/20d2bc13f181e351dd4196b05a5b8ba6 to your computer and use it in GitHub Desktop.
Example script with a control logic to move the iRobot Create 2
#!/usr/bin/env python
import rospy
from ca_msgs.msg import Bumper
from std_msgs.msg import Float32
from geometry_msgs.msg import Twist, Vector3
class MiClase():
def __init__(self):
rospy.init_node("node_name", log_level=rospy.INFO)
self.sub = rospy.Subscriber("bumper", Bumper, self.callback)
self.pub = rospy.Publisher("/cmd_vel", Twist, queue_size=10)
self.motor_pub = rospy.Publisher("/main_motor", Float32, queue_size=10)
rospy.on_shutdown(self.apagar)
self.apagado = False
rospy.spin()
def apagar(self):
self.apagado = True
self.motor_pub.publish(0.0)
def callback(self, data):
if not self.apagado:
rospy.loginfo("{} {}".format(data.is_left_pressed, data.is_right_pressed))
if data.is_left_pressed:
self.rotar_der()
elif data.is_right_pressed:
self.rotar_izq()
else:
self.parar()
self.motor_pub.publish(0.5)
def rotar_izq(self):
self.pub.publish(Twist(Vector3(), Vector3(0, 0, 0.1)))
def rotar_der(self):
self.pub.publish(Twist(Vector3(), Vector3(0, 0, -0.1)))
def mover(self):
msg = Twist()
msg.linear.x = 0
msg.angular.z = -0.1
self.pub.publish(msg)
def parar(self):
linear = Vector3(0, 0 ,0)
angular = Vector3(0, 0, 0)
msg = Twist(linear, angular)
self.pub.publish(msg)
if __name__ == "__main__":
clase = MiClase()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment