###### tags: `程式組工筆` # 20220725 ~ 20220731 # 20220728 # Aslan ## Work Progress * About PID * Complete Learning Theory * Complete Learning How to Practical Theory of Programming ## Progress Briefly ### PID After the learning on PID this week, I've been able to say that I understand it at least over 80%. Basically, except that I haven't tried adjusting the parameters and I'm still a little unfamiliar with practice on it, I've learned almost everything. Below I will briefly describe : 1. What is PID ? 2. Why we need PID ? 3. What the PID doing ? 4. How PID work ? #### 1. What is PID ? PID , <span style="color: red">**P**</span>roportional <span style="color: red">**I**</span>ntegral <span style="color: red">**D**</span>erivative Control, which is a control mode which consists of Proportional Term, Integral Term, and Derivative Term. The results of robot execution can be adjusted by adjusting the **K~P~**, **K~I~**, and **K~D~**. For some applications, only some units of the PID controller are required, and the parameters of the units that are not required can be set to zero. Thus a PID controller can become a **PI**, **PD**, **P** or **I** controller. :::spoiler Block Diagram of a PID Controller ![](https://i.imgur.com/0EyDVPG.png) ::: </br> **Symbols** 1. $Setpoints$ - Target Value 2. $e(t)$ - The Error 3. $de(t) / dt$ - Instantaneous Rate 4. $Output$ - State Value 5. $P$ - The Present Error 6. $I$ - The Accumulation of Past Errors 7. $D$ - The Error Rate of Change The places where PID is more commonly used : 1. Aoto Mode 2. Shooter in Teleop Mode #### 2. Why we need PID ? #### 3. What the PID doing ? #### 4. How PID work ? ## Summarize ### https://zh.wikipedia.org/zh-tw/PID%E6%8E%A7%E5%88%B6%E5%99%A8 https://www.ni.com/zh-tw/innovations/white-papers/06/pid-theory-explained.html https://hackmd.io/@CynthiaChuang/Basic-LaTeX-Commands https://hackmd.io/@sysprog/B1RwlM85Z#HackMD-%E2%80%93-LaTeX-%E8%AA%9E%E6%B3%95%E8%88%87%E7%A4%BA%E7%AF%84 # 倪睿宏 Well, today's lesson is really hard. I didn't do anything today and that's pretty bad :cry:. To solve this problem, I will review the videos about PID this weekend. Though I understand the theory of PID, to implement it is a difficult thing to me. (0728) These days I watched "FRC 0 to autonomous" series videos and learned step by step. I realized that the things is not that complicated. The only problem is lack of practice :joy:(0731) :::spoiler ```java= // Copyright (c) FIRST and other WPILib contributors. // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. package frc.robot; import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX; import com.kauailabs.navx.frc.AHRS; import edu.wpi.first.math.kinematics.MecanumDriveKinematics; import edu.wpi.first.math.kinematics.MecanumDriveOdometry; import edu.wpi.first.math.kinematics.MecanumDriveWheelSpeeds; import edu.wpi.first.wpilibj.Joystick; import edu.wpi.first.wpilibj.SPI; import edu.wpi.first.wpilibj.TimedRobot; import edu.wpi.first.wpilibj.Timer; import edu.wpi.first.wpilibj.drive.MecanumDrive; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; /** * The VM is configured to automatically run this class, and to call the functions corresponding to * each mode, as described in the TimedRobot documentation. If you change the name of this class or * the package after creating this project, you must also update the build.gradle file in the * project. */ public class Robot extends TimedRobot { Joystick js1 = new Joystick(0); // PWM Channels public final int kLeftFrontChannel = 13; public final int kLeftRearChannel = 14; public final int kRightFrontChannel = 11; public final int kRightRearChannel = 12; public final int joystickPort = 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_TalonFX LF = new WPI_TalonFX(1); WPI_TalonFX LR = new WPI_TalonFX(2); WPI_TalonFX RF = new WPI_TalonFX(3); WPI_TalonFX RR = new WPI_TalonFX(4); MecanumDrive mDrive = new MecanumDrive(LF, LR, RF, RR); AHRS m_gyro = new AHRS(SPI.Port.kMXP); MecanumDriveKinematics kinematics = null; // Not understand, remember to fill it. MecanumDriveOdometry odometry = new MecanumDriveOdometry(kinematics, m_gyro.getRotation2d()); // Encoders // distancePerPulse = gearRatio * wheelPerimeter / EncoderCPR // distance = motor.getSelectedSensorPosition() * distancePerPulse // velocity = motor.getSelectedSensorVelocity() * distancePerPulse final double kEncoderCPR = 2048; final double kDistancePerPulse = 0.13; double leftDistance = LF.getSelectedSensorPosition(); double rightDistance = RF.getSelectedSensorPosition(); double currentPosition = (leftDistance + rightDistance) / 2; // Motor speeds double speedFL = LF.getSelectedSensorVelocity() * kDistancePerPulse; double speedFR = RF.getSelectedSensorVelocity() * kDistancePerPulse; double speedRL = LR.getSelectedSensorVelocity() * kDistancePerPulse; double speedRR = RR.getSelectedSensorVelocity() * kDistancePerPulse; MecanumDriveWheelSpeeds wheelSpeeds = new MecanumDriveWheelSpeeds(speedFL, speedFR, speedRL, speedRR); // Timers double elapsedTime; double startTime; double currentTime; // PID double kP = 0.5; double targetPosition; double error; double output; double currentX = odometry.getPoseMeters().getX(); /** * This function is run when the robot is first started up and should be used for any * initialization code. */ @Override public void robotInit() { // invert motors and encoders LF.setInverted(true); LR.setInverted(true); LF.setSensorPhase(true); LR.setSensorPhase(true); // reset encoders resetEncoders(); // set motors' deadband mDrive.setDeadband(0.05); } @Override public void robotPeriodic() { // make odometry updates periodic odometry.update(m_gyro.getRotation2d(), wheelSpeeds); } @Override public void autonomousInit() { // reset encoders again(for sure) resetEncoders(); // set startTime startTime = Timer.getFPGATimestamp(); } /** This function is called periodically during autonomous. */ @Override public void autonomousPeriodic() { // get elapsed time currentTime = Timer.getFPGATimestamp(); elapsedTime = currentTime - startTime; // set move distance targetPosition = 10; error = targetPosition - currentX; output = kP * error; mDrive.driveCartesian(output, 0, 0); } /** This function is called once when teleop is enabled. */ @Override public void teleopInit() {} /** This function is called periodically during operator control. */ @Override public void teleopPeriodic() { // mecanum drive double ySpeed = js1.getRawAxis(leftStick_Y) * 0.6; double xSpeed = js1.getRawAxis(leftStick_X) * 0.6; double zRotation = js1.getRawAxis(rightStick_X) * 0.4; mDrive.driveCartesian(ySpeed, xSpeed, zRotation); } /** This function is called once when the robot is disabled. */ @Override public void disabledInit() {} /** This function is called periodically when disabled. */ @Override public void disabledPeriodic() {} /** This function is called once when test mode is enabled. */ @Override public void testInit() {} /** This function is called periodically during test mode. */ @Override public void testPeriodic() {} /** This function is called once when the robot is first started up. */ @Override public void simulationInit() {} /** This function is called periodically whilst in simulation. */ @Override public void simulationPeriodic() {} // reset encoders to zero public void resetEncoders() { // it will report an error if timeout(10s) LF.setSelectedSensorPosition(0, 0, 10); LR.setSelectedSensorPosition(0, 0, 10); RF.setSelectedSensorPosition(0, 0, 10); RR.setSelectedSensorPosition(0, 0, 10); } } ``` ::: # 魏旭霆 The same problem happened on me again,but I think this time is better. The problem is that I can understand the rogic clearly,but I don't know how to do in the programing.After practicing a lot and watching the video for many times,I can use the PID much handy. I still can't understand the "odometry",even though I had listen for many times and have copied the promram. ``` AHRS m_gyro = nem AHRS(SPI.Port.KMXP) DifferentialDriveOdometry odometry = new DifferentialDriveOdometry(m_gyro.getrotation2d()) public void robotPeriodic() { odometry.update(m_gyro.getrotation2d(),LeftFront.getselectedsensorposition()*distandistanceperpulis,RightFront.getselectedsensorposition()*distandistanceperpulis) } ``` # 江宥伶 We learnt about PID, and it is such a difficult thing for me. On Thursday, I've tried to type the code and many things got an error🥲,so I'm going to watch the video about PID by Aaron again.Oh by the way, this is the first time we stayed after school with FRC, it's fun and I hope I can learn more things in the future. # 粘家榆 I don't know why I forgot "P" and "I" after I learned "D" last Saturday. On Thursday, I watched the video again, and I can remember it really well. # 黃湲宸 During this week, we learnt about "PID", It is the first time I met this, that is really difficult for me. On Thursday, we have tried to type the code by ourselves, but it's so hard, so I type the code and watched the video about PID that Aaron taught, and I had successfully type a little bit, but there's still a lot waiting for me, so I've worked more hard. # 陳宇浩 This class we learned about PID and the Preliminary marcon drive. about the PID the D,I can't not wuderstand very well,but P and D is OK. When I am learning the macon drive, I can't not under stand it. Because it use the high school physics call vector and Trigonometric functions, that is so diffcult for me ,hope in future I can understand this. and this week I watch thw video angain to under stand PID, and some of the name I cannot understand it mean,but most I can(expect D). # 施暄庭 We learnt about PID during the class, and tried to write a code that can make the robot move exactly 3 meter long. Though in the end I'm only able to let the robot move about 2.8 meter long, I felt that I am getting more familier with PID. # 鄭有倫 The course of this week was about"PID",a pragramming about how to operate a robot.It was my first time learnt about pragramming,and I thought that was really hard to understand those difficult code,word,and number.I will try my best to fill up the courses I had missed,I also hope I can know how to write code better. # 謝亞諺 ### WORK PROGRESS 1. Try to make PID concept more clearly by Aaron's video 2. Try to write limelight aiming by CMD 3. Keep going on website coding ### Progress circumstance * About PID 1. Need some practice to make sure i really learn it * About Limelight aiming 1. I hade write Limelight aiming in timed robot long time ago , but haven't try if it will work 2. After leaning PID , i think need some adjust in code * About website coding 1. Start coding this for a long time . 2. But meet some obstacle now 3. Code on github : [AAAA](https://github.com/FUNDAI/website-/tree/beta) # 吳玠廷 ### WORK PROGRESS 1. learn PID by watching video on the internet 2. complete code of the PID 3. try to make the conception of the code about wets clealy ### PROGRESS CIRCUMSTANCE * about code of the PID 1. almost finished but I met a problem while writing codes,the libriary of the gyro can't drploy,soon after I found out that I open the wrong file. * about the website code 1. i ask arron some questions # 王敬銜 The distinguishing feature of the PID controller is the ability to use the three control terms of proportional, integral and derivative influence on the controller output to apply accurate and optimal control. The block diagram below shows the principles of how these terms are generated and applied. It shows a PID controller, which continuously calculates an error value <b>e(t)</b> as the difference between a desired setpoint <b>SP=r(t)</b> and a measured process variable <b>PV=y(t):e(t)=r(t)-y(t)</b>, and applies a correction based on proportional, integral, and derivative terms.(Wiki) ![](https://upload.wikimedia.org/wikipedia/commons/4/43/PID_en.svg) Last week we tried make the robot move exactly 3 meters by using PID...but we ended up at 2.8 meters (lol). - corrections KP->KI->KD(X) KP->KD->KI(O) resources: [PID Auto-line (Part 1)](https://www.youtube.com/watch?v=jIKBWO7ps0w) [PID Auto-line (Part 2)](https://www.youtube.com/watch?v=Z24fSBVJeGs) # 彭柏鈞 My mom got Covid+I got Covid+My classmate got Covid I couldn't come:sob: