# Drivetrain Simulation ###### tags: `程式組教程` ## 目的 - 了解 WPILib 框架後的基本原理 - 使用機器人上的物理參數模擬實際狀況 - 模擬機器人在特定電壓下將如何移動 - 使用模擬 GUI 可視化虛擬場地上的機器人運動 > 1. Understand the basic underlying concepts behind the WPILib simulation framework. > 2. Create a drivetrain simulation model using your robot’s physical parameters. > 3. Use the simulation model to predict how your real robot will move given specific voltage inputs. > 4. Tune feedback constants and squash common bugs (e.g. motor inversion) before having access to physical hardware. > 5. Use the Simulation GUI to visualize robot movement on a virtual field. 以上是廢話 真正的目的如下: - 確保馬達或 encorder 的反轉正確 - 驗證路徑跟踪的準確性 - 一眼看出機器人在場上的實際軌跡 > With the simulation framework, you can verify not only basic functionality, like making sure that the inversions on motors and encoders are correct, but also advanced capabilities such as verifying accuracy of path following. ## 模擬 Encoder - 平常的 Encoder 宣告是 `Encoder`,如果要在 simulation 呈現的話就改寫成 `EncoderSim` 即可 - 馬達內建的 encoder 無法使用這個 ex : falcon > It is not possible to simulate encoders that are directly connected to CAN motor controllers using WPILib classes. ```java= // These represent our regular encoder objects, which we would // create to use on a real robot. private Encoder m_leftEncoder = new Encoder(0, 1); private Encoder m_rightEncoder = new Encoder(2, 3); // These are our EncoderSim objects, which we will only use in // simulation. However, you do not need to comment out these // declarations when you are deploying code to the roboRIO. private EncoderSim m_leftEncoderSim = new EncoderSim(m_leftEncoder); private EncoderSim m_rightEncoderSim = new EncoderSim(m_rightEncoder); ``` ## 模擬 Gyro - 跟 encoder 一樣都是加上 `Sim` 就好了 - Pigeon IMU 和 NavX 的 Gyro 無法使用 > It is not possible to simulate certain vendor gyros (i.e. Pigeon IMU and NavX) using WPILib classes. ```java= // Create our gyro object like we would on a real robot. private AnalogGyro m_gyro = new AnalogGyro(1); // Create the simulated gyro object, used for setting the gyro // angle. Like EncoderSim, this does not need to be commented out // when deploying code to the roboRIO. private AnalogGyroSim m_gyroSim = new AnalogGyroSim(m_gyro); ``` ## 結論 1. 我們這次比賽的 DriveTrain motors, encoder and gyro 都不能函式可以用在 simulation 2. 我放棄看這個了,看到這邊覺得很幹 3. ==以後設計和機械組在購買材料時程式組也看一下,不然就是有需求就要提出來跟購買材料的人講,不要到時候沒有在那邊靠北== 4. 後面還有很多篇請以後的人自己看完 ## 文件 - [Drivetrain Simulation Tutorial](https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/drivesim-tutorial/index.html)