go_away

Author Topic: serial counter for motor encoders?  (Read 5815 times)

0 Members and 1 Guest are viewing this topic.

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
serial counter for motor encoders?
« on: July 06, 2008, 03:20:43 PM »
I am about to purchase two motors from robotics connection.
http://www.roboticsconnection.com/p-51-dc-gearhead-robot-motor.aspx

The motors will serve as direct drive motors.  I want to experiment with dead reckoning, and mapping.  The encoder resolution is 624 pulses per rev of the output shaft.  The encoders are quadrature, so they can sense direction too.

I want to have an external counter(s) to take in the data and send it serially  to my arduino diecimilla board.
Admin recommended a setup like this in the encoder tutorial.  I just don't know where I should be looking.  Any ideas?  I'm also up for any other suggestions or substitutes.

Thanks alot

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: serial counter for motor encoders?
« Reply #1 on: July 06, 2008, 05:59:06 PM »
I would recommend wiring the encoders straight to some input pins on the Arduino. The arduino can then send the data serially to an external source if you need to such as hyperterminal. There is a topic in the software board about quadrature encoders and some suggestions of how to implement them in a microcontroller. That is likely to be the thing that needs the most attention to get it right.

Offline vidam

  • Supreme Robot
  • *****
  • Posts: 423
  • Helpful? 1
  • Robotronics.org
Re: serial counter for motor encoders?
« Reply #2 on: July 06, 2008, 06:10:58 PM »
Here is some basic encoder for the Arduino that our team used. It doesn't do Quadrature encoding though.

Code: [Select]
#define RIGHT_ENC_PIN  2
#define LEFT_ENC_PIN  3

volatile unsigned int rightEncoder = 0;
volatile unsigned int leftEncoder = 0;

void setup() {


  pinMode(RIGHT_ENC_PIN, INPUT);
  pinMode(LEFT_ENC_PIN, INPUT);
 
  attachInterrupt(0, doEncoderRight, RISING);  // encoder pin on interrupt 0 - pin 2
  attachInterrupt(1, doEncoderLeft, RISING);  // encoder pin on interrupt 1 - pin 3
 
//  digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
//  pinMode(encoder0PinB, INPUT);
//  digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor


  Serial.begin (9600);
  Serial.println("start");                // a personal quirk

}

void loop(){
// do some stuff here - the joy of interrupts is that they take care of themselves
//Serial.println("Hello");
  Serial.print(rightEncoder, DEC);
  Serial.print(",");
  Serial.println(leftEncoder, DEC);

}


void doEncoderRight(){
  rightEncoder++;
}

void doEncoderLeft(){
  leftEncoder++;
}

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: serial counter for motor encoders?
« Reply #3 on: July 06, 2008, 08:23:59 PM »
Thanks for such quick responses guys. 
 
I am not sure that wiring the encoder outputs directly to the arduino would be a good idea though. 

Here is my thoughts and additional info I should have included before (my bad).

I am gonna be using a 2x10 sabertooth motor controller (from dimension engineering) via one wire serial to an arduino digital pin, using softwareSerial .  For range finding I plan to use a ping ultrasonic , and or a sharp IR. I am also gonna try to link the arduino with a bluesmirf rp-sma from sparkfun. 

I don't know if I could efficiently run all these and read data from the four encoder outputs at the same time(two per encoder).  An external counter would allow me to access encoder data at any time without having to use interrupts ...maybe.   

Again thanks for such quick responses.  They are greatly appreciated. ;D
 
« Last Edit: July 07, 2008, 12:02:34 AM by sonictj »

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: serial counter for motor encoders?
« Reply #4 on: July 06, 2008, 11:02:46 PM »
That's not a heavy load at all. I'm not sure about the 'arduino' programing, but with a 16MHz ATmega this shouldn't be a problem.

Also why software serial? What chip is this arduino using?
« Last Edit: July 06, 2008, 11:11:47 PM by JesseWelling »

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: serial counter for motor encoders?
« Reply #5 on: July 06, 2008, 11:50:08 PM »
the chip is a atmega168.  I just haven't used interrupts before, so I wasn't sure.  Admin's tutorial suggested to use a digital timer ic so I thought I would check first. I also was trying not to have to use 4 inputs if possible.  I did just find some chips called quadrature decoders that are designed for interfacing quadrature encoders to microprocessors.  However they are a bit pricey $20 per.

