BS1 Specs show 256 bytes program size |
This is the lite version of TINY AI. The Tricore version of Tiny AI is now available as TriCore_AI.bs1, for copy (see below) and for download at the provided link. The code sets up a life cycle for three cores using the available resources provided by a Parallax BS1.
Download the completed software program in PBASIC at the site below, or simply copy and paste the provided code into the BASIC Stamp editor software. Tricore_AI is the smallest AI program for the series of Stamp processors, fitting into a mere 256 bytes EEPROM. The remarkable attributes of the tricore_ai.bs1 software include self enumerating, determinism, memorizing, remember/recall, nap, dream, sleep, wakeup, talk, listen, know what is heard, work, socialize, hibernate and randomization.
tricore_ai.bs1 software is minimized code with minimal comment length.
DOWNLOAD TRICORE_AI.bs1
http://forums.parallax.com/attachment.php?s=9d78fa693110cfab8d93fd0bbf91f082&attachmentid=62393&d=1248147076
' TriCore_AI.bs1
' TriCore AI Artificial Intelligence v10.0
' Stamp AI for the BS1 TriCore Supercomputer
' by Dr. Humanoido July 20, 2009
' Code fills the BS1's 256 byte EEPROM
' Code number at end of field indicates number of statements
' A total of 93 program statements
' ---------------------- Directives ---------------------------------
' {$STAMP BS1} ' Self deterministic
' {$PBASIC 1.0} ' Self enumerating
' ---------------------- Descriptions -------------------------------
' Performance of Life Cycle
' 1) read pin 1 and determine id (self enumerating)
' 2) determine computer number based on id range (deterministic)
' 3) remember computer id & number (memorize)
' 4) recall memory of computer id & # (remember)
' 5) nap and pseudo random dream in "Vers Libre" (nap, dream)
' 6) sleep fuzzy clock - its computer number in seconds (sleep)
' 7) wake to send its computer number and id (wakeup, talk)
' 8) Listen for others' computer numbers (listen)
' 9) if heard, remember other computer numbers and id's (know what is heard)
' 10) do some work, retrieve the other ids... (work)
' 11) go into suspended animation (hibernation)
' ---------------------- Declarations -------------------------------
SYMBOL result = B0 ' (random) Nap & Dream - a random number
SYMBOL n = BIT1 ' (random) bit1 number is 0 or 1 from B0 above
SYMBOL id = B1 ' pin read id number of this computer (0-126)
SYMBOL c = B2 ' determined computer number (1-10)
SYMBOL v = B3 ' id range value for each computer, random dreams counter
SYMBOL h = B4 ' no. of heard/missed coms (never heard - was sleeping)
SYMBOL x = B5 ' slush value for thinking, recycled as needed
SYMBOL epin = 1 ' Enumerating pin
SYMBOL ipin = 0 ' Rx Tx Interface pin for serial in and out
SYMBOL ppin = 7 ' Piezo speaker pin
SYMBOL baud = N1200 ' Baud rate, inverted, n81 format
' code 13
' ---------------------- Self Enumeration ---------------------------
LOW epin ' get id from self enumerate pin, read rc circuit = ID number
PAUSE 2000 ' cap charge & settle time on pin 1
POT epin,54,id ' pin 1, scale, determine id, read pot (POT PotPin, Scale, read_value)
' code 3
' ---------------------- Determine Computer Number ------------------
Loop: ' loop for all ten computer possibilities
LOOKUP c, (12,23,35,45,55,76,95,114,122,134), v ' lookup logged id range values v
c = c + 1 ' next range lookup address
IF id >= v THEN Loop ' if id larger than range value, try again
DEBUG "Hello human!",CR,"Me, Artificial Intelligence Life Form ",CR,"computer ", #c,"id ",#id,CR,CR ' show computer number and id!
' code 5
' ---------------------- Memory Exercise ----------------------------
' Memory Map /eeprom 84% full
' 00 computer id (enumerating)
' 01 computer number (deterministic)
' 02-21 other computers, alternating id & computer # (see below)
' IDEAL DATALOGGING MEMORY MAP FROM 02 TO 21 (FOR COMPUTER #1 ONLY)
' MEMORY LOCATION 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21
' DATA STORED ID1 C1 ID2 C2 ID3 C3 ID4 C4 ID5 C5 ID6 C6 ID7 C7 ID8 C8 ID9 C9 ID10 C10
WRITE 0,id ' memorize id number
WRITE 1,c ' memorize computer number
READ 0,id:DEBUG "(my 1st memory)", c,CR,CR ' show computer number is remembered
READ 1,c :DEBUG "(my 2nd mem)",id,CR,CR,"(Nap Dreams)" ' show computer id is remembered
' code 4
' ---------------------- Nap & Random Dream -------------------------
' NAP INFO
' =========
' Nap# Nap Time
' 0 18 ms
' 1 36 ms
' 2 72 ms
' 3 144 ms
' 4 288 ms
' 5 576 ms
' 6 1.152 s
' 7 2.304 s
v = 0 ' init a new counter
result = id ' seed pseudo random # generator with the id
Again: ' loop
v=v+1 ' count the dreams
IF v=11 THEN FuzzyClock ' do 10 dreams
RANDOM result ' randomize a byte
BRANCH n, (Dream1, Dream2) ' go to bit 0 or 1, BRANCH value, (Case_0, Case_1, Case_2)
Dream1: ' begin 1st dream
NAP 0 ' nap reduce 1ma to 25us
DEBUG CR, "Sweet ",#n,#result ' show dream & random number
GOTO Again ' next random dream
Dream2: ' begin second dream
NAP 0 ' nap 18ms, reduce 1ma to 25us
DEBUG CR, "Dream ",#n,#result ' next random dream or end of dream, show rnd numb
GOTO Again ' repeat
' code 15
' ---------------------- Fuzzy Clock --------------------------------
FuzzyClock: ' this computer will sleep equal to c
x=c*3 ' sleep formula
DEBUG CR,CR,"(Sleeping)",CR ' display sleeping mode
SLEEP x ' its computer number in seconds
DEBUG "(wake up)",CR ' Display clock status
' code 5
' ---------------------- Announce id & c to Everyone ----------------
SEROUT ipin,baud,("!",id,c) ' wake up, announce this computer's ID and #,
DEBUG "(talk) ",#id,#c,CR ' show id & c were sent, computer 10 is last to wake
' code 2
' ---------------------- Listen for Neighbors id & c ----------------
' Written for the 3 computer TriCore Supercomputer, change for seed supercomputer
' if c = 1 x = 1 to 2 listen for two computers x = 1 to (3-c)
' if c = 2 x = 1 to 1 listen for one computer
' if c = 3 goto end do not listen, this is the last computer
' cannot count itself as listening to itself (set for TriCore supercomputer)
v=2 ' init the write addr, start at 2
Listen: ' listen to other computers speaking their id's
IF c = 3 THEN Think ' jump if computer 3, cannot listen as sleeping
x = 3 - c ' equation to determine looping
FOR h = 1 TO x ' do the loops
SERIN ipin,baud,("!"),id,c ' look at serial pin 1 for incoming id & computer #
DEBUG "(hear) ",#id,#c,CR ' show received id & c
GOSUB Memorize ' remember what you hear
NEXT ' next interation
GOTO Recall ' recall what what heard
' code 10
' ---------------------- Memorize Neighbors id & c ------------------
Memorize: ' place heard computer's id and c in memory
DEBUG "(memorizing)",CR,CR ' memorize the id and c values
WRITE v,id ' datalog to eeprom, v is eeprom addr = 2
v=v+1 ' inc eeprom addr
WRITE v,c ' datalog to eeprom
v=v+1 ' increment memory loc
RETURN ' listen again
' code 7
' ---------------------- Recall Neighbors ---------------------------
Recall: ' Read/display neighbors id & c
DEBUG "(recall neighbors)",CR ' monitor recall
v=1 ' init mem loc
DEBUG "mem loc, id/com ",CR ' display memory location, id, computer number
Repeat: ' recall memory subroutine
v = v+1 ' begin at mem loc 2
READ v,x ' recall v and id
DEBUG #v,#x,CR ' display mem loc, read val
PAUSE 500 ' slow to see screen vals
IF v>6 THEN Think ' CHANGE THIS TO REFLECT WHICH SUPERCOMPUTER!
GOTO Repeat ' again
' code 11
' ---------------------- Do Some Thinking/Work ----------------------
Think: ' think routine
DEBUG CR,"(Thinking)",CR ' announce thinking
READ 1,c ' recall computer number from memory
DEBUG "I am computer ",#c,CR ' announce my computer number
READ 0,id ' recall id from memory
DEBUG "My id is ",#id,CR ' announce my id
READ 1,c ' recall this computer number
x=c*3 ' formula for length of sleep
DEBUG "I slept ",#x," sec",CR ' how long did I sleep?
h=3-c ' formula for the number of heard coms
DEBUG "I heard ",#h," com",CR ' announce # of computers heard
h=2-h ' formula for the number of missed coms
DEBUG "I missed ",#h," com",CR ' announce computers not heard
DEBUG "I had 10 dreams ",CR ' announce computers not heard
GOTO Halt ' end
' code 15
' ---------------------- Suspended Animation ------------------------
Halt: ' heart beat every 2.6 seconds
DEBUG CR,"(bye)" ' monitor mode
END ' maintain state
' code 3
' -------------------------------------------------------------------