Buggy Move Task by jennyyingdi

VIEWS: 3 PAGES: 10

									University of Bolton                                          Manchester Science Fest




                                               Flowcode Practical with the Micromouse Buggy

                                               Building up a series of simple decisions to create
                                                  “Infra Red Distance Detection”

                                                The code for this practical builds to produce
                                                Close-Range Infra Red Distance Detection on
                                               one of the Buggy’s IR transmitter/receiver pair
                                                and uses the LED’s to represent the distance.




 The program is run within the Formula Flowcode Environment and the code is downloaded to the
           Micromouse Buggy via the USB Cable connected between PC and Buggy.


  The starting code for this practical is “IR-Test1”, which has a ‘Begin’ & an ‘End’- Nothing Else.


This exercise develops a series of ‘Decisions’ so that the Buggy’s Right Hand Side Infra Red Sensor
  (Connected to pins RA4 & RA5 of the chip) senses when there is an object at different distances
from the right hand side. The closer the object to the sensor the more of the Buggy’s LED’s are lit.
        There are then some ideas for further understanding via experimentation and code
                                     modification/development.
University of Bolton                                               Manchester Science Fest


Infra Red Distance Detection – Developing Flowcode to discern distances close to the Buggy

Open the file “IR-Test1” within the “Formula Flowcode” Environment and
you should see the following flow diagram associated with the Flowcode.

First we need to get a reading from the Infra Red (transmitter and receiver)
pair that we are going to use to discern close distances to the Buggy. For
this example we shall use the Buggy’s Right Hand Side Sensor.

From the left hand column menu, drag and drop the
Component Macro icon in between the Begin and End
blocks.


                                          Double click on the Call Component Macro block to open up
                                          the properties box.




Click on the Formula Flowcode (0) under
Component to bring up the Macro menu.
Scroll down then select ‘ReadIRSensor’.
Replace ‘Call Component Macro’ in the
Display Name box with ‘Read IR Right
sensor’.

Now click on the Variables button

                                                                  Select the IRR variable then click USE
                                                                  VARIABLE to add IRR to the sensors
                                                                  parameter box. Pull down the menu at
                                                                  the end of the ‘Return Value’ box and
                                                                  select IRR again. Click OK.




This now brings an IR reading into the code. Next we need a
decision box to perform one task if we have reading above a
certain level, and another task if we have a reading below that
certain level.
University of Bolton                                               Manchester Science Fest



From the left hand column menu, drag and drop the
Decision icon after the Component Macro block and
before the End.


This should now look like:-

Decision icons allow you to test a condition and re-direct the flow
according to the outcome. Normally the 'Yes' path of execution goes
off to the right of the decision icon and the 'No' path carries on down
the flowchart, however these can be changed if necessary. Icons can
be placed in either branch from the decision icon. The Display Name
‘Decision’ is the default icon-name that appears on the flowchart.

Double click on the Decision block to open up its properties box.


                                                                   Remove the ‘0’ from the IF box.
                                                                   Click on the “Variables ..” button to
                                                                   open the variable manager.

                                                                 This shows the three variables
                                                                 associated with the Infra Red sensors.
                                                                 There are three IR sensors on the
                                                                 buggy;- IR Left, IR Front & IR Right.
Infrared proximity sensors work by sending out a beam of IR light, and then computing the distance to any
nearby objects from characteristics of the
returned (reflected) signal. The IR sensors feed
in an analogue signal into the Analogue to
Digital module on the chip, which produces a
range of values from 0 to 255 depending upon
the distance between the object detected and the
Buggy. If an object is placed close to the Buggy
we would get a small value reading from the IR
sensing pair and the further away from the
Buggy, the larger the value the A-to-D unit
would return. The limit to sensing for these
Buggy-Sensors is about 30cm away.

We are going to be using the IRR variable as
this is associated with Infra Red Right Sensor
which is attached to pins RA4 and RA5 on the
Buggy’s chip.

Click on IRR under variable name then click USE VARIABLE.

This returns to the Decision Properties window with the IR right sensor associated with the decision.

Now we need to define some condition to force a decision one way or the other. In practice, on this
system, if we use the furthest detection value of 240, this will detect at about 30cm away from the IR
sensor. So we can now set up the decision properties so that if the reading is above 240 then the code is
directed one route and if it is less than 240 it is directed the other.
University of Bolton                                             Manchester Science Fest



After the IRR in the ‘If’ entry-box
add:- >= 240 - This reads as “If IR-
Right sensor value is greater than or
equal to a value of 240”.


