Shop OBEX P1 Docs P2 Docs Learn Events
Changing the " waitcnt" on the fly. — Parallax Forums

Changing the " waitcnt" on the fly.

msiriwardenamsiriwardena Posts: 301
edited 2011-06-19 10:19 in Propeller 1
I have successfully constructed the "Computer controlled air/steam engine" which was featured in
the March 2011 Edition of NUTS and VOLTS.

I have the engine running with air using a propeller but does not run very smoothly.
I think if I can change the timing on the fly using " waitcnt" - it will run better.

I really need some help here with code as I am not a good programmer.

Thank you.

Siri
P.S - The code I am using is attached.

Comments

  • markaericmarkaeric Posts: 282
    edited 2011-06-19 08:21
    Is there a reason you're starting the "Timing" method in a new cog, but also calling it from the first cog? Also, both ValveController, and Timing methods are in infinite loops because of using "Repeat" with no conditions, so "main" method never actually calls "Timing" because it's infinitely looping "ValveController".
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-06-19 08:27
    I think you can replace the number with a variable
    waitcnt((clkfreq/YourVariable)+cnt)  
    
    

    Just keep in mind that division by YourVariable will result in an integer.

    EDIT: markaeric might be right about your other programming issues; I was just answering the question about waitcnt.
  • markaericmarkaeric Posts: 282
    edited 2011-06-19 08:32
    Despite the "Timing" method not being called from the first cog, it's running in another cog which is most likely what you would want anyways, so you're code should be working fine. I think any issues you have with the motors performance probably has more to do with the mechanical design than software. I don't have that particular issue of Nuts and Volts, so I'm just speculating.

    Also, welcome to the forum!
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-06-19 09:58
    I would suggest a synchronized loop in your timing cog which you main cog can control through a global variable. Like this:
    pri timing | t                                          ' launch with cognew()
    
      dira[IntakeSolenoid] := 1                             ' The normally  CLOSED
      dira[ExhaustSolenoid] := 1                            ' The exhaust valve normally CLOSED
      
      t := cnt
    
      repeat
        if instate
          outa[IntakeSolenoid] := 0
        else
          outa[IntakeSolenoid] := 1                         ' Set Intake valve to open   
          outa[ExhaustSolenoid] := 0                        ' Set exhaust valve to Close   
        
        if !exstate
          outa[ExhaustSolenoid] := 1                        ' Exhaust valve open   
          outa[IntakeSolenoid] := 0                         ' Intake valve CLOSED
    
        waitcnt(t += valvetiming)
    

    This loop will always run the amount of time (clock ticks) specified in the global variable valvetiming, no matter which path is taken in the loop. Be careful, though, if valvetiming is too small then the waitcnt target will be missed and you'll end up with a big problem. Note, too, that ":= 1" is faster than "~~" and easier for Spin newbies to comprehend.
  • msiriwardenamsiriwardena Posts: 301
    edited 2011-06-19 10:10
    markaeric,Electric Aye,

    I will add a repeat loop to the main Pub.My reason for running continuous loops is because since the the continuously rotates it needs to read the - Total dead Center(High point of the piston at each stroke) and also to read the lower most position - to get the intake and exhaust valves to open/close.
    By the way I am using an old 2 stroke gasoline engine from a old weed-whacker - just the block and cylinder only.

    What I am planing is to program another Pub to change the waitcnt using UP and Down buttons - so I an change the timing on the fly.The Hall-Effect sensors that I am using does not
    get correct timing.

    ElectricAye - This exactly what I had in mind but I wan't to change the value of the variable on the fly.

    Thanks for the quick reply. - Going to change some code as suggested.

    Siri
  • msiriwardenamsiriwardena Posts: 301
    edited 2011-06-19 10:19
    Jon,

    I will make changes as you suggested and try it.
    Thanks

    Siri
Sign In or Register to comment.