Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync libcontroller develop #992

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 12 additions & 3 deletions webots_ros2_driver/webots/include/controller/c/webots/supervisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ WbNodeType wb_supervisor_node_get_type(WbNodeRef node);
WbFieldRef wb_supervisor_node_get_field(WbNodeRef node, const char *field_name);
WbFieldRef wb_supervisor_node_get_field_by_index(WbNodeRef node, const int index);
int wb_supervisor_node_get_number_of_fields(WbNodeRef node);
WbFieldRef wb_supervisor_node_get_proto_field(WbNodeRef node, const char *field_name);
WbFieldRef wb_supervisor_node_get_proto_field_by_index(WbNodeRef node, int index);
int wb_supervisor_node_get_proto_number_of_fields(WbNodeRef node);
WbFieldRef wb_supervisor_node_get_base_node_field(WbNodeRef node, const char *field_name);
WbFieldRef wb_supervisor_node_get_base_node_field_by_index(WbNodeRef node, int index);
int wb_supervisor_node_get_number_of_base_node_fields(WbNodeRef node);
void wb_supervisor_node_remove(WbNodeRef node);
void wb_supervisor_node_save_state(WbNodeRef node, const char *state_name);
void wb_supervisor_node_load_state(WbNodeRef node, const char *state_name);
void wb_supervisor_node_set_joint_position(WbNodeRef node, double position, int index);
WbProtoRef wb_supervisor_node_get_proto(WbNodeRef node);

const char *wb_supervisor_node_get_def(WbNodeRef node);
const char *wb_supervisor_node_get_type_name(WbNodeRef node);
Expand Down Expand Up @@ -139,6 +140,7 @@ const char *wb_supervisor_field_get_name(WbFieldRef field);
WbFieldType wb_supervisor_field_get_type(WbFieldRef field);
const char *wb_supervisor_field_get_type_name(WbFieldRef field);
int wb_supervisor_field_get_count(WbFieldRef field);
WbFieldRef wb_supervisor_field_get_actual_field(WbFieldRef field);

