Got a new video to show you all. The code is much the same as the last time. I included it below.
The video speaks for itself. Would this be a good context for a public school engineering project? It certainly shows the real world as I know it. The kids could see whether they can make the car go around the room staying outside traffic cones which define the inside of the course.
Code:
'************************************************* ***************
SYMBOL trigright = PORTB.0 ' Define output pin for Trigger pulse
SYMBOL trigfront = PORTB.1 ' Define output pin for Trigger pulse
SYMBOL trigleft = PORTB.3 ' Define output pin for TRigger pulse
SYMBOL echoright = PORTC.6 ' Define input pin for Echo pulse
SYMBOL echofront = PORTC.5 ' Define input pin for Echo pulse
SYMBOL echoleft = PORTC.7 ' Define input pin for Echo pulse
SYMBOL radiotransmitterON = PORTC.4 ' Define input pin for Signal
'inside radio receiver indicatin that the transmitter has
'been turn on. Used to stop the car by turning ON the xmitter.
TRISC = %11110000
DATA $1C,$B5'This version's checksum goes here.
' Speed of sound echo is about 2mS per foot distance.
' define pulsin_max 2800 '28mS SF05 spec says it drops at 30mS.
' don't forget channel 3 every 20mS or 50Hz.
'--------------------
frontdanger CON 180 'about 12 inches. Was 450 about 30 inches
stopreversing CON 390' about 26 inches
frontfree CON 1080 'about 72 inches. Was 800 about 53 inches.
desiredtrack CON 540 'about 36 inches. Now only one line.
'outertrack con 450 'about 30 inches. Was 360 about 24 inches
SYMBOL turnon = PORTC.3 ' the signal that if HIGH makes the car steer straight
SYMBOL steerto = PORTC.2 ' the signal that if on turning is on, then decides
'the direction
SYMBOL stopgo =PORTC.1 ' the signal that if HIGH makes the car go
SYMBOL forrev =PORTC.0 ' the signal that if on and is not on stop makes
'the car reverse
'---------------------------
rangeleft VAR WORD 'experimental timing.
rangeright VAR WORD
rangefront VAR WORD
oldrangeright VAR WORD
oldrangefront VAR WORD
CNTR VAR WORD
CNTR = 0
CNTL VAR WORD
CNTL = 0
steeringstate VAR WORD
steeringstate = 0 ' 0=straight,1=left,2=right,3=skid left,4=rightback,
'--------------
'Looks like I need to kick the steering back into straight.
LOW stopgo 'stop
HIGH turnon
'pause 2000 'Wait so I can put the car down and get out of the way.
main:
GOSUB triggers
CheckIfInDanger:
IF (rangefront < frontdanger) AND (steeringstate <> 3) THEN
HIGH stopgo 'go
LOW forrev 'back
LOW turnon 'steer
LOW steerto 'right
steeringstate = 4
GOTO reverseoutoftrouble
' loop till this is secured
ENDIF
tryleftturn:
IF rangefront < frontfree THEN
'something ahead - turn hard left.
'gosub leftforward
IF (steeringstate <> 3) THEN
HIGH stopgo 'go
LOW forrev 'reverse
LOW turnon 'steer
HIGH steerto 'left
steeringstate = 3 'skid left
PAUSE 150
ENDIF
HIGH steerto 'left
HIGH stopgo 'go
HIGH forrev 'forward
GOTO main 'Loop until secured
ENDIF
goingforward:
keepgoingforward:
IF (rangeright < desiredtrack) THEN
'steer left
CNTL = (CNTL+1)
IF CNTL = 2 THEN
LOW turnon 'steer
HIGH steerto 'left
steeringstate = 1
HIGH stopgo 'go
HIGH forrev 'forward
CNTL = 0
ENDIF
PAUSE 150
ENDIF
IF (rangeright > desiredtrack)THEN
'steer right
CNTR = (CNTR+1)
IF CNTR = 2 THEN
LOW turnon 'steer
LOW steerto 'right
steeringstate = 2
HIGH stopgo 'go
HIGH forrev 'forward
CNTR = 0
ENDIF
PAUSE 150
ENDIF
IF steeringstate = 1 THEN 'left
LOW steerto 'right
PAUSE 100
ENDIF
IF steeringstate = 2 THEN 'right
HIGH steerto 'left
PAUSE 60
ENDIF
HIGH turnon
steeringstate = 0
GOTO main
'-------------------------
reverseoutoftrouble:
' below seems reduncant except for the pause.
LOW turnon 'steer
LOW steerto 'right
steeringstate = 4 'new steeringstate
HIGH stopgo 'go
LOW forrev 'back
PAUSE 200 'added to do some reversing. We have a problem of bumping
'against the wall and not backing up. This pause should fix that.
keepreversing:
WHILE (rangefront < stopreversing) OR ((oldrangefront = rangefront) AND (oldrangeright = rangeright))
' We have not reversed far enough
HIGH stopgo 'go
LOW forrev 'back
PAUSE 250 'maybe this will get car to back off wall
GOSUB triggers
WEND
'------------------------------------
'done reversing. Go straight or turn left depending on right
'ping. If > outertrack then out in the middle. If < desiredtrack
'then stuck on wall. This did not work. See TOY_CAR_KICK_TURN_3.wmv
'Try no IF statement.
'if rangeright > desiredtrack then
'steer left
LOW turnon 'steer
HIGH steerto 'left
steeringstate = 1
HIGH stopgo 'go
HIGH forrev 'forward
PAUSE 250
'hpwm 2,Forward,50 --Already going Forward
'endif
GOTO main
'--------------------------
triggers: ' Check two sonars and xmitter ON signal.
oldrangeright = rangeright
' produce 10uS trigger pulse (must be minimum of 10uS)
LOW trigright
HIGH trigright
HIGH trigright
HIGH trigright
LOW trigright
'zero could be legal below
PULSIN echoright,1,rangeright 'measures the range in 10uS steps
PAUSE 10 'Wait for ringing to stop - read SF05 spec.
'pulsin echoleft,1,rangeleft 'see if this slows the triggers to one
'per second. That it did, but it crashed straight into the wall.
pulsf:
oldrangefront = rangefront
LOW trigfront
HIGH trigfront
HIGH trigfront
HIGH trigfront
LOW trigfront
PULSIN echofront,1,rangefront ' measures the range in 10uS steps
PAUSE 10
radioON:
' make ON = LOW for 1/10 toy car to radiotransmitter
do WHILE radiotransmitterON = 0
LOW stopgo
loop
HIGH stopgo 'get going again
RETURN
END
Bookmarks