We are thinking that the best way to do this is to put the code for the different kilobot demos into the child script of the kilobot model. The Kilobots use there own programming language which is a version of C, and the simulator uses LUA which is totally new to me. This would mean learing both languages and manually writing 2 versions of code, one for the physical bots and one for the simulated bots. Obviously this would have potential for human errors and would be time consuming.
I would appreciate any advice from any expert users of CopelliaSim as I.m not sure if this is the way we should be doing this or if it is the correct way please let us know and we will get our heads down and teach ourselves LUA.
The following is a paste of the entire kilobot API library followed by a very short example of kilobot source code and the simulator code required for these bots can be found in the kilobot model child script (written by Harvard University I think).
I have also pasted a link to a 2 minute youtube video of what the example code makes the kilobots do.
Any comments would be greatly appreciated.
Kilolib
Kilobot Library
click to disable panel synchronisation
Kilolib
Kilobot Library API
File List
Global Index
All
Functions
Variables
Macros
Data Structures
Here is a list of all documented functions, variables, defines, enums, and typedefs with links to the documentation:
debug_init : debug.h
delay() : kilolib.h
estimate_distance() : kilolib.h
get_ambientlight() : kilolib.h
get_temperature() : kilolib.h
get_voltage() : kilolib.h
kilo_init() : kilolib.h
kilo_message_rx : kilolib.h
kilo_message_tx : kilolib.h
kilo_message_tx_success : kilolib.h
kilo_start() : kilolib.h
kilo_straight_left : kilolib.h
kilo_straight_right : kilolib.h
kilo_ticks : kilolib.h
kilo_turn_left : kilolib.h
kilo_turn_right : kilolib.h
kilo_uid : kilolib.h
message_crc() : kilolib.h
rand_hard() : kilolib.h
rand_seed() : kilolib.h
rand_soft() : kilolib.h
set_color() : kilolib.h
set_motors() : kilolib.h
spinup_motors() : kilolib.h
Code: Select all
#include <kilolib.h>
// Constants for light following.
#define THRESH_LO 300
#define THRESH_HI 600
// Constants for motion handling function.
#define STOP 0
#define FORWARD 1
#define LEFT 2
#define RIGHT 3
int current_motion = STOP;
int current_light = 0;
// Function to handle motion.
void set_motion(int new_motion)
{
// Only take an action if the motion is being changed.
if (current_motion != new_motion)
{
current_motion = new_motion;
if (current_motion == STOP)
{
set_motors(0, 0);
}
else if (current_motion == FORWARD)
{
spinup_motors();
set_motors(kilo_straight_left, kilo_straight_right);
}
else if (current_motion == LEFT)
{
spinup_motors();
set_motors(kilo_turn_left, 0);
}
else if (current_motion == RIGHT)
{
spinup_motors();
set_motors(0, kilo_turn_right);
}
}
}
// Function to sample light.
void sample_light()
{
// The ambient light sensor gives noisy readings. To mitigate this,
// we take the average of 300 samples in quick succession.
int number_of_samples = 0;
int sum = 0;
while (number_of_samples < 300)
{
int sample = get_ambientlight();
// -1 indicates a failed sample, which should be discarded.
if (sample != -1)
{
sum = sum + sample;
number_of_samples = number_of_samples + 1;
}
}
// Compute the average.
current_light = sum / number_of_samples;
}
void setup()
{
// This ensures that the robot starts moving.
set_motion(LEFT);
}
void loop()
{
sample_light();
if (current_light < THRESH_LO)
{
set_motion(RIGHT);
}
else if (current_light > THRESH_HI)
{
set_motion(LEFT);
}
}
int main()
{
kilo_init();
kilo_start(setup, loop);
return 0;
}