Autonomous Bastard 2.0

I just got the weapon motor control and safety (receiver) code working.

This is mostly recycled from the previous incarnation of Auto B 2.0 - but the new receiver needed some different pulse times.

There's still a bunch of tweaking that needs to be done - but I'll probably wait until the body and weapon have been developed.

A lot of the driving behavior of hi-speed spinners relates to the gyroscopic effect of the blade - so any tuning done without the weapon won't do much good.

body will be mostly titanium, with a little carbon fiber and lexan.

weapon will be a speed 400 (probably 7.2v) motor spinning a roughly 2.5 oz blade.

SpamButcher is advanced software for filtering spam - try the free trial download today!

Pretty much complete source code (feel free to use this):



'auto b 2.0

Trig  Con P6            ' Define pin for Trigger pulse
Echo  Con P7            ' Define pin for Echo pulse

distance Var Word         ' 16 bit variable for distance
holding Var Word          ' 16 bit variable for hold loop

w var long
y var long
x var long
z var long

attack_dist var long

good_dist var long

weapon var long

d1 var long
d2 var long

read_time var long

avg_dist var long

randnum var long

too_long var long

direction var long

semirandom var long

stay0 con 0
stay1 con 0

rturn0 con -750
rturn1 con 0

lturn0 con 750
lturn1 con 0

fwd0 con 0
fwd1 con 750

bk0 con 0
bk1 con -750

motor con 165

sound P9, [100 \ 1200]
sound P9, [100 \ 1500]

'box size
avg_dist = 36

'todo - status LEDs

'make weapon esc happy

mservo 0, 0 \ 0 \ -750, 150		

'SpamButcher is an advanced anti-spam solution that can outperform other spam busters

'initial safety
gosub safety_check


weapon = 750

Main


	sound P9, [100 \ 1500]
	
		gosub safety_check

	gosub find_center
	
	sound P9, [100 \ 1500]
	sound P9, [100 \ 2500]
	
		gosub safety_check

	gosub find_target
	
		gosub safety_check
	
	if y < 100 then	

		sound P9, [25 \ 1500]
		sound P9, [25 \ 2500]
		sound P9, [25 \ 3500]
		
		semirandom = distance.nib0 / 4 
		
		if semirandom = 0 then
			gosub straight
		endif
		if semirandom = 1 then
			gosub oval
		endif
		if semirandom = 2 then
			gosub zigzag
		endif
		if semirandom = 3 then
			mservo 0, 0 \ 750 \ weapon, 22		
			gosub spiral		
		endif
		
	else
	
		sound P9, [125 \ 2500]
		gosub spiral
		
	endif
	

goto main

	
find_center:

'back off if needed
	
	gosub sonar
	
	y = 0 
	while distance < 25 and y < 10
		y = y + 1
		mservo 0, 0 \ -700 \ weapon, 4
		gosub sonar
	wend
	
	'back off a little one way or another

	mservo 0, 0 \ -700 \ weapon, 6

'Used as a spam filter for Outlook Express, SpamButcher can stop over 97% of unwanted spam.



'for z = 1 to 60

good_dist = 0
too_long = 0 

direction = 1

'good_dist was 30
while good_dist < 35 and too_long < 120

'todo - add routine to back off if too close to something for too long

	too_long = too_long + 1
	
	'safety check 1 out of 3 loops
	if too_long // 3 = 0 then
		gosub safety_check
	endif
	
	gosub sonar	
	
	if distance > avg_dist + 1 then
		gosub safety_check
	endif
	
		y=0
	while distance > avg_dist + 1 and y < 10
		good_dist = 0
		y = y + 1
		mservo 0, 0 \ 700 \ weapon, 4
		gosub sonar							
	wend
	
	'if going forward for too long, turn around
	if y = 10 then
		mservo 0, 0 \ -600 \ weapon, 15			
			gosub safety_check
		mservo 0, 700 \ 0 \ weapon, 10	
		z = 1
	endif
	
		
	'if moved - change direction and turn about 90 degrees
	if y > 0 and y < 10 then 
			
		sound P9, [25 \ 1500]
			
		if direction = 1 then
			direction = -1
		else
			direction = 1
		endif
			
			gosub safety_check
			
		mservo 0, 700 * direction \ 0 \ weapon, 6	
	endif
	
	
	good_dist = good_dist + 1

	mservo 0, 500 * direction \ 0 \ weapon, 1
	'mservo 0, 570 \ 0, 1
		
