Track Rotation
Port 2 of the Behavior board can read a quadrature encoder — for instance from a running wheel — instead of a nose poke. The counter is enabled with the EnableEncoders register, its reading mode is selected with EncoderMode, and its value is streamed in the Encoder field of the 1 kHz AnalogData events. This article covers enabling, visualizing, and resetting the encoder.
The complete workflow is shown below:
Enable the Encoder
- Insert a
KeyDownoperator and set theFilterproperty toA. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectEncoderModePayload.EncoderMode- SelectPositionto stream the accumulated count, orDisplacementto stream the change since the previous sample.
- Insert a
MulticastSubjectoperator namedBehavior Commands. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectEnableEncodersPayload.EnableEncoders- SelectEncoderPort2.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow and press A — the quadrature counter starts and its value is reported in every AnalogData event.
Warning
While the encoder is enabled, Port 2 repurposes its infrared and DIO lines as the quadrature inputs: poke events from Port 2 are suspended until the encoder is disabled.
Visualize Encoder Data
- Insert a
SubscribeSubjectoperator namedBehavior Events. - Insert a
FilterMessageTypeoperator and configure theMessageTypeproperty toEvent. - Insert a
Parseoperator and configure theRegisterproperty toAnalogData. - Insert a
MemberSelectoroperator and select theEncoderfield. - Insert a
VisualizerWindowoperator. This will automatically open a window displaying the encoder count when the workflow starts.
Run the workflow, press A to enable the encoder, and turn the encoder shaft — the visualizer plots the count going up in one direction and down in the other.
Reset the Encoder
The EncoderReset register sets the counter of the selected encoders back to zero:
- Insert a
KeyDownoperator and set theFilterproperty toS. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectEncoderResetPayload.EncoderReset- SelectEncoderPort2.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow and press S — the Encoder value in the analog stream returns to zero.
Last updated with:
Hardware v2.1
Firmware v3.3
Bonsai Package v0.2