Shop OBEX P1 Docs P2 Docs Learn Events
help understanding code — Parallax Forums

help understanding code

ispearispear Posts: 20
edited 2012-11-18 12:03 in Propeller 1
Hi,

I am working on a project at kansas state university that uses stepper motors. I have acquired many stepper motor drivers and have the code that works with those drivers but I am having trouble understanding it so I can adapt it. The drivers are very simple: wake the board by sending a high to SleepPin, choose direction by sending high or low to DirPin, and send PWM to StepPin to set speed. The following is the code that works but I am having trouble adapting. it uses a limit switch, a direction switch and a 10K pot to determine speed. What specifically is this segment I've put in BLUE doing?
VAR

  long time
  long temp
  long Stack[20]
  long CurrentDir
  
CON

  _xinfreq = 5_000_000                      
  _clkmode = xtal1 + pll16x

  SleepPin = 22
  ResetPin = 23
  
  PFD = 22
  EnablePin = 24

  
  DirSwitch = 7
  LimitSwitch = 6

  DirPin = 17
  StepPin = 18

  MS1Pin = 21
  MS2Pin = 20
  MS3Pin = 19


  Forward = 0
  Reverse = 1
  
  
PUB Main

    Cognew(StepMotor,@stack[0])
    Cognew(RCMeasure,@stack[10])
                                
PUB StepMotor

    dira[StepPin] := 1
    dira[DirPin] := 1
    dira[MS1Pin] := 1
    dira[MS2Pin] := 1
    dira[SleepPin] := 1

    outa[MS1Pin] := 1
    outa[MS2Pin] := 1
    outa[MS3Pin] := 1

    repeat
      if (temp := time) > 350 'If the pot is turned all the way down...
      
          if (ina[LimitSwitch] == 0) 'Make sure the Limit Switch is not triggered...
          
            [COLOR=#020FC0][B]outa[SleepPin] := 1
            CurrentDir := outa[DirPin] := ina[DirSwitch]
            outa[StepPin] := 1
            waitcnt(clkfreq/temp + cnt )
            outa[StepPin] := 0
            waitcnt(clkfreq/temp + cnt )

          else 'wait for a direction change...
            repeat while (ina[DirSwitch] == CurrentDir)
             'do nothing...[/B][/COLOR]
      else
          outa[SleepPin] := 0

          
PUB RCMeasure      

  ctra[30..26] := 000 ' Set mode to "POS detector"
  ctra[5..0] := 16       ' Set APIN to 16 (P16)
  frqa := 1              ' Increment phsa by 1 for each clock tick

  repeat
  
     dira[16] := outa[16] := 1      ' Set pin to output-high
     waitcnt(clkfreq/100_000 + cnt) ' Wait for circuit to charge
      
     phsa~      ' Clear the phsa register
     dira[16]~  ' Pin to input stops charging circuit

     repeat 22
       waitcnt(clkfreq/60 + cnt)        

     time := (phsa - 624) #> 0
     waitcnt(clkfreq/2 + cnt)

Many thanks,

-Isaac

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2012-11-18 10:55
    The else is simply doing nothing unless the direction has been changed. The point is, that limitSwitch should be a physical switch that's pushed, when the device attached to the motor reached the end. This avoids damage.

    In the if:
    1. set the sleep PIN to 1
    2. copy the direction switch to the direction PIN and store it in CurrentDir for later usage (in the else)
    3. create a pulse on the step pin
  • lardomlardom Posts: 1,659
    edited 2012-11-18 11:03
    Welcome to the forum. I will not have the best answer but I'll just say how I see it: The key is whether "ina[DirSwitch]" goes high. ina is a read-only register that contains the high/low state of an i/o pin. If it is "0" then "0" gets copied to that whole line and drops to the "else" line and stays there until "ina[DirSwitch]" goes high ("ina[DirSwitch] := 1)" .
  • ispearispear Posts: 20
    edited 2012-11-18 12:03
    Lardom and MagIO2, Thanks for your help, I understand that now.

    -Isaac
Sign In or Register to comment.