我试着计算一辆车的当前位置,到目前为止它运行得很好。的确,有时时间会有很大的飞跃,我不知道为什么
例如: 我写了一个函数,它重置了“self.comulatedWay”变量,但是在重置之后,init值有时已经从1000开始,而不是像预期的那样从0开始?速度值很好。它必须是与时间有关的东西
也许整个算法都很糟糕?没有比这更好的办法了
这是跟踪位置的代码。 以毫米/秒为单位的速度(非恒定) 以毫米为单位
import threading
import time
class car:
def __init__(self, speed):
self.t0 = 0
self.speed = speed # current speed of the car
self.comulatedWay = 0
threading.Thread(target=self.locationCalc, name="ThreadLocationCalc", ).start()
# calc the current position of the car in mm
def locationCalc(self):
self.t0 = time.time()
while True:
t1 = time.time()
deltaTime = t1 - self.t0
self.t0 = t1
way = self.speed * deltaTime
self.comulatedWay = way + self.comulatedWay
目前没有回答
相关问题 更多 >
编程相关推荐