Shop OBEX P1 Docs P2 Docs Learn Events
My Very First Basic Stamp project. Ping activated turret gun! — Parallax Forums

My Very First Basic Stamp project. Ping activated turret gun!

wabbitwabbit Posts: 16
edited 2008-03-27 01:53 in Robotics
Hi all,

I bough the boebot kit and went through the book.

This is my first project using a basic stamp and programming.

I have a Ping sensor mounted on top of a servo. It scans back and forth looking for any object closer than 30 in. When it locates and object the toy disc shooter which is also mounted on a servo swings to the same position and fires.

The major challenge I had was with the toy gun. I thought since the gun took battery that it would be all electric and therefore easy for the basic stamp to fire. It turns out the gun has an electric motor that spins up to speed when you turn the toy on. Then it has a mechanical linkage that pushes a foam disk into the motor when the trigger is pressed. I had to use another servo to activate the trigger.

top.jpg


trigger.jpg


Here is a video of it working:
http://youtube.com/watch?v=p3me7-E5SOc

To turn on the motor in the toy I am using a 5v reed relay. I soldered a diode across it to protect the stamp.

I made this mainly to annoy my coworkers as they walk past my cubicle.

Here is the program: This is my first ever program. Please let me know if anything could be streamlined or enhanced in the code.

' {$STAMP BS2}
' {$PBASIC 2.5}
'
pin locations for each component
'p6 = ping sensor
'p8 = relay to activate dc motor
'p10 = piezo speaker
'p12 (servo plug) = sweeping servo that ping sensor is attached to
'p13 (servo plug) = servo that gun is attached to
'p14 (servo plug) = servo that activates mechanical trigger on toy gun
'
constants and variables
inconstant CON 890······························ 'constant to divide raw ping data to convert to inches. Room Temp
counterstep CON 150····························· 'this is the amount of steps the sweep will go through.
'
change this constant to change the trigger range- This number is in inches
trigger CON 30································· 'trigger distance
'
direction VAR Word
ammo VAR Word
counter VAR Word································ 'this counter is for the servo sweep
sweep VAR Word
gunsweep VAR Word······························· 'this counter repeats the sweep until servo is in position
indistance VAR Word
time VAR Word
beeper VAR Word
'
sets trigger servo to neutral start position and motor relay to off
LOW 8·············································· 'sets fire relay to off
FOR counter = 1 TO 20······························ 'sets trigger servo to neutral
··· PULSOUT 14,750
··· PAUSE 20
· NEXT
'
beeps 5 times then slowly scans ping servo right to left (just for show!)
FOR counter = 1 TO 5······························· 'beep at start
· FREQOUT 10, 250, 3500
· PAUSE 100
NEXT
··················································· 'sweep to start position - just for show
FOR counter = 1000 TO 500 STEP 3
····· PULSOUT 12, counter
····· PAUSE 10
NEXT
'
starts scanning/firing sequence
DO·················································· 'start scanning
··· FOR direction = 500 TO 1050 STEP counterstep
··· FOR sweep = 1 TO 6
····· PULSOUT 12, direction
····· PAUSE 10
··· NEXT
····· PULSOUT 6,5··································· 'ping
····· PULSIN 6,1,time
····· indistance = inconstant ** time··············· ' convert ping input to inches
····· DEBUG HOME, DEC5 time, " ping raw data", CR,·· 'displays raw ping data
····· DEC3 indistance, " in", CR,························ 'displays ping results in inches
····· DEC2 ammo, " ammo count"················ 'displays the amount of ammo left
····· PAUSE 10
··· IF indistance = <trigger THEN
····· GOSUB fire
····· ELSE
··· ENDIF
· NEXT

· FOR direction = 900 TO 550 STEP counterstep
··· FOR sweep = 1 TO 6
····· PULSOUT 12, direction
····· PAUSE 10
··· NEXT
····· PULSOUT 6,5··································· 'ping
····· PULSIN 6,1,time
····· indistance = inconstant ** time··············· ' convert ping input to inches
····· DEBUG HOME, DEC5 time, " ping raw data", CR,·· 'displays raw ping data
····· DEC3 indistance, " in"························ 'displays ping results in inches
····· PAUSE 10
··· IF indistance = <trigger THEN··················· 'triggers firing sequence
····· GOSUB fire
····· ELSE
··· ENDIF
· NEXT
LOOP

fire:··············································· 'activates firing relay
ammo = ammo + 1
HIGH 8
FOR gunsweep = 1 TO 10······························ 'move to fire position of ping servo
· PULSOUT 13, direction
· PAUSE 20
NEXT

PAUSE 300··········································· 'pause long enough for dc motor to spin up to speed
· FOR counter = 1 TO 20····························· 'servo to activate mechanical trigger
··· PULSOUT 14,600
··· PAUSE 20
· NEXT
LOW 8
· FOR counter = 1 TO 20····························· 'return trigger servo to neutral
··· PULSOUT 14,750
··· PAUSE 20
· NEXT

