Hardware Reference
In-Depth Information
If ROS is still running, this might give you an error about not being able to access the
NXT Intelligent Brick (only one application at a time can talk to the NXT Brick). Kill any
running ROS processes and try again.
Obviously, your own readings will vary from these examples. Another example in /usr/
share/doc/python-nxt/examples/spin.py shows how you can trigger motors attached
to the NXT Intelligent Brick output ports. This code is short, so we will walk through it
here:
#!/usr/bin/env python
import nxt.locator
from nxt.motor import *
The first line tells the shell that this is a Python program and needs to be run through
the Python interpreter when executed directly. The next two lines import the specific
functions from the python-nxt library that the script is using, the nxt.locator function
that finds and connects to the NXT brick, and all of the nxt.motor functions.
This section defines the spin_around function:
def spin_around(b):
m_left = Motor(b, PORT_B)
m_left.turn(100, 360)
m_right = Motor(b, PORT_C)
m_right.turn(-100, 360)
It creates the m_left variable, which is mapped to the motor connected to the brick's
output Port B, and the m_right variable, which is mapped to the motor connected to
output Port C. It then tells m_left to do a 360° turn with 100 power units (the possible
range is from -127 to 128, and the python-nxt code recommends that you have an
absolute value greater than 64). Next, it tells m_right to do a 360° turn at -100 power.
In other words, this function does exactly what it says: it uses the motors to spin the
robot around.
This last part of the code finds the NXT Intelligent Brick and assigns it to the b variable,
then tells that brick to spin_around :
b = nxt.locator.find_one_brick()
spin_around(b)
Try not to get too dizzy.
There are other good examples of using python-nxt in /usr/share/doc/python-nxt/
examples/ , as well as on the python-nxt website .
Search WWH ::




Custom Search