Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read_background(self):
"""Function meant to be called in a separate
thread to continuosly check for incoming
messages. The frequency at which new
messages are checked could require some
tweaking depending on the hardware limitations.
and the number of robots broadcasting.
"""
while self.broadcasting:
self.read()
sleep(self.period / 15.) # assuming is enough to get all messages
self.awake.wait()
return
class XBeeExpirationNetwork(XBeeNetwork):
"""Extension of XBeeNetwork where the data
received is ignored after *expiration_time*
number of seconds since it was first sent
(according to the sender).
"""
def __init__(self, expiration_time, window_start, window_end,
period=1, ID=None, lock=None):
self.expiration_time = expiration_time
self.expirations = {}
XBeeNetwork.__init__(self, window_start, window_end, period, ID, lock)
return
def parse_state(self, message):
"""Parse a message containing x, y, theta, time, ID.
Store the expiration date of the message in self.expirations."""
try:
def __init__(self, body, network, threshold):
BaseRobot.__init__(self, body, network)
self.threshold = threshold
self.rendezvous_point = None
self.path = []
self.known_lights = []
self.num_lights = 0
return
from marabunta import BaseRobot
from math import sin,cos,pi
class HeadingConsensusRobot(BaseRobot):
"""Robot model for heading consensus.
By iteratively calling the update() method,
this robot will communicate with the rest
of the swarm and align its heading to the
swarm's mean heading.
Obstacle avoidance (implemented in BaseRobot)
will take precence over consensus reaching.
"""
#def __init__(self, body, network):
# BaseRobot.__init__(self, body, network)
# return
def heading_target(self):
"""Get the other agent's state and
compute the mean heading. Note that
from marabunta import BaseRobot
from math import *
class MarchingRobot(BaseRobot):
"""Robot model for marching algorithm.
By iteratively calling the update() method,
this robot will communicate with the rest
of the swarm and move in a way that
simulatenouslty tries to
[Spread] stay away from the closest neighbor ,
[Heading] achieve heading consensus , and
[Group] stay close to the group of neighbors.
The importance of each of these three aspects can
be set with the variables S, H and G.
Typical values are given by default but the optimal
parameters can drastically change depending on the
properties of the agents and the desired outcome.
Takes a *threshold* parameter to determine when it
has reached a "good enough" state. Can be set to 0.
Obstacle avoidance (implemented in BaseRobot)
def __init__(self, setting):
body = MockBody(setting.get("position") ,setting.get("heading"))
network = MockNetwork(setting.get("ID"))
BaseRobot.__init__(self,body, network)
return
from marabunta import BaseRobot, ebotBody, XBeeNetwork, XBeeExpirationNetwork
from math import *
from time import time,sleep
import sys
from threading import Lock
from settings import s
class Leader(BaseRobot):
def update(self, deltat, v=None):
self.broadcast_state()
if self.obstacle_infront():
v = 0.0
self.move_forward(deltat, v)
return
dt=0.05
total_time = 50
speed=0.15
try:
num_friends = float(sys.argv[1])-1
except:
num_friends = 4-1
from marabunta import BaseRobot
from math import *
class PerimeterDefenseRobot(BaseRobot):
"""Robot model for perimeter defense.
By iteratively calling the update() method,
this robot will communicate with the rest
of the swarm and move away from the others
as far as possible. Takes a *threshold*
parameter to determine when it has gone
far enough and reached consensus. Can be
set to 0.
Obstacle avoidance (implemented in BaseRobot)
will take precence over consensus reaching.
"""
def __init__(self, body, network, threshold):
BaseRobot.__init__(self, body, network)
self.threshold = threshold
self.rendezvous_point = None
self.path = []
def __init__(self, body, network, threshold=0.5, w_spread=2., w_heading=1., w_group=0.2):
BaseRobot.__init__(self, body, network)
self.threshold = threshold
self.S = w_spread
self.H = w_heading
self.G = w_group
return
from marabunta import BaseRobot, MockBody, MockNetwork
import random
# for visualization
import numpy as np
import pylab as pl
class myRobot(BaseRobot):
def __init__(self, setting):
body = MockBody(setting.get("position") ,setting.get("heading"))
network = MockNetwork(setting.get("ID"))
BaseRobot.__init__(self,body, network)
return
def spread_target(self):
neis = self.get_agents().values()
pos = self.body.get_position()
# Get both neighbors and obstacles, in relative coordinates
obstacles = self.body.obstacle_coordinates()
points = [ [nei[0]-pos[0], nei[1]-pos[1]] for nei in neis] + obstacles
if points:
target = [0.,0.]
for p in points:
d2 = p[0]**2 + p[1]**2
def new_robot(s, log):
body = MockBody(s.get("pos"), s.get("heading") )
network = MockNetwork(log,s.get("ID"))
bot = PerimeterDefenseRobot( body, network, 0.02)
bot.turn_on()
return bot