buzz_examples

This is an old revision of the document!


Buzz Example Gallery

The aim of this code is to have a group of robots form a distance gradient from a source.

There is one robot that acts as the source; for simplicity here it is the robot with id 0. Every robot, including the source, emits its estimated distance from the source, a listens to other robots. * The robots that can see the source directly emit the distance they sense; * The robots that cannot see the source are in two categories:

  • Those who don't know any distance yet: these robots emit nil
  • Those who received a distance broadcast from one or more neighbors: these robots calculate their distance as the minimum among the received distances

This algorithm keeps running in the step() function, so it can adjust the distance gradient if the robots move around.

gradient.bzz
function init() {
  if(id == 0) {
    # Source robot
    mydist = 0.
  }
  else {
    # Other robots
    mydist = 1000.
    # Listen to other robots' distances
    neighbors.listen("dist_to_source",
      function(value_id, value, robot_id) {
        mydist = math.min(
          mydist,
          neighbors.get(robot_id).distance + value)
      })
  }
}
 
function step() {
  neighbors.broadcast("dist_to_source", mydist)
}
 
function destroy() {
}
hexagon.bzz
# Lennard-Jones parameters
TARGET     = 283.
EPSILON    = 150.
 
# Lennard-Jones interaction magnitude
function lj(dist, target, epsilon) {
  return -(epsilon / dist) * ((target / dist)^4 - (target / dist)^2)
}
 
# Neighbor data to LJ interaction
function to_lj(rid, data) {
  data.x = lj(data.distance, TARGET, EPSILON) * math.cos(data.azimuth)
  data.y = lj(data.distance, TARGET, EPSILON) * math.sin(data.azimuth)
  return data
}
 
# Accumulator of neighbor LJ interactions
function sum(rid, data, accum) {
  accum.x = accum.x + data.x
  accum.y = accum.y + data.y
  return accum
}
 
# Calculates and actuates the flocking interaction
function hexagon() {
  # Calculate accumulator vector
  var accum = neighbors.kin().map(to_lj).reduce(sum, { .x = 0, .y = 0 })
  if(neighbors.count() > 0) {
    accum.x = accum.x / neighbors.count()
    accum.y = accum.y / neighbors.count()
  }
  # Move according to vector
  goto(accum.x, accum.y);
}
 
# Executed at init time
function init() {
}
 
# Executed every time step
function step() {
  hexagon()
}
 
# Execute at exit
function destroy() {
}
hexagon.bzz
# Lennard-Jones parameters
TARGET_KIN     = 283.
EPSILON_KIN    = 150.
TARGET_NONKIN  = 200.
EPSILON_NONKIN = 100.
 
# Lennard-Jones interaction magnitude
function lj(dist, target, epsilon) {
  return -(epsilon / dist) * ((target / dist)^4 - (target / dist)^2)
}
 
# Neighbor data to kin LJ interaction
function to_lj_kin(rid, data) {
  data.x = lj(data.distance, TARGET_KIN, EPSILON_KIN) * math.cos(data.azimuth)
  data.y = lj(data.distance, TARGET_KIN, EPSILON_KIN) * math.sin(data.azimuth)
  return data
}
 
# Neighbor data to non-kin LJ interaction
function to_lj_nonkin(rid, data) {
  data.x = lj(data.distance, TARGET_NONKIN, EPSILON_NONKIN) * math.cos(data.azimuth)
  data.y = lj(data.distance, TARGET_NONKIN, EPSILON_NONKIN) * math.sin(data.azimuth)
  return data
}
 
# Accumulator of neighbor LJ interactions
function sum(rid, data, accum) {
  accum.x = accum.x + data.x
  accum.y = accum.y + data.y
  return accum
}
 
# Calculates and actuates the flocking interaction
function flock() {
  # Create accumulator
  var accum
  accum = {}
  accum.x = 0
  accum.y = 0
  # 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) {
    accum.x = accum.x / neighbors.count()
    accum.y = accum.y / neighbors.count()
  }
  # Move according to vector
  goto(accum.x, accum.y);
}
 
# Executed at init time
function init() {
  s1 = swarm.create(1)
  s1.join()
  s1.select(id % 2 == 0)
  s2 = s1.others(2)
}
 
# Executed every time step
function step() {
  s1.exec(flock)    
  s2.exec(flock)
}
 
# Execute at exit
function destroy() {
}
  • buzz_examples.1472820594.txt.gz
  • Last modified: 2016/09/02 12:49
  • by ilpincy