A Closed-Loop Bidirectional Brain-Machine Interface System For Freely Behaving Animals