Skip to content

Commit

Permalink
Fixed issues with using class methods
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pereira <[email protected]>
  • Loading branch information
Mario13546 committed Feb 26, 2023
1 parent f0e9e14 commit 1724de0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 57 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<li>The <a href="https://pypi.org/project/opencv-contrib-python/" target="_blank">OpenCV Contributor</a> package</li>
<li>The <a href="https://pypi.org/project/numpy/" target="_blank">NumPy</a> package (should install with pupil-apriltags)</li>
<li>The <a href="https://pypi.org/project/robotpy/" target="_blank">RobotPy</a> package</li>
<li>The <a href="https://pypi.org/project/transforms3d/" target="_blank">Transforms3d</a> package</li>
<li>The <a href="https://pypi.org/project/pupil-apriltags/" target="_blank">Pupil Apriltags</a> package</li>
</ul>
</p>
Expand All @@ -28,14 +27,14 @@
</p>
<p>

pip install -U pip wheel setuptools transforms3d opencv-contrib-python pupil-apriltags robotpy[cscore]
pip install -U pip wheel setuptools opencv-contrib-python pupil-apriltags robotpy[cscore]
</p>

<h2> How to install on Windows: </h2>
<p>
Install <a href="https://www.python.org/downloads/release/python-3108/" target="_blank">Python 3.10.8</a> for Windows, being sure to add Python to PATH. Then, install the required packages using this command:</li>

pip install -U pip wheel setuptools transforms3d opencv-contrib-python pupil-apriltags robotpy[cscore]
pip install -U pip wheel setuptools opencv-contrib-python pupil-apriltags robotpy[cscore]
</p>

<h2> Writing Programs for Jetson </h2>
Expand Down
61 changes: 26 additions & 35 deletions frc_apriltags/Utilities/AprilTag.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
# Import Libraries
import typing
from wpimath.geometry import *
from wpimath.geometry import *

# Start of the AprilTag class
class AprilTag:
"""
Use this class to generate an object with the properties of an AprilTag.
"""
@typing.overload
def __init__(self) -> None:
"""
Constructor for the AprilTag class.
"""
# Creates a default tag
self.id = 0
self.pose = Pose3d()

@typing.overload
def __init__(self, id: int, pose: Pose3d) -> None:
def __init__(self, id: int = 0, pose: Pose3d = Pose3d()):
"""
Constructor for the AprilTag class.
@param tagId
Expand All @@ -27,20 +16,22 @@ def __init__(self, id: int, pose: Pose3d) -> None:
self.id = id
self.pose = pose

@typing.overload
def __init__(self, id: int, trans: Translation3d, rot: Rotation3d) -> None:
@classmethod
def fromPoseComponents(cls, id: int, trans: Translation3d, rot: Rotation3d):
"""
Constructor for the AprilTag class.
@param tagId
@param Translation3d
@param Rotation3d
"""
# Localize parameters
self.id = id
self.pose = Pose3d(trans, rot)
# Generate 3D parts
pose = Pose3d(trans, rot)

@typing.overload
def __init__(self, id: int, x: float, y: float, z: float, rMatrix) -> None:
# Return the class
return AprilTag(id, pose)

@classmethod
def fromMatrix(cls, id: int, x: float, y: float, z: float, rMatrix):
"""
Constructor for the AprilTag class.
@param tagId
Expand All @@ -52,31 +43,31 @@ def __init__(self, id: int, x: float, y: float, z: float, rMatrix) -> None:
# Generate 3D parts
rot = Rotation3d(rMatrix)
trans = Translation3d(x, y, z)
pose = Pose3d(trans, rot)

# Localize parameters
self.id = id
self.pose = Pose3d(trans, rot)
# Return the class
return AprilTag(id, pose)

@typing.overload
def __init__(self, id: int, x: float, y: float, z: float, quaterion: Quaternion) -> None:
@classmethod
def fromQuaternion(cls, id: int, x: float, y: float, z: float, q: Quaternion):
"""
Constructor for the AprilTag class.
@param tagId
@param x: The x offset in meters
@param y: The y offset in meters
@param z: The z offset in meters
@param quaterion: The 4x4 Wuaterion
@param q: The 4x4 Quaterion
"""
# Generate 3D parts
rot = Rotation3d(quaterion)
rot = Rotation3d(q)
trans = Translation3d(x, y, z)
pose = Pose3d(trans, rot)

# Localize parameters
self.id = id
self.pose = Pose3d(trans, rot)
# Return the class
return AprilTag(id, pose)

@typing.overload
def __init__(self, id: int, x: float, y: float, z: float, roll: float, pitch: float, yaw: float) -> None:
@classmethod
def fromDetailed(cls, id: int, x: float, y: float, z: float, roll: float, pitch: float, yaw: float):
"""
Constructor for the AprilTag class.
@param tagId
Expand All @@ -90,10 +81,10 @@ def __init__(self, id: int, x: float, y: float, z: float, roll: float, pitch: fl
# Generate 3D parts
rot = Rotation3d(roll, pitch, yaw)
trans = Translation3d(x, y, z)
pose = Pose3d(trans, rot)

# Localize parameters
self.id = id
self.pose = Pose3d(trans, rot)
# Return the class
return AprilTag(id, pose)

def getId(self) -> int:
"""
Expand Down
23 changes: 12 additions & 11 deletions frc_apriltags/Utilities/AprilTagFieldLayout.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Import Libraries
import json
import typing
import numpy as np
from enum import Enum
from typing import Sequence
Expand All @@ -12,13 +11,12 @@
from frc_apriltags.Utilities.AprilTag import AprilTag