Change the Display Name to read
Furthest Detection Distance (Don’t
click the Swap Yes and No box).
Now click OK.


                                        Now we can build up to output a pattern to the Buggy’s on-board
                                        LED’s to signify when the decision statement is true, and we will
                                        switch the LED’s off when the decision is false.

                                        From the left hand column menu, drag and
                                        drop the Component Macro icon in the right
                                        hand route of the decision block.


                                        The Flowcode should
                                        now look like this:-




Double click on the Call Component Macro block to open up
the properties box.




                                                                  Click on the Formula Flowcode (0)
                                                                  under Component to bring up the
                                                                  Macro menu. Scroll down then
                                                                  select ‘WriteLED’s’.
                                                                  Replace ‘Call Component Macro’ in
                                                                  the Display Name box with ‘No
                                                                  LED’s ON’.

                                                                  In the ‘Parameters: byte out box’
                                                                  type in 0x00 which enters the value of
                                                                  0 (0x means:- written in hex format)

                                                                  Click OK to accept these properties.
University of Bolton                                                 Manchester Science Fest


The Flowcode should now resemble:-

Now add another component macro, (from the left hand
column menu, drag and drop the Component Macro icon)
in the NO arm just after the decision block.

It should now look like:-




                                                                  Double click on this new Call
                                                                  Component Macro block to open up the
                                                                  properties box.

                                                                  Click on the Formula Flowcode (0)
                                                                  under Component to bring up the
                                                                  Macro menu. Scroll down then select
                                                                  ‘WriteLED’s’.

Replace ‘Call Component Macro’ in the
Display Name box with ‘1 LED ON’.

In the ‘Parameters: byte out box’ type in
0x01 to enter the value of 1 to switch the first
LED on.

Click OK to accept these properties.




                                                   Save the code so far by the name “Distance1”.

                                                   When this code is compiled and downloaded to the Buggy
                                                   (via the USB cable), it performs as follows:-
                                                   It gets the reading from the IR right sensor on the Buggy,
                                                   it then checks to see if that sensor finds an object within
                                                   the set-range. If there is an object within range, one LED
                                                   will light. If there is no object within range no LED will
                                                   light!
University of Bolton                                                Manchester Science Fest


Now download the code to the buggy and check that this is how it behaves. Connect the USB cable
to the Buggy and note the green LED near the socket. Switch on the Buggy and if the two red LED’s
are flashing, it is ready to program. If the LED’s are not flashing, switch the Buggy off then on
again. Select “Chip” from the Flowcode top Menu and click on “Compile to Chip” which will
convert the Flowcode to machine-code and send it to the chip on the Buggy. When the red lights
stop flashing, the code download is complete. Switch off the Buggy at the switch and remove the
USB lead. Switch on the Buggy again and it will now run according to the code within the chip.

The code as it stands just performs one very fast pass of the code then ends, so to try this out repeatedly,
simply press the RESET button (beside the Buggy’s green LED & positioned in-between the chip and the
Buggy’s USB connector) for another pass of the code.

Notice the difference of how the IR sensor responds to ‘distance’ if we use our hand as an object in front
compared to a sheet of white paper or alternatively a darker object. Hence this kind of sensor will not give
a true indication of precise distance (as the surface of the object has an affect on the amount of IR
reflection), but now we will show how it can be extended to light up more LED’s the closer an object gets
to the IR sensor.

Now, we shall put all the code (in between the start and end points), to create a ‘forever loop’, so that the
code will make the buggy’s IR sensor keep cycling through reading the reflected light level and signalling
whether it is above or below the threshold we have set, continuously.

To add the “forever loop”, drag & drop the loop icon releasing it when the mouse is over the
‘begin block’ at the start of the code. This will insert the loop at the start of the code.

If you look at the properties of the loop block pair, it loops ‘while 1’. This code defines the forever loop!

                                             At the moment there is nothing within the upper and lower
                                             parts of the Loop, so now we need to move (drag and drop)
                                             all the blocks of ‘Distance1’ into the forever loop (keeping
                                             them in the same order).



                                              Try this for
                                              yourself – it
                                             can be done in
                                               two moves.




Once this is done successfully, it should look like the
flowcode on the right.
University of Bolton                                              Manchester Science Fest


Save the code so far by the name “Distance2”. Download it to the Buggy and test it again.

It should run repeatedly lighting the LED whenever an object comes into range of the Right IR sensor.

Now let’s add another ‘IR level decision block’ to make it turn two LED’s On if the object comes closer.

