添加camera相机

  • 1. 添加camera实体
  • 2. 添加camera控制接口代码
  • 3. 运行效果
  • 参考资料

1. 添加camera实体

step1: 在Robot中添加 camera(camera相机)   1 2   step2: 设置这个 camera 相机的children中添加 transform 节点,   3   step3: 设置这个 transform 节点的children中添加shape节点,并设置外观和形状。设置半径 0.01 高度0.02   4 5 6   step4: 设置camera 相机的名称(这个在控制器中会用到),同时设置相机的偏移量为(x=0,y=0.01,z=0.08),旋转偏移量(x=0,y=1.z=0,angle=3.14 rad) 注意:相机的方向的正前方应该是 -z方向,也就是图中蓝色箭头的负方向,在添加相机的时候应当注意。   6   在camera节点的属性栏中可以设置相机的宽、高和视场角等参数   7   注意:添加camera的过程中不设置boundingObject属性和 physics属性  

2. 添加camera控制接口代码

注意:这里必须要添加了控制代码以后,图像的界面框才会显示图像信息,如果在添加了控制器代码以后还未出现图像则关闭webots 重新打开一次就好。 C++代码:  
#include <webots/Camera.hpp>

  camera = getCamera("camera");
  camera->enable(4 * timeStep);
  C代码:  
  WbDeviceTag camera = wb_robot_get_device("camera");
  wb_camera_enable(camera, TIME_STEP);
    // update image
 (void)wb_camera_get_image(camera);
  完整的控制器代码:  
#include <webots/Robot.hpp>
#include <webots/GPS.hpp>
#include <webots/DistanceSensor.hpp>
#include <webots/Motor.hpp>
#include <webots/Keyboard.hpp>
#include <webots/InertialUnit.hpp>
#include <webots/Gyro.hpp>
#include <webots/Accelerometer.hpp>
#include <webots/Compass.hpp>
#include <webots/Camera.hpp>

 
#include <webots/Receiver.hpp>
 

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <limits>
#include <string>

#define TIME_STEP 64
// All the webots classes are defined in the "webots" namespace
using namespace webots;
using namespace std;
int main(int argc, char **argv) {
  // create the Robot instance.
  Robot *robot = new Robot();
  Keyboard kb;
  kb.enable(TIME_STEP);
  
  DistanceSensor *ds[2];
  char dsNames[2][10] = {"ds_right","ds_left"};
  for (int i = 0; i < 2; i++) {
    ds[i] = robot->getDistanceSensor(dsNames[i]);
    ds[i]->enable(TIME_STEP);
  }
 
  Camera *camera;
  //camera=getCamera("camera");
  camera=robot->getCamera("camera");
  camera->enable(TIME_STEP);
  
  // initialise motors
  Motor *wheels[4];
  char wheels_names[4][8] = {"wheel1", "wheel2", "wheel3", "wheel4"};
  for (int i = 0; i < 4; i++) {
    wheels[i] = robot->getMotor(wheels_names[i]);
    wheels[i]->setPosition(INFINITY);
    wheels[i]->setVelocity(0);
  }
  printf("init successd ...\n");
   
  double leftSpeed = 0.0;
  double rightSpeed = 0.0;

   // Main loop:
  // - perform simulation steps until Webots is stopping the controller
 while (robot->step(TIME_STEP) != -1)
 {
   //const unsigned char *dat= camera->getImage();
    int key = kb.getKey();
    if(key== 315)
    {
      leftSpeed = 3.0;
      rightSpeed = 3.0;
    }
    else if(key== 317)
    {
      leftSpeed = -3.0;
      rightSpeed = -3.0;
    }
    else if(key== 314)
    {
      leftSpeed = -3.0;
      rightSpeed = 3.0;
    }
    else if(key== 316)
    {
      leftSpeed = 3.0;
      rightSpeed = -3.0;
    }
    else  
    {
      leftSpeed = 0.0;
      rightSpeed = 0.0;
    }
    
     wheels[0]->setVelocity(leftSpeed);
     wheels[1]->setVelocity(rightSpeed);
     wheels[2]->setVelocity(leftSpeed);
     wheels[3]->setVelocity(rightSpeed);
  };

  // Enter here exit cleanup code.

  delete robot;
  return 0;
}

   

3. 运行效果

  8  

参考资料

[1] 1https://cyberbotics.com/doc/reference/index?version=R2020a-rev1 [2] https://cyberbotics.com/doc/reference/camera 如果大家觉得文章对你有所帮助,请大家帮忙点个赞。O(∩_∩)O 欢迎大家在评论区交流讨论(cenruping@vip.qq.com)