Monday, December 25, 2006

Remembering where we went

You can have a robot remember how long it rolls before something happens and then roll back to its initial position. It is not very well documented but quite easy. You just need to use some of the blocks from the advanced pane in NXT-G (the one with 3 colored squares on). Here is a five-block program that will make the tribot drive until it detects an object using the US sensor, and then drive back to where it came from.
The steps are

  1. We use a rotation sensor from the sensors menu and set it to reset the sensor on motor B
  2. Set motor B and C to drive unlimited ...
  3. ... until the US sensor senses something closer than e.g. 50 cm.
  4. Then we read the rotation sensor. Here we use the data hub on the sensor to find out how many degrees the sensor has rotated since last reset. This is why we needed to reset the sensor just before we started to move forward.
  5. The last move block is set to run a specific number of degrees backwards. The actual amount doesn't matter, because it will read the actual amount from the data hubs "duration" entry. This is tied by a datawire to the rotation sensor, which in turn knows how many degrees that the motor turned when it went forward. That way it will turn backwards just as many degrees and therefore end up where it started.
The documentation for datahubs and datawires is in my mind quite incomprehensible when you first read it, but once you learn how to use it, it is a very powerful tool. I found the tutorial at http://www.bnxt.com/tutorial/NXT-G/DataWires.aspx to be quite instructive.

You can download the .rbt file here: forwards_and_backwards.rbt

No comments: