Final Assignment  1.0
Research Track part 1 - Assignment 2 - RobEng A.A. 2020/2021
wall_follow_service_m.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 
11 
12 import rospy
13 from sensor_msgs.msg import LaserScan
14 from geometry_msgs.msg import Twist
15 from nav_msgs.msg import Odometry
16 from tf import transformations
17 from std_srvs.srv import *
18 
19 import math
20 
21 active_ = False
22 
23 pub_ = None
24 regions_ = {
25  'right': 0,
26  'fright': 0,
27  'front': 0,
28  'fleft': 0,
29  'left': 0,
30 }
31 state_ = 0
32 state_dict_ = {
33  0: 'find the wall',
34  1: 'turn left',
35  2: 'follow the wall',
36 }
37 
38 
40  global active_
41  active_ = req.data
42  res = SetBoolResponse()
43  res.success = True
44  res.message = 'Done!'
45  return res
46 
47 
48 def clbk_laser(msg):
49  global regions_
50  regions_ = {
51  'right': min(min(msg.ranges[0:143]), 10),
52  'fright': min(min(msg.ranges[144:287]), 10),
53  'front': min(min(msg.ranges[288:431]), 10),
54  'fleft': min(min(msg.ranges[432:575]), 10),
55  'left': min(min(msg.ranges[576:713]), 10),
56  }
57 
58  take_action()
59 
60 
61 def change_state(state):
62  global state_, state_dict_
63  if state is not state_:
64  print ('Wall follower - [%s] - %s' % (state, state_dict_[state]))
65  state_ = state
66 
67 
69  global regions_
70  regions = regions_
71  msg = Twist()
72  linear_x = 0
73  angular_z = 0
74  state_description = ''
75 
76  d0 = 1
77  d = 1.5
78 
79  if regions['front'] > d0 and regions['fleft'] > d and regions['fright'] > d:
80  state_description = 'case 1 - nothing'
81  change_state(0)
82  elif regions['front'] < d0 and regions['fleft'] > d and regions['fright'] > d:
83  state_description = 'case 2 - front'
84  change_state(1)
85  elif regions['front'] > d0 and regions['fleft'] > d and regions['fright'] < d:
86  state_description = 'case 3 - fright'
87  change_state(2)
88  elif regions['front'] > d0 and regions['fleft'] < d and regions['fright'] > d:
89  state_description = 'case 4 - fleft'
90  change_state(0)
91  elif regions['front'] < d0 and regions['fleft'] > d and regions['fright'] < d:
92  state_description = 'case 5 - front and fright'
93  change_state(1)
94  elif regions['front'] < d0 and regions['fleft'] < d and regions['fright'] > d:
95  state_description = 'case 6 - front and fleft'
96  change_state(1)
97  elif regions['front'] < d0 and regions['fleft'] < d and regions['fright'] < d:
98  state_description = 'case 7 - front and fleft and fright'
99  change_state(1)
100  elif regions['front'] > d0 and regions['fleft'] < d and regions['fright'] < d:
101  state_description = 'case 8 - fleft and fright'
102  change_state(1)
103  else:
104  state_description = 'unknown case'
105  rospy.loginfo(regions)
106 
107 
108 def find_wall():
109  msg = Twist()
110  msg.linear.x = 0.3
111  msg.angular.z = -0.6
112  return msg
113 
114 
115 def turn_left():
116  msg = Twist()
117  msg.angular.z = 0.8
118  return msg
119 
120 
122  global regions_
123 
124  msg = Twist()
125  msg.linear.x = 0.5
126  return msg
127 
128 
129 def main():
130  global pub_, active_
131 
132  rospy.init_node('reading_laser')
133 
134  pub_ = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
135 
136  sub = rospy.Subscriber('/scan', LaserScan, clbk_laser)
137 
138  srv = rospy.Service('wall_follower_switch', SetBool, wall_follower_switch)
139 
140  rate = rospy.Rate(20)
141  while not rospy.is_shutdown():
142  if not active_:
143  rate.sleep()
144  continue
145  else:
146  msg = Twist()
147  if state_ == 0:
148  msg = find_wall()
149  elif state_ == 1:
150  msg = turn_left()
151  elif state_ == 2:
152  msg = follow_the_wall()
153  else:
154  rospy.logerr('Unknown state!')
155 
156  pub_.publish(msg)
157 
158  rate.sleep()
159 
160 
161 if __name__ == '__main__':
162  main()