Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Get position #53

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
20316f8
add camera
KANBE8810 Nov 5, 2021
6f178f5
change camera model
RikuYokoo Nov 7, 2021
cf5afc6
add camera controller
RikuYokoo Nov 7, 2021
ab9d804
保存機能の追加
KANBE8810 Nov 9, 2021
930036b
一時保存
KANBE8810 Dec 4, 2021
8548294
add super visor
RikuYokoo Dec 23, 2021
1dcb5b7
get the ball coordinates from the supervisor
RikuYokoo Dec 24, 2021
8e189ba
add get_position
yasuohayashibara Dec 26, 2021
01d81a8
get position
yasuohayashibara Dec 26, 2021
ce3a0fd
Merge branch 'pose_capture_test' into get_position
KANBE8810 Jan 2, 2022
984f9cf
ロボットの位置調整・カメラ追加
KANBE8810 Jan 3, 2022
0f71322
シュートコースのランダム化
KANBE8810 Jan 11, 2022
929ee8f
Obtain 3D coordinates to keypoints
KANBE8810 Apr 15, 2022
8ce8367
周期を揃えた
KANBE8810 Apr 25, 2022
cdd8bb1
Merge branch 'main' into get_position
KANBE8810 Apr 25, 2022
71b8981
Migrate annotation_keypoints to get_position
KANBE8810 May 18, 2022
0d3a9ed
annotation_keypointsを一部取り込み
KANBE8810 May 30, 2022
5efb5d9
2次元キーポイントが取得できるように対応
KANBE8810 Aug 18, 2022
7b25ef3
座標変換の追加,シュートコースの変更
KANBE8810 Aug 22, 2022
6f8b8b0
ボールの座標取得を追加
KANBE8810 Aug 25, 2022
5ca4478
キッカーのみに変更
KANBE8810 Aug 29, 2022
36ade05
座標取るタイミングを変更
KANBE8810 Aug 29, 2022
93129a0
斜めのキックができるように変更
KANBE8810 Sep 5, 2022
e527320
ワールドファイルの修正
KANBE8810 Sep 5, 2022
d605f2f
ファイル整理
KANBE8810 Nov 10, 2022
1b2fe80
15度-15度を追加
KANBE8810 Nov 26, 2022
229d976
add
KANBE8810 Nov 26, 2022
955e662
a
KANBE8810 Nov 28, 2022
a0330f0
fix
KANBE8810 Dec 5, 2022
c0ebddf
fix
KANBE8810 Dec 5, 2022
42f4cf9
bag fix
KANBE8810 Dec 6, 2022
d61bab4
add new world
KANBE8810 Dec 6, 2022
02890d0
fix
HirokiHasegawa777 Jan 9, 2023
6005248
add cross kick
HirokiHasegawa777 Feb 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions controllers/hr46_b3m/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include <webots/Gyro.hpp>
#include <webots/Keyboard.hpp>
#include <webots/Camera.hpp>
#include <webots/Supervisor.hpp>
#include <webots/Node.hpp>
#include <cmath>
#include <set>
#include <exception>
Expand Down Expand Up @@ -317,7 +319,8 @@ class webots_motor_control

private:
std::vector<std::pair<int32_t, std::string>> motors_info;
webots::Robot *robot;
webots::Supervisor *robot;
//webots::Robot *robot;
std::vector<std::tuple<int32_t, webots::Motor *, std::string>> robot_motors;
webots::Gyro *robot_gyro;
webots::Accelerometer *robot_accelerometer;
Expand All @@ -331,12 +334,19 @@ class webots_motor_control
//ここの大きさはreceive側と同じにする必要がある
const int32_t message_len;
uint32_t highest_priority;
//webots::Supervisor *super_visor;
webots::Node *node;
webots::Field *field;
bool is_position;

