Now I’m going to show you how to simulate a camera in case you don’t have it physically available.
This is the case, for example, when we have a project deployed somewhere on the line, where we cannot interrupt the production process, but at the same time we need to fine tune our program.
If we run a task, which contains a camera block and we expect some image, then such a task will fall with an error message that the CAM 1 camera is not connected.
In the last section, we showed you how to save images to disk. Let’s try to use these images now to create a kind of simulated camera for this task.
So let’s go to Devices, cameras and we see that there is our unconnected camera because it has a red pictogram.
Also, even if we open this camera block, we see that there is an error message that FIFO Acquisition requires hardware that is not connected.
So we right-click on Cameras, we click Add and here we have the option to select „Image file device“.
We’ll call it Sim Cam 1. I use the „sim“ prefix that tells me right away when I look at those cameras which one is „Image File Device“ and which is a real camera.
I can determine from this „Sim“ keyword that it is an Image File Device and I link this real camera to this Sim Cam 1 I right-click on it and select the option – Simulate
I choose Sim Cam 1, I give Accept.
I see that my pictogram has changed to blue, that is, simulated camera, and in square brackets I can see which image file device is being used.
Now when I click Run All on that task, I can still see that I get an error message.
File name not set. That’s because we have not yet determined where this file camera should take images from.
When I double-click on this camera, I see that I have the option to select a file or folder.
So let’s try to select a file.
And I’m going to choose, for example, this well lit image. Hit Open.
I can see the image file path here. I will give Accept.
And now when I Run the task again, I can see that I already have some image here.
And it’s the one I chose.
When I give Run again, the picture is still the same.
I can see that even the time it took to execute the block of the camera is much lower than the one that was from a real camera, because this was loaded directly from the disk.
So, in such a simple way we can simulate the presence of a camera and work on the saved images that we have available. We still have one option, and that is Choose Directory.
As the button text suggest, we can select specific folder which will be used to load images from. I do not see the images, I can choose folder.
I click Select Folder or right here I insert the path to that folder. I hit Accept.
And now when I give it a Run, I can see that the slides are constantly changing there.
It’s also possible to do it in Run mode.
You can see that the images change there according to what they are in the folder and circulate there by name from first to last and then back to the first.
Designer has a bit of an issue that I have to point out here. If you have a folder with multiple frames and then want to switch to a folder with fewer frames, then there is a change you will hit an error. That’s because this simulated camera keeps an index
which represents next image index in the folder.
I can look at it with watchlist now.
Watchlist is very handy tool, which allows us to view current values of environment variables.
So now I enter devices sim cam 1 current index here and I see that it is 20.
Another variable: devices sim cam 1 next index has value 0. The value is 0 because
The count of images stored here is 21 and are indexed from 0 to 20, so the current image with index 20 is the last, but the next image will be with index 0, so this first one.
If we give Run All, we see that it switches to Current Index 0, Next Index 1, then Current Index 1, Next Index 2.
And now we want to switch to a folder in which there are only 8 images, so the maximum index number will be 7.
So I switch sim cam 1 to that folder
I gave him the path, I give Accept and I see that the Current Index has reset to minus 1 but the Next Index remained 9.
And if we press Run in this way, then the Current Index will be set to 9 Next Index will not be switched anymore, because there is an error and simply the task fails.
So, we have learend to live with this error by using a Script block in which after double-clicking and accepting we can write a simple program in C-sharp language. I will write „if“ condition
Now I will use the dollar sign which is pointing to environment variables and functions, Devices, sim cam 1, current index, equals minus one, than devices, sim cam 1, next index assign to 0. So it will reset this next index as it should be.
And if I run this task and in this script it finds that the simulated camera was set to new folder, that is the current index was set to minus 1, it sets the next index to 0.
So let’s try it out.
We can also try it by just double-clicking on sim cam and giving Accept.
That is, as if I reloaded that folder.
I see that the current index is minus 1 and now when I give Run All
it now shows us, this time correctly, that the current index is 0 and the next index is 1 and I can continue with new images.
However, the problem may arise when this camera is no longer simulated, so we do not have this simulation or in case we do not have this camera set up, and then we do not want this script to run
So, we can do this by using this Condition on this script. That controls the execution of blocks. When the block should run and when it should not.
Through these 3 dots, click on Expression Builder and write the condition. If, devices, cam 1 is simulation
We can also write it without the „if“ condition.
In this case, it is Boolean.
So if it’s a simulated camera, then this script block runs.
If it’s a real camera, it’s not a simulation, then this script will not run.
We give OK and we have this bug treated.
So whenever we use camera blocks, we put in front such a script, we can call it „Correct sim cam“
If I want to use this project on the line now, I have to disable the simulation of the camera. There is an option for it.
When I’m workin on a line where that camera is connected, it should automatically connect to the free camera.
If not, then I save this project, transfer it to that line and run the designer.
I have to make sure it’s the only running instance of the designer because other instances could allocate access to the camera and the new one wouldn’t be able to connect to it.
So if I have any other designers running other projects there, I should close them.
Open this project and then I could work with that real camera.