Kilobot

class Kilosim::Kilobot : public Kilosim::Robot

The abstract class Kilobot provides the implementation of the functions and attributes given by the Kilolib. You can imagine this as standing in for the physical Kilobots that your code will run on.

Your implementation of Kilobot code should be in a class that inherits from the Kilobot class. It must implement the methods setup() and loop(). Unlike when using the Kilolib, these are automatically used as passed to the kilo_start function. Similarly, the following substitutions are made in place of using a main() function in Kilobot:

This means that instead of setting these in a main() function, you simply implement the righthand methods in your Kilobot class.

Note

Any values (attributes) that you want to be accessible to your aggregator functions must be declared public.

Protected Functions

virtual void setup() = 0

[User API] User-implemented setup function that is run once in initialization

virtual void loop() = 0

[User API] User-implemented loop function that is called for the Kilobot on every tick

virtual void message_rx(message_t *message, distance_measurement_t *distance_measurement) = 0

[User API] Function that is called when the Kilobot receives a message On real robots, this is called as an interrupt, so processing here (outside the loop) should be minimized

Parameters
  • message – Contents of the received message

  • distance_measurement – Estimated distance (in mm) from the Kilobot sending the message

virtual message_t *message_tx() = 0

[User API] Produce the message to transmit By default, it returns NULL, which means no message is transmitted

Returns

Contents of the sent message

virtual void message_tx_success() = 0

[User API] Callback for successful message transmission (By default, it does nothing)

inline rgb RGB(double r, double g, double b)

[KiloLib API] Create an RGB color

Parameters
  • r – Red intensity (0-1)

  • g – Green intensity (0-1)

  • b – Blue intensity (0-1)

inline uint8_t estimate_distance(distance_measurement_t *d)

[Kilolib API] Estimate distance in mm based on signal strength measurements.

TODO: This isn’t used but it’s part of the Kilolib API. Not even sure of its accuracy…

Parameters

d – Signal strength measurement for a message

Returns

Positive integer distance estimate in mm

inline void delay(uint16_t ms)

[Kilolib API] Pauses the program for a specified amount of time

This function receives as an argument a positive 16-bit integer ms that represents the number of milliseconds for which to pause the program

TODO: Part of the KiloLib API but does nothing (issue: using kilo_ticks for timing in simulation)

Parameters

ms – Number of milliseconds to pause the program (there are 1000 milliseconds in a second).

inline uint16_t message_crc(message_t *m)

[KiloLib API] Compute a cyclic redundancy check for a message Used as error-detecting code for receiving robot to verify the contents of the message.

Parameters

m – Pointer to the message for which to create a code

Returns

Byte hashing the message data contents

inline uint8_t rand_hard()

[Kilolib API] Hardware random number generator TODO: Currently this does the same thing as rand_soft

inline uint8_t rand_soft()

[Kilolib API] Software random number generator TODO: Currently does the same thing as rand_hard

inline void rand_seed(char seed)

[Kilolib API] Seed software random number generator. TODO: Currently this does nothing

inline int16_t get_ambientlight()

[Kilolib API] Get the 10-bit light intensity from the Kilobot’s light sensor (from World’s LightPattern)

Returns

10-bit monochrome light intensity

inline void set_motors(char l, char r)

[Kilolib API] Set the rate of both the motors. Set both to go straight

Parameters
  • l – Speed of the motor to turn left

  • r – Speed of the motor to turn right

inline void spinup_motors()

[Kilolib API] Spin up both motors to overcome static friction

inline void set_color(rgb c)

[Kilolib API] Set the Kilobot’s LED color

Parameters

c – RGB color to set the LED to

inline virtual bool comm_criteria(double dist)

Determine if another robot is within communication range This is called by a transmitting (tx) robot to verify if the receiver is within range when sending a message OUT. Because of possible asymmetries in communication range, both comm_criteria() must be met by both the tx and rx robots for a message to be successfully transmitted.

Parameters

dist – Distance between the robots (in mm)

Returns

true if robot can communicate with another robot

inline virtual void *get_message()

Get a void pointer to the message the robot is sending and handle any callbacks for successful message transmission

Returns

Pointer to message to transmit

inline virtual void received()

This is called by a transmitting robot (tx) to set a flag for calling the message success callback (message_tx_success)

inline virtual void receive_msg(void *msg, double dist)

This is called when a robot (rx) receives a message. It calls some message handling function (e.g., message_rx) specific to the implementation.

Protected Attributes

uint32_t kilo_ticks = 0

[Kilolib API] Kilobot clock variable

const int kilo_straight_left = 50

[Kilolib API] Calibrated straight (left motor) duty cycle

const int kilo_straight_right = 50

[Kilolib API] Calibrated straight (right motor) duty cycle

const int kilo_turn_left = 50

[Kilolib API] Calibrated turn left duty cycle

const int kilo_turn_right = 50

[Kilolib API] Calibrated turn right duty cycle