public:
webots_motor_control() : mTimeStep(0), current_key(0), // forced_wait(waitForCreateQueue()),
msgq(boost::interprocess::open_or_create, "WEBOTS_PICTURE_COMMUNICATION", 5, 1000), message_len(700 * 480 * 4), highest_priority(0)
msgq(boost::interprocess::open_or_create, "WEBOTS_PICTURE_COMMUNICATION", 5, 1000), message_len(700 * 480 * 4), highest_priority(0), is_position(true)
{
robot = new webots::Robot();
std::cout << "construct in ---------------" << std::endl;
//robot = new webots::Robot();
robot = new webots::Supervisor();
//super_visor = new webots::Supervisor();
motors_info.push_back({FOOT_ROLL_R, "right_ankle_roll_joint"});
motors_info.push_back({LEG_PITCH_R, "right_ankle_pitch_joint"});
// motors_info.push_back({KNEE_R1, "right_ankle_pitch_mimic_joint"});
Expand Down Expand Up @@ -375,6 +385,15 @@ class webots_motor_control
reverse_motors.emplace("left_waist_pitch_joint");
reverse_motors.emplace("right_shoulder_roll_joint");
reverse_motors.emplace("left_shoulder_roll_joint");
std::cout << "getFromDef before ---------------" << std::endl;
//node = robot->getFromDef("head_yaw_joint");
init_supervisor();
if(node == NULL){
std::cerr << "get from def differ" << std::endl;
is_position = false;
}
//is_position = false;//add
std::cout << "getFromDef after ---------------" << std::endl;
for (auto &mp : motors_info)
{
auto motor_ptr = robot->getMotor(mp.second);
Expand Down Expand Up @@ -681,6 +700,39 @@ class webots_motor_control
{
return mTimeStep;
}

/*
const double *getPosition()
{
return node->getPosition();
}
*/

bool isPosition()
{
return is_position;
}

void init_supervisor()
{
node = robot->getRoot();
field = node->getField("children");
const int n = field->getCount();
for(int i=0;i<n;i++){
node = field->getMFNode(i);
std::cout << "-> " << node->getTypeName() << std::endl;
}

std::cout << "children n = " << n << std::endl;
std::cout << "name = " << field->getName() << std::endl;
}

const double *getPosition()
{
node = field->getMFNode(5);
return node->getPosition();
}

};

/*--------------------------------------*/
Expand All @@ -699,6 +751,8 @@ int main(int argc, char *argv[])

OrientationEstimator orientationEst((double)(FRAME_RATE) / 1000.0, 0.1);

const double *position;

#endif
if (argc > 1)
{
Expand All @@ -720,6 +774,10 @@ int main(int argc, char *argv[])
// loop start
for (count_time_l = 0; wb_ganken.step(); count_time_l++)
{
if(wb_ganken.isPosition()){
position = wb_ganken.getPosition();
std::cout << "position = " << position[0] << "," << position[1] << "," << position[2] << std::endl;
}
bool cmd_accept = false;
{
// accept command
Expand Down
2 changes: 2 additions & 0 deletions controllers/test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
test
65 changes: 65 additions & 0 deletions controllers/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
project(test)
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)



set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -ldl")


find_package(OpenCV REQUIRED)
option(ENABLE_IMAGE "enable camera image" OFF)

#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../HCServer ./HCServer)
#add_subdirectory(RTIMULib)


#set(USE_VREP_SIMULATOR CACHE BOOL FORCE)
#IF($ENV{VREP_DIR})
# set(USE_VREP_SIMULATOR True)
#ENDIF($ENV{VREP_DIR})

#IF(USE_VREP_SIMULATOR)
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../SimDriver ./SimDriver)
#add_definitions(-DVREP_SIMULATOR)
#ENDIF(USE_VREP_SIMULATOR)


if (DEFINED ENV{WEBOTS_HOME})
set(WEBOTS_HOME $ENV{WEBOTS_HOME})
else()
set(WEBOTS_HOME /usr/local/webots)
endif()

set (WEBOTS_LIBRARIES Controller CppController)

message(${WEBOTS_LIBRARIES})

add_definitions(-D_AFXDLL -DSIM -D_CRT_SECURE_NO_WARNINGS)

include_directories(
${CMAKE_BINARY_DIR}/gen
${CMAKE_CURRENT_SOURCE_DIR}
${WEBOTS_HOME}/include/controller/c
${WEBOTS_HOME}/include/controller/cpp
${OpenCV_INCLUDE_DIRS}
)

link_directories(

${WEBOTS_HOME}/lib/controller
${OpenCV_LIBRARIES}

)



add_executable(test
main.cpp
)

target_link_libraries(test ${OpenCV_LIBRARIES}
${WEBOTS_LIBRARIES}
)
108 changes: 108 additions & 0 deletions controllers/test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <webots/Robot.hpp>
#include <webots/Camera.hpp>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <chrono>
#include <string>
//#include <experimental/filesystem>
#include <filesystem>

//namespace fs = std::experimental::filesystem;
namespace fs = std::filesystem;
using namespace webots;

// class ImageWriter
// {
// public:
// ImageWriter();
// ~ImageWriter();
// void createCapturedImageDirectory();
// void saveCapturedImage(cv::Mat);
// private:
// bool enable_capture;
// int image_count;
// std::chrono::time_point<std::chrono::high_resolution_clock> last_capture_time;
// std::string capture_save_path;
// };
//
// ImageWriter::ImageWriter() : enable_capture(false), image_count(0)
// {
// createCapturedImageDirectory();
// enable_capture = true;
// last_capture_time = std::chrono::high_resolution_clock::now();
// }
//
//
// ImageWriter::~ImageWriter()
// {
// }
//
// void ImageWriter::createCapturedImageDirectory(void)
// {
// std::string capture_save_path;
// std::string capture_dir("game_images");
// if(!fs::exists(capture_dir)) {
// fs::create_directory(capture_dir);
// }
// std::stringstream ss_capture;
// auto now_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
// ss_capture << capture_dir << "/" << std::put_time(std::localtime(&now_t), "%Y%m%dT%H%M%S");
// capture_save_path = ss_capture.str();
// fs::create_directory(capture_save_path);
// }
//
// void ImageWriter::saveCapturedImage(cv::Mat img)
// {
// static int image_count = 0;
// bool enable_capture = true;
// if(enable_capture) {
// const auto capture_interval = std::chrono::seconds(1);
// if(std::chrono::high_resolution_clock::now() - last_capture_time > capture_interval) {
// std::stringstream ss;
// ss << capture_save_path << "/" << std::setw(6) << std::setfill('0') << image_count << ".jpg";
// image_count++;
// cv::Mat save_img;
// cv::cvtColor(img, save_img, CV_YCrCb2BGR);
// cv::imwrite(ss.str().c_str(), save_img);
// last_capture_time = std::chrono::high_resolution_clock::now();
// }
// }
// }

int main() {
// ImageWriter imagewriter;
cv::Mat img(480, 640, CV_8UC3);
Robot *robot = new Robot();
Camera *camera;
camera = robot->getCamera("camera_sensor");
//camera->enable()
//camera->getImage();
camera->enable(13);
// imagewriter.createCapturedImageDirectory();

while(robot->step(32) != -1){
//mTimeStep = (int)robot->getBasicTimeStep();
//camera->enable(mTimeStep * 13);
const unsigned char *image = camera->getImage();
for(int x =0; x < 640; x++){
for(int y = 0;y <480;y++){
int r = camera->imageGetRed(image, 640, x, y);
int g = camera->imageGetGreen(image, 640, x, y);
int b = camera->imageGetBlue(image, 640, x, y);
img.at<cv::Vec3b>(y, x)[0] = b;
img.at<cv::Vec3b>(y, x)[1] = g;
img.at<cv::Vec3b>(y, x)[2] = r;
}
}

//std::cout << "hello world" << std::endl;
cv::imshow("test", img);
cv::waitKey(1);
// imagewriter.saveCapturedImage(img);
}

delete robot;
delete camera;

return 0;
}
2 changes: 1 addition & 1 deletion protos/GankenKun.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PROTO GankenKun [
field SFString controller "void" # Is `Robot.controller`.
field MFString controllerArgs [] # Is `Robot.controllerArgs`.
field SFString customData "" # Is `Robot.customData`.
field SFBool supervisor FALSE # Is `Robot.supervisor`.
field SFBool supervisor TRUE # Is `Robot.supervisor`.
field SFBool synchronization TRUE # Is `Robot.synchronization`.
field SFBool selfCollision TRUE # Is `Robot.selfCollision`.
field SFInt32 cameraWidth 640 # Is `Camera.width`.
Expand Down
94 changes: 94 additions & 0 deletions protos/test_camera.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
PROTO test_camera [
field SFVec3f translation 4 0.35 0.063504
field SFRotation rotation 1 0 0 0
field SFInt32 cameraWidth 640 # Is `Camera.width`.
field SFInt32 cameraHeight 480 # Is `Camera.height`.
field SFString controller "test"
]
{
Robot {
translation IS translation
rotation IS rotation
# children [
# Shape {
# appearance PBRAppearance {
# baseColor 0.917647 0.145098 0.145098
# #roughness 1
# #metalness 0
# }
# geometry Box {
# size 0.1 0.05 0.585
# }
# #appearance PBRAppearance {
# # baseColor 1 0 1
# #}
# #geometry Box {
# # size 0.1 0.1 0.1
# #}
#
# }
children [
Transform {
translation 0.015000 -0.000000 0.085000
rotation -0.969980 0.000000 0.243186 3.141593
children [
Shape {
appearance DEF grey PBRAppearance {
baseColor 0.500000 0.500000 0.500000
transparency 0.000000
roughness 1.000000
metalness 0
emissiveColor 0.000000 0.000000 0.000000
}
geometry DEF head Box {
size 0.068779 0.072919 0.050000
}
}
]
}
DEF camera_link Solid {
translation 0.036495 0.000000 0.063504 #変更
rotation 0.357407 -0.357407 -0.862856 1.717772
children [
Camera {
rotation 1.0 0.0 0.0 0.0
name "camera_sensor"
fieldOfView 2.1642
width IS cameraWidth
height IS cameraHeight
noise 0.0
spherical TRUE
focus Focus {
focalDistance 2.30
focalLength 16.8
maxFocalDistance 2.30
minFocalDistance 2.30
}
}
]
name "camera_link"
}
]
boundingObject Transform {
translation 0.015000 -0.000000 0.085000
rotation -0.969980 0.000000 0.243186 3.141593
children [
Box {
size 0.068779 0.072919 0.050000
}
]
}
# physics Physics {
# density -1
# mass 0.111692
# centerOfMass [ 0.017325 0.000000 0.065059 ]
# inertiaMatrix [
# 1.148365390e-04 8.968346100e-05 7.895650100e-05
# 9.572310000e-07 -7.715580000e-07 2.138747600e-05
# ]
# }

#boundingObject USE head
controller IS controller
}
}