I put a high maximum value but still I get strange values.
For example, in this experiment a kilobot stops very far from the light source.
Code: Select all
#include<kilolib.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#define MAXLIGHT 900
#define SAMPLEMAX 300
int randomAngle;
int randomForeward;
int iter = 0;
float ambientLightValue = 0;
void ExitKilobot(){
while(true){
set_motors(0, 0);
set_color(RGB(3, 3, 3));
delay(60000);
}
}
void setup(){
uint8_t randSeed = rand_hard();
srand(randSeed);
}
double sampleLight(){
double current_light;
int numberSample = 0;
int sum = 0;
int i;
for(i = 0; i < SAMPLEMAX; i++){
int sample = get_ambientlight();
if (sample != -1){
sum = sum + sample;
numberSample++;
}
}
printf("Light K%d > %f\n", kilo_uid, (current_light = sum / numberSample));
return current_light = sum / numberSample;
}
void loop(){
set_color(RGB(3, 0, 0));
spinup_motors();
set_motors(kilo_turn_left, 0);
randomAngle = rand() % 39 + 1;
randomAngle = randomAngle * 0.2 * 1000;
delay(randomAngle);
set_color(RGB(0, 3, 0));
spinup_motors();
set_motors(kilo_straight_left, kilo_straight_right);
randomForeward = rand() % 12;
randomForeward = randomForeward * 0.5 * 1000;
delay(randomForeward);
set_motors(0, 0);
set_color(RGB(3, 0, 3));
delay(1000);
ambientLightValue = sampleLight();
if(ambientLightValue >= MAXLIGHT){
ExitKilobot();
}
}
int main(){
kilo_init();
kilo_start(setup, loop);
return 0;
}
Code: Select all
<?xml version="1.0" ?>
<argos-configuration>
<!-- ************************* -->
<!-- * General configuration * -->
<!-- ************************* -->
<framework>
<system threads="0" />
<experiment length="0"
ticks_per_second="10"
random_seed="25" />
</framework>
<!-- *************** -->
<!-- * Controllers * -->
<!-- *************** -->
<controllers>
<kilobot_controller id="kbc">
<actuators>
<differential_steering implementation="default" />
<kilobot_led implementation="default" />
</actuators>
<sensors>
<kilobot_light implementation="rot_z_only" show_rays="true" />
</sensors>
<params behavior="build/examples/behaviors/simple_movement" />
</kilobot_controller>
</controllers>
<!-- ****************** -->
<!-- * Loop functions * -->
<!-- ****************** -->
<loop_functions library="build/examples/loop_functions/trajectory_loop_functions/libtrajectory_loop_functions"
label="trajectory_loop_functions" />
<!-- *********************** -->
<!-- * Arena configuration * -->
<!-- *********************** -->
<arena size = "1, 1, 1" center = "0, 0, 0.5">
<box id = "W0" size = "0.2, 0.05, 0.05" movable = "false">
<body position = "0, 0.3, 0" orientation = "0, 0, 0"/>
</box>
<box id = "W1" size = "0.05, 0.65, 0.05" movable = "false">
<body position = "0.125, 0, 0" orientation = "0, 0, 0"/>
</box>
<box id = "W2" size = "0.05, 0.65, 0.05" movable = "false">
<body position = "-0.125, 0, 0" orientation = "0, 0, 0"/>
</box>
<light id="l0"
position="0, -0.4, 0"
orientation="0,0,0"
color="yellow"
intensity="10.0"
medium="leds"/>
<kilobot id="k1">
<body position="0,0.15,0" orientation="0,0,0" />
<controller config="kbc" />>
</kilobot>
<kilobot id="k2">
<body position="0.05,0.15,0" orientation="0,0,0" />
<controller config="kbc" />>
</kilobot>
<kilobot id="k3">
<body position="-0.05,0.15,0" orientation="0,0,0" />
<controller config="kbc" />>
</kilobot>
<kilobot id="k4">
<body position="0,0.25,0" orientation="0,0,0" />
<controller config="kbc" />>
</kilobot>
<kilobot id="k5">
<body position="0.05,0.25,0" orientation="0,0,0" />
<controller config="kbc" />>
</kilobot>
</arena>
<!-- ******************* -->
<!-- * Physics engines * -->
<!-- ******************* -->
<physics_engines>
<dynamics2d id="dyn2d" />
</physics_engines>
<!-- ********* -->
<!-- * Media * -->
<!-- ********* -->
<media>
<led id="leds"/>
</media>
<!-- ****************** -->
<!-- * Visualization * -->
<!-- ****************** -->
<visualization>
<!--
This shows how to add customized drawing in the world
using the qt user functions.
Check 'argos3 -q qt-opengl' for more help.
-->
<qt-opengl>
<user_functions library="build/examples/loop_functions/trajectory_loop_functions/libtrajectory_loop_functions"
label="trajectory_qtuser_functions" />
<camera>
<placements>
<placement index="0" position="0,0,8.14689" look_at="0,0,0" up="1,0,0" lens_focal_length="330"/>
</placements>
</camera>
</qt-opengl>
</visualization>
</argos-configuration>