Go Down

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

chuckbarry

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 am Last 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

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.

See this thread reply #84:

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




Robin2

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

My, admittedly simplistic, first attempt would look like:

Code: [Select]
write A hi
delay x ms
write B hi
delay x ms
write A lo
delay x ms
write B lo
delay x ms


Reverse the positions of A & B for opposite direction.

2ยข
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

MarkT

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