Rotary encoder - SergeGit/rc-tank-platform GitHub Wiki

TODO

  • Principle of operation Opto-coupler
  • Shave the print board and mount (DI2 can be used as Arduino pin)
  • Calibration point to digital input
  • Reference frame in degrees for turret rotation
  • Auto calibration

Links

https://www.electroschematics.com/arduino-optical-position-rotary-encoder/
https://forum.arduino.cc/t/74hc126d-buffer-ic-with-arduino-uno-3/116601/3
https://forum.arduino.cc/t/how-to-make-an-incremental-optical-encoder-with-2-phases-cheaply/889652/6

Motor rotation code Encoder example rpm calculation

Arduino help

Agilent optical encoders

Speed up encoder ISR

Turret rotation measurement

The rotation of the turret as it is being driven by the Turret Rotation motor is measured via an incremental encoder.

The encoder measurement on the turret makes use of a slotted Photo Interrupter module with an IR LED and a Photo Transistor mounted facing each other enclosed in plastic. The Photo Interrupter can detect when an object passes between these two components.

The wiring of the Photo Interrupter has the IR LED fed with +5V limited with 330Ω resistance. This results in an 8mA current. The transistor is set with a Pull-Up resistor. The 47KΩ pullup resistor (and +5V) can be replaced with the internal pullup on a digital pin. In this configuration the digital pin measures:

  • BLOCKED == HIGH
  • UNBLOCKED == LOW

The detection of the change of state on the digital pin is done using the interrupt service routine on an interrupt pin of the Arduino.

image

The encoder measures the teeth on the turret ring as it rotates. The direction of the rotation in combination with the number of teeth measured are used for calculating the degrees of rotation of the turret.

A calibration point pointing to turret 0° direction results in initial point for the incremental encoder measurement. The calibration can be done at the startup of the Tank or update the position automatically when passing the calibration position.

Code

Encoder_config.ino

/*******************************************************************************
   THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTY AND SUPPORT
   IS APPLICABLE TO THIS SOFTWARE IN ANY FORM. CYTRON TECHNOLOGIES SHALL NOT,
   IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL
   DAMAGES, FOR ANY REASON WHATSOEVER.
 ********************************************************************************
   DESCRIPTION:
   The code sets up the optical encoder measurement to measure the rotation position
   of the turret. The measurements is achieved via an interrupt pin.

   CONNECTIONS (Arduino nano v3 - Atmega328P, old bootloader):

   Arduino D2   - Optical encoder (interrupt)
   Arduino D8   - Reset
 *******************************************************************************/

// Define pins

void encoder_config() {
  // Initialization of Light_ctrl 

  // pinout encoder
  pinMode(encoderPin, INPUT_PULLUP);   // Set the LED pin as an output
  pinMode(clearPin, INPUT);    // Set the LED pin as an output

  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, doEncoder, RISING);
}

// Interrupt on A changing state
void doEncoder(){
  // Test transition
  // Adjust counter + if A leads B
  encoderPos += 1;
}

Test code

Encoder_Tets.ino

/*******************************************************************************
   THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTY AND SUPPORT
   IS APPLICABLE TO THIS SOFTWARE IN ANY FORM. CYTRON TECHNOLOGIES SHALL NOT,
   IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL
   DAMAGES, FOR ANY REASON WHATSOEVER.
 ********************************************************************************
   DESCRIPTION:
   This is the test code for testing the light control function.
   The function shows how to set up light control and use the functions for 
   turning separately the front and rear lights On/OFF. And the blinking function. 
   

   CONNECTIONS (Arduino nano v3 - Atmega328P, old bootloader):
 *******************************************************************************/
// Libraries

// Threads

// Pin assignment
const int encoderPin = 2;    // Encoder pin on D2 with interrupt
const int clearPin = 8;      // Reset of encoder position on D8


// Finite State Machine (FSM)

// Parameters
volatile unsigned int encoderPos = 0;
unsigned int lastReportedPos = 1;

// Keyboard assignment for light control

// Initialisation
void setup() {
  encoder_config();

  Serial.begin(9600);
}

void loop() {
  if (lastReportedPos != encoderPos) {
    Serial.print("Index:");
    Serial.print(encoderPos, DEC);
    Serial.println();
    lastReportedPos = encoderPos;
  }
  if (digitalRead(clearPin) == HIGH) {
    encoderPos = 0;
  }  
}