-
Notifications
You must be signed in to change notification settings - Fork 2
/
morphogenesis.c
1164 lines (805 loc) · 24.3 KB
/
morphogenesis.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <stdint.h> // for uint8_t and other types
#include <kilombo.h>
#include "morphogenesis.h"
#include "util.h"
REGISTER_USERDATA(USERDATA)
#ifdef SIMULATOR
#else
#include <avr/io.h> // for microcontroller register defs
//#define DEBUG // for printf to serial port
//#include "debug.h"
#endif
#define COMM_R 85 // Communication range
#define DIFF_R 85 // Diffusion range
#define POLAR_TH 4.0 // Threshold to become polarized
#define EDGE_TH 0.8 // Ratio between the average number of neighbors of the robot and the average number of neighbors' neighbors for edge detection
#define WAIT_BEFORE_MOVE 27000 // kilo_ticks to wait before moving. 75000 for simulation, 27000 for real robots (about 10 minutes)
#define COUNTER_WAIT 8000 // kilo_ticks to wait when the robot tries to orbit but there is another robot orbiting in the area
#define DIST_CRIT 45 // Distance that a robot is considered to be close
#define R2 120 // For probabilistic purposes
// Parameters for the linear reaction-diffusion model
#define A_VAL 0.08
#define B_VAL -0.08
#define C_VAL 0.03
#define D_VAL 0.03
#define E_VAL 0.1
#define F_VAL 0.12
#define G_VAL 0.06
#define D_u 0.5
#define D_v 10
#define LINEAR_R 160
#define SYNTH_U_MAX 0.23
#define SYNTH_V_MAX 0.5
#define DT 0.00005
/*
* Message rx callback function. It pushes message to ring buffer.
*/
void rxbuffer_push(message_t *msg, distance_measurement_t *dist) {
received_message_t *rmsg = &RB_back();
rmsg->msg = *msg;
rmsg->dist = *dist;
RB_pushback();
}
/*
* Transmission of the message
*/
message_t *message_tx()
{
if (mydata->message_lock)
return 0;
return &mydata->transmit_msg;
}
/*
* Changes the state of the robot
*/
void set_bot_state(int state){
mydata->bot_state = state;
}
/*
* Returns the state of the robot
*/
int get_bot_state(void){
return mydata->bot_state;
}
/*
* Changes the type of motion that the robot performs
*/
void set_move_type(int type){
mydata->move_type = type;
}
/*
* Returns the type of motion the robot is performing
*/
int get_move_type(void){
return mydata->move_type;
}
/*
* Returns whether the robot has any neighbor with the specified ID
*/
uint8_t has_neighbor_with_id(uint16_t id){
uint8_t i;
uint8_t flag = 0;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].ID == id){
flag = 1;
break;
}
}
return flag;
}
/*
* Returns whether the robot is polarized, i.e. concentration of molecule U is higher than POLAR_THRESHOLD
*/
uint8_t polarized(){
uint8_t flag = 0;
if(mydata->molecules_concentration[0] > POLAR_TH){
flag = 1;
}
return flag;
}
/*
* Returns the distance to the nearest polarized neighbor
*/
uint8_t get_dist_to_nearest_polarized(){
uint8_t i;
uint8_t dist = 255;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].molecules_concentration[0] > POLAR_TH){
if(mydata->neighbors[i].dist < dist) dist = mydata->neighbors[i].dist;
}
}
return dist;
}
/*
* Returns the id of the nearest neighbor with more than one neighbor
*/
uint16_t find_nearest_N_id(){
uint8_t i;
uint16_t id = 0;
uint8_t dist = 255;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].dist < dist && mydata->neighbors[i].N_Neighbors > 1) {
id = mydata->neighbors[i].ID;
dist = mydata->neighbors[i].dist;
}
}
return id;
}
/*
* Returns the distance to the furthest polarized neighbor
*/
uint16_t find_most_distant_N_id(){
uint8_t i;
uint16_t id = 0;
uint8_t dist = 0;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].dist > dist) {
id = i;
dist = mydata->neighbors[i].dist;
}
}
return id;
}
/*
* Returns the distance of the neighbor with the specified ID
*/
uint8_t get_dist_by_id(uint16_t id){
uint8_t i;
uint8_t dist = 255;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].ID == id){
dist = mydata->neighbors[i].dist;
break;
}
}
return dist;
}
/*
* Returns the state of the neighbor with the specified ID
*/
uint8_t get_state_by_id(uint16_t id){
uint8_t i;
uint8_t state = WAIT;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].ID == id){
state = mydata->neighbors[i].n_bot_state;
break;
}
}
return state;
}
/*
* Returns the difference in distance with respect to the last update of the neighbor with the specified ID
*/
int get_diff_dist_by_id(uint16_t id){
uint8_t i;
int diff_dist = 255;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].ID == id){
diff_dist = mydata->neighbors[i].delta_dist;
break;
}
}
return diff_dist;
}
/*
* Returns true if all neighbors are in WAIT state
*/
uint8_t check_wait_state(){
uint8_t i;
uint8_t flag = 1;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].n_bot_state != WAIT) {
flag = 0;
break;
}
}
return flag;
}
/*
* Computes the average number of neighbors' neighbors taking into account the distance to them
*/
float calc_avg_NNs(){
float sum = 0;
float w_sum = 0;
uint8_t i;
for(i = 0; i < mydata->N_Neighbors; i++){
float w = 1/(float) mydata->neighbors[i].dist;
sum = sum + w * (float) mydata->neighbors[i].N_Neighbors;
w_sum = w_sum + w;
}
return sum/w_sum;
}
/*
* Function to compute a running average by weighing previous and current observations
*/
float calc_apprx_running_avg(float old_avg, float num, float alpha){
float avg;
avg = alpha * num + (1 - alpha) * old_avg;
return avg;
}
/*
* Concentration of U and V is updated based on the linear model for reaction-diffusion
*/
void regulation_linear_model(){
// D_u and D_v
float D[2];
D[0] = D_u;
D[1] = D_v;
// R
float linear_R = LINEAR_R;
// Laplace operator for U and V
float lap[2];
lap[0] = 0;
lap[1] = 0;
// The Laplace operator is calculated for U and V
int i;
for (i = 0; i < mydata->N_Neighbors; i++)
{
float weight;
// All neighbors contribute the same
weight = 1.0;
// Only neighbors nor further apart than DIFF_R (currently 85 mm) and not moving are counted for diffusion
if(mydata->neighbors[i].dist <= DIFF_R && mydata->neighbors[i].n_bot_state != ORBIT && mydata->neighbors[i].n_bot_state != FOLLOW){
lap[0] += weight * (mydata->neighbors[i].molecules_concentration[0] - mydata->molecules_concentration[0]);
lap[1] += weight * (mydata->neighbors[i].molecules_concentration[1] - mydata->molecules_concentration[1]);
}
}
float synth_u_max = SYNTH_U_MAX;
float synth_v_max = SYNTH_V_MAX;
// These variables will have f(u,v) and g(u,v) eventually
float synth_rate_u;
float synth_rate_v;
synth_rate_u = (A_VAL * mydata->molecules_concentration[0] + B_VAL * mydata->molecules_concentration[1] + C_VAL);
synth_rate_v = (E_VAL * mydata->molecules_concentration[0] - F_VAL);
if(synth_rate_u < 0) synth_rate_u = 0;
if(synth_rate_u >= synth_u_max) synth_rate_u = synth_u_max;
if(synth_rate_v < 0) synth_rate_v = 0;
if(synth_rate_v >= synth_v_max) synth_rate_v = synth_v_max;
synth_rate_u = synth_rate_u - D_VAL * mydata->molecules_concentration[0];
synth_rate_v = synth_rate_v - G_VAL * mydata->molecules_concentration[1];
// Rate of change in the concentration
float dG[2];
dG[0] = linear_R * synth_rate_u + D[0] * lap[0];
dG[1] = linear_R * synth_rate_v + D[1] * lap[1];
// Update of the concentration
float dt = DT;
mydata->molecules_concentration[0] += dt * dG[0];
mydata->molecules_concentration[1] += dt * dG[1];
}
/*
* Returns whether the robot is on the edge based on the ratio of its neighbors and its neighbors' neighbors
*/
uint8_t edge_prob_running_avg_ratio_NNs(){
uint8_t prob;
float diff_th = EDGE_TH;
if(mydata->running_avg_Ns/mydata->running_avg_NNs < diff_th) prob = 1;
else prob = 0;
return prob;
}
/*
* Returns whether the robot is on the edge based on the ratio of its neighbors and its neighbors' neighbors
*/
uint8_t test_edge(){
return edge_prob_running_avg_ratio_NNs();
}
/*
* The robot does nothing at the moment while waiting
*/
void wait(){
}
/*
* The robot rotates in one direction until it starts being further apart from the nearest robot, then switches direction.
*/
void move_by_turning(int gradient){
if(mydata->move_switch_flag == 0){
// If getting closer, perhaps change direction of rotation
if(gradient < 0) mydata->move_switch_flag = 1;
// Getting further, keep rotating in the same direction
if(gradient > 0) return;
}
// Getting closer with the current direction
else{
// If getting further, change direction of rotation
if(gradient > 0) {
mydata->move_switch_flag = 0;
switch (get_move_type()) {
case RIGHT:
motion(LEFT);
set_move_type(LEFT);
break;
case LEFT:
motion(RIGHT);
set_move_type(RIGHT);
break;
default:
break;
}
}
}
}
/*
* The robot orbits around its neighbors trying to maintain a constant distance defined by dist_th by switching between left and right motion
*/
void orbit2(uint8_t dist, uint8_t dist_th){
if(dist <= dist_th - 1){
if(mydata->last_turn == RIGHT){
motion(LEFT);
set_move_type(LEFT);
}
else{
motion(RIGHT);
set_move_type(RIGHT);
}
}
if(dist >= dist_th){
if(mydata->last_turn == RIGHT){
motion(RIGHT);
set_move_type(RIGHT);
}
else{
motion(LEFT);
set_move_type(LEFT);
}
}
}
/*
* A random byte is generated
*/
uint8_t rand_byte(){
return rand_soft();
}
/*
* A unique, local ID is found for the robot
*/
uint16_t reset_id(){
uint16_t id;
id = (rand_byte() << 8) | rand_byte();
return id;
}
/*
* Returns whether the robot has at least the specified number of polarized neighbors
*/
uint8_t has_at_least_n_polarized_N(uint8_t n){
uint8_t i;
uint8_t count = 0;
uint8_t flag = 0;
for(i = 0; i < mydata->N_Neighbors; i++){
if(mydata->neighbors[i].molecules_concentration[0] > POLAR_TH){
count++;
}
if(count == n){
flag = 1;
break;
}
}
return flag;
}
/*
* It processes a received message at the front of the ring buffer.
* It goes through the list of neighbors. If the message is from a bot
* already in the list, it updates the information, otherwise
* it adds a new entry to the list
*/
void process_message()
{
uint8_t i,j;
uint16_t ID;
uint8_t *data = RB_front().msg.data;
ID = data[0] | (data[1] << 8);
uint8_t d = estimate_distance(&RB_front().dist);
if(d > COMM_R && mydata->N_Neighbors > 0 && get_bot_state() != FOLLOW) return;
// search the neighbor list by ID
for (i = 0; i < mydata->N_Neighbors; i++)
if (mydata->neighbors[i].ID == ID){ // found it
mydata->neighbors[i].delta_dist = d - mydata->neighbors[i].dist;
break;
}
if (i == mydata->N_Neighbors)
{ // this neighbor is not in list
if (mydata->N_Neighbors < MAXN-1) // neighbor list is not full
mydata->N_Neighbors++;
else
i = find_most_distant_N_id(); // overwrite the most distant neighbor
mydata->neighbors[i].delta_dist = 0;
}
// i now points to where this message should be stored
mydata->neighbors[i].ID = ID;
mydata->neighbors[i].timestamp = kilo_ticks;
mydata->neighbors[i].dist = d;
mydata->neighbors[i].N_Neighbors = data[2];
mydata->neighbors[i].n_bot_state = data[7];
uint8_t signo_rec;
uint8_t exp_rec;
uint16_t mant_rec;
uint16_t bit1;
float mant_fl;
signo_rec=0;
exp_rec=0;
mant_rec=0;
bit1=0;
int jj;
for (j = 0; j < 2; j++){
// recover from "half" precision
signo_rec = data[3+j] >> 7;
exp_rec = (data[3+j] >> 2) & 0x1F;
mant_rec = ((data[3+j] & 0x3) << 8) | data[3+2+j];
mant_fl=0;
for (jj=9; jj>=0; jj--){
bit1 = mant_rec>>jj;
mant_fl = mant_fl + bit1*pow(2,jj-10);
mant_rec = mant_rec - bit1*pow(2,jj);
}
if(exp_rec==31 && signo_rec==0) mydata->neighbors[i].molecules_concentration[j]= 65504;
else if (exp_rec==31 && signo_rec==1) mydata->neighbors[i].molecules_concentration[j]= -65504;
else if (exp_rec==-15 && mant_rec==0) mydata->neighbors[i].molecules_concentration[j] = 0;
else if (exp_rec==-15 && mant_rec!=0) mydata->neighbors[i].molecules_concentration[j] = (float) pow(-1,signo_rec)*pow(2,exp_rec-15+1)*(0+mant_fl);
else mydata->neighbors[i].molecules_concentration[j] = (float) pow(-1,signo_rec)*pow(2,exp_rec-15)*(1+mant_fl);
}
}
/*
* This function:
* - Processes all messages received since the last time
* - Updates neighbors table
* - Updates Number of neighbors and Number of neighbors' neighbors running averages
*/
void receive_inputs()
{
// Processes al messages received since the last time the bot read them (removed after reading)
while (!RB_empty()) {
process_message();
RB_popfront();
}
float alpha = 0.0001;
mydata->running_avg_Ns = calc_apprx_running_avg(mydata->running_avg_Ns, mydata->N_Neighbors, alpha);
if(mydata->N_Neighbors > 0){
mydata->running_avg_NNs = calc_apprx_running_avg(mydata->running_avg_NNs, calc_avg_NNs(), alpha);
}
else mydata->running_avg_NNs = calc_apprx_running_avg(mydata->running_avg_NNs, 0, alpha);
}
/*
* Goes through the list of neighbors and removes entries older than a threshold, currently 2 seconds.
*/
void purgeNeighbors(void)
{
int8_t i;
for (i = mydata->N_Neighbors-1; i >= 0; i--)
if (kilo_ticks - mydata->neighbors[i].timestamp > 64)
{ //this one is too old.
mydata->neighbors[i] = mydata->neighbors[mydata->N_Neighbors-1]; //replace it by the last entry
mydata->N_Neighbors--;
}
}
/*
* The message is updated to reflect changes in state, concentration, number of neighbors and ID
*/
void setup_message(void)
{
mydata->message_lock = 1; // don't transmit while we are forming the message
mydata->transmit_msg.type = NORMAL;
mydata->transmit_msg.data[0] = kilo_uid & 0xff; //0: low ID
mydata->transmit_msg.data[1] = kilo_uid >> 8; //1: high ID
mydata->transmit_msg.data[2] = mydata->N_Neighbors; //2: number of neighbors
mydata->transmit_msg.data[7] = get_bot_state(); // 7: state of the robot
int i;
uint8_t signo;
uint8_t exp;
uint32_t mant;
uint8_t exp_h;
uint16_t mant_h;
uint8_t byte_1;
uint8_t byte_2;
int exp_real;
long fl;
for (i = 0; i < 2; i++){
fl = *(long*)&mydata->molecules_concentration[i];
signo = fl >> 31;
exp = (fl >>23) & 0xff;
mant = fl & 0x7FFFFF;
// Convert to "half" precision
exp_real = exp-127;
//cut the size of the exponent
if(exp_real<-15) exp_real=-15;
else if(exp_real>16) exp_real=16;
exp_h= exp_real + 15;
mant_h = mant >> 13;
byte_1 = signo << 7 | exp_h << 2 | mant_h >> 8;
byte_2 = mant_h & 0xff;
mydata->transmit_msg.data[3+i] = byte_1;
mydata->transmit_msg.data[3+2+i] = byte_2;
}
mydata->transmit_msg.crc = message_crc(&mydata->transmit_msg);
mydata->message_lock = 0;
}
/*
* Returns whether the robot should transit from WAIT state to ORBIT state. Transits if:
* - the robot is on the edge, AND
* - all its neighbors are in WAIT state, AND
* - it isn't polarized OR it is polarized but has no polarized neighbors OR it is polarized and has at least one polarized neighbor but the nearest is further than the critical distance, AND
* - it is allowed to orbit, AND
* - the distance to the nearest polarized neighbor is higher than the critical distance OR the robot is near to a polarized neighbor but there is only one of them, AND
* - it has at least one neighbor to orbit around
*/
uint8_t wait_to_orbit(){
uint8_t flag;
if(
test_edge()
&& check_wait_state()
&& (!polarized() || !has_at_least_n_polarized_N(1) || (has_at_least_n_polarized_N(1) && get_dist_to_nearest_polarized() > (DIST_CRIT)))
&& mydata->counter == 0
&& (get_dist_to_nearest_polarized() > (DIST_CRIT) || !has_at_least_n_polarized_N(2))
&& mydata->N_Neighbors != 0
) flag = 1;
else flag = 0;
return flag;
}
/*
* Returns whether the robot should transit from ORBIT state to WAIT state. Transits if:
* - the nearest neighbor is orbiting, OR
* - the nearest neighbor is too far and cannot be reached by orbiting, OR
* - it is no longer on the edge, OR
* - it is near to a polarized neighbor AND there are at least two polarized neighbors, OR
* - it has no neighbors
*/
uint8_t orbit_to_wait(){
uint8_t flag;
uint16_t id_nearest = find_nearest_N_id();
uint8_t dist_nearest = get_dist_by_id(id_nearest);
if(
get_state_by_id(id_nearest) != WAIT
|| dist_nearest > DIST_CRIT + 15
|| !test_edge()
|| (get_dist_to_nearest_polarized() < (DIST_CRIT - 1) && has_at_least_n_polarized_N(2))
|| mydata->N_Neighbors == 0
) flag = 1;
else flag = 0;
return flag;
}
/*
* Returns whether the robot should transit from WAIT state to FOLLOW state. Transits if:
* - the robot is on the edge, AND
* - the nearest neighbor is in WAIT state, AND
* - the nearest neighbor is too far and cannot be reached by orbiting, AND
* - it has at least one neighbor to move to
*/
uint8_t wait_to_follow(){
uint8_t flag;
uint16_t id_nearest = find_nearest_N_id();
uint8_t dist_nearest = get_dist_by_id(id_nearest);
if(
test_edge()
&& get_state_by_id(id_nearest) == WAIT
&& dist_nearest > DIST_CRIT + 15
&& mydata->N_Neighbors > 0
) flag = 1;
else flag = 0;
return flag;
}
/*
* Returns whether the robot should transit from FOLLOW state to WAIT state. Transits if:
* - the nearest neighbor isn't in WAIT state, OR
* - it has no neighbors, OR
* - there is a neighbor nearby
*/
uint8_t follow_to_wait(){
uint8_t flag;
uint16_t id_nearest = find_nearest_N_id();
uint8_t dist_nearest = get_dist_by_id(id_nearest);
if(
get_state_by_id(id_nearest) != WAIT
|| mydata->N_Neighbors == 0
|| dist_nearest <= DIST_CRIT
) flag = 1;
else flag = 0;
return flag;
}
/*
* Manages the transitions between states
*/
void edge_flow(){
switch (get_bot_state()) {
case ORBIT:
if(orbit_to_wait()){
// The robot stops moving
set_motors(0,0);
set_move_type(STOP);
set_bot_state(WAIT);
return;
}
else if(mydata->N_Neighbors > 0){
uint8_t r1;
r1 = rand_byte();
uint8_t r2;
r2 = rand_byte();
// If too close to the nearest robot, it vibrates with a certain probability to try to escape
if(get_dist_by_id(find_nearest_N_id()) <= DIST_CRIT - 6
&& r1 == 125
&& r2 > R2
){
set_motors(125,125);
delay(150);
}
// The robot orbits around its nearest neighbor
uint16_t id = find_nearest_N_id();
orbit2(get_dist_by_id(id), DIST_CRIT);
}
break;
case FOLLOW:
if(follow_to_wait()){
set_motors(0,0);
set_move_type(STOP);
set_bot_state(WAIT);
return;
}
else{
// It rotates in one direction until it starts being further apart from the nearest robot, then switches direction.
uint16_t id = find_nearest_N_id();
move_by_turning(get_diff_dist_by_id(id));
}
break;
case WAIT:
if(wait_to_orbit()){
spinup_motors();
// If the nearest neighbor is too close, it vibrates a bit randomly
if(get_dist_by_id(find_nearest_N_id()) < DIST_CRIT){
uint8_t r = rand_byte();
if(r < 127) set_motors(125, 0);
else set_motors(0,125);
delay(500);
}
// It starts orbiting clockwise
set_motors(0, kilo_turn_right);
set_move_type(RIGHT);
mydata->last_turn = RIGHT;
set_bot_state(ORBIT);
}
if(wait_to_follow()){
// It starts turning clockwise
set_bot_state(FOLLOW);
set_move_type(RIGHT);
motion(RIGHT);
mydata->move_switch_flag = 1;
}
else wait();
// If not all the neighbors are in WAIT state, the counter is reset
if(!check_wait_state()) mydata->counter = COUNTER_WAIT;
// The robot waits COUNTER_WAIT kilo_ticks after all its neighbots are in WAIT state to be able to transit to ORBIT state, if the other conditions are met
else if (mydata->counter > 0) mydata->counter--;
break;
default:
break;
}
}
/*
* In this function:
* - Random initialisation of concentration of molecules U and V
* - Random ID
* - Variables are initialised to default/empty values
* - Bot is stopped, in WAIT mode and set to clockwise movement
* - The message starts to be sent, with the default/empty variables
*/
void setup() {
// Initialization of the random generator
while(get_voltage() == -1);
rand_seed(rand_hard());
float a0_b;
float a1_b;
// Random percentage of concentration of molecules U and V
a0_b=rand_byte()*100/255;
a1_b=rand_byte()*100/255;
// Number of molecules U and V, from 0 to 6 (continuous values)
mydata->molecules_concentration[0] = (float) a0_b*0.01*6;
mydata->molecules_concentration[1] = (float) a1_b*0.01*6;
// Random ID
kilo_uid = rand_byte();