Created
November 11, 2020 16:40
-
-
Save xeonqq/0520d6f75827291688ad9816dcf28baf to your computer and use it in GitHub Desktop.
Simple demo of bicycle model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from math import * | |
import numpy as np | |
import matplotlib.patches as patches | |
# reference https://dingyan89.medium.com/simple-understanding-of-kinematic-bicycle-model-81cac6420357 | |
class Bicycle(object): | |
def __init__(self): | |
self._length =0.1 # m | |
self._width = 0.01 | |
# position of rear axis | |
self._x = 0.0 # m | |
self._y = 0.0 # m | |
self._orientation = 0.0 # rad | |
self._steer_angle = 0.0 # rad | |
self._steer_rate = 0.0 # rad/s | |
self._velocity = 0.0 # m/s | |
@property | |
def x(self): | |
return self._x | |
@property | |
def y(self): | |
return self._y | |
@property | |
def length(self): | |
return self._length | |
@property | |
def width(self): | |
return self._width | |
@property | |
def orientation(self): | |
return self._orientation | |
def set_steer_angle(self, angle): | |
self._steer_angle = angle | |
def set_velocity(self, velocity): | |
self._velocity = velocity | |
def drive(self, dt): | |
self._x += self._velocity * cos(self._orientation) | |
self._y += self._velocity * sin(self._orientation) | |
orientation_rate = self._velocity * tan(self._steer_angle) / self._length | |
self._orientation += orientation_rate * dt | |
def plot(bicycle, ax, color): | |
rect = patches.Rectangle((bicycle.x,bicycle.y), bicycle.length, bicycle.width, bicycle.orientation/pi*180, linewidth=2,edgecolor=(color,0,0),facecolor='none') | |
ax.add_patch(rect) | |
fig,ax = plt.subplots(1) | |
axes = plt.gca() | |
axes.set_xlim([-5,5]) | |
axes.set_ylim([-5,5]) | |
b = Bicycle() | |
dt = 0.1 | |
b.set_velocity(0.1) | |
b.set_steer_angle(pi/6) | |
steps = 100 | |
for i in range(steps): | |
plot(b,ax, float(i)/float(steps)) | |
b.drive(0.1) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment