Let's Make Robots!

Mark 3: May I Go? Code

Benbo231's picture
Navigates around and makes decisions about environment
Cost to build: 
$200
Time to build: 
80 hours
AttachmentSize
MarkII-Front_View.JPG1.79 MB
MarkII-Top_View.JPG1.98 MB
MarkII-Trophy.JPG2.25 MB

Mark_3.jpg

This is Mark 3

He will never be complete, as he is a simple test platform I use for testing new code ideas I have. It doesn't take much for him to be bumped up a number. As oddbot says,  defective robots seem to have personality, and Mark seems to have a pretty innocent, accepting one, though at some times he can be quite mean (code not responding to two stimuli that rarely occur), but hopefully, with some therapy he can be fixed.


Mark 3 is my first robot. His brains consist of a Basic Stamp 2 on a Board Of Education. He has 2 continuous rotation drive servos, and one 'neck' servo. His only sensor is a PING))) ultrasonic rangefinder.

Basically what he does is drive forward, looking left, right, and straight ahead, until he sees anything closer then about a foot away. If he sees while his head is on the left, he turns right. If he sees it when his head is turned right, he turns left. If his head is facing forward, he backs up and turns 180 degrees.


I have posted my first video on LMR! Yay!


He is upgraded from Mark 2.5 to Mark 3 because he got a new bit of code: My version of Chris the Carpenter's May I Go? Navigation. He scans at 45° left, 22.5° left, 0°, 22.5° right, and finally, 45° right. He then compares the five distance readings he gets from the process and turns to the correct bearing of whichever is greatest. He also got some new drive servo mounts made of wood, as opposed to sticky tape.

' {$STAMP BS2}
' {$PBASIC 2.5}
Ping PIN 15
rawDist VAR Word
leftDist VAR Word
HleftDist VAR Word
rightDist VAR Word
HrightDist VAR Word
BestDist VAR Word

BestRoute VAR Byte
cnt VAR Byte
trigger CON 5
IsHigh CON 1
IsLow CON 0
Scale CON 200

ROOT:
BestDist = 0
FOR cnt = 1 TO 15 'Both Head and Robot go forward
PULSOUT 12, 500
PULSOUT 13, 1000
PULSOUT 14, 750
PAUSE 25
NEXT
GOSUB Get_Sonar ' Center Distance Reading
PAUSE 25
IF rawDist < 300 THEN GOSUB Front
IF rawDist < 300 THEN GOSUB Scan
FOR cnt = 1 TO 15 'Head turns left, Robot goes forward
PULSOUT 12, 500
PULSOUT 13, 1000
PULSOUT 14, 1000
PAUSE 25
NEXT
GOSUB LeftPing ' Left Distance Reading
PAUSE 25
IF leftDist < 300 THEN GOSUB Left
IF leftDist < 300 THEN GOSUB Scan
FOR cnt = 1 TO 15 'Both Head and Robot go forward
PULSOUT 12, 500
PULSOUT 13, 1000
PULSOUT 14, 750
PAUSE 25
NEXT
GOSUB Get_Sonar ' Center Distance Reading
PAUSE 25
IF rawDist < 300 THEN GOSUB Front
IF rawDist < 300 THEN GOSUB Scan
FOR cnt = 1 TO 15 'Head goes right, robot goes forward
PULSOUT 12, 500
PULSOUT 13, 1000
PULSOUT 14, 500
PAUSE 25   'Servo Cooloff
NEXT
GOSUB RightPing ' Right Distance Reading
PAUSE 25
IF rightDist < 300 THEN GOSUB Right
IF rightDist < 300 THEN GOSUB Scan
GOTO ROOT 'loops to top
END

 

Get_Sonar:
Ping = IsLow ' make trigger 0-1-0
PULSOUT Ping, Trigger ' activate sensor
PULSIN Ping, IsHigh, rawDist ' measure echo pulse
rawDist = rawDist */ Scale ' convert to uS
rawDist = rawDist / 2 ' remove return trip
PAUSE 20
RETURN

LeftPing:
Ping = IsLow ' make trigger 0-1-0
PULSOUT Ping, Trigger ' activate sensor
PULSIN Ping, IsHigh, leftDist ' measure echo pulse
leftDist = leftDist */ Scale ' convert to uS
leftDist = leftDist / 2 ' remove return trip
PAUSE 20
RETURN

HalfleftPing:
Ping = IsLow ' make trigger 0-1-0
PULSOUT Ping, Trigger ' activate sensor
PULSIN Ping, IsHigh, HleftDist ' measure echo pulse
HleftDist = HleftDist */ Scale ' convert to uS
HleftDist = HleftDist / 2 ' remove return trip
PAUSE 20
RETURN

