why does x=y?
alphy
Posts: 12
' {$STAMP BS2}
' {$PBASIC 2.5}
'*** PIN Declarations ***··········· 'Rename the Motors to better fit your ROV
'right=BLUE········ left=ORANGE············ front=GREEN····· back=BROWN
'LEFT
MOTOR_0_A·· PIN·· 14················· 'Direction PIN A for motor0
MOTOR_0_B·· PIN·· 13················ 'Direction PIN B for motor0
MOTOR_0_E·· PIN·· 12················ 'Enable PIN for motor0 (use for PWM)
'RIGHT
MOTOR_1_A·· PIN·· 10················· 'Direction PIN A for motor1
MOTOR_1_B·· PIN·· 9················· 'Direction PIN A for motor1
MOTOR_1_E·· PIN·· 8················· 'Enable PIN for motor1 (use for PWM)
'FRONT
MOTOR_2_A·· PIN·· 6················· 'Direction PIN A for motor2
MOTOR_2_B·· PIN·· 5················· 'Direction PIN A for motor2
MOTOR_2_E·· PIN·· 4················· 'Enable PIN for motor2 (use for PWM)
'BACK
MOTOR_3_A·· PIN·· 2················ 'Direction PIN A for motor3
MOTOR_3_B·· PIN·· 1················ 'Direction PIN A for motor3
MOTOR_3_E·· PIN·· 0················ 'Enable PIN for motor3 (use for PWM)
'SENSOR····· PIN·· 15··············· 'Currently not used
'*** Asign Variables ***
motor0··· VAR·· Byte················ 'Forward/Backward movement
motor1··· VAR·· Byte················ 'Left/Right movement
motor2··· VAR·· Byte················ 'Up/Down movement
y VAR Byte·························· ' FW/BK dir
x VAR Byte·························· ' L/R dir
z VAR Byte·························· ' U/D
y_word VAR Word·························· ' FW/BK dir
x_word VAR Word·························· ' L/R dir
'f VAR Byte
motor0_holder· VAR·· Byte··········· 'Forward/Backward Direction
motor1_holder· VAR·· Byte··········· 'Left/Right direction
motor2_holder· VAR·· Byte··········· 'Up/Down direction
a VAR Word
b VAR Word
repeat··· VAR·· Word················ 'repeat variable
freq····· VAR·· Word················ 'Frequency variable
freq_holder··· VAR·· Byte··········· 'Frequency variable
results·· VAR· Word················· 'store raw sensor input
sensor_data··· VAR· Word············ 'store calibrated sample
DO
freq_holder=255····················· 'The begining frequency number
'Communication**************
HIGH 15
PAUSE 1
RCTIME 15,1 , results
sensor_data= -20 * results + 35048
sensor_data = sensor_data / 1000
sensor_data = results
SEROUT 16, 16780, [noparse][[/noparse]sensor_data]
SERIN 16, 16780, [noparse][[/noparse]x]····· 'serial input=pin 1, 2400, motor0
SERIN 16, 16780, [noparse][[/noparse]y]····· 'serial input=pin 1, 2400, motor1
SERIN 16, 16780, [noparse][[/noparse]z]····· 'serial input=pin 1, 2400, motor2
'SERIN 16, 16780, [noparse][[/noparse]f]
IF x<100 THEN x=0
IF y<100 THEN y=0
IF z<100 THEN z=0
'IF f=1 OR f=2 OR f=3····· THEN
'DO
'HIGH MOTOR_0_A
'HIGH MOTOR_1_A
'HIGH MOTOR_0_E
'HIGH MOTOR_1_E
'LOOP
'ENDIF
'Sensor Goes Here***********
'sensor_data = 34
'SEROUT 16, 16780, [noparse][[/noparse]sensor_data]
'Motor Manipulation Here****
'DEBUG SDEC x ,CR
'DEBUG SDEC y , CR
IF x//2=0 THEN
x_Word=x/2
ELSE
x_Word=x/2 * -1
ENDIF
'
IF y//2=0 THEN
y_Word=y/2
ELSE
y_Word=y/2 * -1
ENDIF
a = x_word + y_word
b = y_word - x_word
'DEBUG SDEC x_word ,CR
'DEBUG SDEC y_word , CR
'DEBUG SDEC a·· , CR
'DEBUG SDEC b·· , CR
IF (a < 65409) AND (a > 32767) THEN
··· a = -127
'
ENDIF
IF (a > 127) AND (a < 32767) THEN
·a=127
· ENDIF
'
IF (b < 65409) AND (b > 32767) THEN
··· b= -127
'
ENDIF
IF (b > 127) AND (b < 32767) THEN
· b=127
ENDIF
'DEBUG SDEC a·· , CR
'DEBUG SDEC b·· , CR
IF a > 32767 THEN· 'test for odd number
·a = ABS a
·a = a * 2
·a = a + 1· 'makes it odd
ELSE
·a = a * 2
ENDIF
IF b > 32767 THEN· 'test for odd number
·b = ABS b
·b = b * 2
·b = b + 1· 'makes it odd
ELSE
·b = b * 2
ENDIF
'DEBUG DEC az·· , CR
'DEBUG DEC b·· , CR
motor0_holder = a
motor1_holder = b
motor2_holder = z
'DIRECTION******************········ '**** what does "//" do, use help to find out ****
IF motor0_holder//2=1 THEN·········· 'determines if byte is even or odd for motor0
· HIGH MOTOR_0_A
· LOW· MOTOR_0_B························ 'foreward
· HIGH MOTOR_1_A
· LOW· MOTOR_1_B
ELSE
· LOW MOTOR_0_A
· HIGH MOTOR_0_B
· LOW MOTOR_1_A························· 'backword
· HIGH· MOTOR_1_B
ENDIF······························· 'ends the statement
IF motor1_holder//2=1 THEN·········· 'determines if byte is even or odd for motor1
··· HIGH MOTOR_1_A
··· LOW· MOTOR_1_B··················· 'TURN LEFT
··· LOW MOTOR_0_A
··· HIGH MOTOR_0_B
ELSE································· 'TURN RIGHT
··· LOW MOTOR_0_B
··· HIGH MOTOR_0_A
··· LOW MOTOR_1_A
··· HIGH MOTOR_1_B
ENDIF······························· 'ends statement
IF motor2_holder//2=1 THEN·········· 'determines if byte is even or odd for motor2
··· HIGH MOTOR_2_A·················· 'GO UP
··· LOW· MOTOR_2_B
··· HIGH· MOTOR_3_A
··· LOW MOTOR_3_B
ELSE
··· LOW MOTOR_2_A
··· HIGH MOTOR_2_B·················· 'GO DOWN
··· LOW MOTOR_3_A
··· HIGH MOTOR_3_B
ENDIF······························· 'ends statement
'SPEED********······················ 'This loop controls the PWM to the motor
repeat=5···························· 'Sets variable 'repeat' at 1
FOR repeat=5 TO 0························ 'Starts main loop
··· motor0=motor0_holder/10········· 'The next four lines are equations that takes the byte
··· motor1=motor1_holder/10········· 'sent down and divides it by 10, this lowers the
··· motor2=motor2_holder/10········· 'resolution but speeds up the loop. Adjust as needed.
··· 'freq=freq_holder/10
···································· ' The following 3 If test are used to set the Dead band of the
···································· ' controller (point the control must pass before it turns on).
···································· ' increase number to increase deadband.
··· IF motor0 > 3·· THEN
··· HIGH MOTOR_0_E
··· HIGH MOTOR_1_E
··· ENDIF·········· 'This causes pin 3 to go high, turning on the motor
··· IF motor1 > 3·· THEN
··· HIGH MOTOR_1_E
··· HIGH MOTOR_0_E
··· ENDIF········· 'This causes pin 4 to go high, turning on the motor
··· IF motor2 > 3·· THEN
··· HIGH MOTOR_3_E
··· HIGH MOTOR_2_E········· 'This causes pin 5 to go high, turning on the motor
··· ENDIF
··· 'Frequency_Loop:·················· 'Starts PWM loop
··· FOR freq=freq_holder/10 TO 0 STEP 3
······· IF motor0 = 0·· THEN········· 'determines if byte equals zero(0)
··········· LOW MOTOR_0_E
··········· LOW MOTOR_1_E············ 'if byte is 0, set emable pin low, turning off the motor,
····································· 'if not 0, pin 3 remains high
······· ENDIF························ 'ends statement
······· IF motor1 = 0·· THEN·········· 'determines if byte equals zero(0)
··········· LOW MOTOR_1_E
··········· LOW MOTOR_0_E············ 'if byte is 0, set emable pin low, turning off the motor,
····································· 'if not 0, pin 4 remains high
······· ENDIF························ 'ends statement
······· IF motor2 = 0·· THEN········· 'determines if byte equals zero(0)
··········· LOW MOTOR_2_E
··········· LOW MOTOR_3_E············ 'if byte is 0, set emable pin low, turning off the motor,
····································· 'if not 0, pin 5 remains high
······· ENDIF························ 'ends statement
······· motor0 = motor0 - 3········· 'subtracts varialbe by 1 within PWM loop
······· motor1 = motor1 - 3·········· 'subtracts varialbe by 1 within PWM loop
······· motor2 = motor2 - 3·········· 'subtracts varialbe by 1 within PWM loop
······· 'freq = freq - 3·············· 'subtracts varialbe by 1 within PWM loop
······· IF freq=0 THEN
··········· 'GOTO Frequency_Loop······ 'as long as freq does not equal zero, the PWM loop repeats
··········· END
······· ENDIF
······· NEXT
··· 'repeat=repeat-1·················· 'subtracts varialbe by 1 after freq=0 and PWM loop stops
··· 'IF repeat<> 0 THEN
······· 'GOTO Repeat_loop············· 'as long as repeat does not equal zero, the reapeat loop repeats
··· 'ENDIF
··· NEXT
LOOP···························· 'go back to run again
END·································· 'Ends the program
for some reason the a value is b and b does nothing, if i turn left it goes foreward, right it goes backward, and foreward and backword do the right thing;
' {$PBASIC 2.5}
'*** PIN Declarations ***··········· 'Rename the Motors to better fit your ROV
'right=BLUE········ left=ORANGE············ front=GREEN····· back=BROWN
'LEFT
MOTOR_0_A·· PIN·· 14················· 'Direction PIN A for motor0
MOTOR_0_B·· PIN·· 13················ 'Direction PIN B for motor0
MOTOR_0_E·· PIN·· 12················ 'Enable PIN for motor0 (use for PWM)
'RIGHT
MOTOR_1_A·· PIN·· 10················· 'Direction PIN A for motor1
MOTOR_1_B·· PIN·· 9················· 'Direction PIN A for motor1
MOTOR_1_E·· PIN·· 8················· 'Enable PIN for motor1 (use for PWM)
'FRONT
MOTOR_2_A·· PIN·· 6················· 'Direction PIN A for motor2
MOTOR_2_B·· PIN·· 5················· 'Direction PIN A for motor2
MOTOR_2_E·· PIN·· 4················· 'Enable PIN for motor2 (use for PWM)
'BACK
MOTOR_3_A·· PIN·· 2················ 'Direction PIN A for motor3
MOTOR_3_B·· PIN·· 1················ 'Direction PIN A for motor3
MOTOR_3_E·· PIN·· 0················ 'Enable PIN for motor3 (use for PWM)
'SENSOR····· PIN·· 15··············· 'Currently not used
'*** Asign Variables ***
motor0··· VAR·· Byte················ 'Forward/Backward movement
motor1··· VAR·· Byte················ 'Left/Right movement
motor2··· VAR·· Byte················ 'Up/Down movement
y VAR Byte·························· ' FW/BK dir
x VAR Byte·························· ' L/R dir
z VAR Byte·························· ' U/D
y_word VAR Word·························· ' FW/BK dir
x_word VAR Word·························· ' L/R dir
'f VAR Byte
motor0_holder· VAR·· Byte··········· 'Forward/Backward Direction
motor1_holder· VAR·· Byte··········· 'Left/Right direction
motor2_holder· VAR·· Byte··········· 'Up/Down direction
a VAR Word
b VAR Word
repeat··· VAR·· Word················ 'repeat variable
freq····· VAR·· Word················ 'Frequency variable
freq_holder··· VAR·· Byte··········· 'Frequency variable
results·· VAR· Word················· 'store raw sensor input
sensor_data··· VAR· Word············ 'store calibrated sample
DO
freq_holder=255····················· 'The begining frequency number
'Communication**************
HIGH 15
PAUSE 1
RCTIME 15,1 , results
sensor_data= -20 * results + 35048
sensor_data = sensor_data / 1000
sensor_data = results
SEROUT 16, 16780, [noparse][[/noparse]sensor_data]
SERIN 16, 16780, [noparse][[/noparse]x]····· 'serial input=pin 1, 2400, motor0
SERIN 16, 16780, [noparse][[/noparse]y]····· 'serial input=pin 1, 2400, motor1
SERIN 16, 16780, [noparse][[/noparse]z]····· 'serial input=pin 1, 2400, motor2
'SERIN 16, 16780, [noparse][[/noparse]f]
IF x<100 THEN x=0
IF y<100 THEN y=0
IF z<100 THEN z=0
'IF f=1 OR f=2 OR f=3····· THEN
'DO
'HIGH MOTOR_0_A
'HIGH MOTOR_1_A
'HIGH MOTOR_0_E
'HIGH MOTOR_1_E
'LOOP
'ENDIF
'Sensor Goes Here***********
'sensor_data = 34
'SEROUT 16, 16780, [noparse][[/noparse]sensor_data]
'Motor Manipulation Here****
'DEBUG SDEC x ,CR
'DEBUG SDEC y , CR
IF x//2=0 THEN
x_Word=x/2
ELSE
x_Word=x/2 * -1
ENDIF
'
IF y//2=0 THEN
y_Word=y/2
ELSE
y_Word=y/2 * -1
ENDIF
a = x_word + y_word
b = y_word - x_word
'DEBUG SDEC x_word ,CR
'DEBUG SDEC y_word , CR
'DEBUG SDEC a·· , CR
'DEBUG SDEC b·· , CR
IF (a < 65409) AND (a > 32767) THEN
··· a = -127
'
ENDIF
IF (a > 127) AND (a < 32767) THEN
·a=127
· ENDIF
'
IF (b < 65409) AND (b > 32767) THEN
··· b= -127
'
ENDIF
IF (b > 127) AND (b < 32767) THEN
· b=127
ENDIF
'DEBUG SDEC a·· , CR
'DEBUG SDEC b·· , CR
IF a > 32767 THEN· 'test for odd number
·a = ABS a
·a = a * 2
·a = a + 1· 'makes it odd
ELSE
·a = a * 2
ENDIF
IF b > 32767 THEN· 'test for odd number
·b = ABS b
·b = b * 2
·b = b + 1· 'makes it odd
ELSE
·b = b * 2
ENDIF
'DEBUG DEC az·· , CR
'DEBUG DEC b·· , CR
motor0_holder = a
motor1_holder = b
motor2_holder = z
'DIRECTION******************········ '**** what does "//" do, use help to find out ****
IF motor0_holder//2=1 THEN·········· 'determines if byte is even or odd for motor0
· HIGH MOTOR_0_A
· LOW· MOTOR_0_B························ 'foreward
· HIGH MOTOR_1_A
· LOW· MOTOR_1_B
ELSE
· LOW MOTOR_0_A
· HIGH MOTOR_0_B
· LOW MOTOR_1_A························· 'backword
· HIGH· MOTOR_1_B
ENDIF······························· 'ends the statement
IF motor1_holder//2=1 THEN·········· 'determines if byte is even or odd for motor1
··· HIGH MOTOR_1_A
··· LOW· MOTOR_1_B··················· 'TURN LEFT
··· LOW MOTOR_0_A
··· HIGH MOTOR_0_B
ELSE································· 'TURN RIGHT
··· LOW MOTOR_0_B
··· HIGH MOTOR_0_A
··· LOW MOTOR_1_A
··· HIGH MOTOR_1_B
ENDIF······························· 'ends statement
IF motor2_holder//2=1 THEN·········· 'determines if byte is even or odd for motor2
··· HIGH MOTOR_2_A·················· 'GO UP
··· LOW· MOTOR_2_B
··· HIGH· MOTOR_3_A
··· LOW MOTOR_3_B
ELSE
··· LOW MOTOR_2_A
··· HIGH MOTOR_2_B·················· 'GO DOWN
··· LOW MOTOR_3_A
··· HIGH MOTOR_3_B
ENDIF······························· 'ends statement
'SPEED********······················ 'This loop controls the PWM to the motor
repeat=5···························· 'Sets variable 'repeat' at 1
FOR repeat=5 TO 0························ 'Starts main loop
··· motor0=motor0_holder/10········· 'The next four lines are equations that takes the byte
··· motor1=motor1_holder/10········· 'sent down and divides it by 10, this lowers the
··· motor2=motor2_holder/10········· 'resolution but speeds up the loop. Adjust as needed.
··· 'freq=freq_holder/10
···································· ' The following 3 If test are used to set the Dead band of the
···································· ' controller (point the control must pass before it turns on).
···································· ' increase number to increase deadband.
··· IF motor0 > 3·· THEN
··· HIGH MOTOR_0_E
··· HIGH MOTOR_1_E
··· ENDIF·········· 'This causes pin 3 to go high, turning on the motor
··· IF motor1 > 3·· THEN
··· HIGH MOTOR_1_E
··· HIGH MOTOR_0_E
··· ENDIF········· 'This causes pin 4 to go high, turning on the motor
··· IF motor2 > 3·· THEN
··· HIGH MOTOR_3_E
··· HIGH MOTOR_2_E········· 'This causes pin 5 to go high, turning on the motor
··· ENDIF
··· 'Frequency_Loop:·················· 'Starts PWM loop
··· FOR freq=freq_holder/10 TO 0 STEP 3
······· IF motor0 = 0·· THEN········· 'determines if byte equals zero(0)
··········· LOW MOTOR_0_E
··········· LOW MOTOR_1_E············ 'if byte is 0, set emable pin low, turning off the motor,
····································· 'if not 0, pin 3 remains high
······· ENDIF························ 'ends statement
······· IF motor1 = 0·· THEN·········· 'determines if byte equals zero(0)
··········· LOW MOTOR_1_E
··········· LOW MOTOR_0_E············ 'if byte is 0, set emable pin low, turning off the motor,
····································· 'if not 0, pin 4 remains high
······· ENDIF························ 'ends statement
······· IF motor2 = 0·· THEN········· 'determines if byte equals zero(0)
··········· LOW MOTOR_2_E
··········· LOW MOTOR_3_E············ 'if byte is 0, set emable pin low, turning off the motor,
····································· 'if not 0, pin 5 remains high
······· ENDIF························ 'ends statement
······· motor0 = motor0 - 3········· 'subtracts varialbe by 1 within PWM loop
······· motor1 = motor1 - 3·········· 'subtracts varialbe by 1 within PWM loop
······· motor2 = motor2 - 3·········· 'subtracts varialbe by 1 within PWM loop
······· 'freq = freq - 3·············· 'subtracts varialbe by 1 within PWM loop
······· IF freq=0 THEN
··········· 'GOTO Frequency_Loop······ 'as long as freq does not equal zero, the PWM loop repeats
··········· END
······· ENDIF
······· NEXT
··· 'repeat=repeat-1·················· 'subtracts varialbe by 1 after freq=0 and PWM loop stops
··· 'IF repeat<> 0 THEN
······· 'GOTO Repeat_loop············· 'as long as repeat does not equal zero, the reapeat loop repeats
··· 'ENDIF
··· NEXT
LOOP···························· 'go back to run again
END·································· 'Ends the program
for some reason the a value is b and b does nothing, if i turn left it goes foreward, right it goes backward, and foreward and backword do the right thing;
Comments
a = x_word + y_word a=500
b = y_word - x_word b=200
'DEBUG SDEC x_word ,CR
'DEBUG SDEC y_word , CR
'DEBUG SDEC a , CR
'DEBUG SDEC b , CR
IF (a < 65409) AND (a > 32767) THEN
a = -127
'
ENDIF
IF (a > 127) AND (a < 32767) THEN TRUE, so A=127
a=127
ENDIF
'
IF (b < 65409) AND (b > 32767) THEN
b= -127
'
ENDIF
IF (b > 127) AND (b < 32767) THEN TRUE, so B=127
b=127
ENDIF
'DEBUG SDEC a , CR
'DEBUG SDEC b , CR
IF a > 32767 THEN 'test for odd number
a = ABS a
a = a * 2
a = a + 1 'makes it odd
ELSE
a = a * 2 A=254
ENDIF
IF b > 32767 THEN 'test for odd number
b = ABS b
b = b * 2
b = b + 1 'makes it odd
ELSE
b = b * 2 B=254
ENDIF
End result is A=B=254. Also, your "test for odd number" routine will probably never execute. You have already forced A & B to be either +/-127 or less than 127. I guess there could be a few rare exceptions when it is greater than 65,409 and would execute, but I'm thinking that might not be what you intend.