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

From RoboWiki
Jump to: navigation, search
m
m
Line 5: Line 5:
 
[[Image:keep_distance_with_slider_nxt_g_program_using_messages.png]]
 
[[Image:keep_distance_with_slider_nxt_g_program_using_messages.png]]
  
The robot repeats a loop: receive the message, read the value of the ultrasonic distance sensor on port 4, and compare the sensor readings with the value that was received in the message. Depending on the result of the comparison, the robot moves forward or backs up.
+
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.  
  
Whenever we move the slider in Imagine to the left or right, we will change the distance of the empty buffer that the robot keeps in front of it: Imagine will send this value (in cm) 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):
+
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
 
  to change_msg
 
   
 
   
   let "d1 (div s1'value 10)
+
   ; this procedure is called when the slider is released
  let "d2 (mod s1'value 10)
+
 
   ; in nxt logo, mailboxes are numbered 0-9, meaning 1-10 in NXT-G
 
   ; 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
 
   ; the first 5 mailboxes are reserved for NXT Logo, so we use mailboxes 5-9
   nxt_messagewrite 5 (word :d1 :d2)
+
   ; numeric messages in NXT-G are received as 4-byte binary encoded number
   wait 10
+
  ; 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
 
  end
 
|}
 
|}
  
 
In the next tutorial, we see how to send messages in both directions: [[NXT Logo Examples: A simple interactive control of NXT-G program from Imagine using files|following tutorial]].
 
In the next tutorial, we see how to send messages in both directions: [[NXT Logo Examples: A simple interactive control of NXT-G program from Imagine using files|following tutorial]].

Revision as of 15:50, 26 July 2009

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: following tutorial.