Gramps
Senior Member
Why does the code jitter ? Especially in:
reserveW0 = w0 ; b1:b0
Gramps
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]