I understand if people don't want to help with this because I know there are a LOT of posts on this subject, but I've been reading examples all day and for some reason it's just not clicking. That being said, any and all help is greatly appreciated.
I have a robot with 2 motors being driven by an l298d. The front wheels are casters that go where the back wheels point them. If I give power to the back right wheel, it turns left and vice versa. I want to implement PID to make it go straight using the initial yaw reading (obtained after 15 seconds of the IMU - a GY-521 - running) as my desired heading (setpoint). The input of the PID needs to be the current yaw reading, and the output needs to be to the appropriate motor.
I found what should be excellent forum posts like this one. Or from another site such as here. I genuinely understand the concept of PID, but how to implement it programming wise is just completely going over my head as of now.
For a bit of context, here is what I am working with at the moment.
(code added as attachment due to max character limit - including attempt to define setpoint at end of setup)
Almost all of that was taken from a sketch called MPU6050_DMP6 by Jeff Rowberg. Pretty much the only stuff I added was there at the end under the NEW section. And that part does what I expect it to. The first thing I'm really struggling with is how to get a "snapshot" of the yaw reading to then use as the setpoint. If I do for example:
double setpoint
setpoint = ypr[0] * 180/M_PI;
, then that would be constantly changing my setpoint to whatever the current reading is, correct (and that is actually what I want my input to be)? Furthermore, where exactly to define the setpoint is confusing me. The best attempt I have is adding this to the end of the setup function (included in attached code), but it just returns 180.00 every time!
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
double Setpoint;
delay(15000);
Setpoint=ypr[0] * 180/M_PI;
Serial.print(Setpoint);
As you can tell, I'm pretty lost at the moment, and any help at all is greatly appreciated.
DMP6_Reduced_motorcheck.ino (7.93 KB)