Commits

German Larrain committed 7a0b2ff

demos.sensors: added a usage example of TotalEnergy

  • Participants
  • Parent commits 129905e
  • Branches dev-sensors

Comments (0)

Files changed (1)

File demos/sensors/total_energy1.py

+#!/usr/bin/env python
+
+"""Shows usage of the TotalEnergy 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 = sen.TotalEnergy(ball_object, self.sim.gravity)'
+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.lib.pydispatch import dispatcher
+import ars.model.robot.sensors as sen
+from ars.model.simulator import signals
+
+from base import FallingBalls
+
+
+class TotalEnergy(FallingBalls):
+
+	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 = sen.TotalEnergy(ball_object, self.sim.gravity)  #, True)
+
+	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))
+
+
+if __name__ == '__main__':
+	sim_program = TotalEnergy()
+	sim_program.start()
+
+	sensor = sim_program.sensor
+	print('sensor data queue count %d' % sensor.data_queue.count())
+	print(sensor.data_queue)