Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| buzz_examples [2016/09/02 13:11] – [Hexagonal Pattern Formation] ilpincy | buzz_examples [2018/03/18 22:58] (current) – root | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Buzz Example Gallery ====== | ====== Buzz Example Gallery ====== | ||
| - | ===== Generic robots ====== | + | ===== Calculation of a Distance Gradient |
| - | + | ||
| - | ==== Calculation of a Distance Gradient ==== | + | |
| The aim of this code is to have a group of robots form a distance gradient from a source. | The aim of this code is to have a group of robots form a distance gradient from a source. | ||
| Line 42: | Line 40: | ||
| </ | </ | ||
| - | ==== 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:// | Hexagonal patterns can be formed in a simple way by mimicking particle interaction. A simple model of particle interaction is the [[https:// | ||
| Line 50: | Line 48: | ||
| <code buzz hexagon.bzz> | <code buzz hexagon.bzz> | ||
| # We need this for 2D vectors | # We need this for 2D vectors | ||
| - | include " | + | # Make sure you pass the correct |
| + | include " | ||
| # Lennard-Jones parameters | # Lennard-Jones parameters | ||
| - | TARGET | + | TARGET |
| - | EPSILON = 150.0 | + | EPSILON |
| # Lennard-Jones interaction magnitude | # Lennard-Jones interaction magnitude | ||
| Line 63: | Line 62: | ||
| # Neighbor data to LJ interaction vector | # Neighbor data to LJ interaction vector | ||
| function lj_vector(rid, | function lj_vector(rid, | ||
| - | return math.vec2.newp(lj(data.distance, | + | return math.vec2.newp(lj_magnitude(data.distance, |
| } | } | ||
| # Accumulator of neighbor LJ interactions | # Accumulator of neighbor LJ interactions | ||
| - | function | + | function |
| return math.vec2.add(data, | return math.vec2.add(data, | ||
| } | } | ||
| Line 74: | Line 73: | ||
| function hexagon() { | function hexagon() { | ||
| # Calculate accumulator | # Calculate accumulator | ||
| - | var accum = neighbors.map(to_lj).reduce(lj_sum, | + | var accum = neighbors.map(lj_vector).reduce(lj_sum, |
| if(neighbors.count() > 0) | if(neighbors.count() > 0) | ||
| - | math.vec2.scale(accum, | + | math.vec2.scale(accum, |
| # Move according to vector | # Move according to vector | ||
| - | goto(accum.x, | + | goto(accum.x, |
| } | } | ||
| Line 95: | Line 94: | ||
| </ | </ | ||
| - | ==== Square Pattern Formation ==== | + | ===== Square 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 158: | Line 152: | ||
| # 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 166: | Line 160: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | ===== Wheeled robots ===== | ||
| - | |||
| - | ===== Flying robots ===== | ||