-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw8.bzz
136 lines (112 loc) · 2.97 KB
/
hw8.bzz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#
# In the ARGoS window, you can move the camera with the following commands:
#
# - Translation: W A S D Q E keys, right-click + drag
# - Rotation: left-click + drag
#
# Selection: shift-click on the robot
# Remove selection: shift-click elsewhere
#
# Bug: selection is finnicky in the top-down view (Camera 1).
# Solution: switch to another camera and it'll work.
#
#
# Compiling and running the code
#
# - In the ARGoS window: Pause the simulation (if it's not paused already)
# - In the editor: do what you need, save, press the "Execute" button
# - In the ARGoS window: Press the "Play", "Step", or "Fast Forward" buttons
#
#
# This editor sucks.
#
# I know.
#
# But when it's open, it detects whether the open files have been modified elsewhere.
# So open this editor and then open the file you wanna edit in your favorite editor.
# When you save in your editor, the changes will be reflected here. Press Execute to compile and run.
# If you want syntax highlighting, JavaScript is pretty similar to Buzz.
#
# Obstacle Avoidance Function
function obstacle_avoidance(){
if (proximity[0].value > 0){
# Check for front sensor
set_wheels(10.0,-5.0)
}
else if ( (proximity[1].value > 0) or (proximity[2].value > 0) or (proximity[3].value > 0) ){
# Turn Left
set_wheels(10.0,-5.0)
}
else if ( (proximity[5].value > 0) or (proximity[6].value > 0) or (proximity[7].value > 0) ){
# Turn Right
set_wheels(-5.0,10.0)
}
else {
# Go straight
set_wheels(10.0,10.0)
}
}
# Coupled Oscillators Initialization
function coupled_oscillators_setup(){
# Declare initial state of robot
state = 0
# Set a unique seed
math.rng.setseed(id)
# Generate a random number
c = math.rng.uniform(100)
# Define k
k = 0.3
# Initilize the flash flag
flashed = 0
# Initialize the neighbor listen function
neighbors.listen("neighbor_state", function(vid, neighbor_state, rid) {
# Check if neighbor flashed
if (neighbor_state > 0){
# Set the flash flag to true
flashed = 1
}
})
}
# Coupled Oscillators Function
function coupled_oscillators(){
# Increment the value of c
c = c + 1
# Check if the flash flag is true
if (flashed == 1){
c = c + k*c
}
# Check if c is greater than threshold
if (c >= 100){
state = 1
# Flash the LED
set_leds(255,255,255)
c = 0
}
else {
state = 0
# Dim the LED
set_leds(0,0,0)
}
# Broadcasting a (key,value) pair
neighbors.broadcast("state", state)
}
# This function is executed once at init time
function init() {
# log("INFO: Hello User")
# Couple Oscillators Setup
coupled_oscillators_setup()
}
# This function is executed at each time step
# The main loop is controller by ARGoS
function step() {
# Uncomment the below line to run obstacle avoidance
obstacle_avoidance()
# Uncomment the below line to run coupled oscillators/synchronisation
coupled_oscillators()
}
# This function is executed when you press the reset button
function reset() {
}
# This function is executed once when you close the simulator
function destroy() {
}