In this lesson we will create the part counter component. The component will consist of a base link (the visual part of the component, i.e. the text element), a behavior and an electric input. The electric input will be connected to the sensor in the system. The behavior will interpret the electric input from the sensor and increase a counter when the sensor detects a new product, and that counter will update the text geometry.
Create a new component
Go to the component library and press create a new component give it a suitable name and press ok, then the component editor will open. Press Editor to be able to edit the functionality.
Edit the base link visual geometry from box to text.
Edit the font to look like an seven segment display by changing the font to sevensegment. Use the different settings to manipulate the sizes, to fit nicely in the workspace, use height = 0.5 m and thickness = 0.05 m.
To make the text stand out in the system we add a material to the visual, of course we will choose a nice Simumatik green color (#74B800 or RGB = 116,184,0).
Now lets add an electric input to the component. This will make the component able to read the value from the sensor.
Now lets add a behavior to the component. This will make the component able to interpret the value from the electric input and update the text geometry.
Add two variables to the behavior. The first one will represent the sensor_value and have the following properties:
The second variable will represent the text value and have the following properties:
Connect the variables to their corresponding property in the component. The sensor_value variable connects to the voltage of the electric input and the count variable connects to the value of the text geometry.
Now we will make a script to get the correct behaviour for our component. We start with the initialize block. At first we will define local variables.
- The variable previous_value will contain the value that the sensor had last time the script was executed.
- The variable count will be the numerical counter in which we store the number of boxes passing the sensor.
- The output variable text_output will be initialized with the value of count, but converted to a text string.
if initialize: # Enter your initialization code here # It will be executed after reseting the emulation previous_value = 0 count = 0 text_output = str(count) else: # Enter your behavior code here # It will be executed depending on the step_time and input variable changes pass
The last step is to add the behaviour code. The functionality will be to check if the sensor has updated its value and check if that the sensor has turned on and not off (the voltage is 24.0 V, and not 0V). If thoose conditions has been met we will increase the variable count by 1 and update the text_output to be the value of count, but converted to a text string. The last thing is to store the current value of the sensor in the variable previous_value so that we will only increase once per product passing the sensor. See the flow chart and final code below.
if initialize: # Enter your initialization code here # It will be executed after reseting the emulation previous_value = 0 count = 0 text_output = str(count) else: # Enter your behavior code here # It will be executed depending on the step_time and input variable changes if sensor_value != previous_value and sensor_value == 24.0: count += 1 text_output = str(count) previous_value = sensor_value
Save the script and save the component and go back to the workspace.
Add the component to the system
Go to libraries and add the component to either an existing or a new library.
Go back to the workspace and add the component to the assembly Partscounter. Drag the component to a suitable place in the workspace.
The last thing to do is to connect the electric signal from the sensor to the parts counter.
Test the model
Run the emulation and make sure that the behavior is as expected.
To deepen your understanding try to complete theese two challenges:
Make the counter react when the product has passed the sensor instead of when it arrives to the sensor.
Change the text to also include text, for example “count = 23”.