-
Notifications
You must be signed in to change notification settings - Fork 0
/
walltesting.py
167 lines (121 loc) · 3.51 KB
/
walltesting.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python3
import rospy
import cmath
import string
import time
import random
import csv
import json
from gazebo_msgs.srv import GetModelState
from std_srvs.srv import Empty
from geometry_msgs.msg import Twist, Pose
from sensor_msgs.msg import LaserScan
from gazebo_msgs.msg import ModelState
from gazebo_msgs.srv import SetModelState
from nav_msgs.msg import Path
# 1(medium), 0(close), 2 (far)
state = [0,0,0]
scanData = None
def callback(data):
global scanData
scanData = data.ranges
def getState():
global state
global scanData
state = ["0","0","0"]
#front
Fsum = 0
for i in range(85,95):
Fsum += scanData[i]
#print(scanData[i])
Favg = Fsum/10
if (Favg > 1):
state[0] = '2'
else:
state[0] = '0'
#Angle
Asum = 0
for i in range(40,70):
Asum += scanData[i]
#print(scanData[i])
Aavg = Asum/30
if (Aavg > 1):
state[1] = '2'
elif ((Aavg < .7) and (Aavg > .4)):
state[1] = '1'
else:
state[1] = '0'
#Right
Rsum = 0
for i in range(0,10):
Rsum += scanData[i]
#print(scanData[i])
Ravg = Rsum/10
if (Ravg > 1):
state[2] = '2'
elif ((Ravg < .7) and (Ravg > .3)): #.6-.3
state[2] = '1'
else:
state[2] = '0'
state="".join(state)
print(state)
def getposition(data):
global poses
poses = data.poses
rospy.init_node('laser_scan_publisher')
scan_subscriber = rospy.Subscriber('/scan', LaserScan, callback)
rate = rospy.Rate(2)
velocity_publisher = rospy.Publisher('/triton_lidar/vel_cmd', Twist, queue_size=10)
vel_msg = Twist()
#rospy.wait_for_service('/gazebo/reset_world')
reset_world = rospy.ServiceProxy('/gazebo/reset_world', Empty)
#reset_world()
QTable = {}
with open("Q.txt", "r") as data:
QTable = json.load(data)
# epsilon = Startingepsilon*d^n(current episode), .9*.0985^180
# actions: follow wall, rotate to face wall when dealing with corners, rotate and move when dealing with 'L' shape
while scanData is None:
pass
#
reset_world()
# state_msg = ModelState()
# state_msg.model_name = 'triton_lidar'
# state_msg.pose.position.x = random.uniform(-3.8, 3.8)
# state_msg.pose.position.y = random.uniform(-3.8, 3.8)
# state_msg.pose.position.z = 0
# state_msg.pose.orientation.x = 0
# state_msg.pose.orientation.y = 0
# state_msg.pose.orientation.z = random.uniform(0,2*3.14)
# state_msg.pose.orientation.w = random.uniform(0,2*3.14)
# set_state = rospy.ServiceProxy('/gazebo/set_model_state', SetModelState)
# resp = set_state( state_msg )
while not rospy.is_shutdown():
vel_msg.linear.z = 0
vel_msg.linear.y = 0
vel_msg.angular.z = 0
velocity_publisher.publish(vel_msg)
getState()
# one to move foward, one to move foward and left, and one to move foward and right
# foward
if (QTable[state].index(max(QTable[state])) == 0):
#print("Foward "+state)
vel_msg.linear.y = .25
vel_msg.angular.z = 0
vel_msg.linear.z = 0
velocity_publisher.publish(vel_msg)
#foward right
if (QTable[state].index(max(QTable[state])) == 1):
#print("right "+state)
vel_msg.linear.y = 0.25
vel_msg.angular.z = -0.25
vel_msg.linear.z = 0
velocity_publisher.publish(vel_msg)
#foward left
if (QTable[state].index(max(QTable[state])) == 2):
#print("left "+state)
vel_msg.linear.y = 0.25
vel_msg.angular.z = 0.25
vel_msg.linear.z = 0
velocity_publisher.publish(vel_msg)
rate.sleep()