I've got the system working interrupt driven. I do not have proportional controls implemented - yet.
On the bench the car seems as responsive as before. We'll see when I take it to the gym.
One thing I have not figured. How, now, to I disable interrupts when the radio transmitter has been
turned ON? At the very end of this code pack is my old WHILE loop that worked - in the day.
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 channel3 = PORTC.4 ' Define input pin for Channel 3 PWM
' PORTC.0 'High is R/C. Low is PIC control
TRISC = %11110000
DATA $C0,$0C 'his version's checksum goes here.
define ADC_BITS 8
TRISA.0 = 1 'PORTA.0 as analog input for POT
ANSEL.0 = 1
ADCON1 = %00000000 'left justified
' We use low eight bits. 0 -> 255.
ADCON0 = %01000001 'divide 4Mhz clock by 8.
'------------
OSCCON = %01100000 '4 Mhz
DEFINE OSC 4
LED1 VAR PORTD.4
wsave VAR BYTE $70 SYSTEM
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
'------------
'5.0 mSec OR 200 Hz INTERRUPTS
T1CON = %00000001 'Prescaler = 1:1, TMR1ON
'PRELOAD 60543
PreLoad VAR WORD
PreLoad = 60543
TMR1L = PreLoad.LowByte
TMR1H = PreLoad.HighByte
'@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts
'------------
WHEELS VAR WORD
STEERING VAR WORD
'The following may be a big part of the wall hugging probllem
Potread var word
Forward var word ' was fixed 122. Now sweeps 7 steps. See Checkpot
HalfForward VAR WORD ' Potread/2 +x + 1. See Checkpot
Back VAR WORD 'See Checkpot
HalfBack VAR WORD 'See Checkpot
Brake con 100 'Only used just before Main.
Straight con 150 '?
Right CON 130 '?
HalfRight con 135 '?
QuarterRight CON 140 '?
Left con 170 '?
HalfLeft con 165 '?
QuarterLeft con 160 '?
'---------------------------
frontdanger var word
stopreversing var word
frontfree var word
desiredtrack var word
outertrack var word
'---------------------------
'some of the following are not used.
CNT VAR BYTE 'counts interrupts for TRIGGERS
cnt = 0
CNT2 var byte ' counts interrups for wheels and steering
CNT2 = 0
b0 VAR word
rangeright var word
rangeright = 100 'about 6 inches preset
rangefront var word
oldrangeright var word
oldrangefront var word
reversing VAR WORD
reversing = 0
switchtoPIC VAR WORD
switchtoPIC = 1
radiocontrol var word
radiocontrol = 0
steeringleft var word
steeringleft = 0
steeringright var word
steeringright = 0
'-------------------
Checkpot:
' I think Potread goes from 0 to 6.
adcin PORTA.0, Potread
Potread = Potread/37
Forward = 160+Potread
HalfForward = 155+(Potread/2) 'seems to work.
Back = 140-Potread
HalfBack = 145-(Potread/2)
'----------
frontdanger = 210 + Potread*55 '210-540, 14-36 inches
stopreversing = 180 + Potread*25 '180-330, 12-22 inches
frontfree = 700 + Potread*60 '700-1060, 48-70 inches
desiredtrack = 180 + Potread*20 '180-300, 12-20 inches
outertrack = 360 + Potread*15 '360-450, 24-30 inches
'----------
write 2, word Potread
write 4, word frontdanger
write 6, word stopreversing
write 8, word frontfree
write 10, WORD desiredtrack
write 12, word outertrack
'-------------------
@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts
'-------------------
low PORTC.0
'hpwm 1,Straight,50
STEERING = Straight
'hpwm 2,Brake,50
WHEELS = Brake
'pause 4000 'Wait so I can put the car down and get out of the way.
'-------------------
main:
CheckIfInDanger:
if rangefront < frontdanger then
STEERING = Straight
WHEELS = back
reversing = 1
goto reverseoutoftrouble
' loop till this is secured
endif
'-------------------
tryleftturn:
if rangefront < frontfree then
'something ahead - turn hard left.
STEERING = left
WHEELS = halfforward
steeringleft = 1
goto main 'loop till this is secured
endif
'-------------------
goingforward:
STEERING = straight
WHEELS = forward
'-------------------
keepgoingforward: '----Compare to right wall
if rangeright > outertrack then
STEERING = quarterright
endif
if rangeright < desiredtrack and oldrangeright < rangeright then
STEERING = STRAIGHT
endif
if rangeright > desiredtrack and oldrangeright > rangeright then
STEERING = STRAIGHT
endif
if rangeright > desiredtrack and oldrangeright < rangeright then
STEERING = QUARTERRIGHT
endif
if rangeright < desiredtrack and oldrangeright >= rangeright then
STEERING = QUARTERLEFT
endif
goto main
'-------------------------
reverseoutoftrouble:
while rangefront < stopreversing
STEERING = RIGHT
WHEELS = RIGHT
wend
if rangeright > outertrack or rangeright < desiredtrack then
STEERING = LEFT
steeringleft = 1
WHEELS = HALFFORWARD
endif
goto main
'***********INTERRUPT HANDLER************
ToggleLED1:
TMR1L = PreLoad.LowByte ' Load the timer with preload value.
TMR1H = PreLoad.HighByte
TOGGLE LED1
'------------
' Trigger SONAR alternatively every 25 interrupts
if CNT=25 then
pulsr:
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
endif
'-------------
if CNT=50 then
pulsf:
oldrangefront = rangefront
low trigfront
hIGH trigfront
high trigfront
high trigfront
LOW trigfront
pulsin echofront,1,rangefront ' measures the range in 10uS steps
CNT=0
endif
CNT=CNT+1
'--------------
if cnt2=4 then
' PWM pulses are between 1 and 2 millisec.
low PORTC.1
low PORTC.2
pulsout PORTC.1,WHEELS
PULSOUT PORTC.2,STEERING
CNT2=0
endif
CNT2=CNT2+1
@ INT_RETURN
checkchannel3:
'works with four AA batteries pack. Radio receiver puts pulses
'on this line whenever the transmitter is turned ON.
switchtoPIC = 2
while switchtoPIC >= 2
count channel3,65,switchtoPIC
high portc.0
wend
low portc.0
end
Bookmarks