Make an acceleration/deceleration ramp : any ideas?


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Make an acceleration/deceleration ramp : any ideas?

    According to my calculations -

    endval = 1200
    startval = 20


    endval - startval = 1180
    ramp time in milliseconds = 1000

    1180/1000 = 1.18 (so 1180/1000 = 1 = increment)
    1180//1000 = 180 (=remainder = fractional)



    fractional = 0
    oldfractional = 0

    frequency = 20 ; start value

    ISR:

    Frequency = frequency + increment ; increment the main part

    fractional = fractional + remainder ; Deal with the fractional part
    If fractional dig3 != oldfractional ; keep adding up the partial until we reach 1000
    oldfractional = fractional ; update so we can see the next change
    frequency = frequency + 1 ; add a count - since we broke 1000
    Endif


    This will increment by one every program loop and then add an extra count every 5 (or so) loops. I haven't tested the algorithm, but I believe it should deal properly with the 'wrap' when "fractional" overflows.
    Charles Linquist

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Make an acceleration/deceleration ramp : any ideas?

    Quote Originally Posted by Charles Linquis View Post
    According to my calculations -

    endval = 1200
    startval = 20


    endval - startval = 1180
    ramp time in milliseconds = 1000

    1180/1000 = 1.18 (so 1180/1000 = 1 = increment)
    1180//1000 = 180 (=remainder = fractional)



    fractional = 0
    oldfractional = 0

    frequency = 20 ; start value

    ISR:

    Frequency = frequency + increment ; increment the main part

    fractional = fractional + remainder ; Deal with the fractional part
    If fractional dig3 != oldfractional ; keep adding up the partial until we reach 1000
    oldfractional = fractional ; update so we can see the next change
    frequency = frequency + 1 ; add a count - since we broke 1000
    Endif


    This will increment by one every program loop and then add an extra count every 5 (or so) loops. I haven't tested the algorithm, but I believe it should deal properly with the 'wrap' when "fractional" overflows.
    I've tested this :
    Code:
    DEFINE OSC 32      
    DEFINE LCD_DREG PORTC
    DEFINE LCD_EREG PORTD
    DEFINE LCD_RSREG PORTD
    DEFINE LCD_EBIT 0
    DEFINE LCD_RSBIT 1
    DEFINE ADC_BITS 10
    DEFINE LCD_COMMANDUS 10000
    DEFINE LCD_DATAUS 2000
    
    'Variable init
    freq var word
    maxfreq var word
    minfreq var word
    remain var word
    increm var word
    time var word
    potsense var word
    frac var word
    oldfrac var word
    delta var word
    
    'Variables def
    minfreq=10
    maxfreq=1200
    
    
    'Calculate steps
    delta=maxfreq-minfreq
    time=1000
    
    increm=delta/time
    remain=delta//time
    
    frac=0
    oldfrac=0
    freq=10
    
    main:
    
    ' Counter
    freq=freq+increm
    frac=frac+remain
    if frac dig 3 != oldfrac then
    oldfrac=frac
    freq=freq+1
    endif
    
    ' 1ms time base (simulates an interrupt)
    pause 1
    
    lcdout $fe,$2,"Frequency: ",dec5 freq
    
    goto main
    And it seems to work well, the "freq" value get incremented, and if I change the "time" variable, the "freq" value increments more or less quickly.

    But the "freq" increments without stopping, and this over 1200!

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Make an acceleration/deceleration ramp : any ideas?

    stop the incrementing or stop the interrupt when freq >= maxfreq
    Charles Linquist

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Make an acceleration/deceleration ramp : any ideas?

    Quote Originally Posted by Charles Linquis View Post
    stop the incrementing or stop the interrupt when freq >= maxfreq
    I've tried this on my real PIC with a potentiometer :

    Code:
    DEFINE OSC 32      
    DEFINE LCD_DREG PORTC
    DEFINE LCD_EREG PORTD
    DEFINE LCD_RSREG PORTD
    DEFINE LCD_EBIT 0
    DEFINE LCD_RSBIT 1
    DEFINE ADC_BITS 10
    
    ' ADC registers configuration
    ADCON0=%11101
    ADCON1=%10000    
    ADCON2=%10000110  
    ADCHS=%1010101  
    ANSEL0=%11110000 
    
    ' Variable init
    freq var word
    rfreq var word
    maxfreq var word
    minfreq var word
    dum6 var word
    dum7 var word
    accel var word
    decel var word
    frac var word
    oldfrac var word
    potsense var word
    
    ' Variables def
    maxfreq=1200
    minfreq=10
    accel=1000
    decel=1000
    
    ' Start freq
    freq=minfreq
     
    main:
    
    ' Start ADC conversion
    ADCON0.1=1       
    WHILE ADCON0.1=1:WEND
    
    ' Store ADC results   
    potsense.HighByte=ADRESH 
    potsense.LowByte=ADRESL 
    
    ' Convert pot result
    rfreq=(potsense<<6)**maxfreq
    
    ' Acceleration ramp
    if freq<rfreq then
    dum6=(rfreq-minfreq)/accel
    dum7=(rfreq-minfreq)//accel
    freq=freq+dum6
    frac=frac+dum7
    if frac dig 3<>oldfrac then oldfrac=frac:freq=freq+1
    endif 
    
    ' Deceleration ramp
    if freq>rfreq then
    dum6=(maxfreq-rfreq)/decel
    dum7=(maxfreq-rfreq)//decel
    freq=freq-dum6
    frac=frac-dum7
    if frac dig 3<>oldfrac then oldfrac=frac:freq=freq-1
    endif 
    
    ' Display results
    lcdout $fe,$2,"Frequency: ",dec5 freq
    lcdout $fe,$c0,"Ref: ",dec5 rfreq
    
    pause 1
    
    goto main
    And unfortunately the timing is not very accurate, it depends of the "maxfreq" variable defined. I can have a 1 or 2 seconds gap.
    And above 1000, the "accel"/"decel" values doesn't increase the time of the ramp.

  5. #5
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Make an acceleration/deceleration ramp : any ideas?

    If you use an interrupt, the timing is guaranteed.

    Your LCD can't possibly update every millisecond.
    Charles Linquist

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts