如何解决CARLA驾驶模拟器和Arduino Uno直流电机之间的数据传输延迟
该项目的希望是通过将直流电动机连接到Arduino Uno,在CARLA Driving Simulator中复制车辆的速度。为此,我将CARLA输出速度值输出到文本文件中。该文件由Python脚本读取,并将值发送到Arduino,后者控制电机速度。我遇到的问题是Python脚本报告了非常延迟的值。需要明确的是,Python脚本和Arduino Uno是同步的。当Python脚本报告向Arduino发送数据时,来自Arduino的确认消息报告数据将在之后直接读取/使用。问题似乎与从CARLA到Python脚本的转移有关。假设我让CARLA程序在车辆不移动的情况下运行,一旦开始加速,Python脚本和Arduino直流电机可能需要一分钟(甚至更长的时间)来显示速度变化。他们不断报告0 km / h,而汽车在模拟器中加速。我将附上我的Python脚本和来自CARLA的代码段,其中将数据发送到文本文件。请让我知道是否有任何方法可以改善系统的响应能力。如有必要,我也可以添加我的Arduino文件,但我认为解决该问题不是必需的。
Python脚本:
import serial
import time
#Set by deault to COM4 -- change as needed
arduino = serial.Serial('COM3',9600)
#Allow connection between the ports to clear
time.sleep(5)
#Start an infinite loop to send data from acceleration text file to Arduino
f = open("accels.txt","r")
while 1:
#will need to check later on if file can write while loop is running
for number in f:
#ardunio.write('b'(int)number) #- original
print(number + " entered")
arduino.write(number.encode())
line = arduino.readline()
print(line)
#time.sleep(1) #take out when running with carla
#Close file after program ends
f.close()
manual_control_steeringwheel.py 中的CARLA代码段(accels.txt是速度值的文本文件。它被重命名是因为我最终将使用加速度值来进行此数据传输。电机):
def tick(self,world,clock):
(start of function) ...
self._info_text = [
'Server: % 16.0f FPS' % self.server_fps,'Client: % 16.0f FPS' % clock.get_fps(),'','Vehicle: % 20s' % get_actor_display_name(world.player,truncate=20),#'Map: % 20s' % world.map.name,'Simulation time: % 12s' % datetime.timedelta(seconds=int(self.simulation_time)),'Speed: % 15.0f km/h' % (3.6 * math.sqrt(v.x**2 + v.y**2 + v.z**2)),u'heading:% 16.0f\N{DEGREE SIGN} % 2s' % (t.rotation.yaw,heading),'Accelero: (%5.1f,%5.1f,%5.1f)' % (world.imu_sensor.accelerometer),#new
'Location:% 20s' % ('(% 5.1f,% 5.1f)' % (t.location.x,t.location.y)),'GNSS:% 24s' % ('(% 2.6f,% 3.6f)' % (world.gnss_sensor.lat,world.gnss_sensor.lon)),'Height: % 18.0f m' % t.location.z,'']
f = open("../../../arduino_projects/import_txt_test/accels.txt","a")
f.write(str(round((3.6 * math.sqrt(v.x**2 + v.y**2 + v.z**2)) % 255,2)))
# f.write(str(round(world.imu_sensor.accelerometer[0],2)))
f.write('\n')
f.close()
if isinstance(c,carla.VehicleControl):
... (rest of function)
def toggle_info(self):
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。