1
+ #! /usr/bin/env python
2
+
3
+ import rospy
4
+
5
+ from sensor_msgs .msg import LaserScan
6
+ from geometry_msgs .msg import Twist
7
+
8
+ pub = None
9
+ thresh = 0.25
10
+
11
+ def clbk_laser (msg ):
12
+ regions = {
13
+ '1' :round (min (msg .ranges [0 :44 ]),3 ),
14
+ '2' :round (min (msg .ranges [45 :89 ]),3 ),
15
+ '3' :round (min (msg .ranges [90 :134 ]),3 ),
16
+ '4' :round (min (msg .ranges [135 :179 ]),3 ),
17
+ '5' :round (min (msg .ranges [180 :224 ]),3 ),
18
+ '6' :round (min (msg .ranges [225 :269 ]),3 ),
19
+ '7' :round (min (msg .ranges [270 :314 ]),3 ),
20
+ '8' :round (min (msg .ranges [315 :360 ]),3 ),
21
+ }
22
+ take_action (regions )
23
+
24
+ def take_action (regions ):
25
+ msg = Twist ()
26
+ linear_x = 0
27
+ angular_z = 0
28
+
29
+ state_description = ''
30
+
31
+ if regions ['1' ] > thresh and regions ['2' ] > thresh and regions ['7' ] > thresh and regions ['8' ] > thresh :
32
+ state_description = 'case 1 - nothing'
33
+ linear_x = 0.2
34
+ angular_z = 0
35
+ elif regions ['1' ] < thresh and regions ['2' ] > thresh and regions ['7' ] > thresh and regions ['8' ] < thresh :
36
+ state_description = 'case 2 - front'
37
+ linear_x = 0
38
+ angular_z = - 0.15
39
+ elif regions ['1' ] < thresh and regions ['7' ] > thresh and regions ['2' ] < thresh and regions ['8' ] < thresh :
40
+ state_description = 'case 6 - front and fleft'
41
+ linear_x = 0
42
+ angular_z = - 0.15
43
+ elif regions ['8' ] > thresh and regions ['7' ] > thresh and regions ['2' ] < thresh and regions ['1' ] < thresh :
44
+ state_description = 'case 4 - fleft'
45
+ linear_x = 0
46
+ angular_z = - 0.15
47
+ elif regions ['1' ] > thresh and regions ['3' ] < thresh and regions ['2' ] < thresh and regions ['8' ] > thresh :
48
+ state_description = 'case 2 - left'
49
+ linear_x = 0.12
50
+ angular_z = 0.315
51
+ elif regions ['1' ] > thresh and regions ['3' ] < thresh and regions ['4' ] < thresh and regions ['8' ] > thresh :
52
+ state_description = 'case 2 - back left'
53
+ linear_x = 0
54
+ angular_z = 0.15
55
+ elif regions ['8' ] < thresh and regions ['7' ] < thresh and regions ['2' ] > thresh and regions ['1' ] > thresh :
56
+ state_description = 'case 3 - fright'
57
+ linear_x = 0
58
+ angular_z = - 0.15
59
+ elif regions ['1' ] < thresh and regions ['7' ] < thresh and regions ['2' ] > thresh and regions ['8' ] < thresh :
60
+ state_description = 'case 5 - front and fright'
61
+ linear_x = 0
62
+ angular_z = - 0.15
63
+ elif regions ['1' ] < thresh and regions ['2' ] < thresh and regions ['7' ] < thresh and regions ['8' ] < thresh :
64
+ state_description = 'case 7 - front and fleft and fright'
65
+ linear_x = 0
66
+ angular_z = - 0.15
67
+ elif regions ['1' ] > thresh and regions ['2' ] < thresh and regions ['7' ] < thresh and regions ['8' ] > thresh :
68
+ state_description = 'case 8 - fleft and fright'
69
+ linear_x = 0
70
+ angular_z = - 0.15
71
+ else :
72
+ state_description = 'unknown case'
73
+ linear_x = 0.6
74
+ angular_z = 0
75
+ rospy .loginfo (regions )
76
+
77
+ rospy .loginfo (state_description )
78
+ msg .linear .x = linear_x
79
+ msg .angular .z = angular_z
80
+ pub .publish (msg )
81
+
82
+
83
+ def main ():
84
+ global pub
85
+
86
+ rospy .init_node ('reading_laser' )
87
+
88
+ pub = rospy .Publisher ('/cmd_vel' , Twist , queue_size = 1 )
89
+
90
+ sub = rospy .Subscriber ('/scan' , LaserScan , clbk_laser )
91
+
92
+ rospy .spin ()
93
+
94
+ if __name__ == '__main__' :
95
+ main ()
0 commit comments