by woodygb » 11 Sep 2012, 00:31
The above contains all 3..you want just 2..no serial?
I think this does just the 2....pulse and analogue.
DIM bPactive AS Boolean
DIM bSactive AS Boolean
DIM bAactive AS Boolean
vForwardScaling = 95
vReverseScaling = 35
vTurnScalingFwd = 35
vTurnScalingRev = 35
vMotorResistance = 100
MainLoop:
PRINT ("\n","START OF MAIN LOOP \n")
GoSub FindMode
GoSub SetCommands
PRINT ("\n" , "Joystick THROTTLE = ", vC1, " Joystick STEER = ", vC2, "\n")
'use following three lines if _CIx are M1 and M2 values
' GoSub ScaleSpeedM1M2
' GoSub ScaleTurnM1M2
' GoSub MotorCompensationM1M2
' use following three lines if _CIx are THROTTLE and STEER values
GoSub ScaleSpeedThrStr
GoSub ScaleTurnThrStr
GoSub MotorCompensationThrStr
GoSub SetMotors
Wait (50)
GoTo MainLoop
FindMode:
vP1 = getvalue(_CIP,1)
vP2 = getvalue (_CIP,2)
' vS1 = getvalue(_CIS,1)
' vS2 = getvalue(_CIS,2)
vA1 = getvalue(_CIA,1)
vA2 = getvalue (_CIA,2)
bPactive = TOBOOL (abs(vP1)+abs(vP2) > 0)
bSactive = TOBOOL (abs(vS1)+abs(vS2) > 0)
bAactive = TOBOOL (abs(vA1)+abs(vA2) > 0)
IF bPactive THEN
vMode = 1
ELSEIF bSactive THEN
vMode = 2
ELSEIF bAactive THEN
vMode = 3
ELSE
vMode = 0
END IF
vMode = 1
PRINT ("\n","Mode = ", vMode, "\n \n")
RETURN
SetCommands:
IF vMode = 1 THEN
vC1 = vP1
vC2 = vP2
ELSEIF vMode = 2 THEN
vC1 = vS1
vC2 = vS2
ELSEIF vMode = 3 THEN
vC1 = vA1
vC2 = vA2
ELSE
vC1 = 0
vC2 = 0
END IF
RETURN
ScaleSpeedM1M2:
vSpeed = ((vC1 + vC2)*10)/20 'vC1 = left motor, vC2 = right motor
IF (vSpeed < 0) THEN
vC1 = (vC1*vReverseScaling)/100
vC2 = (vC2*vReverseScaling)/100
ELSE
vC1 = (vC1*vForwardScaling)/100
vC2 = (vC2*vForwardScaling)/100
END IF
PRINT ("\n" , "M1 scaled speed = " , vC1 , " M2 scaled speed = " , vC2, "\n \n")
RETURN
ScaleSpeedThrStr:
vSpeed = vC1 'vC1=THROTTLE
IF (vSpeed < 0) THEN
vScaledSpeed = (vSpeed*vReverseScaling)/100
ELSE
vScaledSpeed = (vSpeed*vForwardScaling)/100
END IF
'set THROTTLE to new speed
vC1= vScaledSpeed
PRINT ("\n" , "THROTTLE/STEER scaled speed = " , vC1 , "\n \n")
RETURN
ScaleTurnM1M2:
vSpeed = ((vC1 + vC2)*10)/20 'vC1 = left motor, vC2 = right motor
'unmix motor settings to find turn value that will be scaled
vTurn = vC1-vC2
IF (vSpeed < 0) THEN
vScaledTurn = (vTurn*vTurnScalingRev)/100
ELSE
vScaledTurn = (vTurn*vTurnScalingFwd)/100
END IF
'apply half of change to each wheel in opposite senses
vChangeTurn = vTurn - vScaledTurn
vC1 = vC1 - vChangeTurn/2 'subtracted
vC2 = vC2 + vChangeTurn/2 'added
PRINT ("M1 scaled turn = " , vC1 , " M2 scaled turn = " , vC2, "\n \n")
RETURN
ScaleTurnThrStr:
vSpeed = vC1 'vC1 = THROTTLE
vTurn = vC2 'vC2 = STEER
IF (vSpeed < 0) THEN
vC2 = (vTurn*vTurnScalingRev)/100
ELSE
vC2 = (vTurn*vTurnScalingFwd)/100
END IF
PRINT ("THROTTLE/STEER scaled turn = " , vC2 , "\n \n")
RETURN
MotorCompensationM1M2:
vILeft = GetValue (_AIC, 3)
vIRight = GetValue (_AIC, 4)
'apply compensation to each motor
vC1 = vC1 + (vILeft*vMotorResistance)/1000
vC2 = vC2 + (vIRight*vMotorResistance)/1000
PRINT ("\n" , "vC1 compensated = " , vC1 , " vC2 compensated = " , vC2, "\n \n")
RETURN
MotorCompensationThrStr:
'find motor currents
vILeft = GetValue (_AIC, 3)
vIRight = GetValue (_AIC, 4)
'do mixing to find motor values (DO NOT TRUNCATE - we will be reversing this below)
vM1 = vC1 + vC2
vM2 = vC1 - vC2
'apply IR compensation
vM1 = vM1 + (vILeft*vMotorResistance)/1000
vM2 = vM2 + (vIRight*vMotorResistance)/1000
'undo mixing to get back new THROTTLE and STEER values
vC1 = (vM1 + vM2)/2
vC2 = (vM1 - vM2)/2
'truncate in case compensation pushed THROTTLE and/or STEER over limits
IF (vC1 > 1000) THEN
vC1 = 1000
END IF
IF (vC1 < -1000) THEN
vC1 = -1000
END IF
IF (vC2 > 1000) THEN
vC2 = 1000
END IF
IF (vC2 < -1000) THEN
vC2 = -1000
END IF
PRINT ("\n" , "compensated THROTTLE = " , vC1 , " compensated STEER = " , vC2, "\n \n")
RETURN
SetMotors:
SetCommand (_G, 1, vC1)
SetCommand (_G, 2, vC2)
PRINT ("\n" , "motor commands set to: THROTTLE = ", vC1, " STEER = ", vC2, "\n \n")
RETURN