Commit 0ef23091 authored by Dave Griffiths's avatar Dave Griffiths

better lsystem and stuff for flotsam

parent 3c92fdb6
......@@ -35,7 +35,7 @@ def value_read_inverse(pins):
v = 0
for i in range(0,len(pins)):
# low is 1, pull up resistor
t = not io.input(len(pins)-pins[i]-1)
t = not io.input(pins[len(pins)-i-1])
v |= t<<i
return v
......@@ -52,9 +52,9 @@ def read_addr_4bit(addr):
# flip addr zero due to plug hw error
if addr==0 or addr==1:
if addr%2==0:
return value_read_inverse(value_pins) & 0x0f
else:
return (value_read_inverse(value_pins)>>4) & 0x0f
else:
return value_read_inverse(value_pins) & 0x0f
if addr%2==0:
return (value_read(value_pins)>>4) & 0x0f
......
# invisible driver...
import RPi.GPIO as io
import time
def quick_setup(pins,i):
for pin in pins:
io.setup(pin,i)
def quick_set(pins,v):
debug = ""
for i in range(0,len(pins)):
debug+=str((v>>i)&1)
#if pins[i]==3:
# print(str(pins[i])+":"+str((v>>i)&1))
#time.sleep(0.1)
io.output(pins[i],(v>>i)&1)
#print(debug)
def quick_read(pins):
for i in range(0,len(pins)):
t = io.input(pins[i])
print("pin "+str(i)+" is "+str(t))
def value_read(pins):
v = 0
for i in range(0,len(pins)):
# low is 1, pull up resistor
t = not io.input(pins[i])
#print("pin:"+str(pins[i])+" is "+str(io.input(pins[i])))
v |= t<<i
return v
value_pins = [7,8,25,24,23,18,15,14]
address_pins = [27,17,4,3,2]
def read_addr(addr):
quick_set(address_pins, addr)
return value_read(value_pins)
def read_addr_4bit(addr):
quick_set(address_pins, addr)
if addr%2==0:
return value_read(value_pins) & 0x0f
else:
return (value_read(value_pins)>>4) & 0x0f
def read_code():
r = []
for i in range(0,16):
r.append(read_addr(i))
return r
try:
io.setmode(io.BCM)
quick_setup(value_pins, io.IN)
quick_setup(address_pins, io.OUT)
quick_set(address_pins, 0)
addr = 1
while (1):
s=""
for addr in range(0,32):
s+=str(read_addr_4bit(addr))+" "
print(s)
#print(read_code())
time.sleep(0.1)
finally:
io.cleanup()
import math
class point:
def __init__(self,x,y,z):
self.x=x
self.y=y
self.z=z
class mat44:
def __init__(self):
self.zero();
self.m[0][0]=1
self.m[1][1]=1
self.m[2][2]=1
self.m[3][3]=1;
def zero(self):
self.m=[[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
def mul(self,rhs):
t=mat44()
for i in range(0,4):
for j in range(0,4):
t.m[i][j]=self.m[i][0]*rhs.m[0][j]+self.m[i][1]*rhs.m[1][j]+self.m[i][2]*rhs.m[2][j]+self.m[i][3]*rhs.m[3][j];
return t
def translate(self, x, y, z):
t=mat44()
t.m[3][0]=x;
t.m[3][1]=y;
t.m[3][2]=z;
self.m=self.mul(t).m
def rotxyz(self, x, y, z):
if x!=0:
t=mat44()
x*=0.017453292
sx=math.sin(x)
cx=math.cos(x)
t.m[1][1]=cx
t.m[2][1]=-sx
t.m[1][2]=sx
t.m[2][2]=cx
self.m=self.mul(t).m
if y!=0:
t=mat44()
y*=0.017453292
sy=math.sin(y)
cy=math.cos(y)
t.m[0][0]=cy
t.m[2][0]=sy
t.m[0][2]=-sy
t.m[2][2]=cy
self.m=self.mul(t).m
if z!=0:
t=mat44()
z*=0.017453292
sz=math.sin(z)
cz=math.cos(z)
t.m[0][0]=cz
t.m[1][0]=-sz
t.m[0][1]=sz
t.m[1][1]=cz
self.m=self.mul(t).m
def scale(self, x, y, z):
t = mat44()
t.m[0][0]=x
t.m[1][1]=y
t.m[2][2]=z
self.m=self.mul(t).m
def transform(self,p):
t = point(0,0,0)
t.x=p.x*self.m[0][0] + p.y*self.m[1][0] + p.z*self.m[2][0]
t.y=p.x*self.m[0][1] + p.y*self.m[1][1] + p.z*self.m[2][1]
t.z=p.x*self.m[0][2] + p.y*self.m[1][2] + p.z*self.m[2][2]
return t
def test():
m = mat44()
print m.m
m.rotxyz(0,90,0)
print(m.m)
p = point(1,0,0)
pp = m.transform(p)
print (pp.x,pp.y,pp.z)
test()
......@@ -7,14 +7,19 @@ DUP = 2
JMP = 3
JMZ = 4
ADD = 5
SUB = 6
FWD = 7
TNL = 8
TNR = 9
TNU = 10
FWD = 6 # 0110
SUB = 7 # 1110
TNL = 8 # 0001
TNR = 9 # 1001
TNU = 10
TND = 11
MAT = 12
MAT = 12 # 0011
BLD = 13 # 1011
instr = ["nop","push","dup","jump","jmpz","add",
"forward","sub","left","right","up",
"down","material","bulldoze"]
class machine:
def __init__(self):
......@@ -26,7 +31,10 @@ class machine:
self.pc=(self.pc+1)%32
def pop_stack(self):
ret = self.stack[0]
if len(self.stack)==0:
ret = 0
else:
ret = self.stack[0]
self.stack=self.stack[1:]
return ret
......@@ -34,6 +42,8 @@ class machine:
v = driver.read_addr(self.pc)
self.incpc()
print(str(self.pc)+": "+instr[v])
if v==PSH:
self.stack.insert(0,driver.read_addr(self.pc))
self.incpc()
......@@ -41,9 +51,13 @@ class machine:
self.stack.insert(0,self.stack[0])
elif v==FWD:
if len(self.stack)==0:
self.turtle.forward(1)
self.turtle.forward(2)
else:
self.turtle.forward(self.pop_stack())
elif v==MAT:
m = driver.read_addr(self.pc)
self.incpc()
self.turtle.material(m)
elif v==TNL:
self.turtle.left()
elif v==TNR:
......@@ -61,6 +75,10 @@ class machine:
print(self.stack[0])
elif v==JMP:
self.pc=driver.read_addr(self.pc)
elif v==BLD:
#print("bulldoze")
self.turtle.reset()
mcturtle.bulldoze(100)
elif v==JMZ:
if len(self.stack)>0 and self.stack[0]!=0:
newpc = driver.read_addr(self.pc)
......@@ -72,11 +90,9 @@ class machine:
driver.init()
mcturtle.bulldoze(100)
time.sleep(10)
print(driver.read_all())
m = machine()
while 1:
m.step()
time.sleep(0.1)
time.sleep(0.4)
......@@ -121,35 +121,114 @@ def bulldoze(size):
box(GRASS,point(-size/2,-1,-size/2),point(size,1,size))
print("finished bulldozing")
class mat44:
def __init__(self):
self.zero();
self.m[0][0]=1
self.m[1][1]=1
self.m[2][2]=1
self.m[3][3]=1;
def zero(self):
self.m=[[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
def mul(self,rhs):
t=mat44()
for i in range(0,4):
for j in range(0,4):
t.m[i][j]=self.m[i][0]*rhs.m[0][j]+self.m[i][1]*rhs.m[1][j]+self.m[i][2]*rhs.m[2][j]+self.m[i][3]*rhs.m[3][j];
return t
def translate(self, x, y, z):
t=mat44()
t.m[3][0]=x;
t.m[3][1]=y;
t.m[3][2]=z;
self.m=self.mul(t).m
def rotxyz(self, x, y, z):
if x!=0:
t=mat44()
x*=0.017453292
sx=math.sin(x)
cx=math.cos(x)
t.m[1][1]=cx
t.m[2][1]=-sx
t.m[1][2]=sx
t.m[2][2]=cx
self.m=self.mul(t).m
if y!=0:
t=mat44()
y*=0.017453292
sy=math.sin(y)
cy=math.cos(y)
t.m[0][0]=cy
t.m[2][0]=sy
t.m[0][2]=-sy
t.m[2][2]=cy
self.m=self.mul(t).m
if z!=0:
t=mat44()
z*=0.017453292
sz=math.sin(z)
cz=math.cos(z)
t.m[0][0]=cz
t.m[1][0]=-sz
t.m[0][1]=sz
t.m[1][1]=cz
self.m=self.mul(t).m
def scale(self, x, y, z):
t = mat44()
t.m[0][0]=x
t.m[1][1]=y
t.m[2][2]=z
self.m=self.mul(t).m
def transform(self,p):
t = point(0,0,0)
t.x=p.x*self.m[0][0] + p.y*self.m[1][0] + p.z*self.m[2][0]
t.y=p.x*self.m[0][1] + p.y*self.m[1][1] + p.z*self.m[2][1]
t.z=p.x*self.m[0][2] + p.y*self.m[1][2] + p.z*self.m[2][2]
return t
##########################################################
# turtle stuff
class turtle:
m_pos = point(0,0,0)
m_dir = point(1,0,0)
m_material = MELON
def __init__(self):
self.reset()
def reset(self):
print("reset...")
self.m_pos = point(0,0,0)
self.m_dir = mat44()
self.m_material = MELON
def material(this,m):
this.m_material=m
def forward(this,distance):
box(this.m_material,this.m_pos,this.m_dir*distance)
this.m_pos+=this.m_dir*distance
dir = this.m_dir.transform(point(0,0,1))
dir.x=round(dir.x)
dir.y=round(dir.y)
dir.z=round(dir.z)
print(dir.x,dir.y,dir.z)
box(this.m_material,this.m_pos,dir*distance)
this.m_pos+=dir*distance
def roty(this,a):
# rotate around y
a = a*0.0174532925
tx = this.m_dir.x * math.cos(a)+this.m_dir.z * math.sin(a)
tz = this.m_dir.x * -math.sin(a)+this.m_dir.z * math.cos(a)
this.m_dir.x = int(tx)
this.m_dir.z = int(tz)
this.m_dir.rotxyz(0,a,0)
def rotx(this,a):
# rotate around x
a = a*0.0174532925
tx = this.m_dir.x * math.cos(a)+this.m_dir.y * -math.sin(a)
ty = this.m_dir.x * math.sin(a)+this.m_dir.y * math.cos(a)
this.m_dir.x = int(tx)
this.m_dir.y = int(ty)
this.m_dir.rotxyz(a,0,0)
def left(this):
this.roty(90)
......
This diff is collapsed.
import driver
import time
print("initing")
driver.init()
while 1:
print(driver.read_all())
time.sleep(0.2)
......@@ -4,9 +4,10 @@ import time
# standard lsystem stuff
def run_rule(str,rule):
if rule[1]=="": return str
ret = ""
for i in range(0,len(str)):
if str[i:i+len(rule[0])]==rule[0]:
if str[i]==rule[0]:
ret+=rule[1]
else:
ret+=str[i]
......@@ -18,7 +19,6 @@ def explode_lsystem(str,rules,gen):
str = run_rule(str,r)
return str
# a weave structure kernel
class kernel:
......@@ -70,20 +70,32 @@ fat_red = 1
thin_red = 2
fat_blue = 4
thin_blue = 8
symbol_x = 3
symbol_y = 5
symbol_z = 9
def lsys_from_code(c):
if c==fat_red: return "R"
if c==fat_blue: return "B"
if c==thin_red: return "r"
if c==thin_blue: return "b"
if c==symbol_x: return "X"
if c==symbol_y: return "Y"
if c==symbol_z: return "Z"
return ""
def rule_from_code(code):
f = lsys_from_code(code[0])
def rule_from_code(sym,code):
t = ""
for i in range(1,8):
for i in range(0,8):
t+=lsys_from_code(code[i])
return [f,t]
return [sym,t]
def remove_symbols(str):
ret=""
for i in range(0,len(str)):
if str[i]!="X" and str[i]!="Y" and str[i]!="Z":
ret+=str[i]
return ret
def lsys_from_blocks(code):
#axiom
......@@ -91,19 +103,18 @@ def lsys_from_blocks(code):
for i in range(0,8):
axiom+=lsys_from_code(code[i])
rule1=rule_from_code(code[8:16])
rule2=rule_from_code(code[16:24])
rule1=rule_from_code("X",code[8:16])
rule2=rule_from_code("Y",code[16:24])
rule3=rule_from_code("Z",code[24:32])
print("------")
print(axiom)
print(rule1)
print(rule2)
print(rule3)
if (rule2[0]!=""):
return explode_lsystem(axiom,[rule1,rule2],3)
else:
return explode_lsystem(axiom,[rule1],3)
return remove_symbols(explode_lsystem(axiom,[rule1,rule2,rule3],6))
def sprite_from_yarn(c,warp):
if warp:
......@@ -151,8 +162,14 @@ emu = [2,0,0,0,0,0,0,0,
frame = 0
k = kernel([1,0,0,1],2,2)
k = kernel([0,0,1,1,
0,1,1,0,
1,1,0,0,
1,0,0,1],4,4)
print("initing")
driver.init()
print(driver.read_all())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment