Skip to content
Snippets Groups Projects
Commit 6ac1dad4 authored by Romina Parada's avatar Romina Parada
Browse files

Add follow left wall

parent b35630f1
No related branches found
No related tags found
No related merge requests found
...@@ -19,11 +19,13 @@ regions_ = { ...@@ -19,11 +19,13 @@ regions_ = {
} }
state_ = 0 state_ = 0
state_dir_ = -1
state_dict_ = { state_dict_ = {
0: 'find the wall', 0: 'find the wall',
1: 'turn left', 1: 'turn',
2: 'follow the wall', 2: 'follow the wall',
} }
state_description_ = ''
def clbk_laser(msg): def clbk_laser(msg):
global regions_ global regions_
...@@ -38,45 +40,45 @@ def clbk_laser(msg): ...@@ -38,45 +40,45 @@ def clbk_laser(msg):
take_action() take_action()
def change_state(state): def change_state(state):
global state_, state_dict_ global state_, state_dict_, state_description_
if state is not state_: if state is not state_:
print('State of Bot - [%s] - %s' % (state, state_dict_[state])) print('State of Bot - [%s] - %s - %s' % (state, state_dict_[state], state_description_))
state_ = state state_ = state
def take_action(): def take_action():
global regions_ global regions_, state_description_, state_dir_
regions = regions_ regions = regions_
msg = Twist() msg = Twist()
linear_x = 0 linear_x = 0
angular_z = 0 angular_z = 0
state_description = ''
d = 1.5 d = 1.5
if regions['front'] > d and regions['fleft'] > d and regions['fright'] > d: if regions['front'] > d and regions['fleft'] > d and regions['fright'] > d:
state_description = 'case 1 - nothing' state_description_ = 'case 1 - nothing'
change_state(0) change_state(0)
elif regions['front'] < d and regions['fleft'] > d and regions['fright'] > d: elif regions['front'] < d and regions['fleft'] > d and regions['fright'] > d:
state_description = 'case 2 - front' state_description_ = 'case 2 - front'
change_state(1) change_state(1)
elif regions['front'] > d and regions['fleft'] > d and regions['fright'] < d: elif regions['front'] > d and regions['fleft'] > d and regions['fright'] < d:
state_description = 'case 3 - fright' state_description_ = 'case 3 - fright'
state_dir_ = -1
change_state(2) change_state(2)
elif regions['front'] > d and regions['fleft'] < d and regions['fright'] > d: elif regions['front'] > d and regions['fleft'] < d and regions['fright'] > d:
state_description = 'case 4 - fleft' state_description_ = 'case 4 - fleft'
change_state(0) state_dir_ = 1
change_state(2)
elif regions['front'] < d and regions['fleft'] > d and regions['fright'] < d: elif regions['front'] < d and regions['fleft'] > d and regions['fright'] < d:
state_description = 'case 5 - front and fright' state_description_ = 'case 5 - front and fright'
change_state(1) change_state(1)
elif regions['front'] < d and regions['fleft'] < d and regions['fright'] > d: elif regions['front'] < d and regions['fleft'] < d and regions['fright'] > d:
state_description = 'case 6 - front and fleft' state_description_ = 'case 6 - front and fleft'
change_state(1) change_state(1)
elif regions['front'] < d and regions['fleft'] < d and regions['fright'] < d: elif regions['front'] < d and regions['fleft'] < d and regions['fright'] < d:
state_description = 'case 7 - front and fleft and fright' state_description_ = 'case 7 - front and fleft and fright'
change_state(1) change_state(1)
elif regions['front'] > d and regions['fleft'] < d and regions['fright'] < d: elif regions['front'] > d and regions['fleft'] < d and regions['fright'] < d:
state_description = 'case 8 - fleft and fright' state_description_ = 'case 8 - fleft and fright'
change_state(0) change_state(0)
else: else:
state_description = 'unknown case' state_description = 'unknown case'
...@@ -85,12 +87,12 @@ def take_action(): ...@@ -85,12 +87,12 @@ def take_action():
def find_wall(): def find_wall():
msg = Twist() msg = Twist()
msg.linear.x = 0.2 msg.linear.x = 0.2
msg.angular.z = -0.3 msg.angular.z = state_dir_*0.3
return msg return msg
def turn_left(): def turn():
msg = Twist() msg = Twist()
msg.angular.z = 0.3 msg.angular.z = state_dir_*-0.3
return msg return msg
def follow_the_wall(): def follow_the_wall():
...@@ -105,7 +107,7 @@ def main(): ...@@ -105,7 +107,7 @@ def main():
pub_ = rospy.Publisher('/cmd_vel', Twist, queue_size=1) pub_ = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
sub = rospy.Subscriber('/front/scan', LaserScan, clbk_laser) sub = rospy.Subscriber('/scan', LaserScan, clbk_laser)
rate = rospy.Rate(20) rate = rospy.Rate(20)
while not rospy.is_shutdown(): while not rospy.is_shutdown():
...@@ -113,7 +115,7 @@ def main(): ...@@ -113,7 +115,7 @@ def main():
if state_ == 0: if state_ == 0:
msg = find_wall() msg = find_wall()
elif state_ == 1: elif state_ == 1:
msg = turn_left() msg = turn()
elif state_ == 2: elif state_ == 2:
msg = follow_the_wall() msg = follow_the_wall()
pass pass
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment