python线程的使用---threading (在carla中)

'''
时间:2024.3.26
作者:wanhe 
功能:线程、获取车速、存数据
'''
import time 
import csv
import datetime
import sys
import threading
try:
    #sys.path.append('D:/carla/CARLA_0.9.13/WindowsNoEditor/PythonAPI/carla/dist/carla-0.9.13-py3.7-win-amd64.egg')
    sys.path.append('D:/soft/CARLA/CARLA_0.9.12/WindowsNoEditor/PythonAPI/carla/dist/carla-0.9.12-py3.7-win-amd64.egg')
except IndexError:
    pass
import carla

#from agents.navigation.basic_agent import BasicAgent
import math
import torch

import threading
from datetime import datetime
import time
import csv

def get_velocity(vechile):
        Vx=vechile.get_velocity().x
        Vy=vechile.get_velocity().y
        V=math.sqrt(Vx*Vx+Vy*Vy)
        return V

def add_timestamp(data):
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
        data_with_timestamp = (data, timestamp)
        return data_with_timestamp

def time_wh(vehicle):
     index=0
     with open("66666666666.csv", mode="w", newline="") as file:
        writer = csv.writer(file)
                    # 写入表头
        writer.writerow(["time", "velocity"])
        while True:
            velocity=get_velocity(vehicle)
            index+=1
            time.sleep(1)
            time_start=datetime.now().strftime("%H:%M:%S.%f")
            print('\n',time_start)
            writer.writerow([time_start,velocity])
            if index==18:
                print('done')
                break
                


def setcar_wh(vehicle):
    while True:
        vehicle.set_autopilot(True) 
        print("here")



if __name__ == '__main__':
    try:
        client = carla.Client("localhost", 2000)
        client.set_timeout(10.0)
        world = client.load_world('Town01')
        #self.world = self.client.get_world()
        blueprint_library = world.get_blueprint_library()
        map = world.get_map()
        spectator = world.get_spectator()
        spectator.set_transform(carla.Transform( carla.Location(x=0,y=60,z=150), carla.Rotation(-90,-90,0)))
        spawn_points = world.get_map().get_spawn_points()
        for i, spawn_point in enumerate(spawn_points):
            world.debug.draw_string(spawn_point.location, str(i), life_time=30)
            if i==214:
                waypoint_113=spawn_point.location
            if i==105:
                waypoint_105=spawn_point.location


        print(waypoint_113)
        vehicle_bp=blueprint_library.filter('model3')[0]
        waypoint = map.get_waypoint(waypoint_113)
        print(waypoint)
        transform = carla.Transform(carla.Location(x=1.943281, y=79.321892, z=0.6), carla.Rotation(pitch=360.000000, yaw=269.991272, roll=0.000000))
        vehicle = world.spawn_actor(vehicle_bp, transform)  #放置车

        thread_list = threading.enumerate()
        #print("\nCurrent number of threads:%d" % len(thread_list))

        t1 = threading.Thread(target=time_wh,kwargs={"vehicle": vehicle})
        t2 = threading.Thread(target=setcar_wh,kwargs={"vehicle": vehicle})
            
        t1.start()
        t2.start()    

    except KeyboardInterrupt:          
        #vehicle.destroy()
        print('done.')

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值