From 55d7a8a25431f7788a62c9632c1ae4001a15f698 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Wed, 20 Jan 2021 16:02:36 +0100 Subject: [PATCH] examples/pup: acceleration and angular velocity --- doc/api/hubs/primehub.rst | 14 +++++++++- doc/api/hubs/technichub.rst | 26 ++++++++++++++----- examples/pup/hub_primehub/imu_read_scalar.py | 20 ++++++++++++++ examples/pup/hub_primehub/imu_read_vector.py | 14 ++++++++++ examples/pup/hub_shared/imu_read_scalar.py | 21 +++++++++++++++ examples/pup/hub_shared/imu_read_vector.py | 15 +++++++++++ .../pup/hub_technichub/imu_read_scalar.py | 20 ++++++++++++++ .../pup/hub_technichub/imu_read_vector.py | 14 ++++++++++ 8 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 examples/pup/hub_primehub/imu_read_scalar.py create mode 100644 examples/pup/hub_primehub/imu_read_vector.py create mode 100644 examples/pup/hub_shared/imu_read_scalar.py create mode 100644 examples/pup/hub_shared/imu_read_vector.py create mode 100644 examples/pup/hub_technichub/imu_read_scalar.py create mode 100644 examples/pup/hub_technichub/imu_read_vector.py diff --git a/doc/api/hubs/primehub.rst b/doc/api/hubs/primehub.rst index 2daa786..8df35c3 100644 --- a/doc/api/hubs/primehub.rst +++ b/doc/api/hubs/primehub.rst @@ -191,8 +191,20 @@ Reading the tilt value .. literalinclude:: ../../../examples/pup/hub_primehub/imu_tilt.py -Reading the tilt value with custom hub orientation +Using a custom hub orientation ************************************************** .. literalinclude:: ../../../examples/pup/hub_primehub/imu_tilt_blast.py + +Reading acceleration and angular velocity vectors +************************************************** + +.. literalinclude:: + ../../../examples/pup/hub_primehub/imu_read_vector.py + +Reading acceleration and angular velocity on one axis +***************************************************** + +.. literalinclude:: + ../../../examples/pup/hub_primehub/imu_read_scalar.py diff --git a/doc/api/hubs/technichub.rst b/doc/api/hubs/technichub.rst index 35ab0b0..22d0383 100644 --- a/doc/api/hubs/technichub.rst +++ b/doc/api/hubs/technichub.rst @@ -19,17 +19,17 @@ Technic Hub .. rubric:: Using the IMU - .. automethod:: pybricks.hubs::PrimeHub.imu.up + .. automethod:: pybricks.hubs::TechnicHub.imu.up - .. automethod:: pybricks.hubs::PrimeHub.imu.tilt + .. automethod:: pybricks.hubs::TechnicHub.imu.tilt - .. automethod:: pybricks.hubs::PrimeHub.imu.acceleration + .. automethod:: pybricks.hubs::TechnicHub.imu.acceleration - .. automethod:: pybricks.hubs::PrimeHub.imu.angular_velocity + .. automethod:: pybricks.hubs::TechnicHub.imu.angular_velocity - .. automethod:: pybricks.hubs::PrimeHub.imu.heading + .. automethod:: pybricks.hubs::TechnicHub.imu.heading - .. automethod:: pybricks.hubs::PrimeHub.imu.reset_heading + .. automethod:: pybricks.hubs::TechnicHub.imu.reset_heading .. rubric:: Using the battery @@ -80,8 +80,20 @@ Reading the tilt value .. literalinclude:: ../../../examples/pup/hub_technichub/imu_tilt.py -Reading the tilt value with custom hub orientation +Using a custom hub orientation ************************************************** .. literalinclude:: ../../../examples/pup/hub_technichub/imu_tilt_blast.py + +Reading acceleration and angular velocity vectors +************************************************** + +.. literalinclude:: + ../../../examples/pup/hub_technichub/imu_read_vector.py + +Reading acceleration and angular velocity on one axis +***************************************************** + +.. literalinclude:: + ../../../examples/pup/hub_technichub/imu_read_scalar.py diff --git a/examples/pup/hub_primehub/imu_read_scalar.py b/examples/pup/hub_primehub/imu_read_scalar.py new file mode 100644 index 0000000..18e558c --- /dev/null +++ b/examples/pup/hub_primehub/imu_read_scalar.py @@ -0,0 +1,20 @@ +from pybricks.hubs import PrimeHub +from pybricks.tools import wait +from pybricks.geometry import Axis + +# Initialize the hub. +hub = PrimeHub() + +# Get the acceleration or angular_velocity along a single axis. +# If you need only one value, this is more memory efficient. +while True: + + # Read the forward acceleration. + forward_acceleration = hub.imu.acceleration(Axis.X) + + # Read the yaw rate. + yaw_rate = hub.imu.angular_velocity(Axis.Z) + + # Print the yaw rate. + print(yaw_rate) + wait(100) diff --git a/examples/pup/hub_primehub/imu_read_vector.py b/examples/pup/hub_primehub/imu_read_vector.py new file mode 100644 index 0000000..ffa4bdf --- /dev/null +++ b/examples/pup/hub_primehub/imu_read_vector.py @@ -0,0 +1,14 @@ +from pybricks.hubs import PrimeHub +from pybricks.tools import wait + +# Initialize the hub. +hub = PrimeHub() + +# Get the acceleration vector. +print(hub.imu.acceleration()) + +# Get the angular velocity vector. +print(hub.imu.angular_velocity()) + +# Wait so we can see what we printed +wait(5000) diff --git a/examples/pup/hub_shared/imu_read_scalar.py b/examples/pup/hub_shared/imu_read_scalar.py new file mode 100644 index 0000000..d3b69a8 --- /dev/null +++ b/examples/pup/hub_shared/imu_read_scalar.py @@ -0,0 +1,21 @@ +# ExampleHub = TechnicHub PrimeHub +from pybricks.hubs import ExampleHub +from pybricks.tools import wait +from pybricks.geometry import Axis + +# Initialize the hub. +hub = ExampleHub() + +# Get the acceleration or angular_velocity along a single axis. +# If you need only one value, this is more memory efficient. +while True: + + # Read the forward acceleration. + forward_acceleration = hub.imu.acceleration(Axis.X) + + # Read the yaw rate. + yaw_rate = hub.imu.angular_velocity(Axis.Z) + + # Print the yaw rate. + print(yaw_rate) + wait(100) diff --git a/examples/pup/hub_shared/imu_read_vector.py b/examples/pup/hub_shared/imu_read_vector.py new file mode 100644 index 0000000..74a92e4 --- /dev/null +++ b/examples/pup/hub_shared/imu_read_vector.py @@ -0,0 +1,15 @@ +# ExampleHub = TechnicHub PrimeHub +from pybricks.hubs import ExampleHub +from pybricks.tools import wait + +# Initialize the hub. +hub = ExampleHub() + +# Get the acceleration vector. +print(hub.imu.acceleration()) + +# Get the angular velocity vector. +print(hub.imu.angular_velocity()) + +# Wait so we can see what we printed +wait(5000) diff --git a/examples/pup/hub_technichub/imu_read_scalar.py b/examples/pup/hub_technichub/imu_read_scalar.py new file mode 100644 index 0000000..8d1334b --- /dev/null +++ b/examples/pup/hub_technichub/imu_read_scalar.py @@ -0,0 +1,20 @@ +from pybricks.hubs import TechnicHub +from pybricks.tools import wait +from pybricks.geometry import Axis + +# Initialize the hub. +hub = TechnicHub() + +# Get the acceleration or angular_velocity along a single axis. +# If you need only one value, this is more memory efficient. +while True: + + # Read the forward acceleration. + forward_acceleration = hub.imu.acceleration(Axis.X) + + # Read the yaw rate. + yaw_rate = hub.imu.angular_velocity(Axis.Z) + + # Print the yaw rate. + print(yaw_rate) + wait(100) diff --git a/examples/pup/hub_technichub/imu_read_vector.py b/examples/pup/hub_technichub/imu_read_vector.py new file mode 100644 index 0000000..c53dc3e --- /dev/null +++ b/examples/pup/hub_technichub/imu_read_vector.py @@ -0,0 +1,14 @@ +from pybricks.hubs import TechnicHub +from pybricks.tools import wait + +# Initialize the hub. +hub = TechnicHub() + +# Get the acceleration vector. +print(hub.imu.acceleration()) + +# Get the angular velocity vector. +print(hub.imu.angular_velocity()) + +# Wait so we can see what we printed +wait(5000)