Basic Usage¶
On this page
Creating a simple robot¶
For this first example, we’ll use the built-in Kilosim::Kilobot
robot. Place this in a src
folder within your project’s root directory. If you want to use a different robot model (or make your own), see the Advanced Usage section.
To create the code that runs on your robot, you need to create a class that inherits from Kilosim::Kilobot
, as shown below. This needs to implement the following functions (based on the Kilobot API):
Kilosim::Kilobot::setup()
: Runs once at the beginning of the simulation. (Note: This is run when the robot is initialized, not when it’s placed in the World, so you don’t yet have access to World-related values like position.)Kilosim::Kilobot::update()
: Runs once per simulation step.Kilosim::Kilobot::message_rx()
: Runs when the robot receiver a message from another robot.Kilosim::Kilobot::message_tx()
: Creates the message that the robot will transmit.Kilosim::Kilobot::message_tx_success()
: Runs when the robot successfully transmits a message.
For more details of the Kilobot API, see the documentation for the Kilosim::Kilobot
class and the Kilobot API.
Below is an example of the minimum requirements for your robot class, which you can use as a template for your own robot. You can also download this directory: minimal_kilobot.py
1/*
2 This is a barebones example implementation of a Kilobot class
3*/
4
5#include <kilosim/Kilobot.h>
6
7#include <iostream>
8
9namespace Kilosim
10{
11class MyKilobot : public Kilobot
12{
13public:
14 // Declare any attributes that you want to be accessible to aggregator
15 // functions as public.
16 int16_t light_intensity = -1;
17
18private:
19 // Declare any other (internal) persistent attributes here.
20
21 // REQUIRED KILOBOT FUNCTIONS
22
23 void setup()
24 {
25 // This code will be run once when you call `robot_init(x, y, theta)`
26 // Note: This should only be done AFTER the Robot has been added to a World
27 }
28
29 void loop()
30 {
31 // This code is run once per kilo_tick.
32 }
33
34 // OPTIONAL KILOBOT FUNCTIONS
35
36 void message_rx(message_t *message, distance_measurement_t *distance_measurement){
37 // This is called when a message is received
38 // On real robots, this is called as an interrupt, so processing here
39 // (outside the loop) should be minimized
40 // Implementing this overrides the default function, which does nothing
41 };
42
43 message_t *message_tx()
44 {
45 // This produces the message to send.
46 // Implementing this overrides the default function, which returns NULL
47 // (No message transmitted)
48 }
49
50 void message_tx_success()
51 {
52 // This is called when a message is successfully transmitted
53 // Implementing this overrides the default function, which does nothing
54 }
55};
56} // namespace Kilosim
Using the Viewer¶
Add it to the main function
Call it once per step (or less frequently)
Configuration and Parameters¶
Configuration files are defined as JSON files and can be loaded with a ConfigParser. The contents of flat JSON files can be automatically saved with your data using the Logger. (Support for saving JSON objects and arrays to HDF5 may be added in the future if someone needs/wants it.)
There are no fixed requirements for the contents of the configuration files; it’s an un-opinionated convenience tool for importing and using whatever (atomic) parameters you want.