-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmain.lua
More file actions
59 lines (48 loc) · 1.6 KB
/
main.lua
File metadata and controls
59 lines (48 loc) · 1.6 KB
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
#!/usr/bin/env lua
-- Lua robot control example
-- Subscribes to robot pose and publishes twist commands
-- Add local msgs folder to path
local script_dir = arg[0]:match("(.*/)") or "./"
package.path = script_dir .. "msgs/?.lua;" .. package.path
package.path = script_dir .. "msgs/?/init.lua;" .. package.path
local lcm = require("lcm")
local PoseStamped = require("geometry_msgs.PoseStamped")
local Twist = require("geometry_msgs.Twist")
local Vector3 = require("geometry_msgs.Vector3")
local lc = lcm.lcm.new()
print("Robot control started")
print("Subscribing to /odom, publishing to /cmd_vel")
print("Press Ctrl+C to stop.\n")
-- Subscribe to pose
lc:subscribe("/odom#geometry_msgs.PoseStamped", function(channel, msg)
msg = PoseStamped.decode(msg)
local pos = msg.pose.position
local ori = msg.pose.orientation
print(string.format("[pose] x=%.2f y=%.2f z=%.2f | qw=%.2f",
pos.x, pos.y, pos.z, ori.w))
end)
-- Publisher loop
local t = 0
local socket = require("socket")
local last_pub = socket.gettime()
while true do
-- Handle incoming messages
lc:handle()
-- Publish at ~10 Hz
local now = socket.gettime()
if now - last_pub >= 0.1 then
local twist = Twist:new()
twist.linear = Vector3:new()
twist.linear.x = 0.5
twist.linear.y = 0
twist.linear.z = 0
twist.angular = Vector3:new()
twist.angular.x = 0
twist.angular.y = 0
twist.angular.z = math.sin(t) * 0.3
lc:publish("/cmd_vel#geometry_msgs.Twist", twist:encode())
print(string.format("[twist] linear=%.2f angular=%.2f", twist.linear.x, twist.angular.z))
t = t + 0.1
last_pub = now
end
end