collapse

Author Topic: PID Tuning with Excel  (Read 6877 times)

BaldwinK

  • Member
  • ****
  • B
  • Posts: 22
PID Tuning with Excel
« on: April 17, 2016, 04:49:07 AM »
Summary

Test Rig A is used to demonstrate MS Excel graphing of a PID response to a step input.  Construction detail is given with block schematic and Arduino code.  Comparing the similar needs of a balancing system with a servo system we end up with an interesting take on servo design.

PID Design

Brett Beauregard has a great blog explaining PID design.  Starting at the simplest form and building up to the code he wrote for the library he makes it all seem easy.  To win a prize, though, you still need to pick three lucky numbers.  Selecting the right constants for proportional, integral and differential gain seems to be a black art.

Tim Wescott’s acclaimed article on tuning is a good starting point.  Searching for more info will probably lead you to the Good Gain method or the Ziegler-Nichols method.  All explanations seem to infer knowledge of the graphical response to a step input.  At the hobby level, specialist test equipment is not really an option.

Disturbing a balanced system will cause it to respond to the change and recover over time.  So we just need to apply a step change at the input and measure the output at intervals.  For some time I struggled with a balancing system because I couldn’t see where to apply the step change and what to measure.

Using an MPU 6050 as a source of roll angle, the PID code notes the ‘error’ between this ‘input’ and the ‘setpoint’ (probably the vertical).  The PID ‘output’ is translated for the control electronics and the motor then moves something to correct the change.  The MPU sends the new rotation angle back to the input, completing the loop.

Now consider a servo system.  It will have all of the same parts in the same loop but the ‘input’ suddenly is obvious.  A simple change of perspective gives the answer as the ‘setpoint’.  Yes, a servo probably uses a potentiometer for angular measurement but the principle is the same.  We need to program a sudden jump in value to the setpoint and measure the angle over time.  Since the real world output of the system is fed back to the input we just measure the roll.  As it happens it the only positional measurement we can make.

Construction

Test Rig A.jpg
*Test Rig A.jpg (42.97 kB . 529x768 - viewed 2144 times)

The test rig holds aloft a small 12-volt motor geared 100:1 with its axis horizontal.  An arm carrying the GY-521 board can swing around like the hand of a clock.  A quad half bridge SN754410 is controlled by the Nano V3 which also loads DMP firmware and interprets the roll data via the i2c link. Power is a 3S lipo for the N20 motor and four AA rechargeables for the rest of the electronics.  If there is a failure in the coding and the motor is left running without control it will wrap the wires around the support.

The motor wires and the MPU cable must be run separately to avoid crosstalk leading to fifo errors.

Coding

The DMP is set to output roll data every 30mS using code previously posted.  The constancy of this time interval is important to the functioning of the PID.  Rotation is clamped to +/- 1.3 radians in order to avoid gimbal lock (90º ~ 1.57 radians).  The PID output is translated for motor control.  The floating-point value is tested for sign to get direction of rotation and scaled 0-255 for the PWM.  AnalogWrite is sufficient to effect changes in duty cycle but I used Oddbot’s chart to change the frequency.  I couldn’t live with any of the lower values so set it ultrasonic.

The Arduino serial monitor is very useful here as it can input float values for the constants with very little effort.  ‘p0.3 Enter’ will change the value for kp instantly so watch the motor response.  Brett’s coding is more sophisticated but the step test is going to really upset the system anyway.  ‘s-0.6 Enter’ will start the data collection routine with a step change of -0.6 radians.  The storage pointer rollPnt is set to 1 to begin the process; at any value after this the step change will be applied.

At each 30mS cycle the data is put into buffer if rollPnt is not zero.  Roll and system clock millis() are kept for 64 time periods.  If the DMP fifo is not serviced on time it could provide data next time before the 30mS is up.  I like to keep an eye on this but it is not essential to the test.  At the end of the data collection, printing to the screen can begin.  From this point the fifo content is suspect so it is reset and the motor control must also be turned off.

Excel

Monitor.JPG
*Monitor.JPG (46.13 kB . 135x563 - viewed 2085 times)

The Arduino screen output uses tab separators which Excel can handle automatically.  Highlight the screen values below the title line ‘Roll   mS’ and above the next ‘reset fifo’ message.  Copy (Ctrl C) and paste (Ctrl V) into cell A4 of the spreadsheet.  The graph will appear instantly with the PID constants showing below.  Change a parameter and run the test again.  The undo function will show the previous graph(s).  Conditional formatting can colour code values if desired.

Graph.JPG
*Graph.JPG (134.92 kB . 795x457 - viewed 2447 times)

