-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf1tenth_ros_agent.py
executable file
·49 lines (37 loc) · 1.35 KB
/
f1tenth_ros_agent.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
#!/usr/bin/env python3
import rospy
from ackermann_msgs.msg import AckermannDriveStamped
from sensor_msgs.msg import LaserScan
import numpy as np
import os
from pkg.drivers import DisparityExtender as Driver
"""
NOTE: Following code enables F1Tenth - Docker - ROS integration.
Please don't change it, unless you know what you're doing
"""
class ROSRunner:
def __init__(self, driver, agent_name):
self.driver = driver
self.agent_name = agent_name
self.pub_drive = None
def lidar_callback(self, data):
ranges = np.asarray(data.ranges)
speed, angle = self.driver.process_lidar(ranges)
# create message & publish
msg = AckermannDriveStamped()
msg.drive.speed = speed
msg.drive.steering_angle = angle
self.pub_drive.publish(msg)
def run(self):
print ("agent_name", agent_name)
rospy.init_node('gym_agent_%s' % self.agent_name, anonymous=True)
self.pub_drive = rospy.Publisher('/%s/drive' % self.agent_name, AckermannDriveStamped, queue_size=5)
# start listening
rospy.Subscriber('/%s/scan' % self.agent_name, LaserScan, self.lidar_callback)
rospy.sleep(3)
rospy.spin()
if __name__ == "__main__":
agent_name = os.environ.get("F1TENTH_AGENT_NAME")
runner = ROSRunner(Driver(), agent_name)
# launch
runner.run()