簡體   English   中英

無法使用 libuvc 移動相機

[英]Cannot move camera using libuvc

我正在嘗試使用 libuvc 控制我的相機。 我嘗試了從示例中修改的這段代碼:

#include <libuvc/libuvc.h>
#include <stdio.h>
#include <unistd.h>

int main() {
  uvc_context_t *ctx;
  uvc_device_t *dev;
  uvc_device_handle_t *devh;
  uvc_stream_ctrl_t ctrl;
  uvc_error_t res;

  /* Initialize a UVC service context. Libuvc will set up its own libusb
   * context. Replace NULL with a libusb_context pointer to run libuvc
   * from an existing libusb context. */
  res = uvc_init(&ctx, NULL);
  if (res < 0) {
    uvc_perror(res, "uvc_init");
    return res;
  }
  puts("UVC initialized");


  /* Locates the first attached UVC device, stores in dev */
  res = uvc_find_device(
      ctx, &dev,
      0, 0, NULL); /* filter devices: vendor_id, product_id, "serial_num" */
  if (res < 0) {
    uvc_perror(res, "uvc_find_device"); /* no devices found */
  } else {
    puts("Device found");
    /* Try to open the device: requires exclusive access */
    res = uvc_open(dev, &devh);
    if (res < 0) {
      uvc_perror(res, "uvc_open"); /* unable to open device */
    } else {
      puts("Device opened");
      
      uvc_print_diag(devh, stderr);

      //uvc_set_pantilt_abs(devh, 100, 100);
      int result = uvc_set_pantilt_abs(devh, 5, 50);
      printf("%d\n", result);
      //sleep(5);

      /* Release our handle on the device */
      uvc_close(devh);
      puts("Device closed");
    }
    /* Release the device descriptor */
    uvc_unref_device(dev);
  }

  /* Close the UVC context. This closes and cleans up any existing device handles,
   * and it closes the libusb context if one was not provided. */
  uvc_exit(ctx);
  puts("UVC exited");
  return 0;
}

我嘗試了uvc_set_pantilt_absuvc_set_pantilt_rel並且都返回0所以這意味着操作成功。 除了相機不動。

我確定相機使用 UVC,因為uvc_print_diag指示

VideoControl: 
        bcdUVC: 0x0110

難道我做錯了什么? 如果不是,我該如何解決?

我剛才找到了答案,但忘了把它放在這里。

我偶然發現了這個項目,它使用帶有 libuvc 的命令行工具來控制相機。

玩了一下並將其與我的代碼進行比較后,我明白了我做錯了什么。 他正在從攝像頭獲取傾斜數據,然后用它來發送請求。 似乎相機需要接收一個數字,該數字必須是相機作為移動單位提供的“步長”的倍數。

這是他請求 pantilt 信息的部分:

int32_t pan;
int32_t panStep;
int32_t panMin;
int32_t panMax;
int32_t tilt;
int32_t tiltStep;
int32_t tiltMin;
int32_t tiltMax;

// get current value
errorCode = uvc_get_pantilt_abs(devh, &pan, &tilt, UVC_GET_CUR);
handleError(errorCode, "Failed to read pan/tilt settings - possibly unsupported by this camera?\n");

// get steps
errorCode = uvc_get_pantilt_abs(devh, &panStep, &tiltStep, UVC_GET_RES);
handleError(errorCode, "Failed to read pan/tilt settings - possibly unsupported by this camera?\n");

// get min
errorCode = uvc_get_pantilt_abs(devh, &panMin, &tiltMin, UVC_GET_MIN);
handleError(errorCode, "Failed to read pan/tilt settings - possibly unsupported by this camera?\n");

// get max
errorCode = uvc_get_pantilt_abs(devh, &panMax, &tiltMax, UVC_GET_MAX);
handleError(errorCode, "Failed to read pan/tilt settings - possibly unsupported by this camera?\n");

這是完整的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM