10390 FRC Programming
      • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 物件宣告與物件導向 ## <font color="#00a0c8 "> 一、什麼是物件宣告Object Declaration?</font> 一種物件導向語言,物件的宣告分為「建立變數」與「初始化(建構)」兩個步驟: ```java // 語法格式 類別名稱 變數名稱 = new 類別名稱(建構子參數); ``` 舉例: ```java Joystick driverController = new Joystick(0); ``` --- ### 常見 FRC 物件宣告實例 1. 馬達控制器(TalonSRX / CANSparkMax) ```java WPI_TalonSRX shooterMotor = new WPI_TalonSRX(5); // 5 為 CAN ID (透過Phoenix Tuner X看) SparkMax intakeMotor = new SparkMax(6, MotorType.kBrushless); // brushless 需指定 / ID由 REV Hardware 看 ``` 2. 控制器(Joystick / XboxController) ```java Joystick driverStick = new Joystick(0); // 0 為 USB 順序(可透過driverstation查看) XboxController operator = new XboxController(1); ``` 3. 感測器(Encoder ) ```java Encoder wheelEncoder = new Encoder(0, 1); // 使用 DIO 通道 0 和 1 ``` 6. 常用儀表板輸出(SmartDashboard) ```java SmartDashboard.putNumber("Encoder Value", wheelEncoder.get()); ``` --- ### 宣告建議放置位置 變數宣告:通常放在 Robot 類別或 Subsystem 類別的 class 內部,方便共享。 初始化(new):可在 robotInit() 或 constructor 中執行。通常會在constructor中執行。 ```java public class Robot extends TimedRobot { private Joystick stick; private WPI_TalonSRX motor; @Override public void robotInit() { stick = new Joystick(0); motor = new WPI_TalonSRX(0); } } ``` --- ### 附註與技巧 - 如果是 PWM 裝置要對照 RoboRIO 上的 PWM 通道插入正確位置,Digital Input亦同 - CAN 裝置需事先用 Phoenix Tuner X / REV Hardware Client 設定好 CAN ID - Joystick 順序由電腦識別的 USB 順序決定(可用 Driver Station 確認) --- ### 延伸資源 WPILib Hardware API 教學: https://docs.wpilib.org/en/stable/docs/software/hardware-apis/index.html 控制器與輸入裝置(Joystick)教學: https://docs.wpilib.org/en/stable/docs/software/basic-programming/joystick.html --- ## <font color="#00a0c8"> 二、什麼是物件導向 Object-Oriented Programming(OOP)</font> 物件導向是一種程式設計方式,強調以「類別與物件」來建構程式結構,具備以下四大核心概念: | 概念 | 說明 | FRC 中的例子 | |----------|------|--------------| | 封裝 | 把資料與操作包在一起 | Subsystem(.java/.cpp) 管理單一模組 | | 繼承 | 子類別擁有父類別功能 | `TimedRobot` 是 `RobotBase` 的子類 | | 多型 | 同一函式有不同表現 | `motor.set()` 可以設 float 或 double | | 抽象 | 定義架構不定義實作 | `CommandBase` 中只定義 `execute()` 方法 | --- ## 基本結構:類別與物件 ```java // 類別定義 public class Shooter { private SparkMax shooterMotor; public Shooter(int canID) { shooterMotor = new SparkMax(canID); } public void shoot(double speed) { shooterMotor.set(speed); } } ``` ```java // 物件創建與使用 Shooter shooter = new Shooter(5); shooter.shoot(0.8); ``` --- ## FRC 中的 OOP 實作範例 #### 1. Subsystem 類別(定義、代表一個物件的模組) ``` java public class DriveSubsystem extends SubsystemBase { private TalonFX leftMotor = new TalonFX(0); private TalonFX rightMotor = new TalonFX(1); public void drive(double speed) { leftMotor.set(speed); rightMotor.set(speed); } } ``` #### 2. Command 類別(代表行動) ```java public class DriveCommand extends CommandBase { private final DriveSubsystem drive; private final Joystick controller; public DriveCommand(DriveSubsystem subsystem, Joystick joystick) { drive = subsystem; controller = joystick; addRequirements(drive); } @Override public void execute() { drive.drive(-controller.getRawAxis(1)); } @Override public void end(boolean interrupted) { drive.drive(0); } } ``` #### 3. RobotContainer 管理所有物件、整合 ```java public class RobotContainer { private final Joystick controller = new Joystick(0); private final DriveSubsystem driveSubsystem = new DriveSubsystem(controller); private final DriveCommand driveCommand = new DriveCommand(driveSubsystem); public RobotContainer() { driveSubsystem.setDefaultCommand(driveCommand); } } ``` --- ### 學習 - 初學者:從 Robot.java 與 Joystick 控制開始,理解 class 與 new 的用法。 - 中階者:學習 Command-based 架構,拆解子系統、命令、控制器三者結構。 - 進階者:嘗試加入PID、Parallel Command(同時執行多個命令)等進階架構。 --- #### 常見錯誤與提醒: - NullPointerException 多因為 robotInit() 沒有正確 new - USB 控制器號碼與 Driver Station 不符 - CAN ID 沒設定或線沒接好導致 motor 不動 - 當功能越來越多時,記得把 Subsystem、Command 拆成不同檔案,保持程式整潔 --- ### 官方資源與延伸閱讀 WPILib OOP 架構教學(Command-based) https://docs.wpilib.org/en/stable/docs/software/commandbased/index.html WPILib Java 教學首頁 https://docs.wpilib.org/en/stable/docs/software/basic-programming/index.html FRC Example Projects(完整物件導向) https://github.com/wpilibsuite/allwpilib/tree/main/wpilibNewCommands --- ### <font color="#f50"> ! 都看完了,我要回家 ! </font> ## [好](https://hackmd.io/ACI3uWclSO6rAX9mMNIxcQ)

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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