-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTASK1-1.py
97 lines (77 loc) · 3.62 KB
/
TASK1-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
pub = None
def callback_laser(msg):
# 120 degrees into 3 regions
regions = {
'right': min(min(msg.ranges[0:2]), 10), # Calculate minimum range for the right region
'front': min(min(msg.ranges[3:5]), 10), # Calculate minimum range for the front region
'left': min(min(msg.ranges[6:9]), 10), # Calculate minimum range for the left region
}
take_action(regions)
def take_action(regions):
threshold_dist = 1.5 # Threshold distance for obstacle detection
linear_speed = 0.6 # Linear speed for forward/backward movement
angular_speed = 1 # Angular speed for rotation
msg = Twist() # Create a Twist message object
linear_x = 0 # Initialize linear velocity
angular_z = 0 # Initialize angular velocity
state_description = '' # Initialize the state description variable
if regions['front'] > threshold_dist and regions['left'] > threshold_dist and regions['right'] > threshold_dist:
# No obstacle case
state_description = 'case 1 - no obstacle'
linear_x = linear_speed
angular_z = 0
elif regions['front'] < threshold_dist and regions['left'] < threshold_dist and regions['right'] < threshold_dist:
# Obstacles in front, left, and right case
state_description = 'case 7 - front and left and right'
linear_x = -linear_speed
angular_z = angular_speed
elif regions['front'] < threshold_dist and regions['left'] > threshold_dist and regions['right'] > threshold_dist:
# Obstacle in front case
state_description = 'case 2 - front'
linear_x = 0
angular_z = angular_speed
elif regions['front'] > threshold_dist and regions['left'] > threshold_dist and regions['right'] < threshold_dist:
# Obstacle on the right case
state_description = 'case 3 - right'
linear_x = 0
angular_z = -angular_speed
elif regions['front'] > threshold_dist and regions['left'] < threshold_dist and regions['right'] > threshold_dist:
# Obstacle on the left case
state_description = 'case 4 - left'
linear_x = 0
angular_z = angular_speed
elif regions['front'] < threshold_dist and regions['left'] > threshold_dist and regions['right'] < threshold_dist:
# Obstacles in front and right case
state_description = 'case 5 - front and right'
linear_x = 0
angular_z = -angular_speed
elif regions['front'] < threshold_dist and regions['left'] < threshold_dist and regions['right'] > threshold_dist:
# Obstacles in front and left case
state_description = 'case 6 - front and left'
linear_x = 0
angular_z = angular_speed
elif regions['front'] > threshold_dist and regions['left'] < threshold_dist and regions['right'] < threshold_dist:
# Obstacles on the left and right case
state_description = 'case 8 - left and right'
linear_x = linear_speed
angular_z = 0
else:
# Unknown case
state_description = 'unknown case'
rospy.loginfo(regions)
rospy.loginfo(state_description)
msg.linear.x = linear_x
msg.angular.z = angular_z
pub.publish(msg)
def main():
global pub
rospy.init_node('crop_row_navigation') # Initialize the ROS node
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1) # Create a publisher for Twist messages
sub = rospy.Subscriber('/scan', LaserScan, callback_laser) # Create a subscriber for LaserScan messages
rospy.spin() # Enter the ROS event loop
if _name_ == '_main_':
main()