RightPing:
Ping = IsLow ' make trigger 0-1-0
PULSOUT Ping, Trigger ' activate sensor
PULSIN Ping, IsHigh, rightDist ' measure echo pulse
rightDist = rightDist */ Scale ' convert to uS
rightDist = rightDist / 2 ' remove return trip
PAUSE 20
RETURN

HalfrightPing:
Ping = IsLow ' make trigger 0-1-0
PULSOUT Ping, Trigger ' activate sensor
PULSIN Ping, IsHigh, HrightDist ' measure echo pulse
HrightDist = HrightDist */ Scale ' convert to uS
HrightDist = HrightDist / 2 ' remove return trip
PAUSE 20
RETURN

Right: 'If it sees something while the head is turned right, it turns left
DO
PULSOUT 12, 500
PULSOUT 13, 500
PAUSE 25
GOSUB LeftPing
LOOP UNTIL leftDist > 300
RETURN

Left: 'If it sees something while the head is turned left, it turns right
DO
PULSOUT 12, 1000
PULSOUT 13, 1000
PAUSE 25
GOSUB rightPing
LOOP UNTIL rightDist > 300
RETURN

Front: 'If it sees something while the head is forward, it backs up and turns left
DO
PULSOUT 12, 1000   '
PULSOUT 13, 500
PAUSE 25
GOSUB Get_Sonar
LOOP UNTIL rawDist > 300
FOR cnt = 1 TO 65
PULSOUT 12, 500
PULSOUT 13, 500
PAUSE 25
NEXT
RETURN

Scan:                 'This is where C.T.C.'s May I Go? Navigation starts.
FOR Cnt = 1 TO 10     'Center Eyes
PULSOUT 14, 750
PAUSE 25
NEXT
GOSUB Get_Sonar       'Center Sonar
FOR Cnt = 1 TO 10     'Eyes hard to left, 45 degrees
PULSOUT 14, 1000
PAUSE  25
NEXT
GOSUB leftPing        'Hard Left Sonar
FOR Cnt = 1 TO 10     'Eyes to 22.5 left
PULSOUT 14, 875
PAUSE 25
NEXT
GOSUB HalfleftPing    'Soft Left Sonar
FOR Cnt = 1 TO 10     'Center Eyes
PULSOUT 14, 750
PAUSE 25
NEXT                  'No sonar needed, did it up top.
FOR Cnt = 1 TO 10     'Eyes to 22.5 right
PULSOUT 14, 625
PAUSE 25
NEXT
GOSUB HalfrightPing   'Soft Right Sonar
FOR Cnt = 1 TO 10     'Eyes hard to right, 45 degrees
PULSOUT 14, 500
PAUSE 25
NEXT
GOSUB RightPing       'Hard Right Sonar
FOR Cnt = 1 TO 10     'Center Eyes
PULSOUT 14, 750
PAUSE 25
NEXT
'Here comes the math!
IF leftDist > BestDist THEN BestRoute = 1           'Sets the best path as One. It knows One is turn 45 degrees left.
IF leftDist > BestDist THEN BestDist = leftDist     'Sets the bar at the distance reading of 45 degrees left.
IF HleftDist > BestDist THEN BestRoute = 2          'Sets the best path as Two. It knows Two is turn 22.5 degrees left.
IF HleftDist > BestDist THEN BestDist = HleftDist   'If 22.5 degrees left is farther then the previous reading, set the bar higher. Otherwise, Throw it away.
IF rawDist > BestDist THEN BestRoute = 3            'Sets the best path as Three. It knows Three is go straight.
IF rawDist > BestDist THEN BestDist = rawDist       'If 0 Degrees is farther then the previous reading, set the bar higher.
IF HrightDist > BestDist THEN BestRoute = 4         'Sets the best path as Four. It knows four is turn 22.5 degrees right.
IF HrightDist > BestDist THEN BestDist = HrightDist 'If 22.5 degrees right is farther then the previous reading, set the bar higher.
IF rightDist > BestDist THEN BestRoute = 5          'Sets the best path as Five. It knows five is turn 45 degrees right.
IF rightDist > BestDist THEN BestDist = rightDist   'If 45 degrees right is farther then the previous reading, set the bar higher.
'Find which way I decided is best, and execute it.
IF BestRoute = 1 THEN GOSUB Hardleft
IF BestRoute = 2 THEN GOSUB Softleft
IF BestRoute = 3 THEN RETURN
IF BestRoute = 4 THEN GOSUB SoftRight
IF BestRoute = 5 THEN GOSUB HardRight
RETURN

