Andrei IRL
Senior Member
Hey every one.
I have finished my Autobot but it keeps bumping into things. Two servos driving a wheel each. then a servo is used for turning Ultrasonic sensor left and right to establish distance. It works OK but not 100%
It was a lot worse, then I slowed down the servos driving the wheels and it its a bit better now.
It feels like some times by the time the program circled back to MAIN: and then ping sub-routine, the car has already advanced too far forward and bumped into something.
Is there problem with the code?
I have been at it for a few days now and still none the wiser.
I will appreciate any help ye might provide on this.
Thanks very much.
I have finished my Autobot but it keeps bumping into things. Two servos driving a wheel each. then a servo is used for turning Ultrasonic sensor left and right to establish distance. It works OK but not 100%
It was a lot worse, then I slowed down the servos driving the wheels and it its a bit better now.
It feels like some times by the time the program circled back to MAIN: and then ping sub-routine, the car has already advanced too far forward and bumped into something.
Is there problem with the code?
I have been at it for a few days now and still none the wiser.
I will appreciate any help ye might provide on this.
Thanks very much.
Code:
'picaxe 08m
`3rd July 2015
disablebod
setfreq m4
`Specifiyng Inputs and Outputs`
symbol motorL=4 'servo output pins - pin 4
symbol motorR=2 'servo output pins - pin 2
symbol motorN=0 'servo output pins - pin 0
symbol sonic=1 'output pin for ultrasonic - pin 1
`Specifiyng Variables`
symbol distance=w6 'result from ultrasonic
symbol distleft=w5 'distance when looking left
symbol distright=w4 'distance when looking righ
`Specifiyng Constants`
symbol forwardL=170 `Right servo position signal for forward
symbol forwardR=152 `Left servo position signal for forward
symbol reverseL=110 `Right servo position signal for reverse
symbol reverseR=195
`symbol motorLstop=158
`symbol motorRstop=164
symbol blocked=40 'minimum safe distance 40cm
`Servo initiating`
servo motorN,152
pause 1000
servopos motorN,192
pause 1000
servopos motorN,172
pause 1000
servo motorL,forwardL 'set pins as servo outputs - forwards
servo motorR,forwardR 'set pins as servo outputs - forwards
pause 10
Main:
gosub ping 'check to see if anything is infront of the robot
if distance>blocked then ` if nothing ahead then move forward
gosub ahead
goto main
end if
gosub jump `if something is ahead go to jump Sub-routine
goto main
end
jump:
low motorL `turn off left servo
low motorR `turn off right servo
gosub look `go to look sub-routine
if distleft < blocked and distright < blocked then
`high motorL,motorR
gosub backwards `if object to the left and right then reverse for a bit and then spin aroung 180
gosub spin180
goto main
end if
if distleft>distright then
`high motorL,motorR
gosub turnleft
goto main
end if
`high motorL,motorR
gosub turnright
goto main
look:
servopos motorN,200 ' turn the head to the left
pause 500
gosub ping
let distleft=distance
servopos motorN,140 ' turn the head to the right
pause 600
gosub ping
let distright=distance
servopos motorN,172 ' return the head to centre
pause 500
return
ping:
ultra sonic, distance
pause 500
return
ahead:
servo motorL,forwardL
servo motorR,forwardR
return
backwards:
servo motorL,reverseL
servo motorR,reverseR
pause 1000
return
turnright:
servo motorL,168
servo motorR,174
pause 50
return
turnleft:
servo motorL,155
servo motorR,154
pause 50
return
spin180:
servo motorL,120
servo motorR,120
pause 300
return