0

我正在尝试为类汽车机器人的非完整运动实施四阶龙格库塔。我不知道我做错了什么,基本上我正在通过 +-Pi/4 来计算硬左转和右转以获得不同的轨迹。但无论我将 +pi/4 或 -pi/4 传递给它,我都会得到相同的答案。我无法弄清楚我做错了什么。我使用的约束方程是:

thetadot = (s/L)*tan(phi)
xdot = s*cos(theta)
ydot = s*sin(theta)

其中 s 是速度,L 是像机器人一样的汽车的长度。

#! /usr/bin/env python
import sys, random, math, pygame
from pygame.locals import *
from math import sqrt,cos,sin,atan2,tan

import numpy as np
import matplotlib.pyplot as plt

XDIM = 640
YDIM = 480
WINSIZE = [XDIM, YDIM]
PHI = 45
s = 0.5
white = 255, 240, 200
black = 20, 20, 40
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
cyan = 0,255,255

pygame.init()
screen = pygame.display.set_mode(WINSIZE)


X = XDIM/2
Y = YDIM/2
THETA = 45
def main():
    nodes = []
    nodes.append(Node(XDIM/2.0,YDIM/2.0,0.0))
    plt.plot(runge_kutta(nodes[0], (3.14/4)))  #Hard Left turn
    plt.plot(runge_kutta(nodes[0], 0))         #Straight ahead
    plt.plot(runge_kutta(nodes[0], -(3.14/4))) #Hard Right turn
    plt.show()

class Node:
    x = 0
    y = 0
    theta = 0
    distance=0  
    parent=None
    def __init__(self,xcoord, ycoord, theta):
         self.x = xcoord
         self.y = ycoord
         self.theta = theta

def rk4(f, x, y, n):
    x0 = y0 =  0
    vx = [0]*(n + 1)
    vy = [0]*(n + 1)
    h = 0.8
    vx[0] = x = x0
    vy[0] = y = y0
    for i in range(1, n + 1):
        k1 = h*f(x, y)
        k2 = h*f(x + 0.5*h, y + 0.5*k1)
        k3 = h*f(x + 0.5*h, y + 0.5*k2)
        k4 = h*f(x + h, y + k3)
        vx[i] = x = x0 + i*h
        vy[i] = y = y + (k1 + k2 + k2 + k3 + k3 + k4)/6
    print "1"
    print vy
    return vy


def fun1(x,y):
    x = (0.5/2)*tan(y)
    print "2"
    print x
    return x

def fun2(x,y):
    x = 0.5*cos(y)
    print "3"
    print x
    return x

def fun3(x,y):
    x = 0.5*sin(y)
    print "4"
    print x
    return x

def runge_kutta(p, phi):
    x1 = p.x
    y1 = p.y
    theta1 = p.theta
    fi = phi
    for i in range(0,5):
        x2 = rk4(fun2, x1, theta1, 5)
        y2 = rk4(fun3, y1, theta1, 5)
        theta2 = rk4(fun1, theta1 ,fi, 5)
        theta1 = theta2
    print "5"
    print zip(x2,y2)
    return zip(x2,y2)





# if python says run, then we should run
if __name__ == '__main__':
    main()
    running = True
    while running:
       for event in pygame.event.get():
        if event.type == pygame.QUIT:
               running = False
4

1 回答 1

1

关于算法我不能说太多,但是你设置我们的rk4函数的方式x, 和y参数永远不会有任何效果:

def rk4(f, x, y, n):
    x0 = y0 =  0          # x0 and y0 will both be 0 after this
    vx = [0]*(n + 1)
    vy = [0]*(n + 1)
    h = 0.8
    vx[0] = x = x0        # now x will be 0
    vy[0] = y = y0        # and y will be 0 too
    ...

在任何情况下,函数的其余部分都将使用x=0and 。y=0

另外,我不知道这是否是故意的,但是其他函数fun1fun2并且fun3永远不要使用作为传递的参数x,它们只使用y. 它们x在本地发生变化,但这不会反映在函数之外。

于 2016-05-01T18:31:26.830 回答