Difference between revisions of "Exercise - Using Simulation Macros"

From Flowcode Help
Jump to navigationJump to search
(Replaced content with "For an excellent introduction guide, we recommend [https://www.flowcode.co.uk/education/ Introduction to microcontroller programming]")
Tag: Replaced
 
(17 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<sidebar>Sidebar: What Is a Macro?</sidebar>
+
For an excellent introduction guide, we recommend [https://www.flowcode.co.uk/education/ Introduction to microcontroller programming]
Simulation macros are a form of Component macros. Their purpose is to make simulation more realistic by adding physical components to the electronic devices.
 
 
 
When the Flowcode program is compiled and downloaded to a microcontroller, they are ignored - hence the name Simulation macros.
 
 
 
Even a cursory glance at the dialogue box used to configure them shows that they are very powerful devices, with a huge range of possible effects.
 
 
 
This exercise uses three of them to illustrate the use of a PIR (Passive Infra-Red sensor) to operate an intruder sensor.
 
 
 
 
 
__TOC__
 
 
 
 
 
==The scenario==
 
 
 
The scene depicts the front of a house.
 
[[File:Exercise SimIcon pan.png|center|350px]]
 
 
 
A PIR sensor is mounted on the wall of the house, above the path leading across the garden.
 
 
 
The data sheet for the PIR shows that it is sensitive to a range of 10 metres, within a cone of angle 100 degrees.
 
 
 
Its detection area is represented in the image by the pale blue cone.
 
 
 
When an intruder enters the detection cone, a warning lamp lights up in the house.
 
 
 
This exercise allows us to investigate factors such as the height of the sensor on the brick wall and the angle of the detection zone, so that these can be optimised for a typical intruder height.
 
 
 
 
 
==The System Panel==
 
 
 
On the System Panel, viewed from above (the default position,)create:
 
* '''a brick wall''', represented by adding a red cuboid and changing it to a rectangle, 15mm wide, 120mm high and 120mm deep,
 
:: located at coordinates x = 0, y = 0, z = 60,
 
:: with rotation settings X = 0, Y = 0, Z = 0;
 
 
 
* '''a path''', represented by a grey rectangle, 240mm wide, 100mm high and 0mm deep,
 
:: located at coordinatesx = 125mm, y = 0mm, z = 0mm,
 
:: with rotation settings of X = 0, Y = 0, Z = 0;
 
 
 
* '''a cone''', couloured light blue, representing the sensing region of the PIR, with width = 70mm, height = 250mm, depth 95mm,
 
:: located at coordinates x = 45mm, y = 0mm, z = 80mm,
 
:: with rotation settings of X = 0, Y = -55, Z = 0.
 
 
 
* Drag the mouse cursor over these three elements and group them together.
 
 
 
This produces 'group 1'.
 
:: Give this group:
 
::: dimensions - width = 265mm, height = 250mm, depth = 135mm;
 
::: coordinates - x = 128, y = 0, z = 68;
 
::: rotation - X = 0, Y = 0, Z = 0.
 
 
 
 
 
Also on the System Panel, create an intruder, by:
 
* adding four spheres, and configuring the properties as follows:
 
:: sphere 1 ('''the head'''):
 
::: colour pink;
 
::: dimensions - width = 12mm, height = 13mm, depth = 15mm;
 
::: coordinates - x = -2.2, y = 0.7, z = 22;
 
::: rotation - X = 90, Y = 0, Z = 0.
 
 
 
:: sphere 2 ('''the body'''):
 
::: colour blue;
 
::: dimensions - width = 14mm, height = 26mm, depth = 24mm;
 
::: coordinates - x = -2.2, y = 0.5, z = 4;
 
::: rotation - X = 90, Y = 0, Z = -180.
 
 
 
:: sphere 3 ('''the legs'''):
 
::: colour dark green;
 
::: dimensions - width = 9mm, height = 23mm, depth = 15mm;
 
::: coordinates - x = -2.2, y = 1, z = -16;
 
::: rotation - X = 90, Y = 0, Z = -180.
 
 
 
:: sphere 4 ('''the feet'''):
 
::: colour black;
 
::: dimensions - width = 6mm, height = 15mm, depth = 25mm;
 
::: coordinates - x = -6, y = 0, z = -25;
 
::: rotation - X = 90, Y = 90, Z = 0.
 
 
 
* Drag the mouse cursor over the four spheres and group them together.
 
 
 
This produces 'group 2'.
 
:: Give it:
 
::: dimensions - width = 18mm, height = 32mm, depth = 60mm;
 
::: coordinates - x = 165, y = 0, z = 28;
 
::: rotation - X = 0, Y = 0, Z = 0.
 
 
 
 
 
The System Panel should resemble the image shown above. (The image has been rotated to show all components. The table-top is coloured green.)
 
 
 
Click on the 'Collisions' icon, [[File:Gen Panel Object Collision Move Around.png|35px]]    and select the 'Move through' option, which allows the intruder to enter the detection cone.
 
 
 
 
 
==The Dashboard Panel==
 
[[File:Exercise SimIcon dash.png|right|200px]]
 
* Open the 'Outputs' toolbox, and locate the 'LED 5mm Panel' device.
 
* Click on the down arrow next to it and choose the 'Add to dashboard panel' option.
 
* Zoom in to the Dashboard Panel to make the LED large enough to see.
 
* Click on the LED and configure its properties as follows:
 
::* choose a suitable colour, such as red;
 
::* connect it to Port A, bit 0.
 
 
 
 
 
==The Flowcode program==
 
 
 
'''The plan:'''
 
* Set the initial position of the intruder, using the 'MoveTo' simulation macro.
 
 
 
* Move the intruder towards the detection cone using the 'MoveAlong' simulation macro.
 
:: This '''changes''' the x coordinate of the intruder '''by an amount specified by the variable 'xpos''''.
 
 
 
* When the intruder enters the detection zone, the simulation macro 'CollisionTest' returns a value of '1' in the variable 'hit'.
 
:: At this point, we stop any more movement so that we can look at the x coordinate of the intruder.
 
:: To do so, we change the value of 'xpos' to zero.
 
 
 
 
 
'''Implementation:'''
 
 
 
* [[Opening Flowcode|Open Flowcode]].
 
 
 
* On the Startup screen, click on 'New project' to [[Creating Flowcharts|create a new flowchart]].
 
 
 
* Add the icons shown in the flowchart:
 
:* a loop icon, configured as an infinite loop, and within it:
 
::* two Simulation macro icons [[File:Btn Simulation Macro.gif|border]];
 
::* a decision icon with:
 
:::* a 'Yes' loop containing an output icon, followed by a delay icon and then a calculation icon;
 
:::* a 'No' loop containing an output icon, followed by a delay icon;
 
[[File:Exercise SimIcon Sim move.png|right|450px]]
 
 
 
 
 
* Configure the icons, by double clicking on each in turn, as follows:
 
::* the top Simulation macro:
 
:::* name it "Move the intruder";
 
:::* click on the 'Panel' tab, and then on the 'Position' folder;
 
:::* click on the 'MoveAlong' macro and:
 
::::* choose 'group2'(the intruder)as the Handle;
 
::::* choose 'shape1'( the brick wall) as the Axis;
 
::::* click on the down arrow on the end of the 'X' row, and create a floating point variable called "xpos", with an initial value 5.0, to use as the X variable;
 
::::* click on OK.
 
 
 
 
 
 
 
 
 
::* the second Simulation macro:
 
:::* name it "Check detection";
 
:::* click on the 'Panel' tab, and then on the 'Collision' folder;
 
[[File:Exercise SimIcon Sim test.png|right|300px]]
 
:::* click on the 'TestSingle' macro and:
 
::::* choose 'shape2'(the detection cone) for 'Pos' and 'shape5' (the intruder's head,) for 'Test';
 
:::* use the down arrow at the end of the 'Return Value' box to create a boolean variable caled "hit" to use as the 'Return Value'.
 
::::* click on OK.
 
 
 
 
 
::* the decision icon:
 
:::* name it "Intruder detected?";
 
:::* insert the condition "hit = 1";
 
:::* click on OK.
 
 
 
:::* In the 'Yes' loop:
 
::::* name the Output icon "Switch on lamp";
 
::::* send value '1' to Port A single bit 0;
 
::::* click on OK.
 
 
 
::::* name the Delay icon "Wait 100ms";
 
::::* configure it to give a delay of 100ms;
 
::::* click on OK.
 
 
 
::::* name the Calculation icon "Stop movement";
 
::::* in the 'Calculations:' box, insert "xpos = 0" to reduce the distance incremented each time to zero;
 
::::* click on OK.
 
 
 
:::* In the 'No' loop:
 
::::* name the Output icon "Switch off lamp";
 
::::* send value '0' to Port A single bit 0;
 
::::* click on OK.
 
 
 
::::* name the Delay icon "Wait 100ms";
 
::::* configure it to give a delay of 100ms;
 
::::* click on OK.
 
 
 
 
 
The Flowcode flowchart resembles the one shown below:
 
 
 
 
 
[[File:Exercise SimIcon Simflow.png|center|500px]]
 
==Testing==
 
 
 
* Run the simulation.
 
: The intruder should move towards the house, until he enters the detection zone.
 
 
 
: At this point, the warning lamp turns on and he stops.
 
 
 
* Stop the simulation, and click on the image of the intruder. You can now read the x coordinate from the Panel properties box.
 
 
 
* Change the height of the PIR sensor on the wall, or the angle of the detection cone, and see what happens to this coordinate.
 
 
 
 
 
==DON'T DOWNLOAD IT!!==
 
The important point is that the two Simulation macros do not compile to code that can be downloaded to a microcontroller. They affect only the simulation of the program.
 

Latest revision as of 13:23, 25 April 2023

For an excellent introduction guide, we recommend Introduction to microcontroller programming