I know wikipedia is commonly frowned upon as a source here, but they have a pretty good list here .
As it says, it can range from 2% to 20% of casualties.
Like you said, it is often caused by the fog of war and miscalculations of artillery. There are no cases of pre gunpowder era incidents in this list. In a battle, melee weapons kill a lot slower than guns. You would often have a couple blow exchanged before you actually kill somebody, compared to a few bullets. You can also more easily distinguish friend and foe as enemies armies were composed in mass formations. Archers only really fired before fights, and would never fire on an active skirmish as the risk of hitting friendly would be too high.
As for cavalry trampling infantry, they mainly were in highly organised pairs. For example, in Alex the Great's armies, you would have the heavy infantry in phalanx fighting the main battle, the companion heavy cavalry would attack the flanks. There was never really men in their way. The light cavalry were at the far tips and stopped others from flanking, so they were not near other parts of the army. The light infantry would serve as guard for the flanks. Here is a picture of formation in Alexander's army
As I described, rarely would anybody be in a spot where they would be in risk of hitting a friendly, and even than, given the chance to battle, would quickly realize through similar armor that they were friendlies.