Skip to content

Examples

Most examples include two variants for serial, and MQTT. Source code can be found here

For more information, see Serial vs. MQTT

Core

Robot Connection

examples/core/connecting_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

while True:
    time.sleep(1)
examples/core/connecting.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect("kevinbot", "localhost", 1883)

while True:
    time.sleep(1)

Robot Enabling and Disabling

examples/core/enable_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

robot.request_enable()  # Ask the core to enable
while not robot.get_state().enabled:  # Wait until the core is enabled
    time.sleep(0.01)

time.sleep(3)

robot.request_disable()  # Ask the core to disable
while robot.get_state().enabled:  # Wait until the core is disabled
    time.sleep(0.01)

time.sleep(3)  # Let the user see that the robot is disabled before disconnecting
examples/core/enable.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

robot.request_enable()  # Ask the core to enable
while not robot.get_state().enabled:  # Wait until the core is enabled
    time.sleep(0.01)

time.sleep(3)

robot.request_disable()  # Ask the core to disable
while robot.get_state().enabled:  # Wait until the core is disabled
    time.sleep(0.01)

time.sleep(3)  # Let the user see that the robot is disabled before disconnecting

Robot Emergency Stop

examples/core/estop_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

robot.e_stop()
examples/core/estop.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

robot.e_stop()

Robot State Retrieval

examples/core/state_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

while True:
    print(robot.get_state())  # noqa: T201
    time.sleep(1)
examples/core/state.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

while True:
    print(robot.get_state())  # noqa: T201
    time.sleep(1)

Robot Timestamp Retrieval MQTT Only

examples/core/timestamp.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

while True:
    print(robot.ts)  # noqa: T201
    time.sleep(1)

Robot Uptime Retrieval

examples/core/uptimes_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

while True:
    print(f"Uptime (s) : {robot.get_state().uptime}")  # noqa: T201
    print(f"Uptime (ms): {robot.get_state().uptime_ms}")  # noqa: T201
    time.sleep(1)
examples/core/uptimes.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

while True:
    print(f"Uptime (s) : {robot.get_state().uptime}")  # noqa: T201
    print(f"Uptime (ms): {robot.get_state().uptime_ms}")  # noqa: T201
    time.sleep(1)

Battery

Robot Battery Readings

examples/battery/readings_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

time.sleep(3)  # Wait to get data

print(f"Voltages: {robot.get_state().battery.voltages}")  # noqa: T201
print(f"Raw Voltages: {robot.get_state().battery.raw_voltages}")  # noqa: T201
print(f"States: {robot.get_state().battery.states}")  # noqa: T201
examples/battery/readings.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

time.sleep(3)  # Wait to get data

print(f"Voltages: {robot.get_state().battery.voltages}")  # noqa: T201
print(f"Raw Voltages: {robot.get_state().battery.raw_voltages}")  # noqa: T201
print(f"States: {robot.get_state().battery.states}")  # noqa: T201

Environment

BME280

examples/environment/bme280_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

while True:
    print(f"Temp  : {robot.get_state().enviro.temperature} *C")  # noqa: T201
    print(f"Humi  : {robot.get_state().enviro.humidity} %")  # noqa: T201
    print(f"Pres  : {robot.get_state().enviro.pressure} hPa")  # noqa: T201
    time.sleep(2)
examples/environment/bme280.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

while True:
    print(f"Temp  : {robot.get_state().enviro.temperature} *C")  # noqa: T201
    print(f"Humi  : {robot.get_state().enviro.humidity} %")  # noqa: T201
    print(f"Pres  : {robot.get_state().enviro.pressure} hPa")  # noqa: T201
    time.sleep(2)

DS18B20

examples/environment/ds18b20_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

while True:
    print(f"Left Motor : {robot.get_state().thermal.left_motor} *C")  # noqa: T201
    print(f"Right Motor: {robot.get_state().thermal.right_motor} *C")  # noqa: T201
    print(f"Internal: {robot.get_state().thermal.internal} *C")  # noqa: T201
    time.sleep(2)
examples/environment/ds18b20.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

