状态和面向对象编程——7.状态定量

不同的移动模型

恒定速度

在第一个移动的例子中,你看到了,如果我们假设车辆以 50 米/秒的速度匀速行驶,我们可以预测它的一个新状态:在 150 米处,速度没有变化。

# Constant velocity case

# initial variables
x = 0
velocity = 50
initial_state = [x, velocity]

# predicted state (after three seconds) # this state has a new value for x, but the same velocity as in the initial state dt = 3 new_x = x + velocity*dt predicted_state = [new_x, velocity] # predicted_state = [150, 50] 
 

对于这个等速模型,我们有:

  • 初始状态 = [0, 50]
  • 预测状态 (三秒后) = [150, 50]
 

恒定加速度

但是在第二个例子中,我们假设汽车以 20 米/平方秒的速度减速,经过 3 秒钟,我们得到了一个不同的状态估计。

为了解决这个定位问题,我们必须使用另一个移动模型。此外,我们必须在状态中包含一个新的值:汽车的加速度。

运动模型是基于恒定加速度的:

  • 距离 = 速度dt + 0.5加速度*dt^2,
  • 速度 = 加速度*dt

模型中的状态包括了加速度,样式如下:[x, velocity, acc]。

# Constant acceleration, changing velocity

# initial variables
x = 0
velocity = 50
acc = -20 initial_state = [x, velocity, acc] # predicted state after three seconds have elapsed # this state has a new value for x, and a new value for velocity (but the acceleration stays the same) dt = 3 new_x = x + velocity*dt + 0.5*acc*dt**2 new_vel = velocity + acc*dt predicted_state = [new_x, new_vel, acc] # predicted_state = [60, -10, -20] 
 

对于这个恒定加速度 模型,我们有:

  • 初始状态 = [0, 50, -20]
  • 三秒后的预测状态 = [60, -10, -20]

正如你所看到的,我们的状态和状态估计是基于我们所使用的移动模型以及假定车辆在移动做出的!

 

多少状态变量?

事实上,状态需要多少变量,取决于我们使用的是什么移动模型。

对于恒定速度模型,x速度(velocity)就足够了。

但是对于恒定的加速度模型,你还需要加速度:acc

但这些只是模型。

小贴士

对于我们的状态,我们总是选择模型可发挥效用的最小表述(即最少的变量数)。

猜你喜欢

转载自www.cnblogs.com/fuhang/p/8988955.html