HardLeft:
FOR cnt = 1 TO 22
PULSOUT 12, 500
PULSOUT 13, 500
PAUSE 25
NEXT
RETURN

SoftLeft:
FOR cnt = 1 TO 11
PULSOUT 12, 500
PULSOUT 13, 500
PAUSE 25
NEXT
RETURN

SoftRight:
FOR cnt = 1 TO 11
PULSOUT 12, 1000
PULSOUT 13, 1000
PAUSE 25
NEXT
RETURN

HardRight:
FOR cnt = 1 TO 22
PULSOUT 12, 1000
PULSOUT 13, 1000
PAUSE 25
NEXT
RETURn

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Benbo231's picture

I am currently saving for a mass-order of pickaxes and arduinos, and a kermillion other parts.

I used parallax continuous rotation servos, this was before I realized there were cheaper alternatives. Any servo is compatible with the BOE. Servos tend to be an integral part of my creations, as they are pretty user friendly in general.

AznCaleb's picture

Like some/many of you, I used to use parallax BS2s. That was my little easy-to-learn language. But it is bank-breaking to save up for a robot when you could have spent half as much using other micros. Parallax is easy to use, but its not a great idea to stick with it. Learning other programming languages is valuable and will save you money. Arduino is a great micro. I switched from PBASIC to the Arduino environment pretty quickly. Theres plenty of reference guides, tutorials, and forums to help you get started. After learning PBASIC it isnt that hard to pick up the Arduino language after a bit. Just my opinion, but I would bet that many people who switched from BS2 to Arduino would agree to some extent. If you are interested, heres the arduino reference guide and forum links;

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl   <-- the forum

http://arduino.cc/en/Reference/HomePage <-- the reference guide

 the forum makes it easy to get started with arduino and theres plenty of people eager to help :)

what motor did u use?

 

ignoblegnome's picture
Would you mind posting your code as a file attachment?  I'm doing PICAXE Basic, and seeing other examples is always a plus.
Benbo231's picture
but their stuff is sooo expensive. if you havent mastered the ping, i could give you some pointers...
Prettybird's picture
   It is refreashing to see another Parallax user here. I too am a newbee and programming is not my experttise. Good simple job but more details are nice. My Mini Monster was my first attempt from a hacked program I modified as I went along. I have some parallax stamp stuff I will be listing here later. From what I heard the propeller is the way to go. Five microprocessors are better then one I guess. Talking about exotic  languages, my new Propeller Devolopment Board has been  collecting dust for almost a month now. The Parallax is the best forum I have seen so far as long as you stay with their stuff. That is understandable. The Ping seems alot more complicatted to me then I first realised of coarse. A compass is also down the line for me. Opinion, stay with Parallax. They are good at helping blockhead programmer newbees like me ant have tons of free software from their site. including books. Good luck with the mandaran. I went through that. I think programming would be easier. lol I am also Prettybird in their forum. I got this LMR site from there.
Benbo231's picture
i plan on arduino next, or maybe the xmos if i win the competition. does anyone know anything about the xmos system? whats the programmang language? is it hard? what is its capabilities? when are they deciding the xmos competition?
ezekiel181's picture

The xmos chips are programmed in XC which is similar to C on which the arduino is based. An arduino is a good introduction to the workings of C with some nice touches for newbies, I can highly recommend it. XC adds support for threads and special timing features over standard C. Its capabilities and information on programming it can be found over on the competition post or you can also just go straight to the homepage where several good tutorials are available, or xlinkers forum for projects, code snippets, libraries etc.

I think they`re waiting for the new dev boards to be done before deciding who gets them.

 

Benbo231's picture
actually not. you can get a program rthat tries with every password on the face of the planet. and more. may take forever, but it works

I wouldn't waste my time or money buying a new MCU, use the BS2 in this Mark 2.0.

That means, disassemble Mark 2.0 and reuse the parts.

Learning a new language is hard you know,

Right now I'm still struggling with PBASIC 2.5 and Chinese (Traditional)

A pro-robot builder sticks to one type of MCU,

If you want to learn multiple languages,

It'll take approxiamately 3-4 years to memorize it all in your head.

It won't be hard to say goodbye to Mark 2.0 because he's going to be the Mark 4, right?

Just don't go and buy the Arduino until you've memorised everything from BS2, Benbo!

If you do, I'll haunt you with my custom made OEM BS2SX!!! (In 1 years time)