Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
buzz_examples [2016/09/02 12:46] – ilpincy | buzz_examples [2016/09/02 13:49] – [Hexagonal Pattern Formation] ilpincy | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Buzz Example Gallery ====== | ====== Buzz Example Gallery ====== | ||
- | ===== Generic | + | ===== Generic |
==== Calculation of a Distance Gradient ==== | ==== Calculation of a Distance Gradient ==== | ||
Line 43: | Line 43: | ||
==== Hexagonal Pattern Formation ==== | ==== Hexagonal Pattern Formation ==== | ||
+ | |||
+ | Hexagonal patterns can be formed in a simple way by mimicking particle interaction. A simple model of particle interaction is the [[https:// | ||
+ | |||
+ | The idea in the code is that every robot can use the '' | ||
<code buzz hexagon.bzz> | <code buzz hexagon.bzz> | ||
+ | # We need this for 2D vectors | ||
+ | # Make sure you pass the correct include path to "bzzc -I < | ||
+ | include " | ||
+ | |||
# Lennard-Jones parameters | # Lennard-Jones parameters | ||
- | TARGET_KIN | + | TARGET |
- | EPSILON_KIN | + | EPSILON |
- | TARGET_NONKIN | + | |
- | EPSILON_NONKIN = 100. | + | |
# Lennard-Jones interaction magnitude | # Lennard-Jones interaction magnitude | ||
- | function | + | function |
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 |
- | function | + | function |
- | | + | |
- | data.y = lj(data.distance, | + | |
- | return data | + | |
- | } | + | |
- | + | ||
- | # Neighbor data to non-kin LJ interaction | + | |
- | function to_lj_nonkin(rid, | + | |
- | data.x = lj(data.distance, | + | |
- | data.y = lj(data.distance, TARGET_NONKIN, | + | |
- | return data | + | |
} | } | ||
# Accumulator of neighbor LJ interactions | # Accumulator of neighbor LJ interactions | ||
- | function | + | function |
- | | + | |
- | accum.y = accum.y + data.y | + | |
- | return | + | |
} | } | ||
# Calculates and actuates the flocking interaction | # Calculates and actuates the flocking interaction | ||
- | function | + | function |
- | # Create accumulator | + | |
- | var accum | + | |
- | accum = {} | + | |
- | accum.x = 0 | + | |
- | accum.y = 0 | + | |
# Calculate accumulator | # Calculate accumulator | ||
- | accum = neighbors.kin().map(to_lj_kin).reduce(sum, accum) | + | |
- | accum = neighbors.nonkin().map(to_lj_nonkin).reduce(sum, accum) | + | if(neighbors.count() > 0) |
- | if(neighbors.count() > 0) { | + | |
- | | + | |
- | | + | |
- | } | + | |
# Move according to vector | # Move according to vector | ||
- | goto(accum.x, | + | goto(accum.x, |
} | } | ||
# Executed at init time | # Executed at init time | ||
function init() { | function init() { | ||
- | s1 = swarm.create(1) | ||
- | s1.join() | ||
- | s1.select(id % 2 == 0) | ||
- | s2 = s1.others(2) | ||
} | } | ||
# Executed every time step | # Executed every time step | ||
function step() { | function step() { | ||
- | | + | |
- | s2.exec(flock) | + | |
} | } | ||
Line 116: | Line 98: | ||
==== Square Pattern Formation ==== | ==== Square Pattern Formation ==== | ||
- | ==== Hexagonal Pattern Formation ==== | + | 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:// |
+ | |||
+ | 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 '' | ||
+ | |||
+ | <code buzz square.bzz> | ||
+ | # We need this for 2D vectors | ||
+ | # Make sure you pass the correct include path to "bzzc -I < | ||
+ | include " | ||
- | <code buzz hexagon.bzz> | ||
# Lennard-Jones parameters | # Lennard-Jones parameters | ||
- | TARGET_KIN | + | TARGET_KIN |
- | EPSILON_KIN | + | EPSILON_KIN |
- | TARGET_NONKIN | + | TARGET_NONKIN |
- | EPSILON_NONKIN = 100. | + | EPSILON_NONKIN = 100.0 |
# Lennard-Jones interaction magnitude | # Lennard-Jones interaction magnitude | ||
- | function | + | function |
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 |
- | function | + | function |
- | | + | |
- | data.y = lj(data.distance, | + | |
- | return data | + | |
} | } | ||
- | # Neighbor data to non-kin | + | # Neighbor data to LJ interaction |
- | function | + | function |
- | | + | |
- | data.y = lj(data.distance, | + | |
- | return data | + | |
} | } | ||
# Accumulator of neighbor LJ interactions | # Accumulator of neighbor LJ interactions | ||
- | function | + | function |
- | | + | |
- | accum.y = accum.y + data.y | + | |
- | return | + | |
} | } | ||
# Calculates and actuates the flocking interaction | # Calculates and actuates the flocking interaction | ||
- | function | + | function |
- | # Create accumulator | + | |
- | var accum | + | |
- | accum = {} | + | |
- | accum.x = 0 | + | |
- | accum.y = 0 | + | |
# Calculate accumulator | # Calculate accumulator | ||
- | accum = neighbors.kin().map(to_lj_kin).reduce(sum, accum) | + | |
- | 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) |
- | | + | |
- | | + | |
- | } | + | |
# Move according to vector | # Move according to vector | ||
- | goto(accum.x, | + | goto(accum.x, |
} | } | ||
# 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 179: | 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 188: | Line 163: | ||
</ | </ | ||
- | ===== Wheeled | + | ===== Wheeled-robot Specific ===== |
+ | |||
+ | TODO | ||
+ | |||
+ | ===== Flying-robot Specific | ||
- | ===== Flying robots ===== | + | TODO |