FRC_7130_4th
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `程式組教程` # Gyro # Equipments ## Software Language Program Language => JAVA ## Model and Brand Gyro => navX-MXP Introduce of gyro => [Link(New Page)](https://pdocs.kauailabs.com/navx-mxp/) ## DriveMode MecanumDerive and TankDrive ## Value Setting Normalmode Speed => (Both Side) x 0.5 Correction Angle Speed => (One Side) x 0.6 , (Another Side) x 0.0 ## Variable Name SpeedControllerGroup => LeftMotor, RightMotor Joystick => Joystick Gyro => Gyro Angle => CurrentAngle Wheels => ``` Head => LimeLight Side LeftFront <= ||________|| => RightFront | | | | | | | | |__|__|__| LeftRear <= || || => RightRear ``` # Official Function ## GetAngle We can use this to get the **cumulative** angle of gyro. Data Type => Double It will like this ```java= YourGyroName.getAngle(); ``` :::danger **NOTE:** Because this is **cumulative** , so we add some function to make sure its numerical value will between 0° to 360° ::: ## Reset We use this function to reset our gyro. Its code will like this ```java= YourGyroName.reset(); ``` ## Reset # Loop and Using ## Bottons ### Btn_Y If you press botton Y , the angle of gyro will be set to zero immediately. It's worth noting that , sometimes the botton might be insensitive , the solution of it is just press it more times. the code of botton Y => [Link(Current Page)](##Botton_Y) ### Btn_X We use botton X to correct robot angle to the angle we want(0°). When you want to use this function, you can **hold down** press botton X , and robot will turn automatically to the right direction ## Set Target Angle We have gyro be set to zero when robot is just enabled. If you want to reset the angle again aftre a drive you can press botton Y , which is on the joystick. # Problems and Solutions ## Declare When we try to declare the gyro we encountered some problems below. ### Install Library When we are searching for gyro's software , we has been unable to learn the reason why declare code is unavailable.Therefore, we suspect that is it because the library hasn't been installed. After we having this guess , we started to understand some library related information of the gyro. At the beginning, we try a lot of offline library , but all of it is failed. Then, we find that there have another method which is using **"Online Library"**.The link below is the gyro's(NavX) library , just copy it to VS code "Manage Vendor Libraries". ``` Online Library Link => https://www.kauailabs.com/dist/frc/2021/navx_frc.json ``` ### Import SPI We encountered a problem which is the red line under "SPI" , which is declare code , can't be eliminated. It will like this. ![](https://i.imgur.com/pi55WV7.png) The solution is import the word below to your code. And it should be eliminated. ``` import edu.wpi.first.wpilibj.SPI; ``` ## Programming ### Robot Correction After many attempts , we finally find the correct numerical value of robot correction loop. Also , we learned that the robot's variable is different from what we think. Correct loop code => [Link(Current Page)](#Robot_Correction) ### Current Angle Because the function which is given by *FRC* official is like this: ```java= Gyro.getAngle() ``` The descripition of *FRC* official: > Return the actual angle in degrees that the robot is currently facing. >The angle is based on the current accumulator value corrected by the oversampling rate, the gyro type and the A/D calibration values. The angle is continuous, that is it will continue from 360 to 361 degrees. This allows algorithms that wouldn't want to see a discontinuity in the gyro output as it sweeps past from 360 to 0 on the second time around. >The angle is expected to increase as the gyro turns clockwise when looked at from the top. It needs to follow NED axis conventions in order to work properly with dependent control loops. >This heading is based on integration of the returned rate from the gyro. By this , we know that the angle of the function is continuous. Therefore , we use some easy loop and a variable called "CurrentAngle" to deal with this problem. Correct loop code => [Link(Current Page)](#Current_angle) ## Movement ### Drift Despite we want to reduce the deviate to as small as possible , it still have some small amount of error. We guess it is because of the friction on the ground. ### Angle Although we hope the robot error can be as low as possible , it still have some error. The reason why we don't directly set the range in the corrected loop to the minimum is because it is very difficlt to ask robot to stop exactly on the angle which we want. What we can do is to minimize the range of loop. In the current test we know the smallest range is 0° ± 5 degrees. # Code ## Setting and declaration ## Botton_Y We write it in the part of **teleopPeriodic** to control continuously it by Joystick. ```java= @Override public void teleopPeriodic() { // Gyro Reset if( Joystick.getRawButton(Btn_Y) ){ Gyro.reset(); } } ``` ## Robot_Correction We write it in the part of **robotPeriodic** because we want it can continuous work. The idea of this loop is: If robot is stray from the right direction, which is 0° , we can use the botton X on the joystick to have it turn automatically to the right direction. Furthermore , we use positive and negative number to correct the robot. The reason why we write " CurrentAngle >= 0 + 5 " and "CurrentAngle >= 0 + 5" is because it will be easier for me to identify. ```java= @Override public void robotPeriodic() { //Turn Robot Angle Autonomously By Gyro if( Joystick.getRawButton( Btn_X ) ){ if( CurrentAngle >= 0 + 5 ){ // easier to identify LeftFront.set(-0.6); LeftRear.set(-0.6); RightFront.set(0.0); RightRear.set(0.0); } if( CurrentAngle <= 0 - 5 ){ // easier to identify LeftFront.set(0.6); LeftRear.set(0.6); RightFront.set(0.0); RightRear.set(0.0); } } } ``` ## Current_angle We write it in the part of **robotPeriodic** because we want it can continuous work. We write it in the part of **teleopInit** because we want to prove gyro will be set to zero when the robot just opened. ```java= @Override public void teleopInit() { Gyro.reset(); } ``` ```java= @Override public void robotPeriodic() { //Turn Robot Angle to Correct if( CurrentAngle >= 360 ) CurrentAngle -= 360; if( CurrentAngle <= -360 ) CurrentAngle += 360; } ``` ## Final Code ```java= package frc.robot; import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX; import com.kauailabs.navx.frc.AHRS; import edu.wpi.cscore.CvSink; import edu.wpi.cscore.CvSource; import edu.wpi.cscore.UsbCamera; import edu.wpi.first.cameraserver.CameraServer; import edu.wpi.first.networktables.NetworkTableEntry; import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.wpilibj.AnalogInput; import edu.wpi.first.wpilibj.Joystick; import edu.wpi.first.wpilibj.SPI; import edu.wpi.first.wpilibj.SpeedControllerGroup; import edu.wpi.first.wpilibj.TimedRobot; import edu.wpi.first.wpilibj.drive.DifferentialDrive; import edu.wpi.first.wpilibj.drive.MecanumDrive; import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; import edu.wpi.first.networktables.NetworkTable; public class Robot extends TimedRobot { // PWM channels public final int kLeftFrontChannel = 3; public final int kLeftRearChannel = 4; public final int kRightFrontChannel = 1; public final int kRightRearChannel = 2; public final int joystickPort = 0; public final int rightUltrasonicPort = 1; public final int rearUltrasonicPort = 0; // driver station ports public final int leftStick_X = 0; public final int leftStick_Y = 1; public final int rightStick_X = 4; public final int rightStick_Y = 5; public final int trigger_L = 2; public final int trigger_R = 3; public final int Btn_A = 1; public final int Btn_B = 2; public final int Btn_X = 3; public final int Btn_Y = 4; public final int Btn_LB = 5; public final int Btn_RB = 6; public final int Btn_LS = 9; public final int Btn_RS = 10; // Motors WPI_TalonSRX LeftFront = new WPI_TalonSRX(kLeftFrontChannel); WPI_TalonSRX LeftRear = new WPI_TalonSRX(kLeftRearChannel); WPI_TalonSRX RightFront = new WPI_TalonSRX(kRightFrontChannel); WPI_TalonSRX RightRear = new WPI_TalonSRX(kRightRearChannel); // SpeedControllerGroup and DifferentialDrive SpeedControllerGroup LeftMotor = new SpeedControllerGroup(LeftFront, LeftRear); SpeedControllerGroup RightMotor = new SpeedControllerGroup(RightFront, RightRear); DifferentialDrive drive = new DifferentialDrive(LeftMotor, RightMotor); // Mecanumderive public MecanumDrive m_Drive; // Joystick Joystick Joystick = new Joystick(joystickPort); // Gyro AHRS Gyro = new AHRS(SPI.Port.kMXP); // Variables (drivetrain) double xSpeed; double ySpeed; double zRotation; // Variables (gyro) double CurrentAngle; @Override public void robotInit() { // Drivetrain m_Drive = new MecanumDrive(LeftFront, LeftRear, RightFront, RightRear); // motors inverted RightFront.setInverted(true); RightRear.setInverted(true); LeftFront.setInverted(true); LeftRear.setInverted(true); } @Override public void robotPeriodic() { //Turn Robot Angle to Correct CurrentAngle = Gyro.getAngle(); if( CurrentAngle >= 360 ) CurrentAngle -= 360; if( CurrentAngle <= -360 ) CurrentAngle += 360; //Turn Robot Angle Autonomously By Gyro if( Joystick.getRawButton( Btn_X ) ){ if( CurrentAngle >= 0 + 5 ){ LeftFront.set(-0.6); LeftRear.set(0.0); RightFront.set(-0.6); RightRear.set(0.0); } if( CurrentAngle <= 0 - 5 ){ LeftFront.set(0.6); LeftRear.set(0.0); RightFront.set(0.6); RightRear.set(0.0); } } // Post To Smart Dashboard SmartDashboard.putNumber("CurrentAngle", CurrentAngle); SmartDashboard.putData("LeftFront", LeftFront); SmartDashboard.putData("LeftRear", LeftRear); SmartDashboard.putData("RightFront", RightFront); SmartDashboard.putData("RightRear", RightRear); SmartDashboard.putNumber("xSpeed", xSpeed); SmartDashboard.putNumber("ySpeed", ySpeed); SmartDashboard.putNumber("zRotation", zRotation); } @Override public void autonomousInit() {} @Override public void autonomousPeriodic() {} @Override public void teleopInit() { Gyro.reset(); } @Override public void teleopPeriodic() { //Turn Robot Angle to Correct CurrentAngle = Gyro.getAngle(); if (CurrentAngle >= 360) CurrentAngle -= 360; if(CurrentAngle <= -360) CurrentAngle += 360; // Gyro Reset if( Joystick.getRawButton(Btn_Y) ){ Gyro.reset(); } // Drivetrain ySpeed = Joystick.getRawAxis(leftStick_X) * 0.5; xSpeed = Joystick.getRawAxis(leftStick_Y) * 0.5; zRotation = -Joystick.getRawAxis(rightStick_X) * 0.5; // Mecanumderive m_Drive.driveCartesian(ySpeed, xSpeed, zRotation); } @Override public void disabledInit() {} @Override public void disabledPeriodic() {} @Override public void testInit() {} @Override public void testPeriodic() {} } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully