Commit 010ac902 authored by he's avatar he
Browse files

pub middle

parent a2b2bcc1
...@@ -9,7 +9,7 @@ import math ...@@ -9,7 +9,7 @@ import math
from skimage import measure from skimage import measure
from cv_bridge import CvBridge, CvBridgeError from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image from sensor_msgs.msg import Image
from std_msgs.msg import String from std_msgs.msg import Float64
import datetime import datetime
...@@ -22,14 +22,9 @@ class Follower: ...@@ -22,14 +22,9 @@ class Follower:
self.bridge = CvBridge() self.bridge = CvBridge()
self.image_sub = rospy.Subscriber('/d415/color/image_raw', Image, self.image_callback) self.image_sub = rospy.Subscriber('/d415/color/image_raw', Image, self.image_callback)
self.cmd_vel_pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1) #self.cmd_vel_pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
self.line_pub = rospy.Publisher('/man3/middle', Float64, queue_size=1)
self.twist = Twist()
self.err = 0
self.turn_cmd = Twist()
self.signal = False
self.begin = 0
def image_callback(self, msg): def image_callback(self, msg):
# To get the image from the camera and convert it to binary image by using opencv # To get the image from the camera and convert it to binary image by using opencv
...@@ -62,53 +57,32 @@ class Follower: ...@@ -62,53 +57,32 @@ class Follower:
# To find the optimal index of contour # To find the optimal index of contour
if len(areas) == 0: if len(areas) == 0:
return False msg = Float64()
msg.data = -1
sort = np.argsort(areas) self.line_pub.publish(msg)
index = sort[-1] else:
print len(areas)
cv2.drawContours(image, contours, index, (0, 0, 255), 3) sort = np.argsort(areas)
index = sort[-1]
if cv2.contourArea(contours[index]) >= 100: if cv2.contourArea(contours[index]) >= 100:
# Computing the centroid of the contours of binary image # Computing the centroid of the contours of binary image
M = cv2.moments(contours[index]) M = cv2.moments(contours[index])
self.signal = True self.signal = True
cx = int(M['m10'] / M['m00']) cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00']) cy = int(M['m01'] / M['m00'])
cv2.circle(image, (cx, cy), 20, (255, 0, 0), -1)
cv2.drawContours(image, contours, index, (0, 0, 255), 3)
# P-controller cv2.circle(image, (cx, cy), 20, (255, 0, 0), -1)
self.err = cx - w/2
self.twist.linear.x = 0.4 # P-controller
self.twist.angular.z = -float(self.err) / 45 msg = Float64()
self.cmd_vel_pub.publish(self.twist) msg.data = cx
self.line_pub.publish(msg)
self.begin = datetime.datetime.now().second else:
# else: msg = Float64()
# # Define actions when the point is not be found msg.data = -1
# # 5 HZ self.line_pub.publish(msg)
# r = rospy.Rate(5)
# # let's go forward at 0.1 m/s
# # by default angular.z is 0 so setting this isn't required
# move_cmd = Twist()
# move_cmd.linear.x = 0.1
# if self.signal is True:
# if self.err < 0:
# self.turn_cmd.angular.z = math.radians(30)
# elif self.err > 0:
# self.turn_cmd.angular.z = math.radians(-30)
# self.cmd_vel_pub.publish(self.turn_cmd)
# now = datetime.datetime.now().second
# # Go forward about 0.2m, then stop
# if math.fabs(now - self.begin) > 2:
# for x in range(0, 10):
# self.cmd_vel_pub.publish(move_cmd)
# r.sleep()
# self.signal = False
cv2.imshow('window1', image) cv2.imshow('window1', image)
cv2.waitKey(1) cv2.waitKey(1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment