# Detect Camera Attached

Source: https://forum.modalai.com/topic/711/detect-camera-attached
Category: Flight Core and Legacy VOXL (https://forum.modalai.com/category/9/flight-core-and-legacy-voxl)
Tags: voxl
Posted: 2022-02-04 18:05:07 UTC by wilkinsaf
Replies: 11 · Views: 2089

## wilkinsaf · 2022-02-04 18:05:07 UTC

I am developing a way to automatically detect when a camera is attached to the VOXL (we need to be able to verify camera attachment before moving onto the next stage of manufacturing process)

When plugging in a uvc camera we verify the camera is plugged in by checking the vendor and product id after running `lsusb`
For other cameras like stereo, tracking, etc... the cameras are attached to ports like J3 and appear to be handled in a different manner (possibly libvoxl_io and some code in voxl-camera-server). 

I am still looking over the code, but my question is: is there a place in linux (a filepath that gets initialized or something) where we can verify that a camera has been plugged in.

Thank you

## Reply by tom (ModalAI staff) · 2022-02-04 18:15:25 UTC

@wilkinsaf Others will know better but I believe when the cameras have enumerated they will show up in `/run/mpa`

## Reply by SmittyHalibut · 2022-02-04 18:15:31 UTC

If `voxl-camera-server` is up and running, you'll see a directory for the camera in `/run/mpa/[camera name]`.

Otherwise, I'd look in the source code for `voxl-configure-cameras` to see wha tit does for detection.

## Reply by SmittyHalibut · 2022-02-04 18:17:38 UTC (in reply to tom)

@tom `/run/mpa` directories are setup by `voxl-camera-server` so it has to be configured and running first.  And, I'm not sure it verifies the presence of the camera before setting up the MPA hierarchy. I could be wrong, I'm relatively new here. :-)

## Reply by tom (ModalAI staff) · 2022-02-04 18:22:10 UTC

@SmittyHalibut That's correct, I made the assumption that voxl-camera-server was being used which could be wrong. There's also https://docs.modalai.com/voxl-uvc-server/

## Reply by wilkinsaf · 2022-02-04 18:31:57 UTC (in reply to tom)

@tom Right, but that just handles the UVC cameras correct?

I am trying to detect the tracking, hi-res, and stereo cameras that are plugging directly into the J* ports on the board. 
I wonder if I have to do something like attempt to read from them to see if they are there. 

I will keep looking in the source code

## Reply by Guest · 2022-02-04 18:34:33 UTC

Hi all,

One of the features in the works for ```voxl-camera-server``` is a mode to only check the hal module directly for the available cameras. Since we're not sure exactly when we're going to be able to have this release up and this has been a very asked for feature, I'm going to extract that code real quick from our internal branch and publish a small executable that will list the cameras that are plugged into the mipi ports. I'll have a link for you if you give me 15-20 minutes.

## Reply by wilkinsaf · 2022-02-04 18:40:51 UTC (in reply to Guest)

@Alex-Gardner Oh ok cool.

Thanks for doing that Alex. I was going to say, I know where that hal code lives. Just having you verify that for me is great help. 
I can work on that code in the branch you post and make a pull request once i make some progress on it.

Is there a TODO list left before release is acceptable?

## Reply by Guest · 2022-02-04 19:37:51 UTC

The package (voxl-list-cameras) is live on our [dev repo](http://voxl-packages.modalai.com/dev/). you can manually download it from there or on voxl run: 
```voxl-configure-opkg dev```

```opkg update```
```opkg install voxl-list-cameras```

There will be a command ```voxl-list-cameras``` that will list the available cameras and their resolutions from the hal module, and return the number of cameras connected. Additionally, since this is a temporary package that won't have a gitlab repo, the source for the executable is in /etc/modalai/voxl-list-cameras-src/ . This tool is pretty crude at the moment, the final design will filter out resolutions that aren't actually available/sort by format, but this is enough for you to tell which cameras are connected. I can tell you that in it's final form, this will be the behavior of running ```voxl-camera-server -l``` or ```voxl-camera-server --list```. 

As for the time frame/publicity of the other branch, it's hard to know, this feature was added as part of a major restructure of camera server for our rb5 platform that will be backported to voxl once proper behavior has been verified on both platforms. Until then, you can use this tool or make your own program that uses it (the entire source for the executable is around 100 lines of c even with comments/license, so if you want to make your own for whatever test bench you're using it shouldn't be too hard). 

Lastly, I can give you a hint that if you're looking at the source and trying to filter different cameras, you can modify the source around the print loop: 
```
printf("Available resolutions for camera: %d:\n", camId);
for (size_t j = 0; j < entry.count; j+=4)
{
    if (entry.data.i32[j + 3] == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT)
    {
        printf("\t%d x %d\n", entry.data.i32[j+1], entry.data.i32[j+2]);
    }
}
```
to also check ```entry.data.i32[j]``` which will be the format of that resolution (which is why you will see duplicates in the available resolutions, same resolutions in different formats). The main ones that you'll see are ```HAL_PIXEL_FORMAT_YCbCr_420_888``` for YUV data, ```HAL_PIXEL_FORMAT_RAW10``` for raw grayscale (note that some of our drivers actually output raw8 data not raw10, you can see the check for this in camera server), or ```HAL_PIXEL_FORMAT_BLOB``` for data coming out of the PMD TOF sensor. 

Hope that's all a good starting point for what you're trying, let me know if you have any additional questions

## Reply by wilkinsaf · 2022-02-05 01:38:08 UTC (in reply to Guest)

@Alex-Gardner Thank you for that information!

I was able to easily get it installed and working. However, I am trying to modify the source code for it.

My question is: did you compile this on the VOXL board or through voxl-emulator/voxl-hexagon/etc...

I want to make sure my environment is the same. Thanks

## Reply by wilkinsaf · 2022-02-05 01:42:08 UTC

Nevermind. Forgot docker does not save image state automatically and I had to reinstall dependencies. 

I swear I fix these issue 5 minutes after I post things

## Reply by wilkinsaf · 2022-02-12 03:12:58 UTC (in reply to Guest)

@Alex-Gardner 

I see why you created a separate tool for checking the connected cameras as the code in that tool is implemented in much lower level files on voxl-camera-server.

I did quickly put together a flag option that would list cameras if they are setup in /etc/modalai/voxl-camera-server.conf

I created a pull request [here](https://gitlab.com/voxl-public/modal-pipe-architecture/voxl-camera-server/-/merge_requests/16)

Let me know if this is useful. If the needed feature requires all cameras to be listed let me know and I can see what I can do. 

Also, I was trying to find a way to get some form of id off of the cameras, but I see in code that we compare the resolutions and that is how we distinguish a stereo from a tracking. Was trying to find some HAL3 data member that could give more of an "id". I found this `ANDROID_REQUEST_ID` variable in [this](https://android.googlesource.com/platform/system/media/+/master/camera/include/system/camera_metadata_tags.h#281) documentation, but I could not get it working
