#picaxe40X2
setfreq m16
'Outputs for lights
Symbol BLKL = B.6 'Blinker Left
Symbol BLKR = A.2 'Blinker Right
Symbol BRKL = D.6 'Brake Lights
Symbol HL = D.4 'Headlights
Symbol RUNL = C.7 'Running Lights
Symbol FOGL = D.7 'Fog Lights
Symbol REVL = C.6 'Reverse Light
Symbol BLUE1 = D.2 'Blue Light 1
Symbol BLUE2 = D.1 'Blue Light 2
Symbol BLUE3 = D.0 'Blue Light 3
Symbol BLUE4 = C.3 'Blue Light 4
Symbol AMBL1 = C.5 'Ambient Light 1
Symbol AMBL2 = C.4 'Ambient Light 2
Symbol AMBL3 = D.3 'Ambient Light 3
'Outputs for main motor
Symbol Motor1 = D.5 'Motor connection 1
Symbol Motor2 = C.2 'Motor connection 2
Symbol PSave = B.7 'MPC17C724 on/off signal
'Outputs for servos
Symbol Servo1 = B.3 'Servo 1
Symbol Servo2 = B.4 'Servo 2
Symbol Servo3 = B.5 'Servo 3
'Transistor buffered outputs for AUX loads up to 100mA each
Symbol AUX1 = C.1 'AUX1 exit (buffered up to 100mA)
Symbol AUX2 = C.0 'AUX2 exit (buffered up to 100mA)
'IR receiver inputs
Symbol IRSENS1 = B.1 'IR receiver 1 in front of the car
Symbol IRSENS1Pin = pinB.1
Symbol IRSENS2 = B.2 'IR receiver 2
Symbol IRSENS2Pin = pinB.2
'IR outputs
Symbol IRLEDL = A.3 'IRLED in the back of the vehicle - left hand side
Symbol IRLEDR = A.6 'IRLED in the back of the vehicle - right hand side
Symbol IRLEDID = A.5 'IRLED for signaling ID of the vehicle - underbody
'For serial connections to additional devices
Symbol DATApin = A.0
Symbol CLKpin = A.1
'Not assigned and not connected
Symbol A7pin = A.7
Symbol B0pin = B.0
'Byte variables
Symbol Hundreds = b1
Symbol Tenths = b2
Symbol Units = b3
Symbol ID = b4 'Vehicle ID
Symbol IRCode = b6 'IR code received
Symbol LoopCounter = b7 'Main Loop counter
Symbol Speed = b8 'Current speed value
Symbol NewSpeedBEMF = b16
Symbol NewSpeed = b9 'New speed value
Symbol BLKCounter = b14 'Counter for blinkers
Symbol BRKLCounter = b15 'Counter for brake lights
Symbol IDCounter = b16
Symbol Counter = b17 'just a counter
'Flag variables
Symbol BLKLONFlag = b10
Symbol BLKRONFlag = b11
Symbol BRKLONFlag = b12
Symbol OvertakingAllowedModeFlag = b13
Symbol DistanceVal = b14
'Word variables
Symbol LiPoVolts = w27 'Battery voltage storage value
Symbol BackEMF = w26 'Main motor back-EMF value
Symbol BackEMFAgr = w25 'Agregated BackEMF for average value calculation
Symbol SpeedBEMF = w24
Symbol MinSpeedBEMF = w23
Symbol MaxSpeedBEMF = w22
'Constants
Symbol BatteryLowTresholdTenths = 3 'Set up "Battery Low" voltage here
Symbol BatteryLowTresholdUnits = 8
Symbol SlowDn = 20
Symbol BEMFFactor = 15
init:
' gosub LoadEEPROM
low BLKL
low BLKR
low BRKL
low RUNL
low HL
low BLUE1
low REVL
low Motor1
low Motor2
low PSave 'to switch the MPC17C724 on
low AUX1
low AUX2
BLKLONFlag = 0
BLKRONFlag = 0
BRKLONFlag = 0
OvertakingAllowedModeFlag = 0
LoopCounter = 0
BLKCounter = 0
BRKLCounter = 0
Speed = 150 'practical MIN= 55(~14%); MAX=190(~48%)
'theoretical 25% = 100; 100% = 400
NewSpeed = 150
SpeedBEMF = 150
'Show the decoder is on
high BLKL : high BLKR
pause 1000
low BLKL : low BLKR
pause 1000
high BLKL : high BLKR
pause 1000
low BLKL : low BLKR
main:
inc LoopCounter
'Set up PWM for the main motor
hpwm 0, 0, %0001, 100, Speed 'forward
' hpwm 0, 0, %0010, 100, Speed 'backward
'IR receiver is idling high, so on incoming signal, the pin goes low
if IRSENS1Pin = 0 then
irin IRSENS1, IRCode
select case IRCode
; case 9 to 40 : NewSpeed = IRCode
; START OF CODE FOR DEBUGING SPEED CONTROL WITH PICAXE/SONY REMOTE
case 11 : if NewSpeed < 190 then : inc NewSpeed : endif
case 98 : if NewSpeed > 55 then : dec NewSpeed : endif
; END OF CODE FOR DEBUGING SPEED CONTROL WITH PICAXE/SONY REMOTE
case 41 : gosub SetUpID
case 61 : NewSpeed = 1
case 62, 0 : BLKLONFlag = 1
case 63, 1 : BLKRONFlag = 1
case 64, 2 : BLKLONFlag = 1 : BLKRONFlag = 1
case 65, 3 : gosub BlinkersOFF
case 66, 4 : high RUNL
case 67, 5 : low RUNL
case 68, 6 : high HL
case 69, 7 : low HL
case 97 : OvertakingAllowedModeFlag = 1
case 98 : OvertakingAllowedModeFlag = 0
endselect
endif
're-write to case statements?
'decrease speed if lower speed command was received
if Speed > NewSpeed then 'deaccelerate, break
dec Speed
hpwmduty Speed
BRKLONFlag = 1 'to start brake light
BRKLCounter = 0 'to restart the brake light in case more than one step down
pause SlowDn
endif
'increase speed if higher speed command was received
if Speed < NewSpeed then 'accelerate
inc Speed
hpwmduty Speed
low BRKL
BRKLONFlag = 0
pause SlowDn
endif
if Speed = NewSpeed then
SpeedBEMF = Speed * 1024 / 4 / 100 + 400
MinSpeedBEMF = SpeedBEMF - BEMFFactor
MaxSpeedBEMF = SpeedBEMF + BEMFFactor
Select case BackEMF
case <= MinSpeedBEMF
NewSpeed = NewSpeed + 10
case >= MaxSpeedBEMF
NewSpeed = NewSpeed - 10
endselect
debug Speed
endif
'Brake light ON for 5 loops
if BRKLONFlag = 1 then
select case BRKLCounter
case <= 4
high BRKL
inc BRKLCounter
case 5
low BRKL
BRKLCounter = 0
BRKLONFlag = 0
endselect
endif
'left blinker ON phase for 5 loops
if BLKLONFlag = 1 and BLKRONFlag = 0 then
select case BLKCounter
case <= 4
high BLKL
inc BLKCounter
case 5 to 9 'switch over to OFF phase for 5 loops
low BLKL
inc BLKCounter
case 10
BLKCounter = 0
endselect
endif
'right blinker ON phase for 10 loops
if BLKLONFlag = 0 and BLKRONFlag = 1 then
select case BLKCounter
case <= 4
high BLKR
inc BLKCounter
case 5 to 9 'switch over to OFF phase for 10 loops
low BLKR
inc BLKCounter
case 10
BLKCounter = 0
endselect
endif
'hazzard lights ON phase for 10 loops
if BLKLONFlag = 1 and BLKRONFlag = 1 then
select case BLKCounter
case <= 4
high BLKL
high BLKR
inc BLKCounter
case 5 to 9 'switch over to OFF phase for 10 loops
low BLKL
low BLKR
inc BLKCounter
case 10
BLKCounter = 0
endselect
endif
irout IRLEDID, 1, ID
irout IRLEDL, 1, Speed
if OvertakingAllowedModeFlag = 0 then 'if overtaking is not allowed, operate rear right hand side IR LED as well
irout IRLEDR, 1, Speed
endif
'Things to do every 10th loop
if LoopCounter = 10 then
'LiPo test
calibadc10 LiPoVolts
LiPoVolts = 10486/LiPoVolts
bintoascii LiPoVolts,Hundreds,Tenths,Units
Sertxd (Tenths,".",Units,10,13)
if Units = 8 then
gosub BatteryLow
endif
'Reset counter
LoopCounter = 0
'Back-EMF code (has to remain the last thing of the loop)
hpwm OFF 'switch off the motor
high PSave 'MPC17C724 outputs to high impedance
pause 4
readadc10 12, BackEMF 'input ADC12, B.0 for now
sertxd (#BackEMF,10,13)
low PSave
endif
goto main
BlinkersOFF:
low BLKL
low BLKR
BLKLONFlag = 0
BLKRONFlag = 0
BLKCounter = 0
return
BatteryLow:
return
SetUpID:
write 0, IRCode
for IDCounter = 1 to IRCode 'recognize the new ID by blinking it with hazzard lights
high BLKL : high BLKR
pause 2000
low BLKL : low BLKR
pause 2000
next IDCounter
return
LoadEEPROM:
read 0, ID
if ID = 0 then
write 0, 1
read 0, ID
endif
return