Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rk4 sim #41

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions rb_ws/src/buggy/scripts/2d_sim/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import sys
import threading

import rospy
from geometry_msgs.msg import Pose, Twist, PoseWithCovariance, TwistWithCovariance
from std_msgs.msg import Float64
Expand Down Expand Up @@ -136,21 +135,19 @@ def step(self):
n_utm = self.n_utm
velocity = self.velocity
steering_angle = self.steering_angle
# Calculate new position
if steering_angle == 0.0:
# Straight
e_utm_new = e_utm + (velocity / self.rate) * np.cos(np.deg2rad(heading))
n_utm_new = n_utm + (velocity / self.rate) * np.sin(np.deg2rad(heading))
heading_new = heading
else:
# steering radius
radius = self.get_steering_arc()
distance = velocity / self.rate
delta_heading = distance / radius
heading_new = heading + np.rad2deg(delta_heading) / 2
e_utm_new = e_utm + (velocity / self.rate) * np.cos(np.deg2rad(heading_new))
n_utm_new = n_utm + (velocity / self.rate) * np.sin(np.deg2rad(heading_new))
heading_new = heading_new + np.rad2deg(delta_heading) / 2

# RK4 to calculate next position
h = 1/self.rate
state = np.array([e_utm, n_utm, np.deg2rad(heading), np.deg2rad(steering_angle)])
k1 = self.dynamics(state, velocity)
k2 = self.dynamics(state + h/2 * k1, velocity)
k3 = self.dynamics(state + h/2 * k2, velocity)
k4 = self.dynamics(state + h/2 * k3, velocity)

final_state = state + h/6 * (k1 + 2 * k2 + 2 * k3 + k4)

e_utm_new, n_utm_new, heading_new, _ = final_state
heading_new = np.rad2deg(heading_new)

with self.lock:
self.e_utm = e_utm_new
Expand Down
Loading