Python API

This documentation is automatically generated based on the source file.

Warning

This API is still under development and might still change. Please take this into account when developing (course material).

class robot.Robot

Robot API

This class allows you to control the robot from Python. The getters and setters are just wrappers calling ROS topics or services.

getAnalogPinValue(pin)

Gets the input value of an analog pin.

Parameters:

pin (str) – The pin number of an analog pin as printed on the microcontroller.

Returns:

Value between 0-255.

Return type:

int

getDigitalPinValue(pin)

Gets the input value of a digital pin.

Parameters:

pin (str) – The pin number of an analog pin as printed on the microcontroller.

Returns:

The input value.

Return type:

bool

getDistance(sensor)

Gets data from a HC-SR04 distance sensor: calculated distance in meters.

Parameters:

sensor (str) – The name of the sensor as defined in the configuration.

Returns:

Range in meters measured by the HC-SR04 sensor.

Return type:

int

Warning

A maximum of 6 distance sensors is supported.

getEncoder(sensor)

Gets data from an encoder: every encoder pulse increments the counter.

Parameters:

sensor (str) – The name of the sensor as defined in the configuration.

Returns:

Number of encoder pulses since boot of the robot.

Return type:

int

getIntensity(sensor, type='analog')

Gets data from an intensity sensor.

Parameters:
  • sensor (str) – The name of the sensor as defined in the configuration.

  • type (str) – The type of the sensor (either ‘analog’ or ‘digital’).

Returns:

Value of the sensor (0-255 when analog, 0-1 when digital).

Return type:

int

getKeypad(keypad)

Gets the value of the keypad: the button that is pressed.

Parameters:

keypad (str) – The name of the sensor as defined in the configuration.

Returns:

The name of the button (‘up’, ‘down’, ‘left’, ‘right’, ‘enter’).

Return type:

str

getTimeSinceLastCall()

Gets the elapsed time in seconds since the last call to this function.

Returns:

Time in seconds since last call to this function. Fractions of a second may be present if the system clock provides them.

Return type:

float

getTimestamp()

Gets the elapsed time in seconds since the initialization fo the Robot.

Returns:

Time in seconds since the initialization of the Robot. Fractions of a second may be present if the system clock provides them.

Return type:

float

setAnalogPinValue(pin, value)

Sets the output value of an analog pin (PWM).

Parameters:
  • pin (str) – The pin number of an analog pin as printed on the microcontroller.

  • value (int) – Value between 0-255.

setDigitalPinValue(pin, value)

Sets the output value of a digital pin.

Parameters:
  • pin (str) – The pin number of an analog pin as printed on the microcontroller.

  • value (bool) – Value to set.

setMotorSpeed(motor, value)

Sets the speed of the motor.

Parameters:
  • motor (str) – The name of the sensor as defined in the configuration.

  • value (int) – The ‘directional duty cycle’ (range [-100, 100]) of the PWM signal (-100: full backward, 0: stand still, 100: full forward).

Returns:

True if set successfully.

Return type:

bool

setOLEDAnimation(oled, animation)

Shows animation on the OLED.

Parameters:
  • oled (str) – The name of the sensor as defined in the configuration.

  • animation (str) – Animation (directory) name as defined in the animations folder of the mirte-oled-images repository.

setOLEDImage(oled, image)

Shows image on the OLED.

Parameters:
  • oled (str) – The name of the sensor as defined in the configuration.

  • image (str) – Image name as defined in the images folder of the mirte-oled-images repository (excl file extension).

setOLEDText(oled, text)

Shows text on the OLED.

Parameters:
  • oled (str) – The name of the sensor as defined in the configuration.

  • text (str) – String to be shown on the 128x64 OLED.

setServoAngle(servo, angle)

Sets the angle of a servo.

Parameters:
  • servo (str) – The name of the sensor as defined in the configuration.

  • angle (int) – The angle of the servo (range [0-360], but some servos might be hysically limited to [0-180].

Returns:

True if set successfully.

Return type:

bool

Warning

The servo uses the Servo library from Arduino (through Telemetrix). This also means that, when a servo is used and the library is enabled, the last timer on the MCU will be used for timing of the servos. This timer therefore can not be used for PWM anymore. For Arduino Nano/Uno this means pins D9 and D10 will not have PWM anymore. For the SMT32 this means pins A1, A2, A3, A15, B3, B10, and B11 will not have PWM anymore.

Warning

A maximum of 12 servos is supported.

stop()

Stops all DC motors defined in the configuration

Note

This function is always called when a script exits (either by the user or when it finished.

robot.createRobot()

Creates and return instance of the robot class.

Returns:

The initialize Robot class.

Return type:

Robot