1.カメラエンティティを追加する
ステップ1:ロボットにカメラを追加します。
STEP2:設定されたこのカメラの子どもたちがトランスフォームノードを追加する。
ステップ3:この子供たちはシェイプノードを追加するノードをトランスフォームセット、および外観や形状を設定します。半径0.01、高さ0.02を設定
step4:カメラの名前を設定し(これはコントローラーで使用されます)、カメラのオフセットを(x = 0、y = 0.01、z = 0.08)に設定し、回転オフセットを(x = 0、y = 1.z = 0、angle = 3.14 rad)
注:カメラの正面方向は-z方向(図の青い矢印の負の方向)である必要があります。カメラを追加するときは注意してください。
カメラノードの属性バーで、カメラの幅、高さ、視野パラメータを設定できます
注:カメラを追加するプロセスでは、boundingObjectプロパティとphysicsプロパティは設定されません。
2.カメラ制御インターフェースコードを追加する
注:画像のインターフェースフレームには、ここに制御コードを追加した後にのみ画像情報が表示されます。コントローラーコードを追加した後に画像が表示されない場合は、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.運用効果
参照
[1] 1https://cyberbotics.com/doc/reference/index?version = R2020a-rev1
[2] https://cyberbotics.com/doc/reference/camera
記事があなたに役立つと思う場合は、それを助けて好きにしてください。O(∩_∩)O
コメントエリア([email protected])で交流し、議論する皆を歓迎します。