Robot library reference
The rone robot contans a number of different libraries to move the robot, to read from sensors and to help keep track of information. This document gives a short overview of the functions you may want to use in this class.
Builtin functions
Builtin functions do not require importing any package. They are called using their name, e.g. abs(2).
-
abs(n): Returns the absolute value of n.
-
chr(n): Returns a one-character string containing the nth ASCII character.
-
dir(): Returns a list of the defined variable names in the current namespace.
-
dir(package): Returns a list of the defined variable names in the given package.
-
len(l): Returns the number of elements in list or tuple l or the number of characters in the string l.
-
ord(s): Returns the ordinal of the one-character string s in the ASCII character set. (The inverse of chr)
-
int(n): Returns the greatest integer less than or equal to n.
-
float(n): Returns the floating point representation of n.
-
pow(b, e): Returns b raised to the power of e.
-
enumerate(l): Returns a list of tuples containing the index of each element in list l with the element itself.
-
range(a): Returns a list of integers from 0 to a-1, inclusive.
-
range(a, b): Returns a list of integers from a to b-1, inclusive.
-
range(a, b, c): Returns a list of integers from a to b-1, inclusive, incrementing by c at each step.
-
sum(l): Returns the arithmetic sum of the elements of l.
The rone library
The rone robots come preloaded with a module named ronethat includes functions for
accessing the various peripherals of the robot. These include motors, encoders, accelerometer, gyroscope,
LEDs, radio, infrared communications, and buttons. This document outlines the functions and their usage.
-
get_id(): Retrieves the unique ID of the robot.
-
radio_get_message():
Retrieves and clears the most recent message on the radio. Returns "" if no message available.
-
radio_send_message(s):
Broadcasts a string over the radio. Radio messages must be 32 characters or fewer.
-
motor_stop(motor):
Stops the specified motor. Motor values can be either "l" or "r", for left or right motor.
-
motor_set_pwm(motor, dutyCycle):
Sets a pwm signal on the specified motor for two seconds on a robot. Duty cycles are between -100 and 100.
To stop use 0, to go in reverse use a negative value and to go forward use
positive values.
-
light_sensor_get_value(light):
Reads the value of a light sensor. Returns a value between 0 and 1023. Light sensors may be "fr", "fl" or "r" for front right, front left or rear.
-
led_set(color, index, brightness):
Sets an LED to the specified brightness. Brightness values can range from 0 to 127. 127 is extremely bright.
The color parameter can either be the colors "red", "green", "blue", or "amber", or the first letter of the
colors for brevity. Index is from 0 to 4 starting at the bottom and moving counterclockwise on the robot for red, green, or blue.
There is only one amber LED.
-
led_set_group(colors, brightness):
This functions sets a whole group of LEDs to the specified brightness. Color may be given as a single string (e.g. led_set_group('r', 100)) or a list of LED groups to set (e.g. led_set_group(['r', 'g'], 50)).
-
ir_comms_send_message():
Transmits a message on the infrared emitters. Message must be 4 characters or fewer.
-
ir_comms_get_message():
Reads and clears the most recent IR message. Returns a tuple containing a the message and a list of receivers that saw the message. For example, it may return ('foo', [0, 2, 3]). Each receiver can be translated into a name using the built-in dictionary rone.ir_comms_names, or into a angular location (in radians) using rone.ir_comms_locations.
-
ir_beacon_set_data(idnumb):
Pulses the beacon according to the data. idnumb must be between 0 and 1023.
-
gyro_get_value():
Returns the angular acceleration of the robot.
-
encoder_get_ticks(motor):
Returns the ticks of the motor. Tick count rolls over at 65535. Motor can be "l" or "r".
-
charger_get_status():
Returns whether or not the robot is allowed to charge. This is distinct from whether or not
the robot is actually charging. Look at the charge light. If it is off, the robot is not charging because the motors are running or the batt is charged, or it is not plugged into usb. If the charge light is flashing, panic and turn off the robot. Something is wrong with the battery.
-
button_get_value(button):
Returns a 1 if a button is being pressed, otherwise it returns 0. Button values can be "r", "g", or "b" referring to the color of the LEDs
surrounding each button.
-
blinky_led_set(state):
Turn the blinky lED on or off.
-
accelerometer_get_value(axis):
Returns the acceleration on either the X, Y, or Z axis. Axis values can be "x", "y", or "z". The Z axis points down when the robot is resting on a table and will reflect the acceleration of gravity. If all values are zero, the robot is either broken or falling.
The sys library
The sys library contains functions that examine and control the internal behavior of the software running on the robot.
-
sleep(ms): Waits for ms milliseconds.
-
time(): Returns the number of milliseconds the robot has been running.
The math library
The math library contains functions for evaluating mathematical functions, such as the square root.
-
sqrt(n): Returns the square root of n. Requires a non-negative number.
-
atan(n): Returns the arctangent (measured in radians) of x.
-
atan2(y, x): Returns the arctangent (measured in radians) of y/x. This function considers the sign of both inputs, so atan2(-1, -1) returns a negative angle.
-
randint(n): Returns a random integer between 0 and n-1.
The dict library
The dict library contains functions for examining and manipulating dictionaries.
-
clear(d): Removes all keys (and values) from dictionary d.
-
keys(d): Returns a list of all keys in dictionary d.
-
values(d): Returns a list of all values in dictionary d.
-
has_key(d, val): Returns True if dictionary d contains key val, returns False otherwise.
The string library
The string library contains functions for examining and manipulating strings.
-
atoi(a): Converts the string of digits a (e.g. "123") into an integer.
-
atoi(a,b ): Converts the string of digits a in base b (e.g. "1b3") into an integer.
-
join(sep, l): Joins each element in l with string sep. Each element in l must be a string. For example: join(", ", ['a', 'b', 'c']) would return "a, b, c".
-
count(s, substring): Returns the number of instances of substring in s.
-
find(s, substring): Returns the the index of the first instance of substring in s.
The list library
The list library contains functions for examining and manipulating lists.
-
append(l, element): Adds element to the end of l.
-
count(l, element): Returns the number of instances of element in l.
-
extend(l, l2): Adds all elements of l2 to the end of l.
-
index(l, element): Returns the the index of the first instance of element in l.
-
insert(l, index, element): Adds element to l at index index.
-
pop(l): Removes and returns last element in l.
-
pop(l, index): Removes and returns element at index index in l.
-
remove(l, element): Removes first instance of element element in l.