# Thread: How to determine speed with arduino

1. ## How to determine speed with arduino

I am having problems determining the speed of my robot using encoders in arduino. My encoders have 24 "clicks" per revolution. I am using a arduino dieclmile with the basic arduino environment. How do I figure out a the robots rpm in arduino code? I know I some how need to time it but I have no idea how. My input value is a simple 1 when it is on black and zero when it is on white. Dose anybody have example code for this? I keep getting stuck when I try to figure I out.   Reply With Quote

2. ## Re: How to determine speed with arduino Originally Posted by ROBOTMAN I am having problems determining the speed of my robot using encoders in arduino. My encoders have 24 "clicks" per revolution. I am using a arduino dieclmile with the basic arduino environment. How do I figure out a the robots rpm in arduino code? I know I some how need to time it but I have no idea how. My input value is a simple 1 when it is on black and zero when it is on white. Dose anybody have example code for this? I keep getting stuck when I try to figure I out. I don't have a code example right now, I typically don't bother with speed control... but:

Have interrupts on each channel of your encoders, when the interrupt for either channel triggers, you increment the value of a Left or Right counter (integer). This may not be quite that simple, especially if you are using quadrature encoders, you'll have to decode them... also, if you can't use the hardware interrupts, the arduino doesnt have a library for the pin-change interrupts (but you can probably find the code out there.. I have a version on my SVN server, under avrra/library/dev/lite.h)

Once you are counting, then you probably want another interrupt, or maybe just your loop (if you are just trying to match the speeds, not find a definite speed). In there, you would compare your counts to your setpoints, and adjust your PWM values as neccessary...

-Fergs  Reply With Quote

3. ## Re: How to determine speed with arduino

Not sure what you mean by interrupts but I do have a counting program that counts the number of ticks that have happened. However this program is complex and slow. Not really sure what to do with this data to get speed though!  Reply With Quote

4. ## Re: How to determine speed with arduino

If you aren't using interrupts, then your program is polling, which is gonna be way slow!

http://lmgtfy.com/?q=arduino+interrupts

-Fergs  Reply With Quote

5. ## Re: How to determine speed with arduino

Ok - so you can count ticks. Woot!

You haven't learned anything helpful in determining speed.

How big are your wheels? Aha. There's the key. Remember that the circumference of a circle is PI * Diameter or 2PIr?  Reply With Quote

6. ## Re: How to determine speed with arduino

15.3025 is my diameter at least that's what I got! Counting ticks indeed did not help me learn how to determine speed. I don't see how interrupts will help me determine speed; my program was slow because my computer was slow.  Reply With Quote

7. ## Re: How to determine speed with arduino

1 wheel revolution is equal to the circumference of your wheel. Use Adrenalyn's formula to find the circumference of your wheel. If the encoder is broken into 24 sections then the 1 "tick" on the encoder is equal circumference / 24 in inches traveled.

Interrupts help because they do just that. They interrupt whatever is going on in your microcontroller so that you don't miss a "tick".  Reply With Quote

8. ## Re: How to determine speed with arduino

Thanks! My micro controller for now is dedicated to my encoders so it should not mater much but as I expand I will switch.  Reply With Quote

9. ## Re: How to determine speed with arduino

Computer was slow? I wouldn't dare count on the computer, you should do most of the processing on the Arduino. General program is like this (I'm gonna only outline one motor, basically you have two loops like this running at the same time for your two motors):

1) Arduino counts the ticks of the encoder (via an interrupt, which is basically hardware that triggers every time the pin changes from high to low on the arduino).
2) The laptop/PC gives the Arduino a desired speed, for instance, if you wanted to go X inches forward/second, you would convert that to # of ticks per second.
3) The arduino has a update loop that runs every Y seconds. In this loop, you compare the actual # of ticks recieved from the motor to the desired #, and then adjust the output speed. Typically you would use something like PID for that.

Some considerations:
A) How often should the loop run (denoted Y above)? The more often it runs, the more accurate your speed will be (in theory), however, the speed at which ticks are generated by the encoder will govern the top speed of the loop.
B) Interrupts on the Arduino. The arduino has libraries using the two hardware interrupts the AVR has. This is probably sufficient, since they aren't multiplexed with the PWM.
C) Are you using the Arduino to give the motors PWM values? If not, you'll have to send data through the PC when you run your update loop.

-Fergs  Reply With Quote

10. ## Re: How to determine speed with arduino

Actually this is part of my robot butler project so the arduino will be streaming the data it gathers threw the serial port to my python code. I want the arduino to do all the math and just send something to my python code like this "A: 100" 100 being the speed and A meaning the encoder. Then my python will look at that number assess it and adjust the phidgets motor controller accordingly. Or at least thats the plan!   Reply With Quote