Difference between revisions of "NXT Logo Examples: A simple interactive control of NXT-G program from Imagine using files"
m |
m |
||
Line 7: | Line 7: | ||
[[Image:keep_distance_with_slider_imagine_slider.png]] | [[Image:keep_distance_with_slider_imagine_slider.png]] | ||
− | Whenever we move the slider to the left or right, we will change the distance of the empty buffer that the robot keeps in front of it: Imagine will write this value (in cm) directly to a file in NXT's flash memory using the following function: | + | Whenever we move the slider to the left or right, we will change the distance of the empty buffer that the robot keeps in front of it: Imagine will write this value (in cm) directly to a file in NXT's flash memory using the following function, which is triggered when the slider s1 changes its value: |
to change_file | to change_file | ||
Line 17: | Line 17: | ||
ignore nxt_write :handle (word :d1 :d2) 128 | ignore nxt_write :handle (word :d1 :d2) 128 | ||
ignore nxt_close :handle | ignore nxt_close :handle | ||
− | wait | + | wait 1000 |
end | end | ||
Line 30: | Line 30: | ||
The robot repeats a loop: read a value from file, close the file, then read the value of the ultrasonic distance sensor on port 4, and finally compare the sensor readings with the value that was read from the file. Depending on the result of the comparison, the robot moves forward or backs up. | The robot repeats a loop: read a value from file, close the file, then read the value of the ultrasonic distance sensor on port 4, and finally compare the sensor readings with the value that was read from the file. Depending on the result of the comparison, the robot moves forward or backs up. | ||
+ | |||
+ | ''Note: be very careful when using a technique like this. NXT Flash memory has a limited number of write/read cycles. You can easily ruin your NXT brick by writing to the memory too many times! Therefore note the "wait 1000" command above, which will prevent the program from changing the value more often than once per second. You can use this technique for setting up some configuration parameters that do not change very often.''' | ||
+ | |||
+ | A better way of interactive control is by sending a BlueTooth message to NXT brick (to be continued...) |
Revision as of 02:58, 24 March 2009
In this project, we build a robot that has an ultrasonic distance sensor on the front and keeps a required distance from the wall: when the distance is lower, the robot moves back, when it is larger, it moves forward. In this way, the robot is also able to follow a person in a required distance, or go back when somebody approaches the robot...
Finally, we need some program for the robot so that it will keep the distance according to the value in the file. This time, we make that program using standard NXT-G programming environment, and it will look as follows:
The robot repeats a loop: read a value from file, close the file, then read the value of the ultrasonic distance sensor on port 4, and finally compare the sensor readings with the value that was read from the file. Depending on the result of the comparison, the robot moves forward or backs up.
Note: be very careful when using a technique like this. NXT Flash memory has a limited number of write/read cycles. You can easily ruin your NXT brick by writing to the memory too many times! Therefore note the "wait 1000" command above, which will prevent the program from changing the value more often than once per second. You can use this technique for setting up some configuration parameters that do not change very often.'
A better way of interactive control is by sending a BlueTooth message to NXT brick (to be continued...)