while True:
    print(f"Left Motor : {robot.get_state().thermal.left_motor} *C")  # noqa: T201
    print(f"Right Motor: {robot.get_state().thermal.right_motor} *C")  # noqa: T201
    print(f"Internal: {robot.get_state().thermal.internal} *C")  # noqa: T201
    time.sleep(2)

IMU

Periodic Polling

examples/imu/periodic_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

while True:
    print(f"Gyro : {robot.get_state().imu.gyro}")  # noqa: T201
    print(f"Accel: {robot.get_state().imu.accel}")  # noqa: T201
    time.sleep(1)
examples/imu/periodic.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot

robot = MqttKevinbot()
robot.connect()

while True:
    print(f"Gyro : {robot.get_state().imu.gyro}")  # noqa: T201
    print(f"Accel: {robot.get_state().imu.accel}")  # noqa: T201
    time.sleep(1)

Plotting with matplotlib

Servo

Sweep demo

Drive

Status

examples/drive/status_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import Drivebase, SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

drive = Drivebase(robot)

robot.request_enable()  # Ask the core to enable
while not robot.get_state().enabled:  # Wait until the core is enabled
    time.sleep(0.01)

time.sleep(1)  # Wait for data to arrive

print(f"Speeds: {drive.get_powers()}")  # noqa: T201
print(f"Watts: {drive.get_watts()}")  # noqa: T201
print(f"Amps: {drive.get_amps()}")  # noqa: T201
examples/drive/status.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import Drivebase, MqttKevinbot

robot = MqttKevinbot()
robot.connect()

drive = Drivebase(robot)

robot.request_enable()  # Ask the core to enable
while not robot.get_state().enabled:  # Wait until the core is enabled
    time.sleep(0.01)

time.sleep(1)  # Wait for data to arrive

print(f"Speeds: {drive.get_powers()}")  # noqa: T201
print(f"Watts: {drive.get_watts()}")  # noqa: T201
print(f"Amps: {drive.get_amps()}")  # noqa: T201

Drive at Power

examples/drive/drive_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import Drivebase, SerialKevinbot

robot = SerialKevinbot()
robot.connect("/dev/ttyAMA2", 921600, 5, 1)

drive = Drivebase(robot)

input("LIFT THE WHEELS OFF THE GROUND FOR THIS TEST!!! [Return] to start test")

robot.request_enable()  # Ask the core to enable
while not robot.get_state().enabled:  # Wait until the core is enabled
    time.sleep(0.01)

time.sleep(1)  # Wait for data to arrive
drive.drive_at_power(0.2, 0.2)
time.sleep(3)
drive.drive_at_power(0.5, 0.2)
time.sleep(3)
drive.drive_at_power(0.2, 0.5)
time.sleep(3)
drive.drive_at_power(0.05, 0.05)
time.sleep(3)
# Will auto-stop on disconnect
# drive.stop()
examples/drive/drive.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import Drivebase, MqttKevinbot

robot = MqttKevinbot()
robot.connect()

drive = Drivebase(robot)

input("LIFT THE WHEELS OFF THE GROUND FOR THIS TEST!!! [Return] to start test")

robot.request_enable()  # Ask the core to enable
while not robot.get_state().enabled:  # Wait until the core is enabled
    time.sleep(0.01)

time.sleep(1)  # Wait for data to arrive
drive.drive_at_power(0.2, 0.2)
time.sleep(3)
drive.drive_at_power(0.5, 0.2)
time.sleep(3)
drive.drive_at_power(0.2, 0.5)
time.sleep(3)
drive.drive_at_power(0.05, 0.05)
time.sleep(3)
# Will auto-stop on disconnect
# drive.stop()

Eyes

Connecting

examples/eyes/connecting_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialEyes

eyes = SerialEyes()
eyes.connect("/dev/ttyUSB0", 115200, 5)

while True:
    time.sleep(1)
examples/eyes/connecting.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttEyes, MqttKevinbot

robot = MqttKevinbot()
robot.connect()

eyes = MqttEyes(robot)

while True:
    time.sleep(1)

State

examples/eyes/state_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialEyes

eyes = SerialEyes()
eyes.connect("/dev/ttyUSB0", 115200, 5)

