Shop OBEX P1 Docs P2 Docs Learn Events
Is there a flaw in this code? — Parallax Forums

Is there a flaw in this code?

lardomlardom Posts: 1,659
edited 2014-08-28 07:09 in Propeller 1
repeat
      if ina[4]
        S++
      repeat while ina[4]
Is it possible for S to increment more than once each time through the loop in this code? In my circuit an LED lights up each time my IR detector sees an encoder stripe. The problem is that the encoder disk rotation is different each time for the same value. I'm using an LM339 with a trimmer. Does an LM339 need to be debounced?

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-08-24 12:55
    If you're looking to debounce an input, you might do something like this:
    ttimer := 0
      t := cnt
      repeat while (ttimer < TIMER_THRESHOLD)
        waitcnt(t += (clkfreq / 1000))
        if (ina[TRIGGER])
          ++ttimer
        else
          ttimer := 0
    


    This bit of code lets you check for an active input up to the value of TIMER_THRESHOLD (specified in milliseconds).

    IMHO, you'll save yourself downstream headaches by avoiding magic #s and single-letter variable names.
  • lardomlardom Posts: 1,659
    edited 2014-08-24 23:33
    JonnyMac, Big thanks. I wrote it so that it loops at least three times before it exits. I'm going to guess that jitter near the zero crossing was a problem. I'll do more complete tests in the AM.
    I stick with descriptive variable names 'except' when I write small pieces of test code. I want things as simple as I can possibly make them.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2014-08-24 23:39
    lardom wrote: »
    JonnyMac, Big thanks. I wrote it so that it loops at least three times before it exits. I'm going to guess that jitter near the zero crossing was a problem. I'll do more complete tests in the AM.
    I stick with descriptive variable names 'except' when I write small pieces of test code. I want things as simple as I can possibly make them.

    As you have probably guessed now that there is "jitter" around the threshold so you need to apply positive feedback to give it that little stable "indent". Just Google "comparator hysteresis".
  • ErNaErNa Posts: 1,752
    edited 2014-08-25 04:14
    Are you detecting bar codes or is it a motion controller?
  • lardomlardom Posts: 1,659
    edited 2014-08-25 07:17
    As you have probably guessed now that there is "jitter" around the threshold so you need to apply positive feedback to give it that little stable "indent". Just Google "comparator hysteresis".
    I searched some images and read an article on "comparator hysteresis" which I found instructive. I also saw the signal through my Parallax usb scope, from my BS2 days, which surprisingly showed noise along some of the edges. Currently, I can only dream of an oscilloscope with high resolution.

    @erna, I made my own wheel encoders and I'm trying to get them to work properly. I built a basic 'bot' and I want it to track straight. I discovered wheel encoders are necessary to control DC motors.
  • lardomlardom Posts: 1,659
    edited 2014-08-27 06:29
    I found a bug in the debouncing object and I don't know the solution. In the object called "encoder_debounced" CNT stops my motor from running. If I comment it out the motor runs! How does one child object stop another child object from executing!?
  • kuronekokuroneko Posts: 3,623
    edited 2014-08-27 07:20
    lardom wrote: »
    I found a bug in the debouncing object and I don't know the solution. In the object called "encoder_debounced" CNT stops my motor from running. If I comment it out the motor runs! How does one child object stop another child object from executing!?
    success := (cog_ER := cognew(increment_loop, @cog_ER) + 1)
    


    shouldn't that read
    success := (cog_ER := cognew(increment_loop, @stack_ER) + 1)
    


    Other than that waitcnt(cnt += ms) should work. I'd argue that waitcnt(ms) is having issues.
  • ErNaErNa Posts: 1,752
    edited 2014-08-27 09:39
    lardom wrote: »
    I
    @erna, I made my own wheel encoders and I'm trying to get them to work properly. I built a basic 'bot' and I want it to track straight. I discovered wheel encoders are necessary to control DC motors.

    OK, in this case you should implement a quadrature detector, that means: have a black/white code wheel and two sensors shifted by 90°. The signal generated is (running forward) 00 01 11 10 00 01 11 10, if now you change direction it will continue 11 01 00 10 11 01. If for any reason you have a flickering signal, 11 10 11 10 11 10, so one bit changes fast, it means you count forward/backward, but you do not make an error in position. Even if this changes happen faster than you can acquire them. So there is no need for debouncing or a hysteresis. There is a quadrature encoder in the OBEX in assembler, easy to use and very fast!
  • lardomlardom Posts: 1,659
    edited 2014-08-27 14:03
    kuroneko wrote: »
    success := (cog_ER := cognew(increment_loop, @cog_ER) + 1)
    


    shouldn't that read
    success := (cog_ER := cognew(increment_loop, @stack_ER) + 1)
    


    Other than that waitcnt(cnt += ms) should work. I'd argue that waitcnt(ms) is having issues.
    I...I...I have no idea how I did that! It was "copy and paste", or so I thought. An error in that block of code was not remotely considered. I would've continued to overlook it. It works now. Thank you!
    ErNa, quadrature encoders will be one of my next challenges. I will have to learn to read Pasm much better because Spin is not fast enough. Once I can grasp the theory of quadrature encoders then understanding the code will be much easier.
  • ErNaErNa Posts: 1,752
    edited 2014-08-27 23:18
    so you just should skip the problems, you have and continue with qe, as this will be the solution! And the asm code is clean, crisp and tricky, so worth learning and understanding
  • lardomlardom Posts: 1,659
    edited 2014-08-28 06:53
    ErNa wrote: »
    so you just should skip the problems, you have and continue with qe, as this will be the solution! And the asm code is clean, crisp and tricky, so worth learning and understanding
    I love problems! If there were no more problems to solve I'd walk away from it. The learning process is the best part of programming.
    I have a motor with a built in quadrature encoder to experiment with but I want to be able to express in plain language what's happening. Once I understand a process, writing code is basically translation and syntax.
    Controlling steppers and servos is simple compared to DC motors. The toughest motor, for me, is a brushless DC motor. I have one without hall sensors. (Maybe I'll give in and get one with built in sensors) :smile:

    Yesterday I visited the Thomas Edison museum in NJ. I was awestruck walking around that historical place seeing his inventions. He was a strong believer in experimentation. Even at 80 years and deaf he was still at it.
  • ErNaErNa Posts: 1,752
    edited 2014-08-28 07:09
    You should only lever away the blocks in your way. That gives problems enough. So if you have a quadrature encoder, you only need a single notch and a third sensor to have a reference signal and you can derive hall signals from the quadrature signal. you best understand qs A/B if you draw two square waves 90° shifted and move a slotted paper along. Or you connect 4 leds arranged in a square to the propeller and output 00 10 11 10 00...
Sign In or Register to comment.