December 21, 2012

Saving a video to local harddrive on Speedgoat

Having trouble with the video signal transmission over UDP, we decided to save the video to the hard drive in the Speedgoat target. It is obligatory to use the xPC File Scope Simulink block to write any simulation data to the Target's hard drive. The problem is that the File Scope is not suited for two-dimensional signals very well. We attempted to write an S-function that would save the data stream to a local file using the standard fwrite function. However, the functions for file manipulation are not supported for S-functions executed on xPC Target. Therefore we were forced to reshape the two-dimensional video signal into a vector that is used as input to the File Scope.


Build and initialization of target model for saving video

The build starts after clicking "Build model" button in the Simulink model or calling the function rtwbuild from Matlab command line. It might seem that the compilation is stuck in the phase of  "Caching model source code", but this phase only takes quite a long time. It takes the longer, the higher resolution video you want to save. Another time demanding phase is the compilation of the source code. One just have to be patient. Then the download to the target starts. A separate signal is created for every single pixel of the video and these signals are added to the File Scope one after another. Information about adding each signal is displayed on the target screen, which slows the whole process down even more. We tried disabling target GUI option in the xPC Explorer to avoid this slowdown, but it did not help, because even with the GUI turned off, there is a textual output on the target screen. We contacted both Speedgoat and Mathworks support to find out whether it would be possible to disable the informative text input during the initialization of the target application, but both of the responses were negative. To avoid the delay, we subsample the video frames before passing them to the File Scope block to reduce the number of signals.

The initialization of the target application takes really a long time with higher number of signals in the File Scope, so it often happens that a TCP/IP timeout occurs during the initialization. However, the initialization on the target continues autonomously, so one can just wait until the initialization finishes and then reconnect to the target with initialized application.

Another useful tip concerns loading a built target application without rebuilding it. This can save quite a lot of time in cases when the build of the target application takes very long. One can simply create a target object and then use it to load the built application
tg = xpc;
tg.load('built_application_name');

Build and initialization of a target application that just acquires and saves a video in 204x204 pixels resolution  on the local hard drive takes around 17 minutes. For a higher resolution video, we got an error during compilation:
'fatal error C1128: number of sections exceeded object file format limit'

In conclusion, xPC Target does not provide any way how to save a high resolution video on the local hard drive. The only possible workaround to achieve video logging is the one described here, which is unfortunately only applicable to video sequences at quite low resolution.

November 8, 2012

Installation of Basler acA2000 with BitFlow Neon on a PC

Software and driver installation

  1. Install Basler Pylon, do not forget to select driver for Camera Link. The Pylon package contains tools for camera configuration (Pylon Viewer) and registration (Pylon CL Configuration Tool)
  2. Install BitFlow SDK (use serial number 0, if you have not purchased a full licence). The BitFlow SDK contains utilities for editing and registration of camera configuration files (CamEd, SysReg) together with live video preview (CiView) and useful throughput testing utility CiBench. Only one of the utilities can access the framegrabber at a time.


Connecting the camera to the framegrabber

Because Neon-CLB only supports Base Camera Link, we connect the cable to camera port marked with "B" (the other port is for Full Camera Link). If the LED indicator glows, the camera is powered over CameraLink. If not, PoCL must be enabled or one can power the camera with a standalone adapter. PoCL can be enabled when you open the camera configuration file in CamEd as it is shown below. Remember to edit the configuration file that you will later register with SysReg utility. The camera does not wake up even with enabled PoCL until you attempt to grab images (e.g. with the CiView utility). However, you have to do some other settings mentioned later on before you can try to grab images.

Enabling PoCL in the CamEd utility

Camera-framegrabber communication settings

The settings are done with Pylon CL Configuration Tool. After launching the program, one can choose the COM ports to be scanned for cameras. Tick the one called "BitFlow"and click "Probe Enabled Ports". The utility should detect the Basler camera. It is also possible to set the baud rate of the serial line under advanced settings. This value must be set the same as the one in the camera settings (recommended value is 460800 bauds per second).


Creating and setting the camera configuration file

