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(*args, **kwargs)
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: str, mode: Literal['percentage']) int
- getAnalogPinValue(pin: str, mode: Literal['raw']) int
- getAnalogPinValue(pin: str, mode: Literal['voltage']) float
Gets the input value of an analog pin.
- Parameters:
pin (str) – The pin number of an analog pin as printed on the microcontroller.
mode (str, optional) – The units of the value, can be “percentage”, “raw” or “voltage”. Defaults to “percentage”.
- Returns:
Value of the pin (0-100 when percentage, 0-<max_mcu_value> when raw, 0-<max_mcu_voltage>V when voltage).
- Return type:
int | float
- getColor(sensor: str) dict[str, float]
Gets the value of the color sensor.
- Parameters:
sensor (str) – The name of the sensor as defined in the configuration.
- Returns:
Hue (0-360), Saturation (0-1), Lightness. {h, s, l}: Hue (0-360), Saturation (0-1), Lightness.
- Return type:
{h, s, l}
- getColorRGBW(sensor: str) dict[str, float]
Gets the value of the color sensor.
- Parameters:
sensor (str) – The name of the sensor as defined in the configuration.
- Returns:
Scaled (0-1) values per R(ed), G(reen), B(lue), and W(hite).
- Return type:
{r, g, b, w}
- getDigitalPinValue(pin: str) bool
Gets the input value of a digital pin.
- Parameters:
pin (str) – The pin number of a digital pin as printed on the microcontroller.
- Returns:
The input value.
- Return type:
bool
- getDistance(sensor: str) float
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. (The distance gets clamped to minimum and maximum range of the HC-SR04 sensor)
- Return type:
float
Warning
A maximum of 6 distance sensors is supported.
- getEncoder(sensor: str) int
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: str, type: Literal['analog']) float
- getIntensity(sensor: str, type: Literal['digital']) int
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.0-1.0 when analog, 0-1 when digital).
- Return type:
int | float
- getKeypad(keypad: str) Literal['', 'up', 'down', 'left', 'right', 'enter']
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
- getROSNode()
- getTimeSinceLastCall() float
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() float
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: str, value: int | float, mode: Literal['percentage']) bool
- setAnalogPinValue(pin: str, value: int, mode: Literal['raw']) bool
- setAnalogPinValue(pin: str, value: float, mode: Literal['voltage']) bool
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: str, value: bool) bool
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.
- setMotorControl(status: bool) bool
Enables/disables the motor controller. This is enabled on boot, but can be disabled/enabled at runtime. This makes the ROS control node pause, so it will not respond to Twist messages anymore when disabled.
- Parameters:
status (bool) – To which status the motor controller should be set.
- Returns:
True if succes (ok)
- Return type:
bool
- setMotorSpeed(motor: str, value: int) bool
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: str, animation: str) bool
Shows animation on the OLED.
- Parameters:
oled (str) – The name of the sensor as defined in the configuration.
animation (str) – Animation (directory) name/path either an absolute path, a path relative to the folder of the mirte-oled-images repository or and package relative path (pkg://PACKAGE_NAME/REST/OF/PATH).
- setOLEDImage(oled: str, image: str) bool
Shows image on the OLED.
- Parameters:
oled (str) – The name of the sensor as defined in the configuration.
image (str) – Image name/path either an absolute path, a path relative to the folder of the mirte-oled-images repository or and package relative path (pkg://PACKAGE_NAME/REST/OF/PATH). The image extension can be omitted if its png.
- setOLEDText(oled: str, text: str) bool
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: str, angle: float) bool
Sets the angle of a servo.
- Parameters:
servo (str) – The name of the sensor as defined in the configuration.
angle (float) – The angle of the servo (range [0-360], but some servos might be physically 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() None
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.