![]() |
Gyro PID User drive - How we did it
Thought I would share how we are using the Gyro and a PID routine to help drive our robot.
Code:
#include <stdio.h> We are using a single joystick to drive our robot. The Y axis controls the forward or reverse speed. The X axis sets the "desired" heading. The gyro and PID routines are used to turn the robot from the "current" heading to the "desired" heading. Some significant routines: normal_drive() - this is the entry point and would be called from user_routines.c auto_trim() - this function reads the current values of the joysticks, subtracts them from 127 and uses this as the "center" value. It is called once when the user drive code starts. So no matter where the trims are when the robot starts, the motors start at "off". We were constantly fiddling with the trims to get the robot to not move when turned on. This adjustment eliminates this issue. adjust_speed() - this function takes the "desired" speed and the last_speed output to the motors and "ramps" the speed up and down rather than allowing instant changes. This helps to save the transmission from sudden direction changes. new_drive() - this routine reads the X and Y axis from the joystick and sets the forward/backward speed based on the Y axis and modifies the "desired" heading based on the X axis reading. PID_adjust() - this is where the magic happens. We are using a slightly non-standard PID calculation from Larry Barello (link in the code comments above). We are actually only using the P & D elements as this was sufficient for us. The additions for the I element are in the code, but commented out. This routine calculates the error between the "desired" heading and the current heading read from the gyro and calculates the value to be sent out to the motors. Note This routine has not been optimized for size or speed. We do use floating point variables and calculations, but once you determine the constants to use for the Kx constants it would be fairly easy to modify to use integer variables and calculations. I hope this of interest and welcome any feedback. Adam Bryant Programming Mentor Team 1583 Ridge View Academy Rambotics |
Re: Gyro PID User drive - How we did it
The algorithm and implementation look great. There's a bit of confusion in the comments, where it misrepresents the equation as being based only on the error and not on the derivative or integral, but the code does what I'd expect from a full PID controller. (Or it would, if you hadn't commented out the line doing the I accumulation. You should probably say something explicit about having removed it, in order not to confuse anyone.)
One other thing is completely unrelated to what the code is doing, and likely won't cause problems, but still ought to be corrected as soon as possible. You're treating 255 as the highest possible pwm value. In the IFI system, the maximum should instead be 254. Where you "invert" the right side output value, you similarly need to subtract from 254 instead of 255 -- that way, the 127 "neutral" value remains as it is. |
Re: Gyro PID User drive - How we did it
Looks great!
Hey Alan, is 254 really the max in the IFI setup? I always thought 255 was the max... :confused: I think I incorrectly corrected a few people if that is the case. |
Re: Gyro PID User drive - How we did it
I would be interested in a photo or description of your drive setup, so I can make better sense of the code.
Nontheless, I very much like your implementation - looks nice and clean, and hope to one day be able to make use of it as a teaching tool. Once I really understand it, that is...:o Don |
Re: Gyro PID User drive - How we did it
Quote:
Quote:
|
Re: Gyro PID User drive - How we did it
one way you could improve your code is remove the floats since they are very expensive to the processor and replace them with a numerator and denominator. Then when doing your math you do everything for that portion and divide last.
ex. char Kp = 5; char kpDiv = 100; pTerm = (Kp * p_error) / KpDiv; You will lose the precision of having a floating point value in the end, but with PWM values I do not think that matters anyways also I am wondering why your errors are floats? |
Re: Gyro PID User drive - How we did it
Alan,
I did sort of say we weren't using the I component by saying we were using P&D, but should have been a little more clear. I took a pass through the comments, but as usual missed a few that no longer matched up with the code. Odd that you have had issues with 255, we have always used it without issues. Don, The drive setup is a typical setup with one motor on each side (facing opposite directions) that power the drive wheels on that side. As the motors face opposite directions we need to do the inversion for (in our case) the right side as shown in the bottom of the PID_adjust() routine. // Invert the right output and put them to the wheels OUT_PWM_LM = left_out; OUT_PWM_RM = 255 - right_out; I definitely urge you to play with the gyro and this code and the other available examples for driving the robot. We hadn't used a gyro in previous years because I didn't understand them or PID routines, but dug into it this year and did some reading and the result has been great. Render, As I mentioned in the original post the code was not optimized for size or speed and we will get rid of the floats in future revisions. The errors were made floats so it didn't have to add code to convert from an integer to floating point before it made the calculations. Thanks all for the feedback! Adam |
Re: Gyro PID User drive - How we did it
your method is very neat indeed, but I still like my little PID engine better
output = pid_control(&part, error); |
Re: Gyro PID User drive - How we did it
We did something cool with our PID stuff this year. We wanted to get an OOP type feel, but we're forced to use the next best thing, structures and private functions.
It pretty much consisted of making a PID structure which contained everything we needed to know about the system (gains, integral bounds, etc.). We would make a new instance of that structure, and pass a pointer of that struct into different functions to set the gains and compute the output values. I believe this is what Uberbots did. |
Re: Gyro PID User drive - How we did it
Uberbots, Tom,
We only needed the PID for our drive system so we didn't go to the effort of making it generic and passing in all the relevant parameters (which is a VERY slick way to do it). I would love to see the code you used so we can learn from it. Have you posted it in another thread somewhere? Thanks |
Re: Gyro PID User drive - How we did it
Quote:
|
Re: Gyro PID User drive - How we did it
Quote:
Quote:
Quote:
|
Re: Gyro PID User drive - How we did it
Tom,
I saw your post of your PID code, thanks! Reviewing this with my programming students will be a great learning exercise as we haven't covered structures yet. Alan, Thanks for the info on the max value of 254. Looks like we need to go through our code and make some changes. |
Re: Gyro PID User drive - How we did it
Quote:
|
Re: Gyro PID User drive - How we did it
Quote:
|
Re: Gyro PID User drive - How we did it
Quote:
The difference in output between 255 and 254 is not going to be noticeable, and Victors have a small center deadband, so both 127 and 128 are going to give you a "full off" from the Victor, so it really doesn't matter which way you do it. Just don't try to use 256 as your "full on" value. :ahh: |
Re: Gyro PID User drive - How we did it
Quote:
|
Re: Gyro PID User drive - How we did it
Quote:
|
| All times are GMT -5. The time now is 00:31. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi