Skip to main content

An object-oriented wrapper for the KIPR library

Project description

BotballKit

An object-oriented wrapper for the KIPR library

Why

The KIPR library is bad in many ways. First of all, it's not even a real Python library, it's just a rushed implementation of its C library.

This is how you import it:

kipr = ctypes.CDLL('/usr/lib/libkipr.so')

Okay, so it's technically not a pure Python library, but it still looks like one, right?

kipr = ctypes.CDLL('/usr/lib/libkipr.so')

status = kipr.digital(0)
print(status)

kipr.motor(0, 100)
kipr.motor(1, 100)
kipr.msleep(2000)

No. This looks like a C library, because that's all it is. It's barely object-oriented and the names of the functions are inconsistent and not Pythonic.

The Solution

The solution to this is a nice, object-oriented wrapper for the official KIPR library. Here's an example that sees if a push sensor is being pressed.

from BotballKit import Bot, Sensor, SensorType

bot = Bot()
sensor = Sensor(bot, SensorType.DIGITAL, 0)
left_motor = Motor(bot, 0)
right_motor = Motor(bot, 1)

status = sensor.status()
print(status)

left_motor.move_async(100, 2)
right_motor.move(100, 2)

Much better, right? You create a bot object, a sensor object, two motor objects for each wheel, then you simply get the status of the sensor and print it and move the bot forward for two seconds.

But It's Longer

While in this example the code is longer than the same example with the official library, as you keep adding on to this program the code will be shorter.

Also having an object-oriented library with much more consistent method names can make it easier to remember and harder to make mistakes.

Documentation

Creating a Bot

The first step to using this library is to create a Bot object. Doing that is as simple as:

from BotballKit import Bot

bot = Bot()

Now we have a Bot object stored in the bot variable.

The Bot constructor can optionally take these parameters:

  • wait_for_port: Optional[int] = None: Waits for a sensor on the specified port to be True before starting anything else. This will block the thread until the condition is met.

  • time_limit: Optional[float] = None: Stops the program after the specified number of seconds.

Bot objects also have these methods:

  • Bot.stop_all_motors(): Stops all motors associated with the bot.

  • Bot.enable_all_servos(): Enables all servos associated with the bot.

  • Bot.disable_all_servos(): Disables all servos associated with the bot.

Sensors

Creating Sensor objects is also very simple:

from BotballKit import Sensor, SensorType

sensor = Sensor(bot, SensorType.DIGITAL, 0)

The Sensor constructor must have these parameters:

  • bot: Bot: Your bot object.

  • type: SensorType: The type of sensor you want to use. The two options are SensorType.DIGITAL and SensorType.ANALOG.

  • port: int: The port your sensor is physically connected to.

It has this method available:

  • Sensor.status() -> Union[bool, int]: Gets the status of the sensor. If the sensor is digital, it will return a bool. If the sensor is analog, it will return an int.

Motors

To create a Motor, all you have to do is:

from BotballKit import Motor

motor = Motor(bot, 0)

The Motor constructor requires these parameters be passed:

  • bot: Bot: Your bot object.

  • port: int: The port your motor is physically connected to.

They have these methods available:

  • Motor.stop(): Stops the motor.

  • Motor.move(power: int, seconds: float): Moves the motor with the specified power between -100 and 100 for the specified amount of seconds.

  • Motor.move_async(power: int, seconds: float): Does the same thing as the Motor.move method except it runs asynchronously.

Servos

Creating servos is also easy:

from BotballKit import Servo

servo = Servo(bot, 0)
servo.enable()

All servos need to be enabled to work. This can be done with the Servo.enable method or the Bot.enable_all_servos method.

Its constructor takes the following parameters:

  • Servo.enable(default_position: Optional[int] = None): Enables the servo. If the default_position parameter is specified, the servo will be set to that position when enabled.

  • Servo.disable(default_position: Optional[int] = None): Disables the servo. If the default_position parameter is specified, the servo will be set to that position when disabled.

  • Servo.is_enabled() -> bool: Returns whether the servo is enabled or not.

  • Servo.toggle(default_position: Optional[int] = None): Toggles the servo. If the default_position parameter is specified, the servo will be set to that position when toggled.

  • Servo.position(position: Optional[int] = None) -> Optional[int]: Gets or sets the position of the servo. If no parameters are specified, it will return the current position as an int. If the position parameter is specified, it will set the servo position to that value and return None.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

BotballKit-1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

BotballKit-1.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file BotballKit-1.0.tar.gz.

File metadata

  • Download URL: BotballKit-1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for BotballKit-1.0.tar.gz
Algorithm Hash digest
SHA256 e0df593a088883c539995ef755f0ccee2154252d589b4aae5f0fc6d5fc23fb92
MD5 562aaa1a2851dc4fb23a7e1f9d68949f
BLAKE2b-256 10a38ade167158e92004d868ab4bacc858e941b475f5f3b33d4201a9f53779ab

See more details on using hashes here.

File details

Details for the file BotballKit-1.0-py3-none-any.whl.

File metadata

  • Download URL: BotballKit-1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for BotballKit-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4eebddcd66e8b6f90c5359dfc74121682c7d6ccf483915a5c93702453823aed
MD5 548d377777792d8935d05bf253ed1865
BLAKE2b-256 5a4454c5a36e9d5ccefebb02cb3de68bec335659137355de8207d93a5e20b3fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page