Collective Behavior

For the collective behavior project, Kevin, Anand, Gabi and I worked together to create a defend swarm. 

The swarm steps:

    • enemy detected
    • The closest robot moves towards the enemy 
    • With the help of the camera, the first robot navigates to the enemy and tries to push it out of the boundary
    • If the first one can’t push it out, the second one will come and join it to form a chain sequence, so does the third one
    • If the enemy has been pushed out of the boundary, all of the three robots are reset to the original position.

We didn’t manage to get all of the functions to work, but we got some functions to work properly. And here is the code:

calculating the position of bot, as well as the angle it is facing (performed by recognizing the four corners of the botā€™s QR code)

def calcBotData(data): #data has the four corners #calcBotData returns the x,y of the center of the bot and the angle it is facing, can also be used for the enemy bot
id = data[0] corner1 = data[1]#corner1data Should be at the location of the head
corner2 = data[2]#corner2data Should be at the location of the tail
#these corners are across from each other

#calculating center of the bot
x = (headx + tailx)/2
y = (heady + taily)/2

headx = corner1[0] heady = corner1[1] dy = heady – y
dx = headx – x

angle = math.atan2(dy, dx)
if angle < 0:
angle = 2*math.pi – angle

#THIS ANGLE IS IN RADIANS
return [id,x,y,angle]

sending ā€˜reinforcement botsā€™ in the case where one bot cannot push out the object

def initiateCall(availablebots, enemy, dest):

reinforcementBot = availablebots[0] #store first bot in list into variable
availablebots = availablebots[1:]

distance = math.hypot(reinforcementBot[0][0] – enemy[0], reinforcementBot[0][1] – enemy[1])

timeA = 6.38 * orient(reinforcementBot, dest)
timeD = 60 * distance

command = str(reinforcementBot[0])+ “,0,”+ str(timeA)
#SEND TO SERIAL
sleep(timeA + 2)
command = str(reinforcementBot[0])+ “,1,”+ str(timeD)
#SEND TO SERIAL

timeA = 6.38 * orient(reinforcementBot, dest)
command = str(reinforcementBot[0])+ “,0,”+ str(timeA)
#SEND TO SERIAL

command = str(reinforcementBot[0])+ “,1,0”
#SEND TO SERIAL

return availablebots

resetting the bot position

def reset(robots):
received = robots
for i in range(3): #for each bot
move[i][0] = bot[i][0] for j in range(1,3): #calculate vector, j = 1 for x, j = 2 for y
move[i][j] = start[i][j] – received[i][j]

if move[i][1] != 0: #exclude x = 0
tanAngle[i] = move[i][2] / move[i][1] #calculate tan, y/x
if move[i][2] > 0: # y > 0
angle[i] = math.atan(tanAngle) #angle = 0 ~ pi
elif move[i][2] < 0:
angle[i] = math.atan(tanAngle) + math.pi #angle = pi ~ 2pi
elif move[i][2] == 0: # angle = 0 or pi
if move[i][1] > 0: #x > 0
angle[i] = 0;
else: #x < 0
angle[i] = math.pi
else: # x = 0
if move[i][2] > 0: # y > 0
angle = math.pi / 2
elif move[i][2] < 0:
angle = math.pi / 2 * 3
else:
angle = 0

These functions required some coding skills and took us some time to test and combine the functions. However, we couldnā€™t get serial communication to work properly. Due to the limit of time, we stopped after trying it a few times. Also, our project is too big to be done in two weeks. For future improvements, we hope to add serial communications and to test if the whole code works properly .

Leave a Reply