Shop OBEX P1 Docs P2 Docs Learn Events
Need help with code for Determining the speed of DC motors based on Hall sensors — Parallax Forums

Need help with code for Determining the speed of DC motors based on Hall sensors

Klaus GawelczykKlaus Gawelczyk Posts: 9
edited 2013-04-28 13:06 in BASIC Stamp
Need help with code for Determining the speed of DC motors (EMG30) based on Hall sensors - using Basic Stamp 2

Has anyone used the I2C to drive the MD25 (motor driver board) with EMG30 (Hall - encoder, motor, gearbox 30:1) see on http://www.robot-electronics.co.uk/htm/md25tech.htm

I use the program written by Chris Clarke - Aug 2008 (Parallax Board of Education and BS2p24 - I2C Bus is connected to P0 and P1) with my changes - it does very well.

I2C bus is working properly, the Hall sensors provide per gear shaft rotation (motor 1 and motor 2) 360 pulses (I can count the pulses continuously)
I'm looking for a way to determine the speed - this requires the number of pulses counted per minute (or second) RPM

speed = (3.1415) *( wheel diameter) * (number of revolutions RPN)

Has anyone written a source code, please help me write a code

Hi to all of you

Klaus Gawelczyk /Germany

Comments

  • ercoerco Posts: 20,256
    edited 2013-04-28 06:42
    Use the COUNT command to count input pulses in a specified interval and calculate RPM.
  • Hal AlbachHal Albach Posts: 747
    edited 2013-04-28 07:04
    How about a DO...LOOP that increments a counter while the pulse is high (or low)? One of the two output levels should bear a relationship with speed of rotation that can be measured.
  • Klaus GawelczykKlaus Gawelczyk Posts: 9
    edited 2013-04-28 07:12
    Hi,
    Thanks for the reply
    Can your proposal be used in I2C bus? - my PIN 0 and 1 are supported by I2C BUS ---> here an example of the frequency measurement without I2C bus (See source code and wiring http://www.parallax.com/dl/docs/books/sw/exp/sw17.pdf) Can the same PIN be used for the bus and for COUNT?

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}

    FreqPin CON 0 ' frequency input pin
    OneSec CON 3484 ' one second - BS2p

    ' OneSec CON 1000 ' BS2
    ' OneSec CON 2500 ' BS2sx
    ' OneSec CON 3484 ' BS2p
    freq VAR Word ' frequency
    Main:
    COUNT FreqPin, OneSec, freq ' collect pulses for 1 second
    DEBUG CLS, "Frequency: ", DEC freq, " Hz" ' display on DEBUG screen
    GOTO Main ' do it again
    END

    Thank you for your support
    Klaus Gawelczyk/Germany
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-28 07:39
    Theoretically you could use pin 0 for both. When the SCL (clock) line of an I2C bus is inactive (high), any I2C devices will ignore the SDA line, so you could have something else connected to the SDA line. The problem is that a Hall sensor is usually active all the time and will generate pulses that will interfere with the use of the I2C devices.

    You don't have to use pins 0 and 1 for an I2C bus. The only time the Stamp makes use of them that way is when your program actually executes an I2C statement. If you're not using the I2C statements, you can use those I/O pins for any other purpose.
  • Klaus GawelczykKlaus Gawelczyk Posts: 9
    edited 2013-04-28 08:09
    Thanks,
    I can make my application differently - default route via count Increments - I want to make a differential gear (steering over different wheel speeds - 2 DC gear motors at a pivot point)
    Perhaps even help come up with an idea

    I have just tried <<< COUNT >>> - it does not go together with the I2C bus

    by
    Klaus Gawelczyk / Germany)
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-28 08:35
    It looks like the easiest way to determine the speed of both motors is to zero the encoder counters (command 32), PAUSE for some amount of time, perhaps 250ms, then read both encoders (read the high order byte of each to freeze the count (registers 2 & 6) and discard the value which should be zero, then read the low order 2 bytes of each (registers 4/5 and 8/9)). Use scaled integer arithmetic to compute the approximate speed as a 16 bit number.
  • Klaus GawelczykKlaus Gawelczyk Posts: 9
    edited 2013-04-28 11:34
    Hi,

    Your idea is good but I can not stop the continuous pulse counting in the loop << for example .... do while (Impulse1 < 3600) .... >> (here it runs the odometry) - I'll think about something to how can time be stopped - interrupt? is that possible with BASIC Stamp?

    by
    Klaus Gawelczyk / Germany
  • Klaus GawelczykKlaus Gawelczyk Posts: 9
    edited 2013-04-28 12:08
    Hi Mike,
    What do you think about POLLIN with BS2P24 - do you have an example code? (I have no experience with POLLIN)
    by
    Klaus Gawelczyk /Germany
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-28 13:06
    1) It's not at all clear what you want to do. You feed us little bits of information, then, when we make suggestions, you give us a reason why it won't work. It's much better to provide us with an overall picture of what devices you have, how you have them connected, what you want to accomplish, what you think will work so far, etc.

    2) I don't recommend using the POLLxxx statements. They're not true interrupts. Specifically, they don't save state (location in the program, variables, etc.) They are sometimes useful, but difficult to use successfully.

    3) Your motor controller already provides counters for the wheel encoders. Are you trying to duplicate this functionality? Why?
Sign In or Register to comment.