Menu

Pulse Discriminator

Help
Paul Haug
2019-05-02
2019-05-04
  • Paul Haug

    Paul Haug - 2019-05-02

    I am using 18f27k40 chip, I have the LCD working (via I2C) so here is the setup.
    I am working on a line follower that follows a rotary laser (commercial unit) If the receiver is dead center on the line I get a constant high, if to the left I get 50ms pulses at 8 Hz rep rate. If to the right of the line I get 100ms pulses at 4.4 Hz rep rate.
    I need a discriminator type code that will provide high B.6 for right, High B.5 for left, high B.4 for “on the path” steady high, and a high B.3 for no pulses and is zero level( 0 pulses.)
    .
    My current logic flow would be:
    Pulse rec'd > 10ms but < 40ms, send right cmnd B.6
    Pulse rec'd > 10 ms but > 80 ms, but < 60 ms, then send left cmnd B.5
    Signal is Steady High, send “On the Line” B.4
    Steady low send “Shut Down Mtr” B.3 (This would be a signal we are way off course and cannot recover, so we are lost)

    The current code is attached but I have stripped out my feeble attempts at discriminating the pulses because I never got even close. It was a confusing mashup illogical code of counters and loops.
    I don't expect you to fix my code, just some code snippit that I can use and edit.
    In the scope pictures, the lower waveforms are what the PIC will receive.

     

    Last edit: Paul Haug 2019-05-02
  • mmotte

    mmotte - 2019-05-03

    Paul,
    PulseIn in a GCB keyword so you can't use it for a variable.

    So GCB has a PulseIn command

    Dim TimeResult as word
     Pulsein PortA.2, TimeResult, ms     'pulse input A.2 (pin4)
    

    This will give half of the time because it is only measuring the pulse.
    There is also a PulseInv command.

    Some If - Else If s should set your outputs

    Is this enough?
    BR
    Mike

     
  • mmotte

    mmotte - 2019-05-03

    Paul
    It is early for me. I didn't consider the other two cases . continuous and none.

    Here is untested code using a modified PulseIn sub making it your own.

    MAINCONTROL:
     do
    
         ADC_VAL= ReadAD10( AN5 )
    
       xspeed = ADC_VAL   ;This will be used later for correction rate
    ;===========================OLD CODE REMOVED ==========================
    'Pulse rec'd > 10ms but < 40ms, send right cmnd B.6
    'Pulse rec'd > 10 ms but > 80 ms, but < 60 ms, then send left cmnd B.5
    'Signal is Steady High, send “On the Line” B.4
    'Steady low send Shut Down Mtr B.3 (This would be a signal we are way off course and cannot recover, so we are lost)
    
      MyPulseIn ( MyTimeResult)     'pulse input A.2 (pin4)
    
      Select Case MyTimeResult
    
        Case 0 to 5
          code1 'Shut Down Mtr B.3
    
        Case 5 to 20
          code2  'send right cmnd B.6
    
        Case 30 To 40
          code3   'then send left cmnd B.5
    
        Case Else
          code4   '“On the Line” B.4
    
        End Select
    
     loop ;and start over again
    ;======================================================================
    ;----- Subroutines ---------------------------------------------------
    'This is a modified pulsein for your application
    sub MyPulseIn ( MyPulseTime as Word)
      myPulseTime = 0
      Do While PortA.2 = On
        Wait 1 ms
        myPulseTime += 1
        If myPulseTime  = 0 Then Exit Do
        If myPulseTime >= 100 Then Exit Do   ' so don't get stuck in continuous tone
      Loop
    
    end Sub
    
    sub code1
    end sub
    
    sub code2
    end sub
    
    sub code3
    end sub
    
    sub code4
    end sub
    

    Hope You can see what I was trying to do.
    GL
    Mike

     
  • Paul Haug

    Paul Haug - 2019-05-03

    Thanks Mike, that should get me rolling afain. Forgot about "pulsein", so need to work with that and your other suggestions.
    Paul

     
  • stan cartwright

    stan cartwright - 2019-05-03

    From help-
    Dir GPIO.0 In
    Dim TimeResult as WORD

    Do while GPIO.0 = Off        'Wait for next positive edge to start measuring
    Loop
    
    Pulsein GPIO.0, TimeResult, ms
    if TimeResult < 75 then code for 50
    else if TimeResult >150 then code for high
    else code for 100
    end if
    
     
  • Paul Haug

    Paul Haug - 2019-05-03

    Thanks Stan and Mike, that pulsein should really solve my problem. I was stuck and trying to kludge code with loop counters and even interrupts/timercounters which all was getting to much for this feeble mind.
    After I fully understand "PulseIn" and some bench top testing I will be off and running using "case" or "if-then-else" statements to sort out "direction/stop/lost" of the line follower.

     

    Last edit: Paul Haug 2019-05-03
    • stan cartwright

      stan cartwright - 2019-05-03

      re else if TimeResult >150 then code for high
      I don't know what pulsein does if the pin is constantly high???

       
  • Paul Haug

    Paul Haug - 2019-05-03

    Stan, excellent point about Pulsein remaining high. I will do some testing and let you know. If it gets stuck I can always use Mikes suggested code as follows:..
    'This is a modified pulsein for your application
    sub MyPulseIn ( MyPulseTime as Word)
    myPulseTime = 0
    Do While PortA.2 = On
    Wait 1 ms
    myPulseTime += 1
    If myPulseTime = 0 Then Exit Do
    If myPulseTime >= 100 Then Exit Do ' so don't get stuck in continuous tone
    Loop

     
  • Paul Haug

    Paul Haug - 2019-05-03

    I can't get Pulsein to co-operate. See attached code listing.
    If I remove the skip_around (line 106) the test LED stops blinking, sort of hangs in pulsein cmnd ?
    Maybe something in my setup config line (25) is wrong ?
    Any suggestions where I went wrong ?
    Paul
    EDIT: Using the latest compiler, downloaded yesterday. 0.98.05

     

    Last edit: Paul Haug 2019-05-03
  • stan cartwright

    stan cartwright - 2019-05-03

    your code has PulsePin=1 and PulsePin =off
    ; Following is for testing only, lights LED 7for PulseIn high
    If PulsePin = 1 then
    TestLED = 1
    else
    TestLED = 0
    End if
    ;*******

    ;Testing pulsein cmnd stuff

    ;goto skipit ;debug test to allow PortB.7 to follow PulsePin
    Do while PulsePin = off 'Wait for positive to start measuring
    Loop

     
  • Paul Haug

    Paul Haug - 2019-05-03

    Stan,
    Your note said > your code has PulsePin=1 and PulsePin =off
    I must be an idiot, but I can't see anywhere I set PulsePin on or off. It is strictly an input pin I hope.
    Reviewed the code line by line and with search, still can't see the problem. Could you please lead me out of my confusion, maybe point out the line numbers where I have gone astray. ?
    This is on a hardware setup, see attachment if your curious. Unit on right is a presion pulse generato to simulate the line scanner reciever.

     

    Last edit: Paul Haug 2019-05-03
  • mmotte

    mmotte - 2019-05-04

    Paul,
    We needed a way to wait for the pulse that may never be there.(which is your Motor off condition). So measuring the off time is the way. MyPulseInInv is a special case of PulseInInv.

    When we have a pulse then we need to measure it. If it is longer than 100ms just return 101 ms so you know to go straight ahead. Again MyPulseIn is a special case.

    Check attached code. it compiles but not tested.

    Br
    Mike

     
  • Paul Haug

    Paul Haug - 2019-05-04

    Thanks Mike,
    Pluged your code in and it works in the hardware. Needed some tweaking for the display as it was the recipical(?) of the pulse width. .. Chngd code to "print 100 - MyOffTime"
    Late and I am tired so tomarrow I will study your code for the solution I need.
    Many thanks for your support.
    Paul

     

    Last edit: Paul Haug 2019-05-04
  • Paul Haug

    Paul Haug - 2019-05-04

    AWWW Jeezzz, it just hit me, that is the OFFTIME, not the on time of the pulse.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.