Skip to content

Commit

Permalink
Merge pull request #87 from Shenzhen-Robotics-Alliance/dev
Browse files Browse the repository at this point in the history
CTRE Generated Swerve simulation
  • Loading branch information
catr1xLiu authored Jan 13, 2025
2 parents 993f48e + 5cb5d00 commit 620540d
Show file tree
Hide file tree
Showing 71 changed files with 5,752 additions and 69 deletions.
2 changes: 1 addition & 1 deletion LICENSE-IronMaple.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#### This project uses [maple-sim](https://github.com/Shenzhen-Robotics-Alliance/maple-sim) for simulation.
If you encounter any bugs related to drivetrain physics simulation, please [submit an issue to maple-sim](https://github.com/Shenzhen-Robotics-Alliance/maple-sim/issues/new/choose).

## MIT License, Copyright (c) 2024 Team5516 "Iron Maple"
## MIT License, Copyright (c) 2024-2025 Team5516 "Iron Maple"

**Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:**

Expand Down
2 changes: 1 addition & 1 deletion LICENSE-WPILib.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2009-2024 FIRST and other WPILib contributors
Copyright (c) 2009-2025 FIRST and other WPILib contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
4 changes: 2 additions & 2 deletions docs/vendordep/maple-sim.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"fileName": "maple-sim.json",
"name": "maplesim",
"version": "0.3.1",
"version": "0.3.2prev",
"frcYear": "2025",
"uuid": "c39481e8-4a63-4a4c-9df6-48d91e4da37b",
"mavenUrls": [
Expand All @@ -13,7 +13,7 @@
{
"groupId": "org.ironmaple",
"artifactId": "maplesim-java",
"version": "0.3.1"
"version": "0.3.2prev"
},
{
"groupId": "org.dyn4j",
Expand Down
2 changes: 1 addition & 1 deletion project/publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'maven-publish'

ext.licenseFile = files("$rootDir/LICENSE.txt")

def pubVersion = '0.3.1'
def pubVersion = '0.3.2prev'

def outputsFolder = file("$buildDir/outputs")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ public class SwerveModuleSimulation {
private Voltage driveMotorAppliedVoltage = Volts.zero();
private Current driveMotorStatorCurrent = Amps.zero();
private Angle driveWheelFinalPosition = Radians.zero();
private AngularVelocity steerVelocity = RadiansPerSecond.zero(), driveWheelFinalSpeed = RadiansPerSecond.zero();
private AngularVelocity driveWheelFinalSpeed = RadiansPerSecond.zero();

private SimulatedMotorController driveMotorController;

private Rotation2d steerAbsoluteFacing = Rotation2d.fromRotations(Math.random());
private final Angle steerRelativeEncoderOffSet = Radians.of((Math.random() - 0.5) * 30);
private final Queue<Angle> driveWheelFinalPositionCache;
private final Queue<Rotation2d> steerAbsolutePositionCache;
Expand All @@ -93,7 +92,7 @@ public SwerveModuleSimulation(SwerveModuleSimulationConfig config) {
driveWheelFinalPositionCache.offer(driveWheelFinalPosition);
this.steerAbsolutePositionCache = new ConcurrentLinkedQueue<>();
for (int i = 0; i < SimulatedArena.getSimulationSubTicksIn1Period(); i++)
steerAbsolutePositionCache.offer(steerAbsoluteFacing);
steerAbsolutePositionCache.offer(getSteerAbsoluteFacing());

this.driveMotorController = new SimulatedMotorController.GenericMotorController(config.driveMotorConfigs.motor);
this.steerMotorSim.useSimpleDCMotorController();
Expand Down Expand Up @@ -160,11 +159,11 @@ public Vector2 updateSimulationSubTickGetModuleForce(
Rotation2d robotFacing,
double gravityForceOnModuleNewtons) {
/* Step1: Update the steer mechanism simulation */
updateSteerSimulation();
steerMotorSim.update(SimulatedArena.getSimulationDt());

/* Step2: Simulate the amount of propelling force generated by the module. */
final double grippingForceNewtons = config.getGrippingForceNewtons(gravityForceOnModuleNewtons);
final Rotation2d moduleWorldFacing = this.steerAbsoluteFacing.plus(robotFacing);
final Rotation2d moduleWorldFacing = this.getSteerAbsoluteFacing().plus(robotFacing);
final Vector2 propellingForce =
getPropellingForce(grippingForceNewtons, moduleWorldFacing, moduleCurrentGroundVelocityWorldRelative);

Expand All @@ -174,18 +173,6 @@ public Vector2 updateSimulationSubTickGetModuleForce(
return propellingForce;
}

/**
*
*
* <h2>updates the simulation for the steer mechanism.</h2>
*/
private void updateSteerSimulation() {
/* update the readings of the sensor */
steerMotorSim.update(SimulatedArena.getSimulationDt());
this.steerAbsoluteFacing = new Rotation2d(steerMotorSim.getAngularPosition());
this.steerVelocity = steerMotorSim.getVelocity();
}

/**
*
*
Expand Down Expand Up @@ -263,7 +250,7 @@ private double getDriveWheelTorque() {
public SwerveModuleState getCurrentState() {
return new SwerveModuleState(
MetersPerSecond.of(getDriveWheelFinalSpeed().in(RadiansPerSecond) * config.WHEEL_RADIUS.in(Meters)),
steerAbsoluteFacing);
getSteerAbsoluteFacing());
}

/**
Expand All @@ -284,7 +271,7 @@ protected SwerveModuleState getFreeSpinState() {
driveMotorAppliedVoltage)
.in(RadiansPerSecond)
* config.WHEEL_RADIUS.in(Meters),
steerAbsoluteFacing);
getSteerAbsoluteFacing());
}

/**
Expand All @@ -301,7 +288,7 @@ private void updateEncoderCaches() {

/* cache sensor readings to queue for high-frequency odometry */
this.steerAbsolutePositionCache.poll();
this.steerAbsolutePositionCache.offer(steerAbsoluteFacing);
this.steerAbsolutePositionCache.offer(getSteerAbsoluteFacing());

this.driveWheelFinalPositionCache.poll();
this.driveWheelFinalPositionCache.offer(driveWheelFinalPosition);
Expand Down Expand Up @@ -460,7 +447,19 @@ public AngularVelocity getSteerRelativeEncoderVelocity() {
* @return the absolute facing of the steer mechanism, as a {@link Rotation2d}
*/
public Rotation2d getSteerAbsoluteFacing() {
return steerAbsoluteFacing;
return new Rotation2d(getSteerAbsoluteAngle());
}

/**
*
*
* <h2>Obtains the Absolute Angle of the Steer Mechanism.</h2>
*
* @return the (continuous) final angle of the steer mechanism, as a {@link Angle}
* @see MapleMotorSim#getAngularPosition()
*/
public Angle getSteerAbsoluteAngle() {
return steerMotorSim.getAngularPosition();
}

/**
Expand All @@ -471,7 +470,7 @@ public Rotation2d getSteerAbsoluteFacing() {
* @return the absolute angular velocity of the steer mechanism
*/
public AngularVelocity getSteerAbsoluteEncoderSpeed() {
return steerVelocity;
return steerMotorSim.getVelocity();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public SimulatedMotorController.GenericMotorController useSimpleDCMotorControlle
*
* <p>This is equivalent to {@link edu.wpi.first.wpilibj.simulation.DCMotorSim#getAngularPosition()}.
*
* @return the angular position of the motor, continuous
* @return the angular position of the mechanism, continuous
*/
public Angle getAngularPosition() {
return state.mechanismAngularPosition;
Expand All @@ -107,7 +107,7 @@ public Angle getEncoderPosition() {
*
* <p>This is equivalent to {@link edu.wpi.first.wpilibj.simulation.DCMotorSim#getAngularVelocity()}.
*
* @return the final angular velocity of the rotter
* @return the final angular velocity of the mechanism
*/
public AngularVelocity getVelocity() {
return state.mechanismAngularVelocity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,14 @@ protected SimMotorConfigs clone() {

return cfg;
}

@Override
public String toString() {
return "SimMotorConfigs {"
+ "\n motor = " + motor // Relies on DCMotor.toString() for details
+ "\n gearing = " + gearing
+ "\n loadMOI (kg·m^2) = " + loadMOI.in(KilogramSquareMeters)
+ "\n friction (N·m) = " + friction.in(NewtonMeters)
+ "\n}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FieldMirroringUtils {
public static final double FIELD_HEIGHT = 8.052;

public static Rotation2d toCurrentAllianceRotation(Rotation2d rotationAtBlueSide) {
return rotationAtBlueSide.times(-1);
return isSidePresentedAsRed() ? rotationAtBlueSide.plus(Rotation2d.k180deg) : rotationAtBlueSide;
}

public static Translation2d toCurrentAllianceTranslation(Translation2d translationAtBlueSide) {
Expand Down Expand Up @@ -45,9 +45,6 @@ public static boolean isSidePresentedAsRed() {
}

public static Rotation2d getCurrentAllianceDriverStationFacing() {
return switch (DriverStation.getAlliance().orElse(DriverStation.Alliance.Red)) {
case Red -> new Rotation2d(Math.PI);
case Blue -> new Rotation2d(0);
};
return toCurrentAllianceRotation(Rotation2d.kZero);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#### This project uses [maple-sim](https://github.com/Shenzhen-Robotics-Alliance/maple-sim) for simulation.
If you encounter any bugs related to drivetrain physics simulation, please [submit an issue to maple-sim](https://github.com/Shenzhen-Robotics-Alliance/maple-sim/issues/new/choose).

## MIT License, Copyright (c) 2024 Team5516 "Iron Maple"
## MIT License, Copyright (c) 2024-2025 Team5516 "Iron Maple"

**Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#### This project uses [maple-sim](https://github.com/Shenzhen-Robotics-Alliance/maple-sim) for simulation.
If you encounter any bugs related to drivetrain physics simulation, please [submit an issue to maple-sim](https://github.com/Shenzhen-Robotics-Alliance/maple-sim/issues/new/choose).

## MIT License, Copyright (c) 2024 Team5516 "Iron Maple"
## MIT License, Copyright (c) 2024-2025 Team5516 "Iron Maple"

**Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#### This project uses [maple-sim](https://github.com/Shenzhen-Robotics-Alliance/maple-sim) for simulation.
If you encounter any bugs related to drivetrain physics simulation, please [submit an issue to maple-sim](https://github.com/Shenzhen-Robotics-Alliance/maple-sim/issues/new/choose).

## MIT License, Copyright (c) 2024 Team5516 "Iron Maple"
## MIT License, Copyright (c) 2024-2025 Team5516 "Iron Maple"

**Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 FRC 6328
// Copyright 2021-2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// This program is free software; you can redistribute it and/or
Expand Down
Loading

0 comments on commit 620540d

Please sign in to comment.