Post on 18-Jan-2016
Data Logging 4 PID Tuning
By Hugh Meyer
Red Alert Robotics FRC Team 1741
Center Grove High School
Greenwood Indiana
October 24, 2015
Compelling Reasons for Data Logging
Capture fast events Thoughtful evaluation process Verify repeatability
Compelling Reasons for Data Logging
Establish normal operating range Detect leading edge of failure before
disaster Verify design target
Different Approaches for Logging Data
Use built in logging feature of the driver station.
Located on the “Charts” tab Click “Launch Viewer” button for more
detail Windows on the left indicate the file and
location of the data files
Driver Station Charts
Charts tab give a continuous display of many useful attributes
If you click on the “Launch Viewer” button it opens the Log File Viewer
Log File Viewer
Different Approaches for Logging Data
The supplied Driver Station Log File Viewer is helpful, but you really need data that is specific to your robot to be helpful
I suggest you create your own logging system
You have several options
Different Approaches for Logging Data
Save the data on the driver station computer
Save the data on the RoboRIO Save the data on an external memory
card Save the data on an external computer
What you need for saving data on the RoboRIO
Code that reads the data
Writes the data to disk
Move data from RoboRIO to processing system
Code to open log file and write data
void Tshirt::WriteToLogFile(){
if (!logfile.is_open()) {logfile.open("logging.csv", ios_base::out);logfile << "SteerSetpoint,SteerActual,SteerOutput\n";
}
logfile << steer_control->GetSetpoint() << ',' << steer_encoder->GetDistance() << ',' << steer_control->Get() << "\n";
}
void Tshirt::DisabledPeriodic(){
if (logfile.is_open()) {logfile << flush; // Make sure any messages still
// in the buffer get written to "disk"logfile.close();
}}
void Tshirt::TeleopPeriodic(){...
WriteToLogFile();}
Moving Data from the RoboRIO
Use FTP to transfer the file back to the driver station after the match is concluded
We create a batch file that does this
This batch file is in the same folder destination where the data is to go
The batch file renames the file to include the date and time in the file name
Data Transfer Batch File
# ftpcmds.txt file
get logging.csvget config.txtbye
# GetLogging.bat file
cd C:\FIRSTrobotics\RA14_LogFileArchive\2014_HomeComingftp -A -s:ftpcmds.txt 10.17.44.2
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%cfor /f "tokens=1-2 delims=: " %%a in ('time /t') do set XTime=%%a.%%brename "config.txt" "config-%XDate%-%XTime%.txt"rename "logging.csv" "logging-%XDate%-%XTime%.csv"
Data Folder Example
Analyzing the Data
Import into a spread sheet program Create specialized evaluation program Moving data from capture device to
evaluation device Archiving data into a subversion
repository
The Data File an Example
SteerSetpoint,SteerActual,SteerOutput0,0,00.0866142,0,00.102362,0,0.04330710.11811,0,0.05118110.125984,0,0.05905510.133858,0,0.06299210.149606,0,0.06692910.165354,0,0.07480320.19685,0,0.08267720.228346,0,0.09842520.251969,0,0.1141730.291339,0.045,0.1047340.314961,0.115,0.08816930.346457,0.1,0.106230.377953,0.0975,0.1402260.401575,0.145,0.1282870.425197,0.2275,0.09884840.448819,0.26,0.08259840.503937,0.26,0.09440950.559055,0.255,0.1244690.598425,0.28,0.1592130.661417,0.44,0.1132090.692913,0.65,0.006958660.748031,0.7225,0.01276570.787402,0.745,0.02120080.818898,0.745,0.03694880.858268,0.745,0.05663390.889764,0.745,0.05663390.944882,0.745,0.0999410.984252,0.745,0.1196260.992126,0.75,0.1210631,0.765,0.11751,0.78,0.11
Open Data in OpenOffice Calc
Text Import to OpenOffice Calc
??? Questions ???
These are the steps to getting the data from the robot to a graph.
Are there any questions about this process before we move on?
PID Tuning Process
Need method to command a step response
Easy ability to change PID gain values
Step Response
One must be able to command an immediate change to a different value
This should be done in a very controlled and precise manner
Essentially the command to the system should be a few cycles of square wave
This can be as simple as a button for the user to press, or as complex as an automation sequence
Step Response
We usually just add a button for testing and have the user command the change
logging-Thu-09-18-09.12.csv
Easy Method to change PID gain values
Setup a configuration file on the RoboRIO
Include in that configuration file the 3 gain values
Develop a simple procedure to apply a changed configuration file to the system
Configuration Code
Config.h
Config.cpp
config.txt
putconfig.bat
ftpcmds.txt
Config File Initialization
void Tshirt::RobotInit()
{
//Start robot
cout << "Starting up..." << endl;
cout<<"Loading Config File..... "<<endl;
Config::LoadFromFile("config.txt");
cout<<"Done!"<<endl;
Using Config function to initialize PID values
cout << "Initializing PID control...";steer_control = new PIDController(
Config::GetSetting("steer_p", 1),Config::GetSetting("steer_i", 0),Config::GetSetting("steer_d", 0),steer_encoder,axle,Config::GetSetting("steer_period", 0.010 ));
steer_control->SetInputRange( - MAX_RANGE_OF_STEER, MAX_RANGE_OF_STEER); // in revolutions, just for testing
steer_control->SetOutputRange(-1, 1);steer_control->SetSetpoint(0);steer_control->Enable();
cout << "PID enabled!" << endl;
Reset code to reload new PID values
void Tshirt::ResetPID(){
Config::LoadFromFile("config.txt");steer_control->SetPID(
Config::GetSetting("steer_p", 1),Config::GetSetting("steer_i", 0),Config::GetSetting("steer_d", 0)
);steer_control->Reset();
cout << "PID:" << endl;cout << "P: " << steer_control->GetP() << endl;cout << "I: " << steer_control->GetI() << endl;cout << "D: " << steer_control->GetD() << endl;
}
Config File Example
# .d8888b. .d88888b. 888b 888 8888888888 8888888 .d8888b. #d88P Y88b d88P" "Y88b 8888b 888 888 888 d88P Y88b #888 888 888 888 88888b 888 888 888 888 888 #888 888 888 888Y88b 888 8888888 888 888 #888 888 888 888 Y88b888 888 888 888 88888 #888 888 888 888 888 Y88888 888 888 888 888 #Y88b d88P Y88b. .d88P 888 Y8888 888 888 Y88b d88P # "Y8888P" "Y88888P" 888 Y888 888 8888888 "Y8888P88
#8888888888 8888888 888 8888888888 #888 888 888 888 #888 888 888 888 #8888888 888 888 8888888 #888 888 888 888 #888 888 888 888 #888 888 888 888 #888 8888888 88888888 8888888888
# This is the config file
steer_p = 0.220steer_i = 0.005steer_d = 0.300
steer_period = 0.010
FTP Transfer System
# putconfig.bat
cd C:/WindRiver/workspace/Tshirtftp -A -s:ftpcmds.txt 10.17.44.2pause
# ftpcmds.txt
asciisend config.txtbye
Questions
We now have in place all we need to be able to easily change the PID gain variables
Are there any questions at this point?
PID Tuning Strategies
Iteration by experience
Ziegler-Nichols and other tuning strategies
Several are mentioned on Chief Delphi http://www.chiefdelphi.com/forums/portal.php
PID without a PhD by Tim Wescott
Iteration by Experience
After doing this for a while you will get to recognize different patterns and what will correct them
P will make slower oscillations
D will make fast high frequency oscillations
I helps reach the target
Not very scientific
Can take really long time to find a solution
Start with P Tuning ProcessFor small, low torque motors with little or no gearing, one procedure you can use to
get a good baseline tune is to probe it’s response to a disturbance.
To tune a PID use the following steps:
1. Set all gains to zero.
2. Increase the P gain until the response to a disturbance is a steady oscillation.
3. Increase the D gain until the oscillations go away (i.e. it’s critically damped).
4. Repeat steps 2 and 3 until increasing the D gain does not stop the oscillations.
5. Set P and D to the last stable values.
6. Increase the I gain until it brings you to the set point with the number of oscillations desired (normally zero but a quicker response can be had if you don’t mind a couple oscillations of overshoot)
What disturbance you use depends on the mechanism the control is attached to. Normally moving the mechanism by hand away from the set point and letting go is enough. If the oscillations grow bigger and bigger then you need to reduce the P gain.
If you set the D gain too high the system will begin to chatter (vibrate at a higher frequency than the P gain oscillations). If this happens, reduce the D gain until it stops.
Start with D Tuning Process
To tune a PID use the following steps:
1. Set all gains to 0.
2. Increase Kd until the system oscillates.
3. Reduce Kd by a factor of 2-4.
4. Set Kp to about 1% of Kd.
5. Increase Kp until oscillations start.
6. Decrease Kp by a factor of 2-4.
7. Set Ki to about 1% of Kp.
8. Increase Ki until oscillations start.
9. Decrease Ki by a factor of 2-4.
Ziegler-Nichols & Other Tuning Strategies
Set one of the variables to where the system just oscillates
Measure the time period of the oscillation
Calculate the PID values from some formula or look up table based on these measurements
Ziegler-Nichols
The Ziegler–Nichols tuning method is a heuristic method of tuning a PID controller. It was developed by John G. Ziegler and Nathaniel B. Nichols.
It is performed by setting the I (integral) and D (derivative) gains to zero. The "P" (proportional) gain, KP is then increased (from zero) until it reaches the ultimate gain KU , at which the output of the control loop oscillates with a constant amplitude.
KU and the oscillation period TU are used to set the P, I, and D gains depending on the type of controller used.
Use the table to determine the final gains.
Ziegler-Nichols
Ziegler-Nichols & Others – Links
1. Ziegler-Nichols Method http://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method
2. Tyreus-Luyben Tuning http://www.chem.mtu.edu/~tbco/cm416/zn.html
3. Comparison of PID Controller Tuning Methods by Mohammad Shahrokhi and Alireza Zomorrodi
Questions?
Are there any questions regarding tuning methods?
Steering System Example
Position control for an Ackermann steering mechanism
http://en.wikipedia.org/wiki/Ackermann_steering_geometry
Servo style motor control Joystick position translates to steer
angle
Ackermann Steering Example
Ackermann Steering Example
First Pass at Tuning
113
2537
4961
7385
97109
121133
145157
169181
193205
217229
241253
265277
289301
313325
337349
361373
385397
409421
433
-8
-6
-4
-2
0
2
4
6
8
SteerSetpoint
SteerActual
SteerOutput
P = .125I = .001D = .00125
Second Pass at Tuning
222
4262
82102
122142
162182
202222
242262
282302
322342
362382
402422
442462
482502
522542
562582
602622
642662
682702
722
-8
-6
-4
-2
0
2
4
6
8
SteerSetpoint
SteerActual
SteerOutput
P = .220I = .005D = .320
Third Pass at Tuning
224
4668
90112
134156
178200
222244
266288
310332
354376
398420
442464
486508
530552
574596
618640
662684
706728
750
-8
-6
-4
-2
0
2
4
6
8
SteerSetpoint
SteerActual
SteerOutput
P = .220I = .005D = .300
Launching Mechanism Example
Position control for a Leonardo Da Vinci cam mechanism
Reflective index sensor for homing each cycle
Schmitt trigger (7414) for optical sensor http://www.digikey.com/product-detail/en/SN7414N/296-33604-5-ND/1575159
http://www.ti.com/lit/ds/symlink/sn7414.pdf
De Vinci CamRed Alert Cam
De Vinci Cam
First Pass at Tuning
-2000
-1500
-1000
-500
0
500
1000
1500
2000
2500
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8
ShooterMotorLeftPositionSetpointShooterMotorPositionActualShooterMotorLeftDemand
# CAM PIDcam_p = .06cam_i = .003cam_d = .003
logging-Wed-02-26-07.37.csv
Next Pass at Tuning
-20
0
20
40
60
80
100
0 1 2 3 4 5 6 7
ShooterMotorLeftPos itionSetpoint
ShooterMotorPos itionActual
ShooterMotorLeftDemand
logging-Mon-02-24-08.13.csv
# CAM PIDcam_p = .75cam_i = 0cam_d = 0
Better But Still Oscillatinglogging-Mon-02-24-08.19.csv
-10
0
10
20
30
40
50
60
70
80
90
0 1 2 3 4 5 6 7 8
ShooterM otorLeftPos itionSetpoint
ShooterM otorPos itionActual
ShooterM otorLeftDem and
# CAM PIDcam_p = .1cam_i = 0cam_d = 0
Another Pass Tuning
-10
0
10
20
30
40
50
60
70
80
90
0 10 20 30 40 50
ShooterMotorLeftPos itionSetpoint
ShooterMotorPos itionActual
ShooterMotorLeftDemand
logging-Mon-02-24-08.22.csv
# CAM PIDcam_p = .01cam_i = 0cam_d = 0
Nearing Final Solutionlogging-Mon-02-10-10.44.csv
-20
0
20
40
60
80
100
120
1 224 447 670 893 1116 1339 1562 1785 2008 2231 2454 2677 2900 3123 3346
ShooterMotorLeftPos itionSetpointShooterMotorPos itionActualShooterMotorLeftDemand
# CAM PIDcam_p = 0.017cam_i = 0.0005cam_d = 0.00
Wind Up Detail
Launch Detail
Sequence of Several Launches
Speed Control for Frisbee Launcher Example
Velocity control for drive wheels
Frisbee Launcher
Shooter Testing - March 11, 2012
0
200
400
600
800
1000
1200
1400
1600
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Time - 50 Millisecond Steps
RP
M
-20
-10
0
10
20
30
40
50
60
70
80
Cu
rren
t &
Vo
ltag
e
BottomShooter RPM Target Bottom Shooter RPM Actual BottomShooter Voltage BottomShooter Current
Drive Base Example
Mecanum Drive
Drive Wheel PID Tuning
-6000
-4000
-2000
0
2000
4000
6000
1 78 155 232 309 386 463 540 617 694 771 848 925 1002 1079
Front Left Target
Front Left Actual
Front Left Voltage
M:\FirstRobotics\RA12_LogFileArchive\20120307_PIDTuning\Logging-Wed-03-07-07.52.csv
Drive Wheel PID Tuning
-6000
-4000
-2000
0
2000
4000
6000
1 83 165 247 329 411 493 575 657 739 821 903 985 1067 1149 1231 1313
Front Left Target
Front Left Actual
Front Left Voltage
M:\FirstRobotics\RA12_LogFileArchive\20120307_PIDTuning\Logging-Wed-03-07-08.43.csv
Conclusion
Log as much data as you can Build your code as modular as possible
so you can reuse it Implement a config file for your robot Save your config file with the log data
file
Questions
Any questions?
Contact Information
Find this presentation at http://hughmeyer.com/workshops
Hugh Meyer Phone: 317 786-9214 Email: hmeyer@redalert1741.org