Robot lego nxt brick resetting
4 stars based on
The NXT is a general-purpose processor that can be used to control motors and sensors; it is ideal robot lego nxt brick resetting building autonomous robots. It can also communicate with more elaborate software located on a robot lego nxt brick resetting, using a Bluetooth communication port.
In this article, we show how to communicate with the NXT by sending the correct bytes. We also introduce a package that manages all the exchanges through functions. Robots are ideal vehicles for testing cognitive theories regarding learning, adaptations, and classification of environmental stimuli.
Most robots are built around a central processor in charge of dealing with the motor output and the sensory input. This brick, equipped with four sensor inputs and three motor outputs, has kilobytes of flash memory in which files can be stored. It can run programs compiled for the brick files ending with the. The versatility and low cost of this brick has made it an ideal vehicle for developing robotic projects. The NXT is well-suited for developing autonomous robots such as scouting robots.
Learning algorithms can also be tested; for example, to find the exit of a maze more aptly than by trial and error. Finally, the NXT can also be used to study social cognition models, in which multiple robots must interact to enhance the group survival rate.
This article shows how to control the brick over a Bluetooth communication link. We also introduce the Math4NXT package, which makes the task easier. At that point, a virtual serial communication port a COM port is allocated to serve as a communication link between the computer and the brick.
The exact COM port given is variable e. COM11 but remains constant for this NXT-computer pair detailed instructions to achieve the pairing can be found in [ 1 ]. Once you know which COM port to use, you do not need to repeat the process. It robot lego nxt brick resetting so even if the brick is currently running a program.
These commands are called direct commands in the LEGO documentation robot lego nxt brick resetting 2 ]. Direct commands include file manipulation, motor control, sensor configuration and reading, and communication between bricks. Commands are sent over the COM port as telegrams composed of a succession of bytes numbers between 0 and For some commands, a reply can be returned by the brick, also in the form of a telegram.
Each telegram is preceded by the length of the telegram, given as a WORD number, that is, using two bytes. Hence, controlling the NXT brick is simply a matter of sending the correct bytes in the correct order over a serial port.
This package has two parts. The second part has the Mathematica functions that can be used in your project. The following figure illustrates the different layers through which information goes during a communication between an NXT brick and a computer. The software architecture by which an NXT brick and a computer can communicate.
The last three boxes on the right represent Mathematica programs, and the last two are given by the package Math4NXT described next. For convenience, you can set your working directory to this path so that the MathLink -compatible program is located automatically using the following instruction.
The command is successful if you can see a process named SerialIO running in the background of your computer.
The following command opens a COM port and therefore establishes a communication link with the brick it must be turned on. If you have an NXT brick and wish to evaluate the input cells in this article, select any input cell while holding the Alt robot lego nxt brick resetting that selects all input cells. Without an NXT brick, running these commands returns error messages or unevaluated input. To see if the connection is open, look at the brick LCD display in the upper-left corner.
The variable mybrick contains the stream descriptor of the COM port and is used in all subsequent communications. Closing the COM port is done with the following instruction. However, for controlling the NXT, it makes more sense to send numbers directly. Hence, we need to convert numbers to the corresponding letters in the ASCII code before sending them. This is a bit awkward and we propose a neater solution later. A telegram always begins with one byte indicating whether the NXT should return a reply telegram or not.
This is your choice, but many commands sent to the brick only return a status byte 0 for success, or an error message. In the case of a status byte, the robot lego nxt brick resetting telegram is not useful. To ask for a reply, the first byte must be 0; means that the NXT must not send a reply.
The second byte of a telegram is always the command number. The subsequent bytes depend on the command sent. Its command number is 3. Then two parameters must be provided: Hence, a tone frequency of Hz for a duration of two seconds corresponds to the following bytes.
We concatenate the two parts to get a complete message, and get a list of the characters corresponding to the numbers given those characters may not be viewable on your computer system with this line of code.
You should hear a tone now. Because we asked for a reply the first byte was zerowe need to read it. Again, the reply is composed of text the characters of which may not be easily viewable. Let us convert them to a list of numbers. The bytes returned are, in order: The command PlayTone does not return other information; some direct commands may return more complex telegrams.
As an example, we send the command GetBatteryLevel. This command has number 11 and requires no parameters no extra information. If a reply is asked for but it is pointless to send this command and not ask for a replyit returns the status of the command 0 for success followed by two bytes indicating the voltage of the batteries on the NXT in millivolts. Let us assemble the telegram, concatenate the telegram length, convert this to a list of characters, and send them.
The first two bytes are the reply length 5 bytes ; 2 and 11 indicate that this is a reply for a GetBatteryLevel command; 0 indicates that the command was successful; and finally, and 27 indicate the battery voltage, which we convert to a number with fromUWORDdefined above.
Because it is more convenient to think in terms of numbers sent to the brick rather than characters, we expand the command SerialWrite contained in the package SerialIO to include the possibility of sending individual integers between 0 and bytesand also to send a list of bytes.
With this expansion, a whole message composed of bytes can be sent in one call to SerialWrite. For example, the following instructions assemble again the PlayTone command this time without a robot lego nxt brick resetting. The above shows how any command can be sent to the NXT brick and its reply read.
Afterward, it is simply a question of assembling a robot lego nxt brick resetting in the right order with the right information, and—if asked for—reading the reply and segmenting it into the correct information. To that end, the documentation robot lego nxt brick resetting by the LEGO Group [ 2 ], particularly the appendix 2 is quite complete. To make things simpler, we coded all the direct commands in a package called Math4NXT.
First get the package Math4NXT. Since it uses the package SerialIO in the background, also install this package in a path that is searched by Mathematica e.
Then load Math4NXT using the following instruction. Since we did not place the package Math4NXT in a folder searched by Mathematicawe provided its path in the Needs instruction. Then open the serial port if this was not already done as before. All the direct commands begin with the letters NXT. By default, the command returns the answer using named strings e. You can extract one piece of information as usual. This format can be changed, for example to raw i. Alternatively, the bytes sent and received can be displayed in a separate window robot lego nxt brick resetting the option.
The Messages window opens automatically when the option is used. The direct commands have many limitations. For one, due to the nature of the COM port, messages cannot be longer than bytes. This is a big limitation when dealing with files that robot lego nxt brick resetting be much longer.
Also, the LEGO sensors all work in very different ways; some sensors are passive, not requiring power, while others are active, and still others are programmable, like the I2C sensors. To make use of the commands more uniformly and to avoid the length limitation, we programmed higher-level commands that deal internally with these difficulties.
One example is the command M4NFileNames that lists all the files present on the brick optionally with the file size. Likewise, to facilitate the use of sensors, a command is provided that informs Mathematica of the sensor types connected. Afterward, reading the sensor is done according to the type of sensor connected. To set the sensor type, use M4NSetSensor.
After this, reading the sensor is done with a universal command, M4NReadSensor. To keep in line with the convention of direct commands, the first sensor is on the NXT input port 0.
Hence, if you connect the ultrasound sensor on the third sensor input, you can issue the following two commands. The ultrasound sensor is a complex sensor aimed at detecting the distance of obstacles in front robot lego nxt brick resetting it in centimeters. It is equipped with a microprocessor robot lego nxt brick resetting follows the I2C protocol. It must first be powered up and time given to it to boot up before a robot lego nxt brick resetting can be made.
Using the optionyou see that much more exchange of information is needed to initialize an ultrasound sensor than robot lego nxt brick resetting initialize a touch sensor. Yet, all these transactions are completely invisible with the M4N commands. The new LEGO motors contain tacho counters that keep track of the number of degrees of rotation executed.
However, the direct command can only stop a motor once a target number of degrees is reached, so that in effect, the motor overturns.