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

Feat/shooting while moving #91

Draft
wants to merge 15 commits into
base: dev
Choose a base branch
from
3 changes: 2 additions & 1 deletion command/flywheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def initialize(self):
self.subsystem.set_velocity_linear(self.velocity)

def execute(self):
print(self.subsystem.get_velocity_linear(), 'Current velocity')
...
# print(self.subsystem.get_velocity_linear(), 'Current velocity')

def isFinished(self):
return self.subsystem.within_velocity_linear(self.velocity, 2)
Expand Down
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Scoring:

speaker_z = speaker_z_top #(speaker_z_top + speaker_z_bottom) / 2

speaker_x = speaker_depth / 2.5
speaker_x = speaker_depth / 1.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this all about? What is speaker_depth?


amp_y = field_width

Expand Down
63 changes: 50 additions & 13 deletions sensors/trajectory_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from subsystem import Elevator, Flywheel
from toolkit.utils.toolkit_math import NumericalIntegration, extrapolate
from utils import POI
from wpimath.geometry import Rotation2d, Translation3d, Translation2d
from wpimath.geometry import Rotation2d, Translation3d, Translation2d, Transform2d



Expand Down Expand Up @@ -44,26 +44,38 @@ def __init__(self, odometry: FieldOdometry, elevator: Elevator, flywheel: Flywhe
self.table = ntcore.NetworkTableInstance.getDefault().getTable('shot calculations')
self.numerical_integration = NumericalIntegration()
self.use_air_resistance = False
self.v0_effective = 0

def init(self):
self.speaker = POI.Coordinates.Structures.Scoring.kSpeaker.getTranslation()
self.speaker_z = POI.Coordinates.Structures.Scoring.kSpeaker.getZ()


def get_drivetrain_speeds_speaker_distance(self):

rvx = self.get_drivetrain_speeds_field_origin().vx

rvs = rvx * np.cos(self.get_rotation_to_speaker().radians())

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure that vy is relevant here.


return rvs

def get_drivetrain_speeds_field_origin(self):
vels = self.odometry.drivetrain.chassis_speeds

vels = self.odometry.drivetrain.chassis_speeds.fromRobotRelativeSpeeds(vels, self.odometry.getPose().rotation())

return vels

def calculate_angle_no_air(self, distance_to_target: float, delta_z) -> radians:
def calculate_angle_no_air(self, distance_to_target: float, delta_z) -> float:
"""
Calculates the angle of the trajectory without air resistance.
"""

vels = self.odometry.drivetrain.chassis_speeds

drivetrain_angle = self.get_rotation_to_speaker()


vels = self.odometry.drivetrain.chassis_speeds.fromRobotRelativeSpeeds(vels, drivetrain_angle)

rvx, rvy, rvo = (
vels.vx, vels.vy, vels.omega
)
rvs = self.get_drivetrain_speeds_speaker_distance()


# Calculate the horizontal angle without considering velocities
Expand All @@ -75,8 +87,11 @@ def calculate_angle_no_air(self, distance_to_target: float, delta_z) -> radians:

# Calculate the effective velocity
# v_effective = self.flywheel.get_velocity_linear() + rvx * np.cos(drivetrain_angle.radians()) + rvy * np.cos(drivetrain_angle.radians())
v_effective = config.v0_flywheel_minimum# + rvx + rvy
# v_effective = self.flywheel.get_velocity_linear()# + rvx + rvy
# v_effective = config.v0_flywheel
v_effective = self.flywheel.get_velocity_linear() + (rvs * np.cos(phi0))
self.v0_effective = v_effective


if v_effective == 0:
return config.Giraffe.kIdle.wrist_angle
Expand All @@ -85,9 +100,9 @@ def calculate_angle_no_air(self, distance_to_target: float, delta_z) -> radians:
result_angle = (
0.5 * np.arcsin(
np.sin(phi0)
+ constants.g
+ (constants.g
* distance_to_target
* np.cos(phi0)
* np.cos(phi0))
/ (v_effective ** 2)
)
+ 0.5 * phi0
Expand Down Expand Up @@ -147,13 +162,26 @@ def get_rotation_to_speaker(self):
robot_pose_2d = self.odometry.getPose()
robot_to_speaker = speaker_translation - robot_pose_2d.translation()
return robot_to_speaker.angle()

def get_rotation_to_speaker_moving(self):
speaker_translation:Translation2d = POI.Coordinates.Structures.Scoring.kSpeaker.getTranslation()
t_total = self.get_distance_to_target() / (self.v0_effective * np.cos(self.get_theta())) if self.v0_effective > 0 else 0


rvels = self.get_drivetrain_speeds_field_origin()

robot_pose_2d = self.odometry.getPose()
robot_pose_2d_w_speeds = robot_pose_2d + Transform2d(rvels.vx * t_total, rvels.vy * t_total, 0)
robot_to_speaker = speaker_translation - robot_pose_2d_w_speeds.translation()
return robot_to_speaker.angle()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this is all about. Can you explain it to me? Again. I think that we need to be thinking about where we WILL need to be aiming, rather than should be aiming right now. So we may need to be thinking ahead with Kalman filter thing.


def update_base(self):
"""
updates rotation of base to score shot
:return: base target angle
"""
self.base_rotation2d = self.get_rotation_to_speaker()
# self.base_rotation2d = self.get_rotation_to_speaker()
self.base_rotation2d = self.get_rotation_to_speaker_moving()
return self.base_rotation2d

def update(self):
Expand All @@ -171,6 +199,15 @@ def update_tables(self):
self.table.putNumber('distance to target', self.distance_to_target)
self.table.putNumber('bot angle', self.get_bot_theta().degrees())
self.table.putNumber('delta z', self.delta_z)
self.table.putNumber('v effective', self.v0_effective)
self.table.putNumber('drivetrain speeds speaker distance',
self.get_drivetrain_speeds_speaker_distance(),
)
self.table.putNumberArray('drivetrain speeds field', [
self.get_drivetrain_speeds_field_origin().vx,
self.get_drivetrain_speeds_field_origin().vy,
self.get_drivetrain_speeds_field_origin().omega
])

def run_sim(self, shooter_theta):
def hit_target(t, u):
Expand Down
Loading