Giter VIP home page Giter VIP logo

pynxt's Introduction

pynxt

Bluetooth control for the Lego NXT.

WARNING

This is a wrapper around another project: C_NXT. That library was written when I had access to a Lego NXT; however, I have not yet tested the bindings. I would love for people to use this but it is currently untested.

When writing that project I hoped that it could be used to introduce programming to kids but being in C caused too many problems. Now that I know Python, I feel like it is a much better language for teaching and have added bindings so that more people have access to this code.

Usage

TODO: sphinx docs.

This library provides one class pynxt.NXT which represents a bluetooth connection to a Lego NXT. The NXT must be paired with the computer, and the constructor is given the MAC address of the NXT, for example:

from pynxt import NXT

nxt = NXT('00:00:00:00:00:00')  # some MAC address

We may also use the NXT object in a context manager to automatically close the connection when we are done.

Attributes

battery_level

The charge remaining in mV.

closed

Is the connection to the Lego NXT closed?

dev_id

The device id of the connected lego NXT.

Methods

close

Close the connection to the Lego NXT.

drive_forward

Tell the nxt to drive forward for some
period of time at a specified power.

Parameters
----------
time : int
    The number of seconds to drive for.
power : int
    How much power should be applied to the motors
    [-100, 100].
left_port : int
    The port where the left motor is connected.
right_port : int
    The port where the right motor is connected.

Raises
------
ValueError
    Raised when the left or right port is out of bounds
    or when the power is not in the range [-100, 100]
IOError
    Raised when communication with the NXT fails.

drive_backward

Tell the nxt to drive backward for some
period of time at a specified power.

Parameters
----------
time : int
    The number of seconds to drive for.
power : int
    How much power should be applied to the motors
    [-100, 100].
left_port : int
    The port where the left motor is connected.
right_port : int
    The port where the right motor is connected.

Raises
------
ValueError
    Raised when the left or right port is out of bounds
    or when the power is not in the range [-100, 100]
IOError
    Raised when communication with the NXT fails.

init_button

Tell the NXT that there is a button plugged to a certain port.

Parameters
----------
port : int
    The port which has a button plugged in.

Raises
------
ValueError
    Raised when the port number is out of bounds.
IOError
    Raised when communication with the NXT fails.

init_light

Tell the NXT that there is a light sensor plugged to a certain
port.

Parameters
----------
port : int
    The port which has a light plugged in.

Raises
------
ValueError
    Raised when the port number is out of bounds.
IOError
    Raised when communication with the NXT fails.

is_pressed

Check if a button is currently pressed.

Parameters
----------
port : int
    The port of the button to check.

Returns
-------
is_pressed : bool
    Is the button currently being pressed?

Raises
------
ValueError
    Raised when the port number is out of bounds.
IOError
    Raised when communication with the NXT fails.

play_tone

Play a tone of a given frequency for a certain amount of time
on the NXT.

Parameters
----------
freq : int
    The frequency to play.
time : int
    The amount of time to play the note for in microsenconds.

Raises
------
IOError
    Raised when communication with the NXT fails.

read_light

Read the value of a light sensor.

 Parameters
 ----------
 port : int
     The port of the light sensor to read.

 Returns
 -------
 value : int
     The value on a scale from 0 to 1024.

 Raises
 ------
 ValueError
     Raised when the port number is out of bounds.
 IOError
     Raised when communication with the NXT fails.

set_motor

Sets the power of a motor.

Parameters
----------
port : int
    The port of the motor to set the power of.
power : int
    The power to set the motor to: [-100, 100].

Raises
------
ValueError
    Raised if the port is out of bounds or the power is not
    in the range [-100, 100].
IOError
    Raised when communication with the NXT fails.

stay_alive

Send a message to the NXT that prevents it from turning off.

If the NXT doesn't see this message for a couple of minutes it
will power down to save battery.

Raises
------
IOError
    Raised when communication with the NXT fails.

stop_all_motors

Stop all of the motors.

Raises
------
IOError
    Raised when communication with the NXT fails.

stop_motor

Stop a motor.

Parameters
----------
port : int
    The port of the motor to stop.

Raises
------
ValueError
    Raised if the port is out of bounds
IOError
    Raised when communication with the NXT fails.

turn_left

Tell the nxt to turn left for some
period of time at a specified power.

Parameters
----------
time : int
    The number of seconds to turn for.
power : int
    How much power should be applied to the motors
    [-100, 100].
left_port : int
    The port where the left motor is connected.
right_port : int
    The port where the right motor is connected.

Raises
------
ValueError
    Raised when the left or right port is out of bounds
    or when the power is not in the range [-100, 100]
IOError
    Raised when communication with the NXT fails.

turn_right

Tell the nxt to turn right for some
period of time at a specified power.

Parameters
----------
time : int
    The number of seconds to turn for.
power : int
    How much power should be applied to the motors
    [-100, 100].
left_port : int
    The port where the left motor is connected.
right_port : int
    The port where the right motor is connected.

Raises
------
ValueError
    Raised when the left or right port is out of bounds
    or when the power is not in the range [-100, 100]
IOError
    Raised when communication with the NXT fails.

License

phorth is free software, available under the terms of the GNU General Public License, version 2 or later. For more information, see LICENSE.

pynxt's People

Stargazers

Ionel Cristian Mărieș avatar

Watchers

James Cloos avatar Joe Jevnik avatar  avatar

Forkers

lepy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.