NXT Logo Examples: A simple interactive control of NXT-G program from Imagine using messages

From RoboWiki
Jump to: navigation, search

back to list of tutorials

In this tutorial, we will keep the same task and setup as in the previous tutorial. Instead of manipulating files in the NXT flash, we will be sending BlueTooth messages to NXT brick.

The program in NXT-G will change as follows:

Keep distance with slider nxt g program using messages.png

We will use two mailboxes: one will be used for sending new threshold value, and the other one will be used to notify NXT that a new threshold value is available: so that it can be copied from the first mailbox to NXT's numeric variable. We have to do it this way, because we do not want to overwrite the numeric variable with 0 when the new threshold value did not arrive. User can drag - or not - the slider at any time, so that the message with a new threshold arrives only occasionally.

The robot repeats a loop: check for new threshold, and store it if available, read the value of the ultrasonic distance sensor on port 4, and compare the sensor readings with the currently set threshold value that was received in the last message. Depending on the result of the comparison, the robot moves forward or backs up.

In Imagine, we hook the following procedure to the onrelease event of the slider. Whenever we move the slider, we will change the distance of the empty buffer that the robot keeps in front of it: the value (in cm) is sent directly to NXT using a BlueTooth message. Now the frequency of sending this information to the robot can be higher (messages are not stored in the flash memory):

to change_msg

 ; this procedure is called when the slider is released

 ; in nxt logo, mailboxes are numbered 0-9, meaning 1-10 in NXT-G
 ; the first 5 mailboxes are reserved for NXT Logo, so we use mailboxes 5-9
 ; numeric messages in NXT-G are received as 4-byte binary encoded number
 ; i.e. the first byte is the least significant byte (0-255), etc.
 ; in our case, the distance is always < 255 thus we use only one byte

 ; the first message contains the new value
 nxt_messagewrite 6 (word char s1'value char 0 char 0 char 0)
 ; the second message is a signal to read the new value from mailbox, this is a text message
 nxt_messagewrite 5 "DIST
 
end


In the next tutorial, we see how to send messages in both directions: sending and receiving messages.