Mark 3: May I Go? Code
| Attachment | Size |
|---|---|
| MarkII-Front_View.JPG | 1.79 MB |
| MarkII-Top_View.JPG | 1.98 MB |
| MarkII-Trophy.JPG | 2.25 MB |

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



@ Sat, 2009-09-19 02:45
Thank you...
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.
@ Sat, 2009-09-19 00:16
Parallax and the BS2s...
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 :)
@ Fri, 2009-09-18 17:49
motors
what motor did u use?
@ Mon, 2009-08-24 16:31
Would you mind posting your
@ Mon, 2009-08-24 04:48
Thanks
@ Sun, 2009-08-23 18:46
Good Start
@ Mon, 2009-07-20 19:32
Thanks Robotologist
@ Tue, 2009-07-21 03:42
The xmos chips are
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.
@ Mon, 2009-07-20 16:22
the computer
Woah! Hold you horses Benbo!
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)