FOR gunsweep = 1 TO 10······························ 'return to center after fire
· PULSOUT 13, 750
· PAUSE 20
NEXT
IF ammo = 10 THEN··································· 'check how much ammo is left
· GOSUB ammoout
· ELSE
ENDIF
RETURN
'
if ammo out return servos to neutral and beep 10 times then shutdown
ammoout:
FOR gunsweep = 1 TO 10······························ 'return to center after ammo out
· PULSOUT 13, 750
· PULSOUT 12, 750
· PAUSE 20
NEXT
FOR beeper = 1 TO 10
· FREQOUT 10, 250,3500
· PAUSE 100
· NEXT
END

Comments

  • James LongJames Long Posts: 1,181
    edited 2008-03-16 23:51
    I'm sorry....but that is classic. A very good first project, and it looks as if you are pretty satisfied with the result.

    You didn't say if the gun was auto repeat. (it keeps shooting if the target doesn't move).

    Sitll a very nice first project.....

    James L

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    James L

    Partner/Designer
    Lil Brother LLC (SMT Assembly Services)
  • wabbitwabbit Posts: 16
    edited 2008-03-17 00:09
    Its not auto repeat. But that would be nice wouldnt it?!

    I guess I'll get started on it.

    I did program it to keep track of how many discs it fires so that it will shut down when it is finished.



    I am also thinking of adding a "doomsday button" (big red button that says "do not push") When pushed the gun will sweep back and forth firing every disc·as fast as possible.



    I need to get a big red button!
  • wabbitwabbit Posts: 16
    edited 2008-03-17 01:21
    adjusted the subroutine code so that after the gun turns to position and the dc motor has come up to speed it will pings again to see if the target is still there.

    Also after each trigger pull it pings to see if the target is still there. If yes it keeps firing (pinging between each fire) until the target leaves or it runs out of ammo.

    Heres the revised code


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    pin locations for each component
    'p6 = ping sensor
    'p8 = relay to activate dc motor
    'p10 = piezo speaker
    'p12 (servo plug) = sweeping servo that ping sensor is attached to
    'p13 (servo plug) = servo that gun is attached to
    'p14 (servo plug) = servo that activates mechanical trigger on toy gun
    '
    constants and variables
    inconstant CON 890······························ 'constant to divide raw ping data to convert to inches. Room Temp
    counterstep CON 150····························· 'this is the amount of steps the sweep will go through.
    '
    change this constant to change the trigger range- This number is in inches
    trigger CON 15································· 'trigger distance
    '
    direction VAR Word
    ammo VAR Word
    counter VAR Word································ 'this counter is for the servo sweep
    sweep VAR Word
    gunsweep VAR Word······························· 'this counter repeats the sweep until servo is in position
    indistance VAR Word
    time VAR Word
    beeper VAR Word
    '
    sets trigger servo to neutral start position and motor relay to off
    LOW 8·············································· 'sets fire relay to off
    FOR counter = 1 TO 20······························ 'sets trigger servo to neutral
    ··· PULSOUT 14,750
    ··· PAUSE 20
    · NEXT

    '
    beeps 5 times then slowly scans ping servo right to left (just for show!)
    FOR counter = 1 TO 5······························· 'beep at start
    · FREQOUT 10, 250, 3500
    · PAUSE 100
    NEXT
    ··················································· 'sweep to start position - just for show
    FOR counter = 1000 TO 500 STEP 3
    ····· PULSOUT 12, counter
    ····· PULSOUT 13, counter
    ····· PAUSE 10
    ··· NEXT
    FOR counter = 1 TO 20······························ 'sets gun to neutral
    ··· PULSOUT 13,750
    ··· PAUSE 20
    NEXT

    '
    starts scanning/firing sequence
    DO·················································· 'start scanning
    ··· FOR direction = 500 TO 1050 STEP counterstep
    ··· FOR sweep = 1 TO 6
    ····· PULSOUT 12, direction
    ····· PAUSE 10
    ··· NEXT
    ····· PULSOUT 6,5··································· 'ping
    ····· PULSIN 6,1,time
    ····· indistance = inconstant ** time··············· ' convert ping input to inches
    ····· DEBUG HOME, DEC5 time, " ping raw data", CR,·· 'displays raw ping data
    ····· DEC3 indistance, " in", CR,························ 'displays ping results in inches
    ····· DEC2 ammo, " ammo count"················ 'displays the amount of ammo left
    ····· PAUSE 10
    ··· IF indistance = <trigger THEN
    ····· GOSUB fire
    ····· ELSE
    ··· ENDIF
    · NEXT

    · FOR direction = 900 TO 550 STEP counterstep
    ··· FOR sweep = 1 TO 6
    ····· PULSOUT 12, direction
    ····· PAUSE 10
    ··· NEXT
    ····· PULSOUT 6,5··································· 'ping
    ····· PULSIN 6,1,time
    ····· indistance = inconstant ** time··············· ' convert ping input to inches
    ····· DEBUG HOME, DEC5 time, " ping raw data", CR,·· 'displays raw ping data
    ····· DEC3 indistance, " in"························ 'displays ping results in inches
    ····· PAUSE 10
    ··· IF indistance = <trigger THEN··················· 'triggers firing sequence
    ····· GOSUB fire
    ····· ELSE
    ··· ENDIF
    · NEXT
    LOOP

    fire:··············································· 'activates firing relay
    HIGH 8
    FOR gunsweep = 1 TO 10······························ 'move to fire position of ping servo
    · PULSOUT 13, direction
    · PAUSE 20
    NEXT

    PAUSE 250··········································· 'pause long enough for dc motor to spin up to speed
    PULSOUT 6,5········································· 'ping to check if target is still in location
    ····· PULSIN 6,1,time
    ····· indistance = inconstant ** time··············· ' convert ping input to inches
    ····· IF indistance = <trigger THEN
    ······· GOSUB triggerpull
    ······· ELSE
    ······· LOW 8
    ······· FOR gunsweep = 1 TO 10······················ 'return to center after fire
    ········· PULSOUT 13, 750
    ········· PAUSE 20
    ······· NEXT
    ····· ENDIF
    RETURN
    triggerpull:······· ammo = ammo + 1
    ········· IF ammo = 11 THEN··································· 'check how much ammo is left
    ········· GOSUB ammoout
    ········· ELSE
    ········· ENDIF
    ········· FOR counter = 1 TO 20····························· 'servo to activate mechanical trigger
    ··········· PULSOUT 14,600
    ··········· PAUSE 20
    ········· NEXT
    ········· FOR counter = 1 TO 20····························· 'return trigger servo to neutral
    ············· PULSOUT 14,750
    ············· PAUSE 20
    ········· NEXT
    ··················· PULSOUT 6,5··································· 'ping to check if target is still in location
    ··················· PULSIN 6,1,time
    ··················· indistance = inconstant ** time··············· 'convert ping input to inches
    ··················· IF indistance = <trigger THEN
    ······················ GOSUB triggerpull
    ··················· ELSE·········································· 'turn off relay
    ··················· LOW 8
    ······················· FOR gunsweep = 1 TO 10···················· 'return to center after fire
    ······················· PULSOUT 13, 750
    ······················· PAUSE 20
    ······················· NEXT
    ··················· ENDIF
    ········· RETURN

    '
    if ammo out return servos to neutral and beep 10 times then shutdown
    ammoout:
    FOR gunsweep = 1 TO 10······························ 'return to center after ammo out
    · PULSOUT 13, 750
    · PULSOUT 12, 750
    · PAUSE 20
    NEXT
    FOR beeper = 1 TO 10
    · FREQOUT 10, 250,3500
    · PAUSE 100
    · NEXT
    END
  • UghaUgha Posts: 543
    edited 2008-03-17 14:24
    That's awesome wabbit.

    A couple suggestions...
    Can you somehow mount the Ping on top of the gun's turrent and use that to scan so there's less of a delay in firing?

    You need to add wheels! A mobile foam disc killing machine!

    Another thing to consider is a target... a simple robot that moves randomly in the gun's field of view with a giant target
    sitting on it... maybe put an impact switch on it so it knows when its been hit and sits still (plays dead).

    Edit: What's that white plastic mount you have the Ping attached to? It looks like a perfect fit.
  • WhitWhit Posts: 4,191
    edited 2008-03-17 14:52
    Great project wabbit! My only suggestion is a giant tube of foam disks to feed the disk shooter! wink.gif

    Coworkers be warned.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Whit+


    "We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keeps leading us down new paths." - Walt Disney

    Post Edited (Whit) : 3/17/2008 2:59:37 PM GMT
  • wabbitwabbit Posts: 16
    edited 2008-03-17 16:13
    Nice suggestions.

    Yes I could mount the ping directly on the gun, but I am a little worried about the gun mount right now. The gun is just epoxied to the servo horn. I dont want to put too much weight on it. I think I might mount this on the boebot and chase my dogs with it.

    The little plastic thing on the ping sensor is a perfect fit!· I modelled it in Solidworks and printed it out at work on our Dimension 3D rapid prototyping machine.

    Here are the ideas for my next 2 projects:

    Hack this toy parrot I have that says insults and flaps its wings- Make it say different more specific things to my coworkers.

    Built a LED clock of some type. I am thinking of the design ideas now.



    ·
  • WhitWhit Posts: 4,191
    edited 2008-03-17 18:25
    Check out Chris Savage's·Binary Digital Clock here - http://forums.parallax.com/showthread.php?p=552892

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Whit+


    "We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keeps leading us down new paths." - Walt Disney
  • jsliljslil Posts: 11
    edited 2008-03-26 19:20
    Sweet Machine. I am totally jealous, rapid prototyping is an unfair advantage.
    Keep it up
  • Larry SutherlandLarry Sutherland Posts: 77
    edited 2008-03-27 01:53
    Whit-
    new avatar?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔






    "Understand a grain of rice through and through and you will understand the nature of the·universe."
    ·- Hsueh-feng, ·Blue Cliff Record
Sign In or Register to comment.