I would say that you need to take a few steps back and answer a few fundamental questions:
- What does an IMU actually do?
- How does this apply to the robot?
- How can I use the relationship between the IMU and the robot to control the robot?
I’ll answer each to make it clear how your thought process should be when solving these kinds of problems instead of just seeking a copy and paste answer.
The IMU is simply a sensor that measures a change in rotation (usually in degrees) of a body it’s attached to. The geometry and shape of the body does not affect the net angular displacement the sensor measures (if the body is fully rigid).
This applies to the robot because the sensor is mounted rigidly to it. The mechanism (for lack of a better word) that allows the robot to turn is a differential-style chassis (tank drive). So by spinning the left side in one direction and the right side in the opposite direction, you can rotate the robot and it is then measured by the sensor.
So how can I create a relationship between the sensor and the mechanism I want to control?
Psuedocode:
if(!atAngle) //while the robot is not at the desired angle
robot -> spin
else
robot -> stop
So, with a basic control structure, you can use the sensor values and relate it to rotations of the wheels on the base. Implementing PID in this is essentially the pseudocode with a lot more math. But the basic premise is sort of this: while the robot is not at the target angle, spin towards angle. If you want more insight on the math then look here are all the resources you could need (and feel free to ask more questions):