Table of Contents

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:

Track Rotation

Enable the Encoder

Enable the Encoder

  • Insert a KeyDown operator and set the Filter property to A.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select EncoderModePayload.
    • EncoderMode - Select Position to stream the accumulated count, or Displacement to stream the change since the previous sample.
  • Insert a MulticastSubject operator named Behavior Commands.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select EnableEncodersPayload.
    • EnableEncoders - Select EncoderPort2.
  • Insert a MulticastSubject operator named Behavior 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

Visualize Encoder Data

  • Insert a SubscribeSubject operator named Behavior Events.
  • Insert a FilterMessageType operator and configure the MessageType property to Event.
  • Insert a Parse operator and configure the Register property to AnalogData.
  • Insert a MemberSelector operator and select the Encoder field.
  • Insert a VisualizerWindow operator. 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:

Reset the Encoder

  • Insert a KeyDown operator and set the Filter property to S.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select EncoderResetPayload.
    • EncoderReset - Select EncoderPort2.
  • Insert a MulticastSubject operator named Behavior 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