In the first step, we have two options depending on the answer to question if we were provided with a set of ready-to-use camera files by the camera vendor or not. In the first case, we just copy the vendor camera files into the directory "..\BitFlow SDK x.xx\Config\R64". If we don't have any appropriate configuration file (one might be also present in the above directory after installation of the BitFlow SDK), we have to create our own configuration file using the CamEd utility from the BitFlow SDK. To do this, we run the utility, open any R64 configuration file, edit the settings so that they suit our needs and capabilities of our camera and save the file as our own one. In fact, the camera configuration file specifies, how the data from the camera are ordered as well as their format to the framegrabber.
The most important settings in camera configuration file include the following:
  1. Active pixels per line (image width)
  2. Active lines per frame (image height)
  3. Sensor type - if we use an RGB camera, in the 'Sensor' tab, we can enable hardware Bayer decoding on the framegrabber under 'Sensor architecture'. This option decreases the maximum acquisition rate because the amount of transferred data triples, as we get full resolution image in all three color layers every period. Otherwise, we get only a raw Bayer array output from the framegrabber.
  4. Camera bit depth in the 'Bayer' tab specifies number of bits per pixel in one layer of the framegrabber output.
  5. Enable/disable Power over Camera Link (PoCL). This is quite an useful option how to turn off the power supply to the camera when we need to break the Camera Link connection without turning off the computer with framegrabber. We can just uncheck the 'Power over Camera Link' box, save the configuration file and then choose 'Display' > 'Open' to propagate the settings to the framegrabber. Before finally breaking the connection, we make sure that the power is really turned off by checking the LED indicator on the camera.
Enabling hardware Bayer decoding in CamEd utility
We do not need to care about the other settings as Exposure control, because we do not use the camera control signals for image acquisition, but use the settings stored in the camera registry instead.

In the second step, we register the camera configuration file that will be used for image acquisition. We launch the SysReg utility from the BitFlow SDK and set the required camera configuration file to be used by clicking "Configure..." and then choosing the appropriate camera file.


Camera configuration

Camera settings can be edited in Pylon Viewer, which is a software from Basler. It is better to see the camera user's manual as a reference of various settings. We have to keep in mind that the camera settings must correspond with the settings in the camera configuration file registered with SysReg utility! If you change any settings that affect the format of the data from the camera, such as resolution, you have to update the registered camera configuration file so that the settings are consistent. Otherwise, you will get errors or destroyed image.
The Pylon Viewer provides tools for exposure settings (white balance, exposure time, gain etc.).
A very useful and important functionality is the possibility to specify up to 4 configuration sets that can be saved in the camera memory and loaded at any time through the Pylon Viewer. A configuration set stores all the camera settings available in the Pylon Viewer. It is also possible to specify, which configuration set is loaded during the camera startup. If we don't specify our own startup configuration set, all settings will be reverted to default values every time we reboot the camera.

Setting the configuration sets

First image acquisition

Having set camera, the configuration file and framegrabber communication, you can try to display a live video with CiView. If you use PoCL, the framegrabber will at first fail to acquire images, because at the first attempt, the camera will not be started yet. This first attempt should wake the camera up, and the LED indicator should glow (green+red during startup, green when the camera is ready). When the camera starts up, just re-open the Display Surface window in CiView and you should see live video from the camera.

Screenshot from live view

BitFlow NEON-CLB with MATLAB&Image Acquisition Toolbox

BitFlow offers a comfortable solution for users that are using Neon or other framegrabbers on a PC with Matlab together with the Image Acquisition Toolbox. It is possible to download adapter for BitFlow framegrabbers, which provides interface for Image Acquisition Toolbox. We find this very useful, because it is quite hard to visualize anything on the xPC Target, so it is easier to debug and test the algorithms on a standard PC with Matlab. A short demo script:


%% TEST IMAGE ACQUISITION WITH BitFlow FRAMEGRABBER
%
% register the BitFlow adaptor
clear all, close all, clc
adapt_path = 'C:\BitFlow SDK 5.60\Bin64\MATLAB Adaptor\WIN64\BitFlow.dll';
imaqregister(adapt_path, 'register');

% create video object and get source object

vidobj = videoinput('bitflow');
srcobj = vidobj.Source;
srcobj.TriggerMode = 'One Shot';

%% grab and display 200 frames

for it=1:200
    frame = getsnapshot(vidobj);
    imagesc(frame), axis equal
    disp(['Frame no. ' num2str(it)])
end


November 6, 2012

Connecting a Camera Link camera to Speedgoat

Having tested the USB webcam together with the Speedgoat platform, we moved on to try out the functionality with a Camera Link camera. The platform is provided with BitFlow Neon-CLB framegrabber. Neon-CLB offers Base Camera Link interface for one camera with safe Power over Camera Link (PoCL), which means that a PoCL compatible camera does not need a separate power cable. It fits into a PCIe x4 slot and supports cameras with Camera Link clock frequency up to 85 MHz.

Camera Link camera

