Shop OBEX P1 Docs P2 Docs Learn Events
Function Confusion — Parallax Forums

Function Confusion

ajwardajward Posts: 1,130
edited 2013-10-15 16:09 in Propeller 1
Hi All...

I know there's probably a simple answer, but I'll be darned if I can find it!

After getting my tracked vehicle running with the Stingray demo code, I've started using Kye's "PWM2C_HBDEngine" from the OBEX for more control. My code (See below) has functions to ramp up, ramp down and run forward and pivot turn to the right. Running the first three functions on the MSR1 board, Ted ramps up, runs forward for a bit, ramps down and stops. However, when I add the fourth function "pivot_rt", he ramps up runs forward, ramps down, but the vehicle never stops pivoting. What am I missing here?

Any wisdom appreciated!!!

Amanda

PS- Happy Monday!
CON


  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000


  _leftFW = 25
  _leftBW = 24
  _rightFW = 26
  _rightBW = 27


  _timeStepInMilliseconds = 20
  _updateFrequencyInHertz = 8_000


OBJ


  hbd: "PWM2C_HBDEngine.spin"


Pub Main


  ramp_up
  waiTcnt(clkfreq + cnt)


  run_fwd
  waitcnt(clkfreq + cnt)


  ramp_dn
  waitcnt(clkfreq + cnt)


  pivot_rt
  waitcnt(clkfreq + cnt)




PUB ramp_up | timeCounter, frequencyCounter


  ifnot(hbd.HBDEngineStart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz))
    reboot


  timeCounter := ((clkfreq / 1_000) * _timeStepInMilliseconds)


    repeat frequencyCounter from 0 to 1000 step 50
      hbd.leftDuty(frequencyCounter)
      hbd.rightDuty(frequencyCounter)


      waitcnt(timeCounter + cnt)
return


PUB run_fwd | timeCounter, frequencyCounter


  ifnot(hbd.HBDEngineStart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz))
    reboot


  timeCounter := ((clkfreq / 1_000) * _timeStepInMilliseconds)


    repeat 2
      frequencyCounter := 1000
      hbd.leftDuty(frequencyCounter)
      hbd.rightDuty(frequencyCounter)


      waitcnt(timeCounter + cnt)
return


PUB ramp_dn | timeCounter, frequencyCounter


  ifnot(hbd.HBDEngineStart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz))
    reboot


  timeCounter := ((clkfreq / 1_000) * _timeStepInMilliseconds)


    repeat frequencyCounter from 1000 to 0 step 50
      hbd.leftDuty(frequencyCounter)
      hbd.rightDuty(frequencyCounter)


      waitcnt(timeCounter + cnt)
return


PUB pivot_rt | timeCounter, frequencyCounter


  ifnot(hbd.HBDEngineStart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz))
    reboot


  timeCounter := ((clkfreq / 1_000) * _timeStepInMilliseconds)


    repeat 2
      frequencyCounter := 1000
      hbd.leftDuty(frequencyCounter)
      hbd.rightDuty(-frequencyCounter)


      waitcnt(timeCounter + cnt)
return


