sensor
tina
Posts: 3
hi...I have a code for my quad crawler to walk and sense objects ...for some reasons it started walking just in backward· direction after it sense an object.Please help me out with this one..
Set_Direction:
· SELECT dir
··· CASE Ahead
····· ptrEEPROM = Forward
····· GOSUB Equal
··· CASE ForwardRight
····· ptrEEPROM = Forward
····· GOSUB SlowRight
··· CASE RightSpin
····· ptrEEPROM = RSpin
····· GOSUB Equal
··· CASE BackRight
····· ptrEEPROM = Back
····· GOSUB SlowRight
···· CASE Backward
····· ptrEEPROM = Back
····· GOSUB Equal
··· CASE BackLeft
····· ptrEEPROM = Back
····· GOSUB SlowLeft
··· CASE LeftSpin
····· ptrEEPROM = LSpin
····· GOSUB Equal
···· CASE ForwardLeft
····· ptrEEPROM = Forward
····· GOSUB SlowLeft
· ENDSELECT
SetDelay:
· IF vel < 9 THEN
··· delay = 30
· ELSE
··· delay = 10
· ENDIF
·SELECT dist
··· CASE < 10
····· ptrEEPROM = Back
··· CASE < 15
····· ptrEEPROM = LSpin
··· CASE < 30
····· ptrEEPROM = Forward
····· GOSUB SlowLeft
··· CASE ELSE
····· ptrEEPROM = Forward
····· GOSUB Equal
· ENDSELECT
RETURN
Write_PSC:
· SEROUT PSC,Baud,[noparse][[/noparse]"!SC",ServoAddr,
········ Ramp,servoPosition.LOWBYTE,
········ servoPosition.HIGHBYTE, CR]
· IF rowCounter = 1 THEN
··· PAUSE delay
· ENDIF
RETURN
Get_Sonar:
· Ping = IsLow
· PULSOUT Ping, Trigger
· PULSIN· Ping, IsHigh, dist
· dist = dist */ Scale
· dist = dist / 2··························
RETURN
C_Centimeter:
· dist = dist ** RawToCm··················· ' convert to centimeters
RETURN
Set_Direction:
· SELECT dir
··· CASE Ahead
····· ptrEEPROM = Forward
····· GOSUB Equal
··· CASE ForwardRight
····· ptrEEPROM = Forward
····· GOSUB SlowRight
··· CASE RightSpin
····· ptrEEPROM = RSpin
····· GOSUB Equal
··· CASE BackRight
····· ptrEEPROM = Back
····· GOSUB SlowRight
···· CASE Backward
····· ptrEEPROM = Back
····· GOSUB Equal
··· CASE BackLeft
····· ptrEEPROM = Back
····· GOSUB SlowLeft
··· CASE LeftSpin
····· ptrEEPROM = LSpin
····· GOSUB Equal
···· CASE ForwardLeft
····· ptrEEPROM = Forward
····· GOSUB SlowLeft
· ENDSELECT
SetDelay:
· IF vel < 9 THEN
··· delay = 30
· ELSE
··· delay = 10
· ENDIF
·SELECT dist
··· CASE < 10
····· ptrEEPROM = Back
··· CASE < 15
····· ptrEEPROM = LSpin
··· CASE < 30
····· ptrEEPROM = Forward
····· GOSUB SlowLeft
··· CASE ELSE
····· ptrEEPROM = Forward
····· GOSUB Equal
· ENDSELECT
RETURN
Write_PSC:
· SEROUT PSC,Baud,[noparse][[/noparse]"!SC",ServoAddr,
········ Ramp,servoPosition.LOWBYTE,
········ servoPosition.HIGHBYTE, CR]
· IF rowCounter = 1 THEN
··· PAUSE delay
· ENDIF
RETURN
Get_Sonar:
· Ping = IsLow
· PULSOUT Ping, Trigger
· PULSIN· Ping, IsHigh, dist
· dist = dist */ Scale
· dist = dist / 2··························
RETURN
C_Centimeter:
· dist = dist ** RawToCm··················· ' convert to centimeters
RETURN
Comments
Also, is this your full code? I don't see any sort of main program loop that tells what is called where, and also a variable declaration might be of issue here.
Finally, I notice that some of your functions have returns, others don't. While it might work, you may want to go all returns or all GOTOs. Mix and match can make for more difficult code to read.
On a syntax level, code is generally easier to read if you choose either GOSUB or GOTO, and avoid mixing them. Personally, I prefer GOSUB since it fits better an OO paradigm.
Also, when there is lots of code, use the attachment manager to attach it so that it keeps the native format (.bs2) and can be opened in the editor. If you have short blocks of code and you want to embed it into your post, use the [noparse][[/noparse]code] tags so that it keeps the format. You'll get better answers that way if you limit how much effort it takes to read your post.
Cheers.