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:43] – [Gradient formation] 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 42: | Line 42: | ||
</ | </ | ||
- | ===== Wheeled robots ===== | + | ==== Hexagonal Pattern Formation |
- | ===== Flying | + | 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> | ||
+ | # We need this for 2D vectors | ||
+ | # Make sure you pass the correct include path to "bzzc -I < | ||
+ | include " | ||
+ | |||
+ | # Lennard-Jones parameters | ||
+ | TARGET | ||
+ | EPSILON | ||
+ | |||
+ | # Lennard-Jones interaction magnitude | ||
+ | function lj_magnitude(dist, | ||
+ | return -(epsilon / dist) * ((target / dist)^4 - (target / dist)^2) | ||
+ | } | ||
+ | |||
+ | # Neighbor data to LJ interaction vector | ||
+ | function lj_vector(rid, | ||
+ | return math.vec2.newp(lj_magnitude(data.distance, | ||
+ | } | ||
+ | |||
+ | # Accumulator of neighbor LJ interactions | ||
+ | function lj_sum(rid, data, accum) { | ||
+ | return math.vec2.add(data, | ||
+ | } | ||
+ | |||
+ | # Calculates and actuates the flocking interaction | ||
+ | function hexagon() { | ||
+ | # Calculate accumulator | ||
+ | var accum = neighbors.map(lj_vector).reduce(lj_sum, | ||
+ | if(neighbors.count() > 0) | ||
+ | math.vec2.scale(accum, | ||
+ | # Move according to vector | ||
+ | goto(accum.x, | ||
+ | } | ||
+ | |||
+ | # Executed at init time | ||
+ | function init() { | ||
+ | } | ||
+ | |||
+ | # Executed every time step | ||
+ | function step() { | ||
+ | hexagon() | ||
+ | } | ||
+ | |||
+ | # Execute at exit | ||
+ | function destroy() { | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== 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 | ||
+ | |||
+ | <code buzz square.bzz> | ||
+ | # We need this for 2D vectors | ||
+ | # Make sure you pass the correct include path to "bzzc -I < | ||
+ | include " | ||
+ | |||
+ | # Lennard-Jones parameters | ||
+ | TARGET_KIN | ||
+ | EPSILON_KIN | ||
+ | TARGET_NONKIN | ||
+ | EPSILON_NONKIN = 100.0 | ||
+ | |||
+ | # Lennard-Jones interaction magnitude | ||
+ | function lj_magnitude(dist, | ||
+ | return -(epsilon / dist) * ((target / dist)^4 - (target / dist)^2) | ||
+ | } | ||
+ | |||
+ | # Neighbor data to LJ interaction vector | ||
+ | function lj_vector_kin(rid, | ||
+ | return math.vec2.newp(lj_magnitude(data.distance, | ||
+ | } | ||
+ | |||
+ | # Neighbor data to LJ interaction vector | ||
+ | function lj_vector_nonkin(rid, | ||
+ | return math.vec2.newp(lj_magnitude(data.distance, | ||
+ | } | ||
+ | |||
+ | # Accumulator of neighbor LJ interactions | ||
+ | function lj_sum(rid, data, accum) { | ||
+ | return math.vec2.add(data, | ||
+ | } | ||
+ | |||
+ | # Calculates and actuates the flocking interaction | ||
+ | function square() { | ||
+ | # Calculate accumulator | ||
+ | var accum = neighbors.kin().map(lj_vector_kin).reduce(lj_sum, | ||
+ | accum = neighbors.nonkin().map(lj_vector_nonkin).reduce(lj_sum, | ||
+ | if(neighbors.count() > 0) | ||
+ | math.vec2.scale(accum, | ||
+ | # Move according to vector | ||
+ | goto(accum.x, | ||
+ | } | ||
+ | |||
+ | # Executed at init time | ||
+ | function init() { | ||
+ | # Divide the swarm in two sub-swarms | ||
+ | s1 = swarm.create(1) | ||
+ | s1.select(id % 2 == 0) | ||
+ | s2 = s1.others(2) | ||
+ | } | ||
+ | |||
+ | # Executed every time step | ||
+ | function step() { | ||
+ | s1.exec(square) | ||
+ | s2.exec(square) | ||
+ | } | ||
+ | |||
+ | # Execute at exit | ||
+ | function destroy() { | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Wheeled-robot Specific ===== | ||
+ | |||
+ | TODO | ||
+ | |||
+ | ===== Flying-robot Specific | ||
+ | |||
+ | TODO |