Comments

  • Mark_TMark_T Posts: 1,981
    edited 2013-10-07 13:37
    ajward wrote: »
    Hi All...

    I know there's probably a simple answer, but I'll be darned if I can find it!

    After getting my tracked vehicle running with the Stingray demo code, I've started using Kye's "PWM2C_HBDEngine" from the OBEX for more control. My code (See below) has functions to ramp up, ramp down and run forward and pivot turn to the right. Running the first three functions on the MSR1 board, Ted ramps up, runs forward for a bit, ramps down and stops. However, when I add the fourth function "pivot_rt", he ramps up runs forward, ramps down, but the vehicle never stops pivoting. What am I missing here?

    Any wisdom appreciated!!!

    I suspect you aren't allowed a negative rightDuty value?
  • ratronicratronic Posts: 1,451
    edited 2013-10-07 14:39
    Amanda after your pivot_rt method you should probably run your ramp_dn method to stop. Also you only need to run the engine start instructions once in the beginning of your code. The methods automatically return so you do not

    need to write return in each one unless there is a variable you would like to return.
  • ajwardajward Posts: 1,130
    edited 2013-10-07 15:16
    Mark_T wrote: »
    I suspect you aren't allowed a negative rightDuty value?

    Thanks for your reply Mark!
    Yeah... I guess that has to be it. I've used that obex module before and I could swear I've sent negative values to the function before. Of course, I can't find my previous code to verify. <grumble>

    Anyhow... thanks again!!!!

    Amanda
  • ajwardajward Posts: 1,130
    edited 2013-10-12 14:48
    @Dave - Thanks for that. Working on the little beastie right now. I'll give your ideas a try!!!

    Amanda
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-12 15:03
    ajward wrote: »
    I could swear I've sent negative values to the function before.

    I'm pretty sure you're right about negative values being valid. I think negative values put the motor in reverse.

    It would be easier if you attached the code as an archive and then we could also see the child object.
  • ratronicratronic Posts: 1,451
    edited 2013-10-12 15:22
    In the object she is using the values for leftduty and rightduty go from -1000 to 1000 to go either way.

    Edit: Duane is right you have to put a negative # to go one way and a positive # to go the other with 0 being stop.
  • ratronicratronic Posts: 1,451
    edited 2013-10-12 17:55
    After looking again at your pivot_rt and ramp_dn methods your ramp_dn method first puts the wheels at full speed before ramping down. So you might not want to put ramp_dn after pivot_rt. You can stop the wheels by -
    hbd.leftduty(0)
    hbd.rightduty(0)
    
  • ratronicratronic Posts: 1,451
    edited 2013-10-15 16:09
    I had never used Kye's object before so I tried this example on my Stingray and it works. It also shows another use of return to escape a repeat loop. Maybe you can get some use from it.
    Con                                                         
                                                             
      _CLKMODE = XTAL1 + PLL16X                              
      _XINFREQ = 5_000_000
       _leftFW = 24   
       _leftBW = 25   
       _rightFW = 26  
       _rightBW = 27
        
       '_timeStepInMilliseconds = 10     
       _updateFrequencyInHertz = 8_000
        
       RAMPING = 5  '*100us delay per step duty cycle
       
    Var
      long left_old, right_old
      
    Obj
      
      hbd : "pwm2c_hbdengine"
      
    Pub Main
      
      hbd.hbdenginestart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz)  'start pwm
        '
      Wheels(-500, 500)    'forward @ half speed
      
      waitcnt(clkfreq+cnt) 'pause before setting wheels again
          
      Wheels(500, -500)    'reverse @ half speed
      waitcnt(clkfreq+cnt) 'pause before setting wheels again  
      Wheels(500, 500)     'turn left @ half speed
      waitcnt(clkfreq+cnt) 'pause before setting wheels again
      Wheels(-500, -500)   'turn right @ half speed
      waitcnt(clkfreq+cnt) 'pause before setting wheels again
      Wheels(0, 0)         'stop
      
    Pub Wheels(left, right) 
       
      repeat
      
        if left < left_old                       'increment or decrement by one to new speed'
          hbd.leftduty(--left_old)
          
        if left > left_old
          hbd.leftduty(++left_old)
          
        if right < right_old  
          hbd.rightduty(--right_old)
          
        if right > right_old
          hbd.rightduty(++right_old)
           
        if left_old == left                      'check if both left and right wheels-
          if right_old == right                  '-are @ new speeds and return from-
            left_old := left                     '-this method when met
            right_old := right       
            return
      
        waitcnt(clkfreq / 10000 * RAMPING + cnt) 'RAMPING * 100us pause per step duty cycle
    
Sign In or Register to comment.