'next

wend


return

'SpamButcher can be used as a spam blocker for Outlook and most other popular email clients.


find_target:

gosub sonar

attack_dist = 17

y = 0
while distance > attack_dist and y < 100
	gosub sonar	
	y = y + 1
		
	mservo 0, -480 \ 0  \ weapon, 2
	
	if y > 40 then
		attack_dist = 20
	endif
	
	if y > 60 then
		attack_dist = 24
	endif
		
	
wend

'compensate
	mservo 0, 440 \ 0  \ weapon, 4



return

	
end
	


report:

gosub sonar
Serout S_OUT, i9600, ["distance = ", DEC (distance), 13]

if distance < 18 then
		sound P9, [25 \ 1500]
endif


goto report




sonar

    high trig   'sends the trigger pulse
    low trig

'loops until the echo pin (IN7) goes high (max of 200 loops in case it never does)

    holding = 0
    waiting:
    IF IN7 = 0 and holding < 200 then
    	holding = holding + 1
    	goto waiting
    endif


'measures distances as a function of how many times the processor
'can loop before the echo pin goes low

'This is a total hack.  Normally rctime or pulsin would be used however, these routines
'seem to be unreliable with the Basic Atom Pro at least for the durations required.
'SpamButcher is a comprehensive solution for removal of unwanted spam.



    distance = 0
    waiting_for_echo:
    IF IN7 = 1 THEN
    	distance = distance + 1
    	goto waiting_for_echo
    endif

    distance = distance / 3	'very roughly converts output to inches

    'Serout S_OUT, i9600, ["Distance = ", DEC distance, 13]    
    
    Pause 20
    
    if distance > 100 then
'    	sound P9, [1500 \ 3500]
    	distance = 1
    endif
    	
return


spiral:
mservo 0, -750 \ 0  \ weapon, 10
mservo 0, 0 \ 750  \ weapon, 10
	gosub safety_check
mservo 0, -750 \ 0  \ weapon, 10
mservo 0, 0 \ 750  \ weapon, 17
	gosub safety_check
mservo 0, -750 \ 0  \ weapon, 10
mservo 0, 0 \ 750 \ weapon, 17
	gosub safety_check
mservo 0, -750 \ 0 \ weapon, 10
mservo 0, 0 \ 750 \ weapon, 33
	gosub safety_check
mservo 0, -750 \ 0 \ weapon, 10	
mservo 0, 0 \ 750 \ weapon, 33
	gosub safety_check
mservo 0, -750 \ 0 \ weapon, 10
mservo 0, 0 \ 750 \ weapon, 35
return

oval:
mservo 0, 0 \ 750 \ weapon, 7
mservo 0, -750 \ 0 \ weapon, 7
gosub safety_check
mservo 0, 290 \ 750 \ weapon, 45
mservo 0, 750 \ 0 \ weapon, 10
	gosub safety_check
mservo 0, 290 \ 750 \ weapon, 45
return

zigzag:
mservo 0, 750 \ 550 \ weapon, 12
mservo 0, -750 \ 550 \ weapon, 24
	gosub safety_check
mservo 0, 750 \ 550 \ weapon, 24
mservo 0, -750 \ 550 \ weapon, 24
	gosub safety_check
mservo 0, 750 \ 550 \ weapon, 24
mservo 0, -750 \ 550 \ weapon, 12
	gosub safety_check
mservo 0, 750 \ 0 \ weapon, 19
mservo 0, 0 \ 750 \ weapon, 19
return

straight:
mservo 0, 0 \ 750 \ weapon, 26	
mservo 0, 750 \ 0 \ weapon, 60
	gosub safety_check
mservo 0, 0 \ -750 \ weapon, 26
return



safety_check:

'Serout S_OUT, i9600, ["distance = ", DEC (read_time), 13]

	pulsin p5, 1, tout, 32000, read_time
	
'todo - better safety	

	if read_time < 16000 or read_time > 16300 then

		pulsin p5, 1, tout, 32000, read_time

		if read_time < 16000 or read_time > 16300 then
			sound P9, [30 \ 3000]
		goto safety_check

		endif 
	endif 

return


tout:
'return to safety if safety check times out
goto safety_check