# Creates the AprilTagFieldLayout class
class AprilTagFieldLayout:
class AprilTagFieldLayout:
class Origin(Enum):
kBlueAllianceWallRightSide = "BlueWall"
kRedAllianceWallRightSide = "RedWall"

@typing.overload
def __init__(self, tags: Sequence[AprilTag], fieldLength: float, fieldWidth: float, isRed = False) -> None:
def __init__(self, tags: Sequence[AprilTag], fieldLength: float, fieldWidth: float, isRed = False):
"""
Generates an field with AprilTags for testing.
@param allTags: A list of all known tags
Expand Down Expand Up @@ -59,15 +57,15 @@ def __init__(self, tags: Sequence[AprilTag], fieldLength: float, fieldWidth: flo
elif (isRed == False):
self.setOrigin(AprilTagFieldLayout.Origin.kBlueAllianceWallRightSide)

@typing.overload
def __init__(self, name: str, isRed: bool) -> None:
@classmethod
def fromJson(cls, name: str, isRed: bool):
"""
Generates an field with AprilTags from the WPILib json file.
@param name: The name of the json file in the year-gamename format
@param isRed: If you are on the red alliance
"""
# Creates a blank array
self.allTags = [Pose3d()] * 9
allTags = [AprilTag()] * 9

# Returns the JSON as a dictionary
with resources.open_binary("frc_apriltags.Tag_Layouts", name + ".json") as fp:
Expand All @@ -93,14 +91,17 @@ def __init__(self, name: str, isRed: bool) -> None:
q = Quaternion(w_rot, x_rot, y_rot, z_rot)

# Creates a Pose3d object
pose = Pose3d(x_trans, y_trans, z_trans, Rotation3d(q))
tag = AprilTag.fromQuaternion(id, x_trans, y_trans, z_trans, q)

# Adds the tag to the allTags array
self.allTags[id] = pose
allTags[id] = tag

# Sets the field dimensions
self.fieldLength = data["field"]["length"]
self.fieldWidth = data["field"]["width"]
fieldLength = data["field"]["length"]
fieldWidth = data["field"]["width"]

# Return the class
return cls(allTags, fieldLength, fieldWidth, isRed)

def setOrigin(self, origin):
"""
Expand Down
5 changes: 1 addition & 4 deletions frc_apriltags/apriltags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
import cv2 as cv
import numpy as np
import pupil_apriltags
from wpilib import Timer
from wpimath.geometry import Pose3d, Rotation3d, Translation3d

# Import Classes
from frc_apriltags.communications import NetworkCommunications

# Import Utilities
from frc_apriltags.Utilities.Units import Units
from frc_apriltags.Utilities.Units import Units
from frc_apriltags.Utilities.Logger import Logger

# The size of the tag in meters
Expand All @@ -25,7 +23,6 @@ def __init__(self) -> None:
Constructor for the Detector class.
"""
# Instance creation
self.timer = Timer()
self.comms = NetworkCommunications()

# Creates a pupil apriltags detector
Expand Down
2 changes: 1 addition & 1 deletion frc_apriltags/camera.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Import Libraries
import cv2 as cv
import cv2 as cv
import numpy as np

# Import Classes
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name = "frc-apriltags" # Required
#
# For a discussion on single-sourcing the version, see
# https://packaging.python.org/guides/single-sourcing-package-version/
version = "0.4.1.b0" # Required
version = "0.5.0" # Required
# Pre-releases denotations:
# dev releases (denoted with a “.devN” suffix)
# alpha releases (denoted with a “.aN” suffix)
Expand All @@ -43,7 +43,7 @@ readme = "README.md" # Optional
# 'Programming Language' classifiers above, 'pip install' will check this
# and refuse to install the project if the version does not match. See
# https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires
requires-python = ">=3.8,<3.12"
requires-python = ">=3.7,<3.12"

# This is either text indicating the license for the distribution, or a file
# that contains the license
Expand All @@ -56,7 +56,7 @@ license = {file = "LICENSE.txt"}
# Note that this is a list of additional keywords, separated
# by commas, to be used to assist searching for the distribution in a
# larger catalog.
keywords = ["FRC", "AprilTags", "OpenCV"] # Optional
keywords = ["FRC-AprilTags", "FRC", "AprilTags", "OpenCV"] # Optional

# This should be your name or the name of the organization who originally
# authored the project, and a valid email address corresponding to the name
Expand Down
30 changes: 30 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np
from wpimath.geometry import *
from frc_apriltags.Utilities.AprilTagFieldLayout import AprilTagFieldLayout

field = AprilTagFieldLayout.fromJson("2023-chargedup", False)
print(field.getTagPose(7))

pose1 = Pose3d(
Translation3d(0, 0, 0),
Rotation3d(0, 0, np.pi*0)
)

# Gets things in the right direction
pose2 = Pose3d(
Translation3d(1, 1, 0),
Rotation3d(0, 0, 0)
)
trans1 = Transform3d(
Translation3d(),
Rotation3d(pose1.rotation().X(), pose1.rotation().Y(), pose1.rotation().Z())
)
pose2 = pose2.transformBy(trans1)

#
trans2 = Transform3d(
pose2.translation(),
pose2.rotation()
)

print(pose1.transformBy(trans2).toPose2d())

0 comments on commit 1724de0

Please sign in to comment.