void wb_supervisor_field_enable_sf_tracking(WbFieldRef field, int sampling_period);
void wb_supervisor_field_disable_sf_tracking(WbFieldRef field);
Expand Down Expand Up @@ -199,6 +201,13 @@ void wb_supervisor_field_import_mf_node_from_string(WbFieldRef field, int positi
void wb_supervisor_field_remove_sf(WbFieldRef field);
void wb_supervisor_field_import_sf_node_from_string(WbFieldRef field, const char *node_string);

const char *wb_supervisor_proto_get_type_name(WbProtoRef proto);
bool wb_supervisor_proto_is_derived(WbProtoRef proto);
WbProtoRef wb_supervisor_proto_get_parent(WbProtoRef proto);
WbFieldRef wb_supervisor_proto_get_field(WbProtoRef proto, const char *field_name);
WbFieldRef wb_supervisor_proto_get_field_by_index(WbProtoRef proto, int index);
int wb_supervisor_proto_get_number_of_fields(WbProtoRef proto);

bool wb_supervisor_virtual_reality_headset_is_used();
const double *wb_supervisor_virtual_reality_headset_get_position();
const double *wb_supervisor_virtual_reality_headset_get_orientation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef unsigned short WbDeviceTag; // identifier of a device
typedef struct WbImageStructPrivate *WbImageRef;
typedef struct WbMotionStructPrivate *WbMotionRef;
typedef struct WbNodeStructPrivate *WbNodeRef;
typedef struct WbProtoInfoStructPrivate *WbProtoRef;
typedef struct WbFieldStructPrivate *WbFieldRef;

// define "bool" type for C controllers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace webots {
Type getType() const;
std::string getTypeName() const;
int getCount() const;
Field *getActualField() const;

void enableSFTracking(int samplingPeriod);
void disableSFTracking();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define WB_USING_CPP_API
#include <string>
#include <webots/Field.hpp>
#include <webots/Proto.hpp>
#include "../../c/webots/contact_point.h"
#include "../../c/webots/types.h"

Expand All @@ -27,6 +28,7 @@ namespace webots {
typedef WbContactPoint ContactPoint;

class Field;
class Proto;
class Node {
public:
typedef enum {
Expand Down Expand Up @@ -133,13 +135,14 @@ namespace webots {
std::string getBaseTypeName() const;
Node *getParentNode() const;
bool isProto() const;
Proto *getProto() const;
Node *getFromProtoDef(const std::string &name) const;
int getNumberOfFields() const;
int getProtoNumberOfFields() const;
int getNumberOfBaseNodeFields() const;
Field *getField(const std::string &fieldName) const;
Field *getProtoField(const std::string &fieldName) const;
Field *getBaseNodeField(const std::string &fieldName) const;
Field *getFieldByIndex(const int index) const;
Field *getProtoFieldByIndex(const int index) const;
Field *getBaseNodeFieldByIndex(const int index) const;
const double *getPosition() const;
const double *getOrientation() const;
const double *getPose() const;
Expand Down
46 changes: 46 additions & 0 deletions webots_ros2_driver/webots/include/controller/cpp/webots/Proto.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 1996-2023 Cyberbotics Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PROTO_HPP
#define PROTO_HPP

#define WB_USING_CPP_API
#include <string>
#include <webots/Field.hpp>
#include "../../c/webots/types.h"

namespace webots {
class Field;
class Proto {
public:
std::string getTypeName() const;
bool isDerived() const;
Proto *getParent() const;
Field *getField(const std::string &fieldName) const;
Field *getFieldByIndex(const int index) const;
int getNumberOfFields() const;

// DO NOT USE THESE FUNCTIONS: THEY ARE RESERVED FOR INTERNAL USE:
static Proto *findProto(WbProtoRef ref);
static void cleanup();

private:
Proto(WbProtoRef ref);
~Proto() {}

WbProtoRef protoRef;
};
} // namespace webots

#endif // PROTO_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from controller.field import Field # noqa
from controller.proto import Proto # noqa
from controller.node import Node, ContactPoint # noqa
from controller.ansi_codes import AnsiCodes # noqa
from controller.accelerometer import Accelerometer # noqa
Expand Down Expand Up @@ -52,6 +53,6 @@
__all__ = [
Accelerometer, Altimeter, AnsiCodes, Brake, Camera, CameraRecognitionObject, Compass, Connector, ContactPoint, Display,
DistanceSensor, Emitter, Field, GPS, Gyro, InertialUnit, Joystick, Keyboard, LED, Lidar, LidarPoint, LightSensor, Motion,
Motor, Mouse, MouseState, Node, PositionSensor, Radar, RadarTarget, RangeFinder, Receiver, Robot, Skin, Speaker,
Motor, Mouse, MouseState, Node, PositionSensor, Proto, Radar, RadarTarget, RangeFinder, Receiver, Robot, Skin, Speaker,
Supervisor, TouchSensor, VacuumGripper
]
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ class Field:

wb.wb_supervisor_field_get_name.restype = ctypes.c_char_p
wb.wb_supervisor_field_get_type_name.restype = ctypes.c_char_p
wb.wb_supervisor_node_get_proto_field.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_proto_field_by_index.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_field.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_field_by_index.restype = ctypes.c_void_p
wb.wb_supervisor_field_get_actual_field.restype = ctypes.c_void_p
wb.wb_supervisor_field_get_sf_float.restype = ctypes.c_double
wb.wb_supervisor_field_get_sf_vec2f.restype = ctypes.POINTER(ctypes.c_double)
wb.wb_supervisor_field_get_sf_vec3f.restype = ctypes.POINTER(ctypes.c_double)
Expand All @@ -65,17 +62,8 @@ class Field:
wb.wb_supervisor_virtual_reality_headset_get_position = ctypes.POINTER(ctypes.c_double)
wb.wb_supervisor_virtual_reality_headset_get_orientation = ctypes.POINTER(ctypes.c_double)

def __init__(self, node, name: typing.Optional[str] = None, index: typing.Optional[int] = None, proto: bool = False):
if proto:
if name is not None:
self._ref = ctypes.c_void_p(wb.wb_supervisor_node_get_proto_field(node._ref, str.encode(name)))
else:
self._ref = ctypes.c_void_p(wb.wb_supervisor_node_get_proto_field_by_index(node._ref, index))
else:
if name is not None:
self._ref = ctypes.c_void_p(wb.wb_supervisor_node_get_field(node._ref, str.encode(name)))
else:
self._ref = ctypes.c_void_p(wb.wb_supervisor_node_get_field_by_index(node._ref, index))
def __init__(self, ref: ctypes.c_void_p):
self._ref = ref
if self._ref:
self.type = wb.wb_supervisor_field_get_type(self._ref)
else:
Expand All @@ -93,6 +81,10 @@ def getTypeName(self) -> str:
def getCount(self) -> int:
return self.count

def getActualField(self) -> 'Field':
field = wb.wb_supervisor_field_get_actual_field(self._ref)
return Field(field) if field else None

def enableSFTracking(self, samplingPeriod: int):
wb.wb_supervisor_field_enable_sf_tracking(self._ref, samplingPeriod)

Expand Down
39 changes: 28 additions & 11 deletions webots_ros2_driver/webots/lib/controller/python/controller/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .wb import wb
from .constants import constant
from .field import Field
from .proto import Proto
import struct
import typing

Expand All @@ -40,6 +41,11 @@ class Node:
wb.wb_supervisor_node_get_root.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_selected.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_from_def.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_proto.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_field.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_field_by_index.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_base_node_field.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_base_node_field_by_index.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_self.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_from_device.restype = ctypes.c_void_p
wb.wb_supervisor_node_get_from_id.restype = ctypes.c_void_p
Expand Down Expand Up @@ -90,6 +96,10 @@ def getParentNode(self) -> Node:
def isProto(self) -> bool:
return wb.wb_supervisor_node_is_proto(self._ref) != 0

def getProto(self) -> Proto:
proto = wb.wb_supervisor_node_get_proto(self._ref)
return Proto(proto) if proto else None

def getFromProtoDef(self, DEF: str) -> Node:
node = wb.wb_supervisor_node_get_from_proto_def(self._ref, str.encode(DEF))
return Node(ref=node) if node else None
Expand All @@ -109,24 +119,27 @@ def remove(self):
def exportString(self):
return wb.wb_supervisor_node_export_string(self._ref).decode()

def getField(self, name: str) -> Field:
field = Field(self, name=name)
return field if field._ref else None
def getField(self, fieldName: str) -> Field:
field = wb.wb_supervisor_node_get_field(self._ref, str.encode(fieldName))
return Field(field) if field else None

def getFieldByIndex(self, index: int) -> Field:
field = Field(self, index=index)
return field if field._ref else None
field = wb.wb_supervisor_node_get_field_by_index(self._ref, index)
return Field(field) if field else None

def getNumberOfFields(self) -> int:
return self.number_of_fields

def getProtoField(self, name: str) -> Field:
field = Field(self, name=name, proto=True)
return field if field._ref else None
def getBaseNodeField(self, fieldName: str) -> Field:
field = wb.wb_supervisor_node_get_base_node_field(self._ref, str.encode(fieldName))
return Field(field) if field else None

def getBaseNodeFieldByIndex(self, index: int) -> Field:
field = wb.wb_supervisor_node_get_base_node_field_by_index(self._ref, index)
return Field(field) if field else None

def getProtoFieldByIndex(self, index: int) -> Field:
field = Field(self, index=index, proto=True)
return field if field._ref else None
def getNumberOfBaseNodeFields(self) -> int:
return self.number_of_base_node_fields

def getPosition(self) -> typing.List[float]:
p = wb.wb_supervisor_node_get_position(self._ref)
Expand Down Expand Up @@ -235,6 +248,10 @@ def base_type_name(self) -> str:
def number_of_fields(self) -> int:
return wb.wb_supervisor_node_get_number_of_fields(self._ref)

@property
def number_of_base_node_fields(self) -> int:
return wb.wb_supervisor_node_get_number_of_base_node_fields(self._ref)


Node.NO_NODE = constant('NODE_NO_NODE')
Node.APPEARANCE = constant('NODE_APPEARANCE')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 1996-2023 Cyberbotics Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import ctypes
from .wb import wb
from .field import Field


class Proto:
wb.wb_supervisor_proto_get_type_name.restype = ctypes.c_char_p
wb.wb_supervisor_proto_get_parent.restype = ctypes.c_void_p
wb.wb_supervisor_proto_get_field.restype = ctypes.c_void_p
wb.wb_supervisor_proto_get_field_by_index.restype = ctypes.c_void_p

def __init__(self, ref: ctypes.c_void_p):
self._ref = ref

def getTypeName(self) -> str:
return self.type_name

def getParent(self) -> 'Proto':
proto = wb.wb_supervisor_proto_get_parent(self._ref)
return Proto(proto) if proto else None

def getField(self, name: str) -> Field:
field = wb.wb_supervisor_proto_get_field(self._ref, str.encode(name))
return Field(field) if field else None

def getFieldByIndex(self, index: int) -> Field:
field = wb.wb_supervisor_proto_get_field_by_index(self._ref, index)
return Field(field) if field else None

def getNumberOfFields(self) -> int:
return self.number_of_fields

def isDerived(self) -> bool:
return self.is_derived

@property
def type_name(self) -> str:
return wb.wb_supervisor_proto_get_type_name(self._ref).decode()

@property
def number_of_fields(self) -> int:
return wb.wb_supervisor_proto_get_number_of_fields(self._ref)

@property
def is_derived(self) -> bool:
return wb.wb_supervisor_proto_is_derived(self._ref) != 0
16 changes: 12 additions & 4 deletions webots_ros2_driver/webots/src/controller/c/Controller.def
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ wb_supervisor_animation_stop_recording
wb_supervisor_export_image
wb_supervisor_field_disable_sf_tracking
wb_supervisor_field_enable_sf_tracking
wb_supervisor_field_get_actual_field
wb_supervisor_field_get_count
wb_supervisor_field_get_name
wb_supervisor_field_get_mf_bool
Expand Down Expand Up @@ -525,6 +526,8 @@ wb_supervisor_node_enable_contact_point_tracking
wb_supervisor_node_enable_contact_points_tracking
wb_supervisor_node_enable_pose_tracking
wb_supervisor_node_export_string
wb_supervisor_node_get_base_node_field
wb_supervisor_node_get_base_node_field_by_index
wb_supervisor_node_get_base_type_name
wb_supervisor_node_get_def
wb_supervisor_node_get_field
Expand All @@ -538,15 +541,14 @@ wb_supervisor_node_get_contact_point
wb_supervisor_node_get_contact_points
wb_supervisor_node_get_contact_point_node
wb_supervisor_node_get_id
wb_supervisor_node_get_number_of_fields
wb_supervisor_node_get_number_of_base_node_fields
wb_supervisor_node_get_number_of_contact_points
wb_supervisor_node_get_number_of_fields
wb_supervisor_node_get_orientation
wb_supervisor_node_get_parent_node
wb_supervisor_node_get_pose
wb_supervisor_node_get_position
wb_supervisor_node_get_proto_field
wb_supervisor_node_get_proto_field_by_index
wb_supervisor_node_get_proto_number_of_fields
wb_supervisor_node_get_proto
wb_supervisor_node_get_root
wb_supervisor_node_get_selected
wb_supervisor_node_get_self
Expand All @@ -564,6 +566,12 @@ wb_supervisor_node_save_state
wb_supervisor_node_set_joint_position
wb_supervisor_node_set_velocity
wb_supervisor_node_set_visibility
wb_supervisor_proto_get_field
wb_supervisor_proto_get_field_by_index
wb_supervisor_proto_get_number_of_fields
wb_supervisor_proto_get_parent
wb_supervisor_proto_get_type_name
wb_supervisor_proto_is_derived
wb_supervisor_set_label
wb_supervisor_simulation_get_mode
wb_supervisor_simulation_physics_reset
Expand Down
1 change: 1 addition & 0 deletions webots_ros2_driver/webots/src/controller/c/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
#define C_SUPERVISOR_NODE_RESET_STATE 75
#define C_SUPERVISOR_NODE_SET_JOINT_POSITION 76
#define C_SUPERVISOR_NODE_EXPORT_STRING 77
#define C_SUPERVISOR_NODE_GET_PROTO 78

// ctr <-> sim
#define C_ROBOT_WAIT_FOR_USER_INPUT_EVENT 80
Expand Down
Loading