Skip to content

Commit

Permalink
Non-Swig Python API (cyberbotics#5297)
Browse files Browse the repository at this point in the history
* simplified

* Support more field operations

* swiched API to PEP8 snake case

* ENU fix

* Update ansi_codes.c

* Update __init__.py

* Update ansi_codes.py

* Update WB_CONSTANTS.py

* Update camera.py

* Update constants.py

* Update distance_sensor.py

* Update emitter.py

* Update field.py

* Update keyboard.py

* Update motor.py

* Update node.py

* Update receiver.py

* Update wb.py

* Update mybot_simple.py

* Update robot.py

* Update sensor.py

* Update supervisor.py

* Fixed line endings

* Fixed line endings

* useless

* progress with old API implementation

* Working mybot_simple with legacy API

* slave.py working with legacy API

* progress with driver.py legacy support

* Fixed imports

* Fully working python example with legacy API

* cleanup

* clean-up

* clean-up

* Update mybot.wbt

* Update .mybot.wbproj

* Disabled old PYTHONPATH

* Removed python swig build system

* updated distribution and synchronization scripts

* Led support

* Support for accelerometer

* Altimeter

* Brake

* Brake

* Camera (unfinished)

* Working camera example

* Support for non-ascii user folders

* Working camera_auto_focus

* PEP8

* camera_recognition working

* compass

* connector

* display

* Display sample code

* working display

* clean-up

* camera segmentation

* Fixed lookup_table

* distance sensor

* emitter

* gps

* Better receiver

* GPS & Receiver

* Added deprecation message for Receiver.getData

* gps

* Better emitter

* GPS examples

* Support for tuples in emitter

* gyro

* InertialUnit

* improved description

* fixed restypes

* Better parameter naming

* Progress with Lidar

* Update connector.py

* Update connector.py

* Update display.py

* Update distance_sensor.py

* Lidar (preliminary)

* Fixed Camera.getImageArray

* completed lidar

* light sensor

* Fixed line ending for lidar_point.py

* Fixed PEP8

* Added battery support

* motor

* Removed python vehicle libraries

* Removed Darwin-OP python manager library

* motor

* pen

* position sensor

* Propeller example

* radar

* RangeFinder

* receiver

* skin

* speaker

* bumper

* force/force3d demos

* Added mouse, keyboard and joystick

* Fixed mouse

* robot

* supervisor node and field APIs

* supervisor

* Vehicle libraries skeleton

* fixed line ending

* Removed python library for manager

* Motion API

* Various fixes

* Removed duplicates

* Supervisor example

* Fixed retrival of contact points

* Fixed mouse

* removed debug statement

* Progress with Driver API

* Increased robustness of node/field APIs

* Progress with driver API

* Progress with driver API

* Driver API

* Car API

* replaced marathon Python controller with a C++ controller

* battery device example

* Coupled motors

* display supervisor

* encoders

* hinge_joint_with_backlash

* Removed binary controller: sick_cloud_point

* clang-format

* pep8

* Cross platform shared library loader

* Updated change log

* Cross-platform libcar/libdriver

* Update driver.py

* Added creation of python soft link on macOS installation

* Update projects/samples/devices/controllers/altimeter/altimeter.py

Co-authored-by: Benjamin Délèze <[email protected]>

* Added missing methods

* Fixed missing type annotation

* Fixed missing setExposure method

* Fixed return data type

* Fixed Camera.getImageArray

* Fixed Camera.getRecognitionSegmentationImageArray

* Fixed example to use documented methods instead of getters

* Update projects/samples/devices/controllers/compass/compass.py

Co-authored-by: Benjamin Délèze <[email protected]>

* Update projects/samples/devices/controllers/connector/connector.py

Co-authored-by: Benjamin Délèze <[email protected]>

* Fixed display polygon methods

* Update projects/samples/robotbenchmark/humanoid_marathon/controllers/marathon/marathon.cpp

Co-authored-by: Benjamin Délèze <[email protected]>

* Update projects/samples/robotbenchmark/humanoid_marathon/controllers/marathon/marathon.cpp

Co-authored-by: Benjamin Délèze <[email protected]>

* Fixed missing Makefile target (marathon)

* Update projects/samples/devices/controllers/emitter_receiver/emitter_receiver.py

Co-authored-by: Benjamin Délèze <[email protected]>

* Fixed emitter_receiver example

* Fixed emitter range

* type annotation clean-up

* Added missing return type annotation

* Add missing camera method to fix nao_demo_python

* Remove debug statements in lidar example

* Fixed Lidar.getLayerRangeImage

* Fixed light sensor lookup table retrieval method

* Fixed motor missing return values

* Fixed missing PositionSensor.getType method

* Update lib/controller/python/controller/range_finder.py

Co-authored-by: Benjamin Délèze <[email protected]>

* Receiver.getDataSize()

* Fixed Lidar.getFrequency()

* Fixed lidar example

* Added lidar point cloud example

* Fixed Lidar.getLayerPointCloud

* Fixed setting of PYTHON_HOME path

* Fixed PYTHON_PATH in Windows workflows

* Fixed mouse state

* Fixed Supervisor.getFromProtoDef

* Fix LightSensor.getLookupTable

* Fixed Camera.get*ImageArray() methods

* Added default value for Robot.getUrdf()

* Fixed support for Python versions > 10

* Update WbLanguageTools.cpp

* Fixed compilation on macOS

* Update src/webots/control/WbLanguageTools.cpp

Co-authored-by: Yannick Goumaz <[email protected]>

* Updated extern controller documentation

* Update lib/controller/python/vehicle/driver.py

Co-authored-by: Yannick Goumaz <[email protected]>

* Update lib/controller/python/vehicle/driver.py

Co-authored-by: Yannick Goumaz <[email protected]>

* Update lib/controller/python/vehicle/driver.py

Co-authored-by: Yannick Goumaz <[email protected]>

* Update lib/controller/python/vehicle/driver.py

Co-authored-by: Yannick Goumaz <[email protected]>

Co-authored-by: Benjamin Délèze <[email protected]>
Co-authored-by: Yannick Goumaz <[email protected]>
3 people authored Nov 7, 2022

Verified

This commit was signed with the committer’s verified signature.
chjj Christopher Jeffrey (JJ)
1 parent 0ca7cb5 commit a04d858
Showing 151 changed files with 7,685 additions and 2,380 deletions.
13 changes: 3 additions & 10 deletions .github/workflows/test_suite_windows.yml
Original file line number Diff line number Diff line change
@@ -68,19 +68,12 @@ jobs:
choco install -y visualcpp-build-tools
- name: Install Webots Compilation Dependencies
run: |
export PYTHON_INSTALLATION_FOLDER=/C/hostedtoolcache/windows/Python/
export PYTHON37_REVISION=`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.7\.[0-9]\+$' | cut -c5- | sort -n | tail -n1`
export PYTHON38_REVISION=`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.8\.[0-9]\+$' | cut -c5- | sort -n | tail -n1`
export PYTHON39_REVISION=`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.9\.[0-9]\+$' | cut -c5- | sort -n | tail -n1`
export PYTHON310_REVISION=`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.10\.[0-9]\+$' | cut -c6- | sort -n | tail -n1`
export PYTHON_HOME=/C/hostedtoolcache/windows/Python/3.10.`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.10\.[0-9]\+$' | cut -c6- | sort -n | tail -n1`/x64
echo 'export JAVA_HOME=/C/Program\ Files/OpenJDK/`ls /C/Program\ Files/OpenJDK`' >> ~/.bash_profile
echo 'export PYTHON37_HOME='$PYTHON_INSTALLATION_FOLDER'3.7.'$PYTHON37_REVISION'/x64' >> ~/.bash_profile
echo 'export PYTHON38_HOME='$PYTHON_INSTALLATION_FOLDER'3.8.'$PYTHON38_REVISION'/x64' >> ~/.bash_profile
echo 'export PYTHON39_HOME='$PYTHON_INSTALLATION_FOLDER'3.9.'$PYTHON39_REVISION'/x64' >> ~/.bash_profile
echo 'export PYTHON310_HOME='$PYTHON_INSTALLATION_FOLDER'3.10.'$PYTHON310_REVISION'/x64' >> ~/.bash_profile
echo 'export PYTHON_HOME='$PYTHON_HOME >> ~/.bash_profile
echo 'export VISUAL_STUDIO_PATH="/C/Program Files (x86)/Microsoft Visual Studio/2017"' >> ~/.bash_profile
echo 'export INNO_SETUP_HOME="/C/Program Files (x86)/Inno Setup 6"' >> ~/.bash_profile
echo 'export PATH=$PYTHON38_HOME:$PYTHON38_HOME/Scripts:$GITHUB_WORKSPACE/msys64/mingw64/bin:$GITHUB_WORKSPACE/bin/node:/mingw64/bin:/usr/bin:$JAVA_HOME/bin:$PATH' >> ~/.bash_profile
echo 'export PATH=$PYTHON_HOME:$PYTHON_HOME/Scripts:$GITHUB_WORKSPACE/msys64/mingw64/bin:$GITHUB_WORKSPACE/bin/node:/mingw64/bin:/usr/bin:$JAVA_HOME/bin:$PATH' >> ~/.bash_profile
export WEBOTS_HOME=$GITHUB_WORKSPACE
./scripts/install/msys64_installer.sh --all
- name: Set Commit SHA in Version
13 changes: 3 additions & 10 deletions .github/workflows/test_suite_windows_develop.yml
Original file line number Diff line number Diff line change
@@ -64,19 +64,12 @@ jobs:
choco install -y visualcpp-build-tools
- name: Install Webots Compilation Dependencies
run: |
export PYTHON_INSTALLATION_FOLDER=/C/hostedtoolcache/windows/Python/
export PYTHON37_REVISION=`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.7\.[0-9]\+$' | cut -c5- | sort -n | tail -n1`
export PYTHON38_REVISION=`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.8\.[0-9]\+$' | cut -c5- | sort -n | tail -n1`
export PYTHON39_REVISION=`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.9\.[0-9]\+$' | cut -c5- | sort -n | tail -n1`
export PYTHON310_REVISION=`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.10\.[0-9]\+$' | cut -c6- | sort -n | tail -n1`
export PYTHON_HOME=/C/hostedtoolcache/windows/Python/3.10.`ls $PYTHON_INSTALLATION_FOLDER | grep '^3\.10\.[0-9]\+$' | cut -c6- | sort -n | tail -n1`/x64
echo 'export JAVA_HOME=/C/Program\ Files/OpenJDK/`ls /C/Program\ Files/OpenJDK`' >> ~/.bash_profile
echo 'export PYTHON37_HOME='$PYTHON_INSTALLATION_FOLDER'3.7.'$PYTHON37_REVISION'/x64' >> ~/.bash_profile
echo 'export PYTHON38_HOME='$PYTHON_INSTALLATION_FOLDER'3.8.'$PYTHON38_REVISION'/x64' >> ~/.bash_profile
echo 'export PYTHON39_HOME='$PYTHON_INSTALLATION_FOLDER'3.9.'$PYTHON39_REVISION'/x64' >> ~/.bash_profile
echo 'export PYTHON310_HOME='$PYTHON_INSTALLATION_FOLDER'3.10.'$PYTHON310_REVISION'/x64' >> ~/.bash_profile
echo 'export PYTHON_HOME='$PYTHON_HOME >> ~/.bash_profile
echo 'export VISUAL_STUDIO_PATH="/C/Program Files (x86)/Microsoft Visual Studio/2017"' >> ~/.bash_profile
echo 'export INNO_SETUP_HOME="/C/Program Files (x86)/Inno Setup 6"' >> ~/.bash_profile
echo 'export PATH=$PYTHON38_HOME:$PYTHON38_HOME/Scripts:$GITHUB_WORKSPACE/msys64/mingw64/bin:$GITHUB_WORKSPACE/bin/node:/mingw64/bin:/usr/bin:$JAVA_HOME/bin:$PATH' >> ~/.bash_profile
echo 'export PATH=$PYTHON_HOME:$PYTHON_HOME/Scripts:$GITHUB_WORKSPACE/msys64/mingw64/bin:$GITHUB_WORKSPACE/bin/node:/mingw64/bin:/usr/bin:$JAVA_HOME/bin:$PATH' >> ~/.bash_profile
export WEBOTS_HOME=$GITHUB_WORKSPACE
./scripts/install/msys64_installer.sh --all
- name: Set Commit SHA in Version
6 changes: 5 additions & 1 deletion dependencies/Makefile.mac
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ PACKAGES_CLEAN = $(addsuffix -clean, $(PACKAGES))

.PHONY: release debug distrib profile clean cleanse $(PACKAGES) $(PACKAGES_CLEAN)

release debug distrib profile: ../.clang-format $(PACKAGES) ../Contents/Resources ../Contents/include ../Contents/lib/controller/matlab ../Contents/projects
release debug distrib profile: ../.clang-format $(PACKAGES) ../Contents/Resources ../Contents/include ../Contents/lib/controller/matlab ../Contents/lib/controller/python ../Contents/projects
clean:
cleanse: $(PACKAGES_CLEAN)
@rm -f ../.clang-format
@@ -47,6 +47,10 @@ cleanse: $(PACKAGES_CLEAN)
@mkdir -p ../Contents/lib/controller
@ln -s ../../../lib/controller/matlab $@

../Contents/lib/controller/python:
@mkdir -p ../Contents/lib/controller
@ln -s ../../../lib/controller/python $@

../Contents/projects:
@ln -s ../projects $@

30 changes: 4 additions & 26 deletions docs/guide/running-extern-robot-controllers.md
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ It is also possible to connect `<extern>` controllers from remote computers usin

Running an extern robot controller requires that the controller is launched manually.
This may seem inconvenient, but in several cases, it turns out to be very useful, because the user has full control over the controller process.
For example, it may run it within a debugging environment, like *gdb*, a command line tool like *Python shell*, or within some Integrated Development Environment (IDE), such as *Visual C++*, *Eclipse* or *PyCharm*.
For example, it may run it within a debugging environment, like *gdb*, a command line tool like *$ shell*, or within some Integrated Development Environment (IDE), such as *Visual C++*, *Eclipse* or *PyCharm*.
Also, the standard output and error streams (`stdout` and `stderr`) remain under the user control and are not sent to the Webots console.
It is even possible to read the standard input stream (`stdin`) like with any standard program.
Moreover, starting external controllers remotely allows to run Webots on a different machine than the controller, which can be useful if the specifications required by the two processes are very different.
@@ -36,21 +36,14 @@ Webots environment variables needed by extern controllers:
| Path (all controllers) | add `%WEBOTS_HOME%\lib\controller` |
| Path (all controllers except Python >= 3.8) | add `%WEBOTS_HOME%\msys64\mingw64\bin` |
| Path (for C++, Python < 3.8, and Java controllers) | add `%WEBOTS_HOME%\msys64\mingw64\bin\cpp` |
| PYTHONPATH (for Python) | add `${WEBOTS_HOME}\lib\controller\python3X` |
| PYTHONPATH (for Python) | add `${WEBOTS_HOME}\lib\controller\python` |
| PYTHONIOENCODING (for Python) | `UTF-8` |
| WEBOTS\_PROJECT (for MATLAB) | `C:\Users\MyUsername\my_folder\my_webots_project` |
| WEBOTS\_CONTROLLER\_NAME (for MATLAB) | `my_robot_controller` |
| WEBOTS\_VERSION (for MATLAB) | `{{ webots.version.full }}` |

&nbsp;

**Python**: Setting the correct python version:
When setting or extending the `PYTHONPATH` environment variable, make sure to replace the `X` in `python3X` with your python version.
This can be found by typing `python --version` in the command line.
If for example the answer is `Python 3.8.10`, `python3X` should be `python38`.

&nbsp;

**MATLAB**: Here is an example of what you should enter in the MATLAB console:

```matlab
@@ -81,21 +74,14 @@ java -classpath $WEBOTS_HOME\lib\controller\java\Controller.jar:$WEBOTS_HOME\pro
|-------------------------------------------------------|--------------------------------------------------|
| WEBOTS\_HOME | `/usr/local/webots` |
| LD\_LIBRARY\_PATH (not needed for Python controllers) | add `${WEBOTS_HOME}/lib/controller` |
| PYTHONPATH (for Python) | add `${WEBOTS_HOME}/lib/controller/python3X` |
| PYTHONPATH (for Python) | add `${WEBOTS_HOME}/lib/controller/python` |
| PYTHONIOENCODING (for Python) | `UTF-8` |
| WEBOTS\_PROJECT (for MATLAB) | `/home/my_username/my_folder/my_webots_project` |
| WEBOTS\_CONTROLLER\_NAME (for MATLAB) | `my_robot_controller` |
| WEBOTS\_VERSION (for MATLAB) | `{{ webots.version.full }}` |

&nbsp;

**Python**: Setting the correct python version:
When setting or extending the `PYTHONPATH` environment variable, make sure to replace the `X` in `python3X` with your python version.
This can be found by typing `python3 --version` in the terminal.
If for example the answer is `Python 3.8.10`, `python3X` should be `python38`.

&nbsp;

**MATLAB**: Here is an example of what you should enter in the MATLAB console:

```matlab
@@ -126,22 +112,14 @@ java -classpath $WEBOTS_HOME/lib/controller/java/Controller.jar:$WEBOTS_HOME/pro
|-------------------------------------------------------|------------------------------------------------------------|
| WEBOTS\_HOME | `/Applications/Webots.app` |
| DYLD\_LIBRARY\_PATH | add `${WEBOTS_HOME}/Contents/lib/controller` |
| PYTHONPATH (for the official python.org Python) | add `${WEBOTS_HOME}/Contents/lib/controller/python3X` |
| PYTHONPATH (for the Homebrew Python) | add `${WEBOTS_HOME}/Contents/lib/controller/python3X_brew` |
| PYTHONPATH | add `${WEBOTS_HOME}/Contents/lib/controller/python` |
| PYTHONIOENCODING (for Python) | `UTF-8` |
| WEBOTS\_PROJECT (for MATLAB) | `/Users/my_username/my_folder/my_webots_project` |
| WEBOTS\_CONTROLLER\_NAME (for MATLAB) | `my_robot_controller` |
| WEBOTS\_VERSION (for MATLAB) | `{{ webots.version.full }}` |

&nbsp;

**Python**: Setting the correct python version:
When setting or extending the `PYTHONPATH` environment variable, make sure to replace the `X` in `python3X` with your python version.
This can be found by typing `python3 --version` in the terminal.
If for example the answer is `Python 3.8.10`, `python3X` should be `python38`.

&nbsp;

**MATLAB**: Here is an example of what you should enter in the MATLAB console:

```matlab
1 change: 1 addition & 0 deletions docs/reference/changelog-r2023.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
## Webots R2023a
Released on December, 12th, 2022.
- New Features
- Added support for any current and upcoming version of Python for robot controllers ([#5297](https://github.com/cyberbotics/webots/pull/5297)).
- **Added new `projection` field to [Camera](camera.md), [RangeFinder](rangefinder.md), and [Lidar](lidar.md) nodes. This field replaces the deprecated `spherical` field ([#5266](https://github.com/cyberbotics/webots/pull/5266)).**
- Added a non-interactive terminal for web streaming ([#5119](https://github.com/cyberbotics/webots/pull/5119)).
- Added a customizable window for animations to display graphs or other user defined content ([5265](https://github.com/cyberbotics/webots/pull/5265)).
5 changes: 1 addition & 4 deletions docs/reference/display.md
Original file line number Diff line number Diff line change
@@ -621,10 +621,7 @@ namespace webots {
%tab "Python"
```python
from controller import Display, ImageRef
class ImageRef:
# ...
from controller import Display
class Display (Device):
RGB, RGBA, ARGB, BGRA, ABGR
34 changes: 17 additions & 17 deletions docs/reference/mouse.md
Original file line number Diff line number Diff line change
@@ -16,17 +16,17 @@ The state of a mouse is defined like this:

```c
typedef struct {
// mouse buttons state
bool left;
bool middle;
bool right;
// mouse 2D position in the 3D window
double u;
double v;
// mouse 3D position
double x;
double y;
double z;
// mouse buttons state
bool left;
bool middle;
bool right;
} WbMouseState;
```
%tab-end
@@ -38,17 +38,17 @@ typedef struct {

namespace webots {
typedef struct {
// mouse buttons state
bool left;
bool middle;
bool right;
// mouse 2D position in the 3D window
double u;
double v;
// mouse 3D position
double x;
double y;
double z;
// mouse buttons state
bool left;
bool middle;
bool right;
} MouseState;
}
```
@@ -61,12 +61,12 @@ namespace webots {
from controller import MouseState
class MouseState:
@property
left, middle, right # mouse button state
@property
u, v # mouse 2D position in the 3D window
@property
x, y, z # mouse 3D position
@property
left, middle, right # mouse button state
```

%tab-end
@@ -77,14 +77,14 @@ class MouseState:
import com.cyberbotics.webots.controller.MouseState;

public class MouseState {
public boolean getLeft();
public boolean getMiddle();
public boolean getRight();
public double getU();
public double getV();
public double getX();
public double getY();
public double getZ();
public boolean getLeft();
public boolean getMiddle();
public boolean getRight();
}
```

@@ -94,14 +94,14 @@ public class MouseState {

```MATLAB
structs.WbMouseState.members = struct(
'left', 'int8',
'middle', 'int8',
'right', 'int8',
'u', 'double',
'v', 'double',
'x', 'double',
'y', 'double',
'z', 'double'
'z', 'double',
'left', 'int8',
'middle', 'int8',
'right', 'int8'
);
```

4 changes: 2 additions & 2 deletions docs/reference/robot.md
Original file line number Diff line number Diff line change
@@ -1135,7 +1135,7 @@ In that case, the sampling period is expressed in real time and not in simulatio

#### `wb_robot_battery_sensor_enable`
#### `wb_robot_battery_sensor_disable`
#### `wb_robot_get_battery_sampling_period`
#### `wb_robot_battery_sensor_get_sampling_period`
#### `wb_robot_battery_sensor_get_value`

%tab-component "language"
@@ -1148,7 +1148,7 @@ In that case, the sampling period is expressed in real time and not in simulatio
void wb_robot_battery_sensor_enable(int sampling_period);
void wb_robot_battery_sensor_disable();
double wb_robot_battery_sensor_get_value();
int wb_robot_get_battery_sampling_period(WbDeviceTag tag);
int wb_robot_battery_sensor_get_sampling_period();
```
%tab-end
8 changes: 4 additions & 4 deletions include/controller/c/webots/mouse_state.h
Original file line number Diff line number Diff line change
@@ -24,17 +24,17 @@
#include "types.h"

typedef struct {
// mouse buttons state
bool left;
bool middle;
bool right;
// mouse 2D position in the 3D window
double u;
double v;
// mouse 3D position
double x;
double y;
double z;
// mouse buttons state
bool left;
bool middle;
bool right;
} WbMouseState;

#endif /* WB_MOUSE_STATE_H */
47 changes: 47 additions & 0 deletions lib/controller/python/controller/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 1996-2022 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
#
# http://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.

from controller.field import Field # noqa
from controller.node import Node # noqa
from controller.ansi_codes import AnsiCodes # noqa
from controller.accelerometer import Accelerometer # noqa
from controller.altimeter import Altimeter # noqa
from controller.brake import Brake # noqa
from controller.camera import Camera # noqa
from controller.compass import Compass # noqa
from controller.connector import Connector # noqa
from controller.display import Display # noqa
from controller.distance_sensor import DistanceSensor # noqa
from controller.emitter import Emitter # noqa
from controller.gps import GPS # noqa
from controller.gyro import Gyro # noqa
from controller.inertial_unit import InertialUnit # noqa
from controller.led import LED # noqa
from controller.lidar import Lidar # noqa
from controller.light_sensor import LightSensor # noqa
from controller.motor import Motor # noqa
from controller.position_sensor import PositionSensor # noqa
from controller.radar import Radar # noqa
from controller.range_finder import RangeFinder # noqa
from controller.receiver import Receiver # noqa
from controller.robot import Robot # noqa
from controller.skin import Skin # noqa
from controller.speaker import Speaker # noqa
from controller.supervisor import Supervisor # noqa
from controller.touch_sensor import TouchSensor # noqa
from controller.keyboard import Keyboard # noqa
from controller.mouse import Mouse # noqa
from controller.mouse import MouseState # noqa
from controller.joystick import Joystick # noqa
from controller.motion import Motion # noqa
43 changes: 43 additions & 0 deletions lib/controller/python/controller/accelerometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 1996-2022 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
#
# http://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.

from controller.sensor import Sensor
from controller.wb import wb
import ctypes
import typing


class Accelerometer(Sensor):
wb.wb_accelerometer_get_values.restype = ctypes.POINTER(ctypes.c_double)
wb.wb_accelerometer_get_lookup_table.restype = ctypes.POINTER(ctypes.c_double)

def __init__(self, name: typing.Union[str, int], sampling_period: int = None):
self._enable = wb.wb_accelerometer_enable
self._get_sampling_period = wb.wb_accelerometer_get_sampling_period
super().__init__(name, sampling_period)

def getValues(self) -> typing.List[float]:
return self.value

def getLookupTable(self) -> typing.List[float]:
return self.lookup_table

@property
def lookup_table(self) -> typing.List[float]:
size = wb.wb_accelerometer_get_lookup_table_size(self._tag)
return wb.wb_accelerometer_get_lookup_table(self._tag)[: 3 * size]

@property
def value(self) -> typing.List[float]:
return wb.wb_accelerometer_get_values(self._tag)[:3]
Loading

0 comments on commit a04d858

Please sign in to comment.