Problem of Jittering in "reserveW0"?

Gramps

Senior Member
Why does the code jitter ? Especially in:

reserveW0 = w0 ; b1:b0

Gramps

Code:
Code:
#Picaxe 28X2
#No_Data
#No_Table

Symbol DESIRED_POT      = 17 ; C.5 = ADC17
Symbol FEEDBACK_POT     =  4 ; C.3 = ADC4

; N = Vin * 1023 / Vpsu, Vpsu = 4.83V

Symbol FEEDBACK_TOP     = 830 ; 3.92V
Symbol FEEDBACK_MID     = 512 ; 2.47V
Symbol FEEDBACK_BOT     = 216 ; 1.02V

Symbol FEEDBACK_TOP_GAP = FEEDBACK_TOP - FEEDBACK_MID
Symbol FEEDBACK_BOT_GAP = FEEDBACK_MID - FEEDBACK_BOT

Symbol  DIRECTION       = C.4
#Define CLOCKWISE       High DIRECTION
#Define ANTICLOCKWISE   Low  DIRECTION

Symbol MOTOR            = C.2
Symbol MOTOR_PERIOD     = 100
Symbol MAX_SPEED        = 300

Symbol PAUSE_MS         =2000 ; Millisecond delay when target reached

Symbol reserveW0        = w0 ; b1:b0
Symbol desiredPosition  = w1 ; b3:b2
Symbol currentPosition  = w2 ; b5:b4
Symbol speed            = w3 ; b7:b6

Do
  Gosub ReadDesiredPostion
 
  Gosub ReadCurrentPosition
 
  Select Case desiredPosition
    Case  > currentPosition : Gosub TurnClockwise
    Case  < currentPosition : Gosub TurnAntiClockwise
    Else                    : Gosub StopTurning
  End Select
Loop

ReadDesiredPostion:
  ReadAdc10 DESIRED_POT, w0
  debug
CalcDesiredPosition:
  desiredPosition = w0
  Return

ReadCurrentPosition:
  ReadAdc10 FEEDBACK_POT, w0
  debug
CalcCurrentPosition:
  If w0 >= FEEDBACK_MID Then
    w0 = w0 - FEEDBACK_MID
    w0 = w0 * 128 / FEEDBACK_TOP_GAP * 4 Max 511
    w0 = w0 + 512
  Else
    w0 = FEEDBACK_MID - w0
    w0 = w0 * 128 / FEEDBACK_BOT_GAP * 4 Max 512
    w0 = 512 - w0
  End If
  currentPosition = w0
  Return

TurnClockwise:
  Do
    If speed = 0 Then
      speed = MAX_SPEED
      CLOCKWISE
      PwmOut MOTOR, MOTOR_PERIOD, speed
    Else
      CLOCKWISE
      PwmDuty MOTOR, speed
    End If
    Gosub ReadDesiredPostion
    Gosub ReadCurrentPosition
  Loop Until currentPosition >= desiredPosition
  Gosub StopTurning
  Return

TurnAntiClockwise:
  Do
    If speed = 0 Then
      speed = MAX_SPEED
      ANTICLOCKWISE
      PwmOut MOTOR, MOTOR_PERIOD, speed
    Else
      ANTICLOCKWISE
      PwmDuty MOTOR, speed
    End If
    Gosub ReadDesiredPostion
    Gosub ReadCurrentPosition
  Loop Until currentPosition <= desiredPosition
  Gosub StopTurning
  Return

StopTurning:
  PwmOut MOTOR, MOTOR_PERIOD, 0
  If speed <> 0 Then
    Pause PAUSE_MS
  End If
  speed = 0
Return   
[code]
 

lbenson

Senior Member
Debug can cause unexpected delays because of the time it takes to execute. Replace all with appropriate SERTXDs. Then if there's still a problem, report in more detail, including gear being controlled and what the intention is for maths in the program (comments are good).
 

Gramps

Senior Member
Replace all with appropriate SERTXDs
Yes we will try that and report back.
correction. Hippies original code contained all the necessary scrtxd's plus a whole lot more code to illustrate different simulations. The code posted in this thread is only a portion of his work.
Gramps asks your forgiveness
 
Last edited:

erco

Senior Member
Sertxds also slow down execution to a crawl. Comment them all out to run at full speed.
 

erco

Senior Member
Seems like an odd naming convention IMHO. The variable name "reserveW0" is never used other than to make it equal to word variable W0. WO is used directly multiple times as a disposable value in subsequent ADC10 calculations.

My take is it's a convention just to alert a future programmer that W0 (comprised of B0 and B1) is used in the program and to avoid using it and overwriting it. Likely good practice in demo code for budding programmers to study & emulate.

My personal preference is to not use variable names. I just use B0, B1, W2 etc and keep track of variables used myself. Keeps my programs shorter and (for me anyway) more readable. But then again I'm not a real programmer. :)
 

Aries

New Member
Those of us (are there any others?) who started life as Univac assembler programmers, used registers (A0-15, X0-X15, R0-R15) to manipulate values, and memory locations (nameable) to store them. Like Erco, I have continued to use the same principle, using b0-bxx, w0-wxx etc as they are, and using other RAM memory locations by name via SYMBOLs. I do appreciate that there can be virtue in using aliases for the w and b variables, but for complex programs they have to be reused anyway.
 

erco

Senior Member
Besides, I use bit0-bit7 (which comprise b0) a lot for flags within programs, so I avoid using b0, b1 and w0 (comprised of b0+B1) since they all overlap.
 
Top