Skip to content

Tutorial Third Party Library Integration

CarolinaSerra edited this page Nov 18, 2022 · 2 revisions

In this tutorial we will learn how to add a third party dependency to an ESROCOS component.

Prerequisites: Please make sure a camera device with id 0 (video0) is available in your machine. In a Virtual Box system, this is possible by installing the Virtual Box Extension Pack and selecting a camera from Devices/Webcams in the main window of the running virtual machine. In a Docker image, it can be achieved by editing the docker run call.

Create a Component

If you do not know how to create an ESROCOS component, please have a look into the previous tutorial.

  1. Go to the ESROCOS workspace folder and create a new project with the name tutorials/cam_capture. Choose universe as package set.
  2. Enter the following package dependencies: types/base and external/opencv.
  3. Checkout and install dependencies calling the esrocos_fetch_dependencies script in the directory of your project.

If you had not previously fetched the external/opencv module, notice that the command esrocos_fetch_dependencies automatically fetches, updates, builds and installs the dependency.

Next, open the TASTE GUI using the esrocos_edit_project script.

  1. Add a function called camera. In its properties, set the language to C++ (CPP).
  2. Add a sporadic PI and name it capture.

Save your work and generate the code skeletons using the esrocos_generate_skeletons command. Then, edit the camera.cc file to look like this:

#include "camera.h"
#include "camera_state.h"
//#include <iostream>

#include <opencv2/opencv.hpp>

// Define and use function state inside this context structure
// avoid defining global/static variable elsewhere
camera_state ctxt_camera;

using namespace cv;

void camera_startup(void)
{
   // Write your initialisation code, but DO NOT CALL REQUIRED INTERFACES
   // std::cout << "[camera] Startup" << std::endl;
}

void camera_PI_capture(void)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
    
    namedWindow("raw",1);
    namedWindow("edges",2);
    
    Mat frame;
    Mat edges;
    cap >> frame; // get a new frame from camera
    cvtColor(frame, edges, COLOR_BGR2GRAY);
    GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    Canny(edges, edges, 0, 30, 3);
    imshow("raw", frame);    
    imshow("edges", edges);
    if(waitKey(100) >= 0);
}

In the CMakeLists.txt located in the model directory of your project, change the reference to the default hello_world source directory to the camera source directory.

Run amake. Similarly to the previous tutorial, add the camera.zip to the source text property of the camera TASTE function. Run amake again.

Congratulations, you successfully added an external dependency to an ESROCOS component.

Clone this wiki locally