Source

ars / demos / sensors / accelerometer1.py

"""Shows usage of the Accelerometer sensor, with the FallingBalls program.
Sensor data is stored in a queue, which is retrieved after the simulation ends
(but it can be accessed at any time).

Sensor is created in the `create_sim_objects` method.
	'self.sensor = sensors.Accelerometer(ball_object, self.sim.time_step)'
It is updated in the `on_post_step` method
	'self.sensor.on_change(time)'
Sensor measurements can be retrieved at any time
	'self.sensor.data_queue.pull()'

"""
from ars.app import dispatcher
from ars.model.robot import sensors
from ars.model.simulator import signals

from .base import FallingBalls, PrintDataMixin


class Accelerometer(FallingBalls, PrintDataMixin):

	def __init__(self):
		FallingBalls.__init__(self)
		dispatcher.connect(self.on_post_step, signals.SIM_POST_STEP)

	def create_sim_objects(self):
		FallingBalls.create_sim_objects(self)
		ball_object = self.sim.get_object(self.ball1)
		self.sensor = sensors.Accelerometer(ball_object, self.sim.time_step)

	def on_post_step(self):
		try:
			time = self.sim.sim_time
			self.sensor.on_change(time)
		except Exception as e:
			print('Exception when executing on_post_step: %s' % str(e))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.