Man tells court he beat up police officer thinking he was a robber