-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kulbabu.lua
414 lines (349 loc) · 9.89 KB
/
Kulbabu.lua
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
local ros = require('ros')
local signal = require('posix.signal')
local socket = require('socket')
local classic = require 'classic'
local Kulbabu = classic.class('Kulbabu')
-- Constructor
function Kulbabu:_init(opts)
opts = opts or {}
self.width = opts.width or 8
self.height = opts.height or 1
-- Second channel for goal direction/distance.
self.channels = 2
self.screen = torch.Tensor(self.channels, self.height, self.width):zero()
-- Max-range for goal sensor
self.goal_max = 10
self.goal_min = 0.25
-- Frame-rate
self.frame_rate = 10
self.frame_time = 1/self.frame_rate
-- Escape sequence
self.escape_steps = 10 * 20 -- seconds at 10Hz
self.escape_min = 0.15 -- Distance in metres
self.repeat_for = 10 * 4 -- seconds at 10Hz
self.repeat_steps = 0
self.repeat_action = 0
self.ns = "kulbabu"
if __threadid then
self.ns = self.ns .. __threadid
else
self.ns = self.ns .. "0"
end
self.range_topics = {
"range_1l",
"range_2l",
"range_3l",
"range_4l",
"range_1r",
"range_2r",
"range_3r",
"range_4r"
}
self.range_msg = "sensor_msgs/Range"
self.cmd_vel_topic = "/" .. self.ns .. "/diff_drive_controller/cmd_vel"
self.cmd_vel_msg = ros.MsgSpec('geometry_msgs/Twist')
self.model_state_topic = "/gazebo/set_model_state"
self.model_state_msg = ros.MsgSpec('gazebo_msgs/ModelState')
self.train = false
self.subs = {}
self.pubs = {}
self.robot_pose = {
position = {
x = 0,
y = 0,
z = 0
},
orientation = {
x = 0,
y = 0,
z = 0,
w = 0
}
}
self.goal_pose = {
position = {
x = 0,
y = 0,
z = 0
},
orientation = {
x = 0,
y = 0,
z = 0,
w = 0
}
}
self.robot_pose_log = {}
self.steps = 0
if not ros.isInitialized() then
ros.init(self.ns .. "_dqn")
end
if not ros.isStarted() then
__ros_spinner = ros.AsyncSpinner()
__ros_spinner:start()
end
self.nh = ros.NodeHandle()
-- TODO: Capture sigint destroy pub/subs and `ros.shutdown()``
signal.signal(signal.SIGINT, function(signum)
io.write("\n")
ros.shutdown()
os.exit(128 + signum)
end)
end
function Kulbabu:getStateSpec()
return {'real', {self.channels, self.height, self.width}, {0, 1}}
end
-- forward, left-forward, right-forward, left, right
function Kulbabu:getActionSpec()
return {'int', 1, {0, 4}}
end
function Kulbabu:getDisplaySpec()
return {'real', {self.channels, self.height, self.width}, {0, 1}}
end
-- Min and max reward
function Kulbabu:getRewardSpec()
return 0, 1
end
function Kulbabu:training()
self.train = true
end
function Kulbabu:evaluate()
self.train = false
end
-- Starts new game
function Kulbabu:start()
self.steps = 0
-- Reset screen
self.screen:zero()
-- Subscribe to ROS topics
self:createSubs()
-- Setup ROS publishers
self:createPubs()
-- Return observation
return self.screen
end
-- Steps in a game
function Kulbabu:step(action)
-- Reward is 0 by default
local reward = 0
local nextAction
-- Publish twists for actions
self:pubAction(action)
-- Delay execution, giving state time to change
-- TODO: What does `ros.sleep()` do? Sleep ros or current process?
socket.sleep(self.frame_time)
-- Spin ROS and get messages
if ros.ok() then
ros.spinOnce()
end
-- Get/update relative location
local rad, dis = self:goalLocation()
self:goalRefresh(rad, dis)
-- Calculate reward based on reaching goal
--reward = math.max(0,1 - (dis / self.goal_max))
if dis < self.goal_min then
log.info('Goal reached')
--self:pubGoalMove()
reward = 1
end
--log.info("Reward: " .. reward)
if self.train then
-- Escape sequence, triggered when robot position doesn't change over time
if self.repeat_steps > 0 then
nextAction = self.repeat_action
self.repeat_steps = self.repeat_steps - 1
elseif self.steps % self.escape_steps == 0 and self:escapeCheck() then
self.repeat_steps = self.repeat_for
self.repeat_action = math.random(3,4)
end
self.robot_pose_log[(self.steps % self.escape_steps) + 1] = self.robot_pose.position
end
-- TODO: Check terminal condition
local terminal = false
self.steps = self.steps + 1
return reward, self.screen, terminal, nextAction
end
function Kulbabu:escapeCheck()
local begin = self.robot_pose_log[1]
local finish = self.robot_pose_log[#self.robot_pose_log]
if not begin or not finish then
return false
end
--`tan(rad) = Opposite / Adjacent = (y2-y1)/(x2-x1)`
local rad = math.atan2(
finish.y - begin.y,
finish.x - begin.x
)
-- `Hypotenuse = (y2-y1)/sin(rad)`
local dis = math.abs(
(finish.y - begin.y)/math.sin(rad)
)
return dis < self.escape_min
end
-- Returns (RGB) display of screen
function Kulbabu:getDisplay()
--return torch.repeatTensor(self.screen, 3, 1, 1)
return self.screen
end
function Kulbabu:createSubs()
self:destroySubs()
local subscriber
-- Subscribe to range sensor topics and update state
for i, topic in ipairs(self.range_topics) do
log.info("Subscribe: /" .. self.ns .. "/" .. topic)
subscriber = self.nh:subscribe("/" .. self.ns .. "/" .. topic, self.range_msg, 100)
subscriber:registerCallback(function(msg, header)
--log.info(msg.range / msg.max_range)
self.screen[{{1}, {1}, {i}}] = math.max(0,1 - (msg.range / msg.max_range))
end)
table.insert(self.subs, subscriber)
end
-- Subscribe to states for goal relative position
subscriber = self.nh:subscribe("/gazebo/model_states", "gazebo_msgs/ModelStates", 100)
subscriber:registerCallback(function(msg, header)
local robot_key = get_key_for_value(msg.name, self.ns)
--log.info("robot: " .. msg.pose[robot_key].position.x .. "/" .. msg.pose[robot_key].position.y)
if robot_key then
self.robot_pose.position = msg.pose[robot_key].position
self.robot_pose.orientation = msg.pose[robot_key].orientation
end
local goal_key = get_key_for_value(msg.name, self.ns .. "/goal")
--log.info("goal: " .. msg.pose[goal_key].position.x .. "/" .. msg.pose[goal_key].position.y)
if goal_key then
self.goal_pose.position = msg.pose[goal_key].position
self.goal_pose.orientation = msg.pose[goal_key].orientation
end
end)
table.insert(self.subs, subscriber)
end
-- Destroy any existing subscriptions
function Kulbabu:destroySubs()
for i, subscriber in ipairs(self.subs) do
subscriber:shutdown()
end
self.subs = {}
end
function Kulbabu:createPubs()
self:destroyPubs()
local pub_cmd_vel = self.nh:advertise(self.cmd_vel_topic, self.cmd_vel_msg, 100, false, connect_cb, disconnect_cb)
table.insert(self.pubs, pub_cmd_vel)
local pub_model_state = self.nh:advertise(self.model_state_topic, self.model_state_msg, 100, false, connect_cb, disconnect_cb)
table.insert(self.pubs, pub_model_state)
ros.spinOnce()
end
-- Destroy any existing publishers
function Kulbabu:destroyPubs()
for i, publisher in ipairs(self.pubs) do
publisher:shutdown()
end
self.pubs = {}
end
function connect_cb(name, topic)
log.info("subscriber connected: " .. name .. " (topic: '" .. topic .. "')")
end
function disconnect_cb(name, topic)
log.info("subscriber diconnected: " .. name .. " (topic: '" .. topic .. "')")
end
function Kulbabu:pubAction(action)
local msg = ros.Message(self.cmd_vel_msg)
local publisher = self.pubs[1]
if publisher:getNumSubscribers() == 0 then
--log.info('waiting for subscriber')
else
-- Forward
if action == 0 then
msg.linear.x = 1.00
msg.angular.z = 0.00
-- Forward-left
elseif action == 1 then
msg.linear.x = 1.00
msg.angular.z = -0.80
-- Forward-right
elseif action == 2 then
msg.linear.x = 1.00
msg.angular.z = 0.80
-- Left
elseif action == 3 then
msg.linear.x = 0.00
msg.angular.z = -1.00
-- Right
elseif action == 4 then
msg.linear.x = 0.00
msg.angular.z = 1.00
end
--log.info(msg)
publisher:publish(msg)
end
end
function Kulbabu:pubGoalMove(x, y, z)
if not x then
x = self.goal_pose.position.x + math.random(-3,3)
end
if not y then
y = self.goal_pose.position.y + math.random(-3,3)
end
if not z then
y = self.goal_pose.position.z
end
local msg = ros.Message(self.model_state_msg)
local publisher = self.pubs[2]
if publisher:getNumSubscribers() == 0 then
--log.info('waiting for subscriber')
else
msg.model_name = self.ns .. "/goal"
msg.pose.position = {
x = x,
y = y,
z = z
}
msg.reference_frame = 'world'
print(msg)
publisher:publish(msg)
end
end
function Kulbabu:goalLocation()
--`tan(rad) = Opposite / Adjacent = (y2-y1)/(x2-x1)`
local rad = math.atan2(
self.goal_pose.position.y - self.robot_pose.position.y,
self.goal_pose.position.x - self.robot_pose.position.x
)
-- `Hypotenuse = (y2-y1)/sin(rad)`
local dis = math.abs(
(self.goal_pose.position.y - self.robot_pose.position.y)/math.sin(rad)
)
-- Extract angles from quaternion.
-- http://stackoverflow.com/a/18115837/2438830
local q = self.robot_pose.orientation
local robot_yaw = math.atan2(2.0*(q.x*q.y + q.w*q.z), q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z);
-- Minus robot pose from goal direction.
rad = rad - robot_yaw
if rad > math.pi then
rad = rad - (2 * math.pi)
elseif rad < -math.pi then
rad = rad + (2 * math.pi)
end
return rad, dis
end
function Kulbabu:goalRefresh(rad, dis)
if self.channels > 1 and dis > 0 then
local fov = (2*math.pi)/self.width
for x=1,self.width do
local seg_begin = -math.pi+(fov*(x-1))
local seg_finish = seg_begin + fov
if seg_begin < rad and seg_finish >= rad then
self.screen[{{2}, {1}, {x}}] = math.max(0,1 - (dis / self.goal_max))
else
self.screen[{{2}, {1}, {x}}] = 0
end
end
end
end
function get_key_for_value(t, value)
for k,v in pairs(t) do
if v==value then
return k
end
end
return nil
end
return Kulbabu