I don't think here "I did what I had to do" the meaning is present/future, but it isn't "a past infinitive".
Right. Obviously, the meaning is
past obligation. Where 'have to' expresses present/future obligation or necessity, 'had to' expresses past obligation or necessity.
From these words I can conclude that the difference between sentence 3 and 4 is "I did what I should (usually) do" and "I did what I should have done (at that moment in the past)", is my understanding correct?
Basically, yes. However, I'd strongly advise you against analysing the semantics of uncontextualised sentences that include modal verbs. If you're going to make up your own examples, make sure you get the meaning clear in your own mind before doing so, and provide enough contextual information to make that meaning clear.