Right Mouse on the Furthest Detection Distance block and select copy, then right mouse on the 1
LED ON block and paste to insert an exact copy of the first decision block within the first one.


                                                    Now we need to modify the NEW blocks.

                                                    First delete the ‘1 LED ON’ block which is IN-
                                                    BETWEEN the two decision-blocks as this is not
                                                    necessary.

                                                    Double click on the lower (NEW) decision block and
                                                    edit the properties giving it the Decision Name of 2nd
                                                    Distance Detection and amend the If box to read:-
                                                    ‘IRR >= 239’ then click OK.




Now move the 1LED ON component macro box (on
the ‘NO’ arm of the 2nd Decision block), to the ‘YES’
arm of the same block.




                                    And move the NO LED’s
                                    block (on the ‘YES’ arm of
                                    the 2nd Decision block), to
                                    the ‘NO’ arm of the same
                                    block.
University of Bolton                                            Manchester Science Fest


Edit this ‘No LED’s ON’ block below the 2nd
Decision block and change the Display Name to
read ‘2 LED's ON’ and change the Parameters byte
out to ‘0x03’, to switch two LED’s ON.

Once this is done successfully, it should look like
the flowcode on the right.

Save this code with the name “Distance2”.
Download it to the Buggy and test it again.

Now two LED’s should work showing the two
different detection levels.

How about a third level?

Right Mouse on the 2nd Detection Distance
block and select copy, then right mouse on the 2
LED’s ON block and paste to insert an exact
copy of the 2nd decision block within the 2nd
one.

First delete the ‘2 LED’s ON’ block which is IN-
BETWEEN the two lower decision-blocks as this is
not necessary.

Double click on the lower (NEW) decision block and edit the properties giving it the Decision Name of
3rd Distance Detection and amend the If box to read:-
‘IRR >= 237’ then click OK.

                                                        Now move the 2LED’s ON component macro
                                                        box (on the ‘NO’ arm of the 3rd Decision block),
                                                        to the ‘YES’ arm of the same block. And move
                                                        the 1 LED block (on the ‘YES’ arm of the 3rd
                                                        Decision block), to the ‘NO’ arm of the same
                                                        block.
                                                        Edit this ‘1 LED ON’ block below the 3rd
                                                        Decision block and change the Display Name to
                                                        read ‘3 LED's ON’ and change the Parameters
                                                        byte out to ‘0x07’, to switch three LED’s ON.

                                                        Once this is done successfully, it should look
                                                        like the flowcode on the left.

                                                        Save this code with the name “Distance3”.
                                                        Download it to the Buggy and test it again.
University of Bolton                                              Manchester Science Fest


Can you continue in this fashion to produce a sensor that can distinguish between 7 or 8 different
distances away from the buggy?

Here are some tips to help with coding the remaining 5 decisions.
To light up the appropriate number of LED’s, we use the ‘WriteLED’s’ component macro and tell it how
many LED’s in the ‘byte out’ box are required.
So far we have used
0x00 – to display 0 LED’s lit –      in binary this is     0000 0000
0x01 – to display 1 LED lit –        in binary this is     0000 0001
0x03 – to display 2 LED’s lit –      in binary this is     0000 0011
0x07 – to display 3 LED lit –        in binary this is     0000 0111
So for the remaining number of LED’s we can use the following values
0x0F – to display 4 LED’s lit –      in binary this is     0000 1111
0x1F – to display 5 LED lit –        in binary this is     0001 1111
0x3F – to display 6 LED’s lit –      in binary this is     0011 1111
0x7F – to display 7 LED lit –        in binary this is     0111 1111
0xFF – to display 8 LED’s lit –      in binary this is     1111 1111


As for the values of the reading for the IRR >= VALUE,
We have used the following values so far:-
Furthest Distance Detection IRR >= 240
2nd Distance Detection         IRR >= 239
 rd
3 Distance Detection           IRR >= 237
For the remaining values try using the following to get it working then they can be tuned accordingly to
make it more accurate if desired.
4th Distance Detection         IRR >= 230
 th
5 Distance Detection           IRR >= 210
6th Distance Detection         IRR >= 190
7th Distance Detection         IRR >= 150
University of Bolton                                              Manchester Science Fest


This is the Flowcode for Discerning 7 Distance Detections with the IR sensor.




Final Task:-

From the flow-code you have created (IR distance sensor with 7 LEDS) – try to work out a calibration
chart for the IR sensor with a sheet of white paper representing the object it’s detecting. i.e. work out
distance against IR value and from the readings currently used in this current code try to refine the
program to detect readings of 20mm, 40mm, 60mm, 80mm, 100mm, 120mm and 150mm away from the
IR Sensor.

								
To top