Go Down

Topic: Simulate encoder output for stepper motor (Read 233 times)previous topic - next topic

chuckbarry

Nov 15, 2018, 02:25 am
Hi All,

First off, I'm a complete amateur, and know nothing about this. Got thrown into this project against my will...

anyways, I'm trying to simulate an encoder output for a stepper motor that doesn't come with an encoder. We're just going to trust that the motor spins as it should, but I need the encoder output to run another system as the motor spins.

This is the code i have running my motor. slow spinning, and gives a pause for switching parts the motor is spinning one revolution around.

Looking to add a quadrature encoder output or 5V TTL as i've been told i need. This could come off any of the other two pins, but i want the signal to be in sync with the motor motion. Any suggestions? I have no clue where to start.

int PUL=7; //define Pulse pin
int DIR=6; //define Direction pin
int ENA=5; //define Enable Pin
void setup() {
pinMode (PUL, OUTPUT);
pinMode (DIR, OUTPUT);
pinMode (ENA, OUTPUT);
}
void loop() {
for (int j=0; j<5; j++)
{
for (int i=0; i<23040; i++) //Forward 5000 steps
{
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
}

delay(5000);
}

}

Robin2

#1
Nov 15, 2018, 09:49 amLast Edit: Nov 15, 2018, 09:53 am by Robin2
Every time the motor makes a step you need to change the output twice (I think) on the pins that represent the encoder - so that the "encoder pins" go LOW HIGH and HIGH LOW for every step. There should be plenty of time between steps to do that.

IIRC a quadrature encoder can identify the direction of travel because, for one direction, pin A is HIGH and pin B is low and vice versa for the other direction - but I am not an expert on that,

If I was developing this project I would get a real quadrature encoder and write a program that could correctly interpret its output, Then I would write my stepper program  so that it would also work with the encoder program (Obviously this requires two separate Arduinos).

...R
Stepper Motor Basics
Simple Stepper Code
Two or three hours spent thinking and reading documentation solves most programming problems.

ard_newbie

#2
Nov 15, 2018, 11:50 am
If you could have an arduino DUE, you could simulate an quadrature encoder with one of the Timer Counter features (TC_SMMR, Stepper Motor Mode Register) or PWM synchro channels, and with the same board make speed and/or position measurements to tune your software.

You can find example sketches for both quadrature encoder speed measurement and quadrature encoder position measurement in the DUE sub forum.

https://forum.arduino.cc/index.php?topic=537904.0

Robin2

#3
Nov 15, 2018, 02:32 pm
If you could have an arduino DUE,
That sounds like an excessive solution for a simple problem

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

dougp

#4
Nov 15, 2018, 04:29 pm
My, admittedly simplistic, first attempt would look like:

Code: [Select]
`write A hidelay x mswrite B hidelay x mswrite A lodelay x mswrite B lodelay x ms`

Reverse the positions of A & B for opposite direction.

2ยข
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

MarkT

#5
Nov 15, 2018, 07:10 pm
Pseudocode something like:

Code: [Select]
`void fake_encoder (boolean step_direction){  static byte encoder_phase = 0 ;  // ie a private global var  encoder_phase += step_direction ? 1 : -1;  encoder_phase &= 3;  digitalWrite (pinA, encoder_phase >= 2) ;                 // 2 or 3  digitalWrite (pinB, ((encoder_phase+1) >> 1) & 1) ;  // 1 or 2}`
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up