Undergraduate Kilobot Course UoA

Typically: "How do I... ", "How can I... " questions
Post Reply
Hedgie
Posts: 9
Joined: 19 Mar 2020, 14:48

Undergraduate Kilobot Course UoA

Post by Hedgie »

Hi I'm helping to set up a new robotics and kinematics course for students at the University of Aberdeen. Due to the current pandemic it is important for us to have simulations as well as physical Labs as students and staff are currently all working from home for the foreseeable future. We want to use CoppeliaSim as not only is it the best but it already has a model of a kilobot and kilobot controller.
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;
}
https://www.youtube.com/watch?v=pd1CseHrvA8

fferri
Posts: 1224
Joined: 09 Sep 2013, 19:28

Re: Undergraduate Kilobot Course UoA

Post by fferri »

Hi,

I don't know the Kilobot robot. But if you want to keep that very same C programming interface, you can implement all the Kilobot API functions in C++ to use the CoppeliaSim remote API to affect the simulated kilobot model.

Have a look at Remote API

Hedgie
Posts: 9
Joined: 19 Mar 2020, 14:48

Re: Undergraduate Kilobot Course UoA

Post by Hedgie »

Thank you very much fferri. That's given me another alternative now, when I read the manual it offered 6 ways to do it but with your help we will now either switch between the 2 languages for real life and VR or interface the kilobot API into the simulator. Some of the undergrads that will be doing robotics are very good at programming etc so I'll bring this up at the next Microsoft teams meeting.

Oh sorry to ask but which remote API version would you choose to do this......BO or Legacy?

fferri
Posts: 1224
Joined: 09 Sep 2013, 19:28

Re: Undergraduate Kilobot Course UoA

Post by fferri »

B0-based API is newer and I think is more versatile in terms of extending it with more commands. But I'm not really experienced with that.

Hedgie
Posts: 9
Joined: 19 Mar 2020, 14:48

Re: Undergraduate Kilobot Course UoA

Post by Hedgie »

Thank you again fferri. That is what I was thinking based on what I read on the link you sent me. I dont know how to do this remote API stuff but I will have a go over the next few days and let you know how I get on.
Your input here is much appreciated.

Post Reply