-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic vector and rotation logic
- Loading branch information
1 parent
7edd6fc
commit 3f9d499
Showing
6 changed files
with
201 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
asyncio | ||
fastapi | ||
jinja2 | ||
numpy | ||
opencv-python | ||
uvicorn[standard] | ||
websockets |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,182 @@ | ||
import unittest | ||
import math | ||
import numpy as np | ||
from src.robot.v1.structs import vector2d, rotation2d | ||
|
||
|
||
class TestVector2D(unittest.TestCase): | ||
def test_instantiation(self): | ||
pass | ||
def test_add(self): | ||
total = vector2d(1, 2) + vector2d(3, 4) | ||
self.assertEqual(total[0], 4) | ||
self.assertEqual(total[1], 6) | ||
|
||
def test_iadd(self): | ||
vec_a = vector2d(1, 2) | ||
vec_a += vector2d(5, 5) | ||
self.assertEqual(vec_a[0], 6) | ||
self.assertEqual(vec_a[1], 7) | ||
|
||
def test_sub(self): | ||
total = vector2d(5, 5) - vector2d(3, 4) | ||
self.assertEqual(total[0], 2) | ||
self.assertEqual(total[1], 1) | ||
|
||
def test_isub(self): | ||
vec_a = vector2d(5, 5) | ||
vec_a -= vector2d(1, 2) | ||
self.assertEqual(vec_a[0], 4) | ||
self.assertEqual(vec_a[1], 3) | ||
|
||
def test_45deg_rotation(self): | ||
angle = math.pi / 4 | ||
starting_vec = vector2d(1, 2) | ||
|
||
# Figure out the expected angles programmatically | ||
r = math.sqrt(5) | ||
expected_angle_cw = math.atan2(starting_vec[1], starting_vec[0]) - (math.pi / 4) | ||
expected_vec_cw = vector2d( | ||
r * math.cos(expected_angle_cw), r * math.sin(expected_angle_cw) | ||
) | ||
|
||
expected_angle_ccw = math.atan2(starting_vec[1], starting_vec[0]) + ( | ||
math.pi / 4 | ||
) | ||
expected_vec_ccw = vector2d( | ||
r * math.cos(expected_angle_ccw), r * math.sin(expected_angle_ccw) | ||
) | ||
|
||
def test_ | ||
for theta, expected in [(angle, expected_vec_cw), (-angle, expected_vec_ccw)]: | ||
rotation = rotation2d(theta) | ||
|
||
@dataclass | ||
class Vector2D: | ||
x: float | ||
y: float | ||
rotated = starting_vec.dot(rotation) | ||
self.assertEqual(rotated.shape, (2,)) | ||
self.assertAlmostEqual(rotated[0], expected[0]) | ||
self.assertAlmostEqual(rotated[1], expected[1]) | ||
|
||
def __add__(self, other): | ||
assert isinstance(other, Vector2D) | ||
def test_90deg_rotation(self): | ||
angle = math.pi / 2 | ||
starting_vec = vector2d(1, 2) | ||
expected_vec = vector2d(2, -1) | ||
|
||
return Vector2D(self.x + other.x, self.y + other.y) | ||
for theta, expected in [(angle, expected_vec), (-angle, -expected_vec)]: | ||
rotation = rotation2d(theta) | ||
|
||
def __iadd__(self, other): | ||
assert isinstance(other, Vector2D) | ||
rotated = starting_vec.dot(rotation) | ||
self.assertEqual(rotated.shape, (2,)) | ||
self.assertAlmostEqual(rotated[0], expected[0]) | ||
self.assertAlmostEqual(rotated[1], expected[1]) | ||
|
||
self.x += other.x | ||
self.y += other.y | ||
def test_180deg_rotation(self): | ||
angle = math.pi | ||
starting_vec = vector2d(1, 2) | ||
expected_vec = vector2d(-1, -2) | ||
|
||
def __sub__(self, other): | ||
assert isinstance(other, Vector2D) | ||
for theta, expected in [(angle, expected_vec), (-angle, expected_vec)]: | ||
rotation = rotation2d(theta) | ||
|
||
rotated = starting_vec.dot(rotation) | ||
self.assertEqual(rotated.shape, (2,)) | ||
self.assertAlmostEqual(rotated[0], expected[0]) | ||
self.assertAlmostEqual(rotated[1], expected[1]) | ||
|
||
def test_270deg_rotation(self): | ||
angle = (3 * math.pi) / 2 | ||
starting_vec = vector2d(1, 2) | ||
expected_vec = vector2d(-2, 1) | ||
|
||
for theta, expected in [(angle, expected_vec), (-angle, -expected_vec)]: | ||
rotation = rotation2d(theta) | ||
|
||
rotated = starting_vec.dot(rotation) | ||
self.assertEqual(rotated.shape, (2,)) | ||
self.assertAlmostEqual(rotated[0], expected[0]) | ||
self.assertAlmostEqual(rotated[1], expected[1]) | ||
|
||
def test_360deg_rotation(self): | ||
angle = 2 * math.pi | ||
starting_vec = vector2d(1, 2) | ||
expected_vec = vector2d(1, 2) | ||
|
||
for theta, expected in [(angle, expected_vec), (-angle, expected_vec)]: | ||
rotation = rotation2d(theta) | ||
|
||
rotated = starting_vec.dot(rotation) | ||
self.assertEqual(rotated.shape, (2,)) | ||
self.assertAlmostEqual(rotated[0], expected[0]) | ||
self.assertAlmostEqual(rotated[1], expected[1]) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
|
||
|
||
# @dataclass | ||
# class Vector2D: | ||
# x: float | ||
# y: float | ||
|
||
# def __add__(self, other): | ||
# assert isinstance(other, Vector2D) | ||
|
||
# return Vector2D(self.x + other.x, self.y + other.y) | ||
|
||
# def __iadd__(self, other): | ||
# assert isinstance(other, Vector2D) | ||
|
||
# self.x += other.x | ||
# self.y += other.y | ||
|
||
return Vector2D(self.x - other.x, self.y - other.y) | ||
# def __sub__(self, other): | ||
# assert isinstance(other, Vector2D) | ||
|
||
# return Vector2D(self.x - other.x, self.y - other.y) | ||
|
||
def __isub__(self, other): | ||
assert isinstance(other, Vector2D) | ||
# def __isub__(self, other): | ||
# assert isinstance(other, Vector2D) | ||
|
||
self.x -= other.x | ||
self.y -= other.y | ||
# self.x -= other.x | ||
# self.y -= other.y | ||
|
||
|
||
@dataclass | ||
class Rotation2D: | ||
theta: float | ||
# @dataclass | ||
# class Rotation2D: | ||
# theta: float | ||
|
||
|
||
@dataclass | ||
class Position2D: | ||
location: Vector2D | ||
rotation: Rotation2D | ||
# @dataclass | ||
# class Position2D: | ||
# location: Vector2D | ||
# rotation: Rotation2D | ||
|
||
def __init__(self, x, y, theta): | ||
self.location = Vector2D(x, y) | ||
self.rotation = Rotation2D(theta) | ||
# def __init__(self, x, y, theta): | ||
# self.location = Vector2D(x, y) | ||
# self.rotation = Rotation2D(theta) | ||
|
||
|
||
@dataclass | ||
class MemorySegment: | ||
byte_offset: int | ||
byte_size: int | ||
# @dataclass | ||
# class MemorySegment: | ||
# byte_offset: int | ||
# byte_size: int | ||
|
||
|
||
@dataclass | ||
class DynamixelMotorAddressConfig: | ||
torque_enable: MemorySegment | ||
led_enable: MemorySegment | ||
d_gain: MemorySegment | ||
i_gain: MemorySegment | ||
p_gain: MemorySegment | ||
goal_position: MemorySegment | ||
moving_speed: MemorySegment | ||
torque_limit: MemorySegment | ||
present_position: MemorySegment | ||
present_speed: MemorySegment | ||
present_load: MemorySegment | ||
present_input_voltage: MemorySegment | ||
present_temperature: MemorySegment | ||
registered: MemorySegment | ||
moving: MemorySegment | ||
lock: MemorySegment | ||
punch: MemorySegment | ||
realtime_tick: MemorySegment | ||
goal_acceleration: MemorySegment | ||
# @dataclass | ||
# class DynamixelMotorAddressConfig: | ||
# torque_enable: MemorySegment | ||
# led_enable: MemorySegment | ||
# d_gain: MemorySegment | ||
# i_gain: MemorySegment | ||
# p_gain: MemorySegment | ||
# goal_position: MemorySegment | ||
# moving_speed: MemorySegment | ||
# torque_limit: MemorySegment | ||
# present_position: MemorySegment | ||
# present_speed: MemorySegment | ||
# present_load: MemorySegment | ||
# present_input_voltage: MemorySegment | ||
# present_temperature: MemorySegment | ||
# registered: MemorySegment | ||
# moving: MemorySegment | ||
# lock: MemorySegment | ||
# punch: MemorySegment | ||
# realtime_tick: MemorySegment | ||
# goal_acceleration: MemorySegment |