We have chosen high speed color camera Basler acA2000-340kc with Camera Link interface for our application. It can capture video in full resolution of 2046x1086 pixels @ 340 frames per second. It is provided with a 2/3´´ CMOS sensor with bit depth at most 12 bits per pixel.
Basler cameras from the Ace series are not intended as high-end (they use CMOS sensors instead of CCD), but they can offer good basic parameters (resolution and speed) for a price that is two- or even more times lower than for other cameras. The price for monochrome and color variant of acA2000-340 is 29440CZK or 29980CZK, respectively. Therefore we decided for the color variant, because it could be useful to distinguish more balls on the platform marked with different colors. Just to compare the prices of other series and manufacturers, let's name a few other cameras
  • SVS Vistek svs1050 (1024x1024@90fps)  57239CZK
  • SVS Vistek evo1050 (1024x1024@190fps) 68068CZK
  • AVT Bonito CL400 (2320x1726@193fps) 81900CZK
  • Optronis CamRecord CL300x2 (1280x1024@125fps) 141960CZK
  • Basler avA1600-65 (1600x1200@67fps) 52260CZK
  • Basler A405k (2320x1726@167fps) 179400CZK
Generally, the CMOS sensors have heavier image noise compared to more expensive CCD sensors, but the noise becomes dominant in bad light conditions, which will not be the case in our application, so we rather focused on the image resolution and framerate parameters.

We have to be aware of several specific limitations for the use of the camera, too, as we could damage it otherwise.

  1. The camera body has a standard C-mount for lens, but the thread length is limited due to IR-cut filter in front of the CMOS sensor.
  2. We must be careful when making/breaking the Camera Link connections. When using PoCL, the power to the framegrabber card must be turned off before connecting/disconnecting the camera. When using a standalone power adapter, we can only manipulate with the PoCL cable when the power cable is disconnected or the adapter turned off.
  3. Of course, we must protect the CMOS sensor against dust and dirt.

Limitations of using Camera Link cameras on Speedgoat

Although xPC Target provides a Simulink block for signal acquisition from BitFlow Neon framegrabber, we ran into problems here. The Simulink block must be provided with a camera configuration file that specifies the format of the camera data for the framegrabber as well as how the data should be processed. However, one cannot perform any camera settings like exposure or AOI (area of interest) control from Speedgoat, because these settings are done through Basler Pylon software that can be installed only on Windows. Therefore, unless you want to run the camera with the default factory settings, you need another framegrabber that will be plugged into a PC with the Basler Pylon software installed. We could avoid buying another framegrabber by moving the one from Speedgoat to a PC, doing the necessary settings and moving it back again, but we would loose the Speedgoat warranty by opening the case.

Transfer of Basler camera settings to Speedgoat

When we want to use some other than default settings of the camera when connected to Speedgoat, we use the following procedure:
  1. Connect the camera to the host PC with BitFlow SDK and Basler SW installed, do all the necessary settings, save them as a user configuration set in the Basler Pylon Viewer and set this User Set as Default Startup Set. This will ensure that our settings are loaded every time when the camera is booted or re-booted. (Installation and setting of Basler camera together with BitFlow Neon CLB framegrabber on a PC is described in the next post.)
  2. Disconnect the camera from the host PC
  3. Connect the camera to the target PC and run the target application. The camera is booted with our own settings.

Live video transfer from Target to Host PC

Having configured the camera and Simulink models for Target and Host PC properly, we tried to transfer and display live video on the Host PC, similarly to the case with USB webcam. We faced the same problem with low framerate again and did not discover any working solution even asking the MathWorks support.

Setting the PCI slot of the framegrabber in xPC Target Simulink block

The xPC Target block 'NEON BitFlow Image Input' has an option to specify the address of PCI slot that hosts the BitFlow framegrabber card in the Target PC. First of all, we had to find out, which PCI slot it is using command 
getxpcpci('all')
This command lists all PCI devices installed in the Target PC. The PCI slot is then set in the configuration dialog of the Simulink block using notation '[bus,slot]'. The autosearch works as well, but it lengthens the initialization of the target application and causes that the Target disconnects from the Host during initialization. 
Setting the parameters of acquisition including the PCI location of the framegrabber in the target

October 30, 2012

Connecting a USB camera to Speedgoat

Our aim is to use a high speed camera connected through CameraLink interface in the future. For the first attempts with video, we used a USB2.0 webcam.

Choosing the camera

We were looking for some friendly-priced camera with full resolution around 2Mpx at framerate 30 fps. Another criterion was the possibility to set the parameters of the camera (white balance, exposure time, etc.) manually.

When choosing the camera, we had to be aware that the manufacturers always present the highest resolution and highest framerate at once, but they often omit the fact that the highest resolution is not available at the highest framerate.