I'm planning to use solftwareSerial to drive the 2x10 over a single digital pin at 9600 baud.  This way I can conserve inputs and have pins 1 and 2 run a bluesmirf to my computer, at the same time.

« Last Edit: July 07, 2008, 12:01:42 AM by sonictj »

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: serial counter for motor encoders?
« Reply #6 on: July 07, 2008, 12:25:21 AM »
I just checked up on some of the arduino references pertaining to interrupts, and found that I can only use two interrupts at a time.  This will pose a problem as I understand it, because the quadrature encoders have two outputs each.  I won't be able to use interrupts on enough pins.  This will cause the arduino to not know the direction of the motors, and steps will be missed when the motors reverse.
   

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 331
  • Helpful? 3
Re: serial counter for motor encoders?
« Reply #7 on: July 07, 2008, 12:55:39 AM »
You can use as many interrupts as your program can handle.  If the Arduino libraries don't support more than two simultaneous interrupts, define them yourself.  It's really not hard.

- Ben

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: serial counter for motor encoders?
« Reply #8 on: July 07, 2008, 01:02:51 AM »
I think I found a solution!!! I just remembered I stumbled over this board when I was still choosing motors to buy.  Here is a link to a pdf of a encoder interpreter board from http://banebots.com.
link http://banebots.com/docs/EN-A0001-KT-Manual.pdf.  Two of these boards ought to do the trick.  They output a direction signal and a pulsed signal.
I could set an interrupt to sense the pulsed pin (the quadrature signal) and determine direction of movement by detecting either a high or low signal on a direction pin.  This would only require 2 interrupts (one per motor) which the arduino has.

thoughts?

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: serial counter for motor encoders?
« Reply #9 on: July 07, 2008, 01:07:05 AM »
to ben

how would I go about defining additional interrupts?  I just started working with robots this summer, so I'm still quite a noob.

« Last Edit: July 07, 2008, 01:07:48 AM by sonictj »

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 331
  • Helpful? 3
Re: serial counter for motor encoders?
« Reply #10 on: July 07, 2008, 02:43:35 AM »
I can try to give you a brief code example if you can tell me exactly what you want to do.  Mainly I just suggest you start looking at the mega168 datasheet if you want to start seriously using your microcontroller.  It can be a bit indimidating, but if you restrict it to looking at the sections you currently care about it's not that bad.

The mega168 has an interrupt vector table at the start of the program memory.  When you define an interrupt service routine (ISR), the corresponding interrupt vector is set to the location of the routine so that the microcontroller knows what code to execute when the interrupt occurs.  Basically you just need to define the ISRs you want to use and then set the appropriate registers to enable the corresponding interrupts.  For example:

ISR(TIMER0_OVF_vect)
{
  // put code here to run when timer0 overflows
}

ISR(INT0_vect)
{
  // put code here to run when there is an external interrupt on pin INT0
}

- Ben

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: serial counter for motor encoders?
« Reply #11 on: July 07, 2008, 04:40:31 AM »
I think that you are talking about external interrupt pins for the encoders? If so then you only need 2 interrupts for the quadratur encoders to work. The only draw back is that you wont be able to use them at half step resolution

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: serial counter for motor encoders?
« Reply #12 on: July 07, 2008, 11:06:59 PM »
if you are using avrgcc, this is a good companion to the Atmega data sheets to learn how to program them.

Specifically see this page for interrupts.

Offline sonictjTopic starter

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: serial counter for motor encoders?
« Reply #13 on: July 16, 2008, 01:58:47 AM »
I purchased the encoder dividers from banebots and tried them out.  They work pretty well.  I really appreciate the help from you guys.  I adapted the code vidam posted.  The only difference is that when an external interrupt is triggered a on say pin 3 the mcu checks to see if pin 13 is HIGH.  If so the motor is turning CCW.  Otherwise the motor is turning CW.

I am also going to try my best to become knowledgeable enough to interpret the atmega 168 data sheet.  Because I know it will help me in the future.

 


Get Your Ad Here

data_list