Rename this. Also break down the apply method.

This commit is contained in:
Danny Staple 2022-12-28 11:26:40 +00:00
parent 584e189cd1
commit 9cff9fe1f1
13 changed files with 11 additions and 58 deletions

View File

@ -1,51 +0,0 @@
import numpy as np
from matplotlib import pyplot as plt, patches
from robot import arena
import random
fig, ax = plt.subplots()
# for line in arena.boundary_lines:
# plt.plot([line[0][0], line[1][0]], [line[0][1], line[1][1]], color="black")
poses = np.random.normal([250, 300], [2, 2], size=(200, 2))
ax.scatter(poses[:, 0], poses[:, 1])
# create a line for a motion vector
motion_angle = 30
motion_scale = 300
motion_line = np.array([[250, 300],
[motion_scale * np.cos(np.radians(motion_angle)), motion_scale * np.sin(np.radians(motion_angle))]])
triangular_proportion = np.sqrt(6) / 2
def get_triangular_sample(mean, standard_deviation):
base = triangular_proportion * (random.uniform(-standard_deviation, standard_deviation) + random.uniform(-standard_deviation, standard_deviation))
return mean + base
motion_rotation = np.array([get_triangular_sample(motion_angle, 15) for _ in range(poses.shape[0])])
motion_translation = np.array([get_triangular_sample(motion_scale, 10) for _ in range(poses.shape[0])])
new_poses = np.zeros_like(poses)
new_poses[:, 0] = poses[:, 0] + motion_translation * np.cos(np.radians(motion_rotation))
new_poses[:, 1] = poses[:, 1] + motion_translation * np.sin(np.radians(motion_rotation))
# new_poses[:, 0] = poses[:, 0] + motion_scale * np.cos(np.radians(motion_angle))
# new_poses[:, 1] = poses[:, 1] + motion_scale * np.sin(np.radians(motion_angle))
ax.scatter(new_poses[:, 0], new_poses[:, 1])
# plot the vector arrow
ax.arrow(*motion_line[0], *motion_line[1], color="red", width=5)
# plot the angles on top
# line from original cluster middle, going east.
ax.plot([250, 250+100], [300, 300], color="black")
angle = patches.Wedge((250, 300), 100, 0, motion_angle, width=20, color=(0.3, 0.3, 0.3, 0.3))
ax.add_patch(angle)
ax.text(370, 330, f"{motion_angle}°", horizontalalignment="center", verticalalignment="center",
fontsize="x-large")
plt.show()

View File

@ -1201,7 +1201,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
"version": "3.9.15 (main, Oct 11 2022, 22:27:25) \n[Clang 14.0.0 (clang-1400.0.29.102)]"
},
"orig_nbformat": 4,
"vscode": {

View File

@ -104,6 +104,14 @@ class Simulation:
rot2 = rot1
return rot1, arc_length, rot2
def apply_motion_to_poses(self, rot1, trans, rot2):
self.poses[:,2] += rot1
rot1_radians = np.radians(self.poses[:,2])
self.poses[:,0] += trans * np.cos(rot1_radians)
self.poses[:,1] += trans * np.sin(rot1_radians)
self.poses[:,2] += rot2
self.poses[:,2] = np.array([float(theta % 360) for theta in self.poses[:,2]])
def motion_model(self):
"""Apply the motion model"""
new_encoder_left = robot.left_encoder.read()
@ -114,12 +122,8 @@ class Simulation:
new_encoder_right - self.last_encoder_right)
self.last_encoder_left = new_encoder_left
self.last_encoder_right = new_encoder_right
self.poses[:,2] += rot1
rot1_radians = np.radians(self.poses[:,2])
self.poses[:,0] += trans * np.cos(rot1_radians)
self.poses[:,1] += trans * np.sin(rot1_radians)
self.poses[:,2] += rot2
self.poses[:,2] = np.array([float(theta % 360) for theta in self.poses[:,2]])
self.apply_motion_to_poses(rot1, trans, rot2)
print(
json.dumps(
[self.poses.tolist(), rot1, trans, rot2]