Finally, we have chosen Creative Live! Cam Socialize HD 1080. For a reasonable price (around 1300CZK), it offers capturing video sequences in full-HD resolution 1920x1080 at framerates up to 30 fps. The parameters of the camera can be set manually (through software). In addition, the camera has glass lens, which is another advantage compared to cheaper models that are often provided with plastic optics.

Experience with USB camera on Speedgoat

Speedgoat offers in fact a 'plug-and-play' use of the USB camera. You only need to know which blocks to put in the Simulink model and interconnect them correctly to start capturing the video.
  1. boot Speedgoat
  2. plug-in the USB camera
  3. connect to Speedgoat from the host PC using the xPC Explorer
  4. open your Simulink model and choose preferred camera configuration
  5. you can start the video capture by running the model
Be sure not to plug-in the camera until the target machine has booted. If the target machine detects a USB device connected when you turn it on, it tries to boot from it as if it was a USB stick.

We tried to transmit the video to the host PC over the Ethernet cable, but the transmission does not work very well. Even though the camera is capturing frames at 30 Hz, the resulting effective framerate on the host PC is around 3 Hz. The capture rate was checked by an xPC Target Scope connected to a single image pixel, through which we could observe the time intervals between changes of the pixel's intensity value. We did not find any error in our model nor any other working solution on the web. The problem might be that the communication between the target and host PC during the execution of the target application generates so heavy network traffic that the video frames which are transmitted via UDP get lost. A possible solution could be to build a standalone target application to eliminate the host-target communication during execution and only use the Ethernet for video transmission. However, one needs an xPC Target Embedded Option licence to build standalone target applications, so we could not verify this hypothesis.

October 24, 2012

Getting started with Speedgoat platform

We will be using Speedgoat Performance real-time target machine for our experiments. It is running under FreeDOS and is aimed to be used together with xPC Target package from Mathworks, which means that one creates a Simulink model, which is afterwards executed in external mode (i.e. it is compiled into an executable code and launched on the target machine). The target machine can either run in standalone mode, or be connected to a host PC during execution of the target application. We are using the second mode of operation, because one needs a licence for xPC Target Embedded Option to be able to deploy standalone applications.
The target machine is connected to the host PC via Ethernet cable (it can be connected to the LAN as well, but we are using a direct connection with dedicated Network Adapters).

Settings

The easiest way is to use the graphical interface of the xPC Explorer. To launch the explorer, type 'xpcexplr' in Matlab command line window. Through the xPC Explorer, you can connect/disconnect target machines, launch/stop target applications, monitor/trace/log signals from the target application when it is running etc. Various settings of the target machine including the target-host communication settings are available through graphical interface of xPC Explorer. To communicate successfully, you have to set the host and target IP addresses according to your real network configuration. (The default settings are 192.168.7.1 for target and 192.168.7.2 for host PC)
To make the settings persistent, save the settings in the xPC Explorer, insert a USB stick into the host PC (will connect e.g. as drive 'E:') and then type 'speedgoatmachineboot('E:')' in Matlab command line window. This will generate boot files on the USB stick. Then disconnect the USB stick from the host PC, plug it into the target machine and boot the target machine. It should load the new core including all your settings. Then you are asked to remove the USB stick and re-boot the target machine, this time already with the new settings.
The settings can be checked using a pre-implemented configuration test which is launched by command 'xpctest'.

Timeout during the initialization of the target application

With a more complex target application, it can happen that a timeout occurs during its initialization on the Target PC and the Target disconnects from the Host as a result. Even though it is possible to reconnect to the Target with initialized target application, it is annoying. Fortunately, it is quite easy to fix this issue by setting the communication timeout in the Model Configuration Parameters of the target model in Simulink (choose pane Code Generation > xPC Target options).
Setting the communication timeout to avoid problems during initialization of the target application

October 3, 2012

Aim and Motivation

The aim of this project is to test additional methods for measurement of position of objects (steel balls) on the surface of Magnetic Platform for Planar Manipulation, which is developed in research group AA4CC at Department of Control Engineering, FEE CTU in Prague.

Current method of measurement based on usage of resistive foil has a weak spot for more complex applications. It is not able to measure position of more than one object at once. It would be much more interesting to have the possibility to detect the position of multiple objects on the platform simultaneously, which would allow us to design feedback control of position of those objects.

For this purpose, we are going to test two alternatives. First, it will be combination of a camera and image processing, which is quite promising and second, it will be the capacitive foil.
In addition, the whole control software, which is currently run in Matlab Real-Time Workshop, will be moved to Speedgoat real-time target machine.
Magnetic Platform
Magnetic Platform, courtesy of AA4CC