buzz_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revisionBoth sides next revision
buzz_examples [2016/09/02 13:16] – [Hexagonal Pattern Formation] ilpincybuzz_examples [2018/03/18 22:58] root
Line 1: Line 1:
 ====== Buzz Example Gallery ====== ====== Buzz Example Gallery ======
  
-===== Generic robots ======+===== Generic examples ======
  
 ==== Calculation of a Distance Gradient ==== ==== Calculation of a Distance Gradient ====
Line 50: Line 50:
 <code buzz hexagon.bzz> <code buzz hexagon.bzz>
 # We need this for 2D vectors # We need this for 2D vectors
-# Make sure you pass the correct include path to bzzc -I path+# Make sure you pass the correct include path to "bzzc -I <path1:path2> ..."
 include "include/vec2.bzz" include "include/vec2.bzz"
  
Line 77: Line 77:
   var accum = neighbors.map(lj_vector).reduce(lj_sum, math.vec2.new(0.0, 0.0))   var accum = neighbors.map(lj_vector).reduce(lj_sum, math.vec2.new(0.0, 0.0))
   if(neighbors.count() > 0)   if(neighbors.count() > 0)
-    math.vec2.scale(accum, neighbors.count())+    math.vec2.scale(accum, 1.0 / neighbors.count())
   # Move according to vector   # Move according to vector
   goto(accum.x, accum.y)   goto(accum.x, accum.y)
Line 98: Line 98:
 ==== Square Pattern Formation ==== ==== Square Pattern Formation ====
  
-<code buzz hexagon.bzz>+To form square lattice, we can build upon the previous example. The insight is to notice that, in a square lattice, we can color the nodes forming the lattice with two shades, e.g., red and blue, and then mimic the [[http://www.metafysica.nl/turing/nacl_complex_motif_4.gif|crystal structure of kitchen salt]]. In this structure, if two nodes have different colors, they stay at a distance //D//; if they have the same color, they stay at a distance //D// * sqrt(2). 
 + 
 +With this idea in mind, the following script divides the robots in two swarms: those with an even id and those with an odd id. Then, using ''neighbors.kin()'' and ''neighbors.nonkin()'', the robots can distinguish which distance to pick and calculate the correct interaction vector. 
 + 
 +<code buzz square.bzz> 
 +# We need this for 2D vectors 
 +# Make sure you pass the correct include path to "bzzc -I <path1:path2> ..." 
 +include "include/vec2.bzz" 
 # Lennard-Jones parameters # Lennard-Jones parameters
-TARGET_KIN     = 283. +TARGET_KIN     = 283.0 
-EPSILON_KIN    = 150. +EPSILON_KIN    = 150.0 
-TARGET_NONKIN  = 200. +TARGET_NONKIN  = 200.0 
-EPSILON_NONKIN = 100.+EPSILON_NONKIN = 100.0
  
 # Lennard-Jones interaction magnitude # Lennard-Jones interaction magnitude
-function lj(dist, target, epsilon) {+function lj_magnitude(dist, target, epsilon) {
   return -(epsilon / dist) * ((target / dist)^4 - (target / dist)^2)   return -(epsilon / dist) * ((target / dist)^4 - (target / dist)^2)
 } }
  
-# Neighbor data to kin LJ interaction +# Neighbor data to LJ interaction vector 
-function to_lj_kin(rid, data) { +function lj_vector_kin(rid, data) { 
-  data.x = lj(data.distance, TARGET_KIN, EPSILON_KIN) * math.cos(data.azimuth) +  return math.vec2.newp(lj_magnitude(data.distance, TARGET_KIN, EPSILON_KIN)data.azimuth)
-  data.y = lj(data.distance, TARGET_KIN, EPSILON_KIN) * math.sin(data.azimuth) +
-  return data+
 } }
  
-# Neighbor data to non-kin LJ interaction +# Neighbor data to LJ interaction vector 
-function to_lj_nonkin(rid, data) { +function lj_vector_nonkin(rid, data) { 
-  data.x = lj(data.distance, TARGET_NONKIN, EPSILON_NONKIN) * math.cos(data.azimuth) +  return math.vec2.newp(lj_magnitude(data.distance, TARGET_NONKIN, EPSILON_NONKIN)data.azimuth)
-  data.y = lj(data.distance, TARGET_NONKIN, EPSILON_NONKIN) * math.sin(data.azimuth) +
-  return data+
 } }
  
 # Accumulator of neighbor LJ interactions # Accumulator of neighbor LJ interactions
-function sum(rid, data, accum) { +function lj_sum(rid, data, accum) { 
-  accum.x = accum.x + data.x +  return math.vec2.add(dataaccum)
-  accum.y = accum.y + data.y +
-  return accum+
 } }
  
 # Calculates and actuates the flocking interaction # Calculates and actuates the flocking interaction
-function flock() { +function square() {
-  # Create accumulator +
-  var accum +
-  accum = {} +
-  accum.x = 0 +
-  accum.y = 0+
   # Calculate accumulator   # Calculate accumulator
-  accum = neighbors.kin().map(to_lj_kin).reduce(sumaccum+  var accum = neighbors.kin().map(lj_vector_kin).reduce(lj_summath.vec2.new(0.0, 0.0)
-  accum = neighbors.nonkin().map(to_lj_nonkin).reduce(sum, accum) +  accum = neighbors.nonkin().map(lj_vector_nonkin).reduce(lj_sum, accum) 
-  if(neighbors.count() > 0) { +  if(neighbors.count() > 0) 
-    accum.x = accum.x / neighbors.count(+    math.vec2.scale(accum, 1./ neighbors.count())
-    accum.y = accum.y / neighbors.count() +
-  }+
   # Move according to vector   # Move according to vector
-  goto(accum.x, accum.y);+  goto(accum.x, accum.y)
 } }
  
 # Executed at init time # Executed at init time
 function init() { function init() {
 +  # Divide the swarm in two sub-swarms
   s1 = swarm.create(1)   s1 = swarm.create(1)
-  s1.join() 
   s1.select(id % 2 == 0)   s1.select(id % 2 == 0)
   s2 = s1.others(2)   s2 = s1.others(2)
Line 159: Line 154:
 # Executed every time step # Executed every time step
 function step() { function step() {
-  s1.exec(flock    +  s1.exec(square
-  s2.exec(flock)+  s2.exec(square)
 } }
  
Line 167: Line 162:
 } }
 </code> </code>
- 
-===== Wheeled robots ===== 
- 
-===== Flying robots ===== 
  • buzz_examples.txt
  • Last modified: 2018/03/18 22:58
  • by root