The graph above is a compromise with this motor – reasonable response time but with overshoot and a subsequent drift.  The mechanical slop in the gear train accounts for nearly 3º of ‘dead zone’ but the graph suggests more than 5º.  In the code, LED pin 13 lights when the system is within +/- 0.05 radians of vertical.  The LED is always on somewhere within the slop angle when stopped.



The first part of the clip shows the process of testing the untuned PID response; it kicks to the right and oscillates, then returns to normal but never stops oscillating.  The second part shows the tuned motor stepping 0.2 radians in servo mode.  Note that the arm follows the same path even when the rig is turned.

Conclusion

The servo constructed is a very special case as it only works in the vertical plane.  No attempt has been made here to have the arm venture into the upper quadrants though it could be done.

The same process might be used to test a purchased hobby servo.  Simply fix the MPU carrier to the servo arm, step the servo and start data collection.

The graphing technique demonstrated should assist in tuning a PID but that still requires experience.  Note the possible extremes in scale between constants.  Testing the response in both directions will check symmetry.

*PID_Demo.ino

*Roll Graph.xls

*PID Demo Block Diagram.doc




BaldwinK

  • Member
  • ****
  • B
  • Posts: 22
Re: PID Tuning with Excel
« Reply #1 on: April 17, 2016, 05:05:30 AM »
Messed up again!  File mp4 didn't register so now on You Tube at
https://youtu.be/tWP7xYaCcQ0

 

* Search


* Recent Topics

The unnamed (yet) quatruped spider project by tinhead
[July 01, 2020, 04:22:11 PM]


"1984 Nixie Time" by 1 what
[May 08, 2020, 01:04:18 AM]


2D Side Scroller Cyberpunk themed by Killer Angel
[February 06, 2020, 06:39:40 AM]


A new wing design for model aircraft / drones by OddBot
[February 06, 2020, 04:42:06 AM]


SDR (Software Defined Radio) by Gareth
[February 02, 2020, 06:15:42 AM]


Circuit Math by ZeroMax
[January 31, 2020, 01:50:18 PM]


NanOMeter by Protowrxs
[January 01, 2020, 12:59:44 PM]


Investigating the VL53L0X Laser Rangefinder by erco
[December 30, 2019, 10:45:44 PM]


PS4 Single Handed Controller Deployed (part 7 of 7) by Gareth
[December 30, 2019, 09:52:29 AM]


"D" -Pad Workio just like Magic (Will Merlin stay or Go) (part 6 of 7) by Gareth
[December 30, 2019, 09:51:27 AM]


PS4 Joystick Digitals 4,5,6,7,10 - Analog's Lx,Ly,Rx,Ry Workio (part 5 of 7) by Gareth
[December 30, 2019, 09:50:37 AM]


Menu Workio ! (part 4 of 7) by Gareth
[December 30, 2019, 09:49:49 AM]


L1 trigger design Workio (Hori controller) (part 3 of 7) by Gareth
[December 30, 2019, 09:48:50 AM]


Hori aka PS4 Joystick Mappings (part 2 of 7) by Gareth
[December 30, 2019, 09:47:17 AM]


PS4 Single Left-Handed Controller (part 1 of 7) by Gareth
[December 30, 2019, 09:44:58 AM]

* Recent Posts

Re: The unnamed (yet) quatruped spider project by tinhead
[July 01, 2020, 04:22:11 PM]


Re: The unnamed (yet) quatruped spider project by jinx
[July 01, 2020, 04:06:19 PM]


Re: "1984 Nixie Time" by 1 what
[May 08, 2020, 01:04:18 AM]


Re: "1984 Nixie Time" by tomasp
[April 13, 2020, 06:03:28 PM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[February 06, 2020, 06:39:40 AM]


A new wing design for model aircraft / drones by OddBot
[February 06, 2020, 04:42:06 AM]


Re: "1984 Nixie Time" by Gareth
[February 02, 2020, 06:23:01 AM]


Re: SDR (Software Defined Radio) by Gareth
[February 02, 2020, 06:15:42 AM]


Re: SDR (Software Defined Radio) by ZeroMax
[January 31, 2020, 01:54:21 PM]


Re: "1984 Nixie Time" by ZeroMax
[January 31, 2020, 01:52:29 PM]


Circuit Math by ZeroMax
[January 31, 2020, 01:50:18 PM]


Re: 2D Side Scroller Cyberpunk themed by ZeroMax
[January 31, 2020, 01:45:33 PM]


NanOMeter by Protowrxs
[January 01, 2020, 12:59:44 PM]


Re: Investigating the VL53L0X Laser Rangefinder by erco
[December 30, 2019, 10:45:44 PM]


PS4 Single Handed Controller Deployed (part 7 of 7) by Gareth
[December 30, 2019, 09:52:29 AM]