Shop OBEX P1 Docs P2 Docs Learn Events
Help with motor test loop using incrementing PWM pulse widths — Parallax Forums

Help with motor test loop using incrementing PWM pulse widths

macrobeakmacrobeak Posts: 354
edited 2011-12-01 16:41 in Propeller 1
I am trying to implement a simple PWM generating loop to experiment with an electric motor.
I have a working H Bridge/motor system interfaced to the Prop, but I am stuck on the spin programming.
The PWM spin module to generate PWM pulses is quite straightforward, thanks to examples by Harpit Sandhu and others.
However, I would like to run a repeating loop with incrementing PWM pulse widths, allowing half a second or so motor run time for each pulse width.
My unsuccessful spin program is attached below.
Please could someone suggest the correct spin coding to achieve this.
Many thanks in advance . . .
===============================================================================================================================
[FONT=&quot]'' PWM_5[/FONT]
[FONT=&quot]'' 25/11/11 Labbook[/FONT]
[FONT=&quot]'' Simple PWM control program based upon Sandhu[/FONT]
[FONT=&quot]'' Chap 7 prog 2 [/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot]CON[/FONT]
[FONT=&quot]_clkmode = xtal1 + pll2x[/FONT]
[FONT=&quot]_xinfreq = 5_000_000[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot]VAR[/FONT]
[FONT=&quot] long Cycle_Time[/FONT]
[FONT=&quot] long period[/FONT]
[FONT=&quot] long stack [50][/FONT]
[FONT=&quot] long PWMPeriod[/FONT]
[FONT=&quot] long PWMPulse[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot]OBJ[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot]PUB Main[/FONT]
[FONT=&quot] PWMPeriod:=clkfreq/1000 'max PWM pulse width = 1ms, same as below[/FONT]
[FONT=&quot] cognew (PWMgen(PWMPulse), @stack) 'start the PWM generation cog[/FONT]

[FONT=&quot] repeat PWMPulse from 0 to PWMPeriod step 500 'repeat cycle for increasing pulse widths[/FONT]

[FONT=&quot] waitcnt (40_000_000 + cnt) 'allow 0.5sec motor run steps for each pulse width [/FONT]

[FONT=&quot] [/FONT]
[FONT=&quot]PUB PWMgen(PulseWidth) 'Run the PWM generation in a separate cog[/FONT]
[FONT=&quot] dira[7]~~[/FONT]
[FONT=&quot] ctra[30..26] :=%00100 'PWM mode Apin - Prop7 output[/FONT]
[FONT=&quot] ctra[5..0] :=7[/FONT]
[FONT=&quot] frqa :=1[/FONT]
[FONT=&quot] PulseWidth:=-5000[/FONT]
[FONT=&quot] Cycle_Time:=clkfreq/1000 'max PWM pulse width = 1ms[/FONT]
[FONT=&quot] period:=cnt[/FONT]
[FONT=&quot] repeat[/FONT]
[FONT=&quot] phsa:=Pulsewidth 'endless loop generating PWM pulses[/FONT]
[FONT=&quot] period:=period + Cycle_Time[/FONT]
[FONT=&quot] waitcnt (period)
==========================================================================================================================
[/FONT]

Comments

  • JonnyMacJonnyMac Posts: 9,198
    edited 2011-11-30 20:07
    I've done a few motor-control projects with the Propeller and wrote the attached object for those times when PWM frequency up to about 25kHz is fine. It will help you by allowing you to see the PWM level to anything you desire.
  • macrobeakmacrobeak Posts: 354
    edited 2011-12-01 15:42
    Jon,
    Thanks for the response. Actually, I had moved on and made use of your article to run my motor experiment.
    However I really would like some help to clear up my misunderstanding of spin programming in the above faulty program.
    Regards
  • kuronekokuroneko Posts: 3,623
    edited 2011-12-01 16:41
    macrobeak wrote: »
    However I really would like some help to clear up my misunderstanding of spin programming in the above faulty program.
    It's basically down to mis-communication. When you start the PWM cog you pass PWMPulse by value. Any change to this variable in the top level program will not be noticed by the second cog. Another thing is that you basically overwrite any initial parameter to PWMgen with -5000.

    So, given that it's all the same file PWMgen could look at PWMPulse directly as it's a global variable (but shouldn't modify it to avoid confusing the repeat loop). Does that make sense?

    When you post code please wrap it in [noparse]
    
    [/noparse] tags.                        
Sign In or Register to comment.