print(eyes.get_state().model_dump_json(indent=4))  # noqa: T201

time.sleep(1)
examples/eyes/state.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttEyes, MqttKevinbot

robot = MqttKevinbot()
robot.connect()

eyes = MqttEyes(robot)

print(eyes.get_state().model_dump_json(indent=4))  # noqa: T201

time.sleep(1)

Backlight

examples/eyes/backlight_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import SerialEyes

eyes = SerialEyes()
eyes.connect("/dev/ttyUSB0", 115200, 5)

for i in range(100):
    eyes.set_backlight(i / 100)
    time.sleep(0.05)
examples/eyes/backlight.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import MqttKevinbot
from kevinbotlib.eyes import MqttEyes

robot = MqttKevinbot()
robot.connect()

eyes = MqttEyes(robot)

for i in range(100):
    eyes.set_backlight(i / 100)
    time.sleep(0.05)

Motion Mode Control

examples/eyes/motions_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import EyeMotion, EyeSkin, SerialEyes

eyes = SerialEyes()
eyes.connect("/dev/ttyUSB0", 115200, 5)

eyes.set_skin(EyeSkin.SIMPLE)

eyes.set_motion(EyeMotion.DISABLE)
time.sleep(3)

eyes.set_motion(EyeMotion.LEFT_RIGHT)
time.sleep(3)

eyes.set_motion(EyeMotion.JUMP)
time.sleep(3)
examples/eyes/motions.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import EyeMotion, EyeSkin, MqttEyes, MqttKevinbot

robot = MqttKevinbot()
robot.connect()

eyes = MqttEyes(robot)

eyes.set_skin(EyeSkin.SIMPLE)

eyes.set_motion(EyeMotion.DISABLE)
time.sleep(3)

eyes.set_motion(EyeMotion.LEFT_RIGHT)
time.sleep(3)

eyes.set_motion(EyeMotion.JUMP)
time.sleep(3)

Skin Setting Control

examples/eyes/skins_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import EyeSkin, SerialEyes

eyes = SerialEyes()
eyes.connect("/dev/ttyUSB0", 115200, 5)

eyes.set_skin(EyeSkin.TV_STATIC)
time.sleep(2)

eyes.set_skin(EyeSkin.SIMPLE)
time.sleep(2)

eyes.set_skin(EyeSkin.METAL)
time.sleep(2)

eyes.set_skin(EyeSkin.NEON)
time.sleep(2)
examples/eyes/skins.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import time

from kevinbotlib import EyeSkin, MqttEyes, MqttKevinbot

robot = MqttKevinbot()
robot.connect()

eyes = MqttEyes(robot)

eyes.set_skin(EyeSkin.TV_STATIC)
time.sleep(2)

eyes.set_skin(EyeSkin.SIMPLE)
time.sleep(2)

eyes.set_skin(EyeSkin.METAL)
time.sleep(2)

eyes.set_skin(EyeSkin.NEON)
time.sleep(2)

Manual Motions Demo

examples/eyes/manual_serial.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import random
import time

from kevinbotlib import EyeMotion, EyeSkin, SerialEyes

eyes = SerialEyes()
eyes.connect("/dev/ttyUSB0", 115200, 5)

eyes.set_skin(EyeSkin.SIMPLE)

eyes.set_motion(EyeMotion.MANUAL)

for _i in range(10):
    eyes.set_manual_pos(random.randint(0, 240), random.randint(0, 240))  # noqa: S311
    time.sleep(1)
examples/eyes/manual.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# SPDX-FileCopyrightText: 2024-present Kevin Ahr <meowmeowahr@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import random
import time

from kevinbotlib import EyeMotion, EyeSkin, MqttEyes, MqttKevinbot

robot = MqttKevinbot()
robot.connect()

eyes = MqttEyes(robot)

eyes.set_skin(EyeSkin.SIMPLE)

eyes.set_motion(EyeMotion.MANUAL)

for _i in range(10):
    eyes.set_manual_pos(random.randint(0, 240), random.randint(0, 240))  # noqa: S311
    time.sleep(1)

Simple Skin Full Options Demo

Metal Skin Full Options Demo

Neon Skin Full Options Demo