Just like the militaries of Egypt, Pakistan, Thailand, Burma etc., Turkish military had been the fundamental tenet of the establishment that wanted to create a society that were Sunni (No Alawites/Shia), secular (No conservatives) and Turk (no Kurds or other nationalities).
Every time there was a threat to the Turkish state, either through communism or Islamism, the military acted. It is in the army’s DNA to “protect” the nation as a Sunni, secular and Turkish entity. Those genes still exist in the military.
Every time things “went wrong”, the military contemplated the possibility of overthrowing the government. In the mid-1990s (against Islamist govt), in 2003 (to protest Annan’s Cyprus plan), in 2007 (against Erdogan) and in 2016 (against Erdogan).
The Turkish military was so powerful until 2007 that any type of anti-government activity in the military took place with complete impunity. Soldiers plotted and imposed complete domination over media, judiciary and bureaucracy.
Erdogan and his close friends (Gul, Arinc, Sener) knew that everytime they are elected to the government, the powerful military would topple them down. They decided (in 2003) to push Turkey to join the EU so that Brussels can limit the military’s role in politics.