forked from stepjam/PyRep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added gyroscope and accelerometer sensors (stepjam#86)
* Add distance acquisition method to ProximitySensor * List of changes: * add get_base_velocities method to NonHolomicBase * change name of the measure method from ProximitySensor * List of changes: * created sensors package * gyroscope sensor added * accelerometer sensor added * added functions to vrep.py * Added unit tests for accelerometer, gyroscope and read method of proximity sensor
- Loading branch information
Showing
11 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import List | ||
|
||
from pyrep.backend import sim | ||
from pyrep.const import ObjectType | ||
from pyrep.objects.force_sensor import ForceSensor | ||
from pyrep.objects.object import Object | ||
from pyrep.objects.shape import Shape | ||
|
||
|
||
class Accelerometer(Object): | ||
"""An object able to measure accelerations that are applied to it. | ||
""" | ||
|
||
def __init__(self, name): | ||
super().__init__(name) | ||
self._mass_object = Shape('%s_mass' % (self.get_name())) | ||
self._sensor = ForceSensor('%s_force_sensor' % (self.get_name())) | ||
|
||
def _get_requested_type(self) -> ObjectType: | ||
return ObjectType(sim.simGetObjectType(self.get_handle())) | ||
|
||
def read(self) -> List[float]: | ||
"""Reads the acceleration applied to accelerometer. | ||
:return: A list containing applied accelerations along | ||
the sensor's x, y and z-axes | ||
""" | ||
forces, _ = self._sensor.read() | ||
accel = [force / self._mass_object.get_mass() for force in forces] | ||
return accel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from pyrep.backend import sim | ||
from pyrep.const import ObjectType | ||
from pyrep.objects.object import Object | ||
|
||
|
||
class Gyroscope(Object): | ||
"""An object able to measure angular velocities that are applied to it. | ||
""" | ||
def __init__(self, name): | ||
super().__init__(name) | ||
self._ref = '%s_reference' % (self.get_name()) | ||
|
||
self._last_time = sim.simGetSimulationTime() | ||
self._old_transformation_matrix = self.get_matrix() | ||
|
||
def _get_requested_type(self) -> ObjectType: | ||
return ObjectType(sim.simGetObjectType(self.get_handle())) | ||
|
||
def read(self): | ||
"""Reads the angular velocities applied to gyroscope. | ||
:return: A list containing applied angular velocities along | ||
the sensor's x, y and z-axes. | ||
""" | ||
current_time = sim.simGetSimulationTime() | ||
dt = current_time - self._last_time | ||
|
||
inv_old_matrix = sim.simInvertMatrix(self._old_transformation_matrix) | ||
transformation_matrix = self.get_matrix() | ||
mat = sim.simMultiplyMatrices(inv_old_matrix, transformation_matrix) | ||
euler_angles = sim.simGetEulerAnglesFromMatrix(mat) | ||
|
||
self._last_time = current_time | ||
self._old_transformation_matrix = transformation_matrix | ||
|
||
if dt != 0: | ||
return [euler_angle / dt for euler_angle in euler_angles] | ||
return [0.0, 0.0, 0.0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import unittest | ||
from tests.core import TestCore | ||
from pyrep.sensors.accelerometer import Accelerometer | ||
|
||
|
||
class TestAccelerometer(TestCore): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.sensor = Accelerometer('accelerometer') | ||
|
||
def test_read(self): | ||
accelerations = self.sensor.read() | ||
self.assertEqual(len(accelerations), 3) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import unittest | ||
from tests.core import TestCore | ||
from pyrep.sensors.gyroscope import Gyroscope | ||
|
||
|
||
class TestGyroscope(TestCore): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.sensor = Gyroscope('gyroscope') | ||
|
||
def test_read(self): | ||
angular_velocities = self.sensor.read() | ||
self.assertEqual(len(angular_velocities), 3) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters