# Commits

committed 364ca94

demos: no code changes, just docstrings and comments formatting according to
PEPs 8 and 257.

• Participants
• Parent commits 1168697

# demos/CentrifugalForceTest.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.01.30`
`-#`
`-# @author: german`
`-`
`-"""`
`-Demo of a test of ODE's and ARS's capability of simulating correctly a system were`
`-inertia and centrifugal force intervene. The theoretical angle (theta) of the cable that`
`+"""Demo of a test of ODE's and ARS's capability of simulating correctly a`
`+system where inertia and centrifugal force intervene. The theoretical angle`
`+(theta) of the cable that`
` holds the ball can be obtained with this equation:`
` `
` 	tan(theta) / (d + l*sin(theta)) = omega^2 / g`
` 		omega: angular velocity of pole and ball`
` 		g: gravity`
` `
`-However, theta must be calculated with a numerical solver since no simple analytical`
`-solution exists.`
`+However, theta must be calculated with a numerical solver since no simple`
`+analytical solution exists.`
`+`
` """`
` from ars.app import Program`
` import ars.utils.mathematical as mut`
` from ars.model.simulator import signals`
` `
` #View point`
`-#===============================================================================`
`+#==============================================================================`
` # vp_hpr = (90.0, 0.0, 0.0) # orientation [degrees]`
` # `
` # QUICKSTEP_ITERS = 20 # # of iterations for the QuickStep function`
` # GRAVITY = -9.81`
` # GLOBAL_CFM = 1e-10 # default for ODE with double precision`
` # GLOBAL_ERP = 0.8`
`-#===============================================================================`
`+#==============================================================================`
`+`
` `
` def get_sphere_volume(radius):`
` 	return 4.0 / 3.0 * mut.pi * (radius ** 3)`

# demos/ControlledSimpleArm.py

` #!/usr/bin/env python`
` `
`-# Created on 2011.12.01`
`-#`
`-# @author: german`
`+"""Runs a simulation of a controlled (PD) simple arm, with 2 links and 2 rotary`
`+joints.`
`+`
`+There's friction proportional to angle speed, for both joints.`
` `
` """`
`-Runs a simulation of a controlled (PD) simple arm, with 2 links and 2 rotary joints.`
`-There's friction proportional to angle speed, for both joints.`
`-"""`
`-`
` from ars.app import Program`
` import ars.utils.mathematical as mut`
` import ars.constants as cts`
` 		return self.sim.get_joint('r2').joint.angle_rate`
` `
` 	def get_compensation(self, sp, q, time_step):`
`-		"""PD controller"""`
`+		"""PD controller."""`
` 		error = (sp - q)`
` 		error_p = (error - self.previous_error) / time_step`
` 		torque = self.R2_KP * error + self.R2_KD * error_p`

# demos/FallingBall.py

` #!/usr/bin/env python`
` `
`-# Created on 2011.12.09`
`-#`
`-# @author: german`
`+"""Runs the simplest simulation ever: a falling ball impacts the floor.`
` `
` """`
`-Runs the simplest simulation ever: a falling ball impacts the floor`
`-"""`
`-`
` from ars.app import Program`
` `
` `

# demos/FallingBalls.py

` #!/usr/bin/env python`
` `
`-# Created on 2011.12.09`
`-#`
`-# @author: german`
`+"""Runs a very simple simulation: three falling balls impact the floor.`
` `
` """`
`-Runs a very simple simulation: three falling balls impact the floor`
`-"""`
`-`
` from ars.app import Program`
` `
` `

# demos/IROS/example1_bouncing_ball.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.03.08`
`-#`
`-# @author: german`
`+"""Example #1. To achieve the same results reported in the paper, the contact`
`+joint bounce parameter (in :func:`ars.model.simulator.collision near_callback`)`
`+must be set to 0.9, instead of the default 0.2 value.`
` `
` """`
`-Example #1. To achieve the same results reported in the paper,`
`-the contact joint bounce parameter (in ars.model.simulator.collision near_callback function)`
`-must be set to 0.9, instead of the default 0.2 value.`
`-"""`
`-`
` from ars.app import Program`
` from ars.lib.pydispatch import dispatcher`
` from ars.model.simulator import signals`

# demos/IROS/example1_bouncing_balls-no_data.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.03.08`
`-#`
`-# @author: german`
`+"""Example #1, with 3 balls (different colors) and no data output.`
` `
` """`
`-Example #1, with 3 balls (different colors) and no data output`
`-"""`
`-`
` from ars.app import Program`
` `
` `

# demos/IROS/example2_conical_pendulum.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.03.09`
`-#`
`-# @author: german`
`+"""Example #2.`
` `
` """`
`-Example #2`
`-"""`
`-`
` from ars.app import Program, Simulation`
` from ars.lib.pydispatch import dispatcher`
` from ars.model.simulator import signals`

# demos/IROS/example3_speed_profile.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.01.11`
`-#`
`-# @author: german`
`+"""Example #3.`
` `
` """`
`-Example #3`
`-"""`
`-`
` import os`
` import sys`
` `
` 	speed_i = 0`
` `
` 	def __init__(self):`
`-		"""Constructor, calls the superclass constructor first"""`
`+		"""Constructor, calls the superclass constructor first."""`
` 		VehicleWithArm.__init__(self)`
` 		self.sim.get_object(self.chassis).actor.set_color((0.8,0,0))`
` `

# demos/IROS/example4_sinusoidal_terrain.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.01.16`
`-#`
`-# @author: german`
`+"""Example #4.`
` `
` """`
`-Example #4`
`-"""`
` import os`
` import sys`
` from random import random`
` `
` `
` def random_heightfield(num_x, num_z, scale=1.0):`
`-	"""A height field where values are completely random"""`
`+	"""A heightfield where values are completely random."""`
` 	# that x and z are integers, not floats, does not matter`
` 	verts=[]`
` 	for x in range(num_x):`
` `
` `
` def sinusoidal_heightfield(num_x, num_z, height_scale=1.0, frequency_x=1.0):`
`-	"""Creates the vertices corresponding to a sinusoidal heightfield along the`
`-	X axis. The height_scale controls the amplitude of the wave, and`
`-	frequency_x its frequency"""`
`+	"""A sinusoidal heightfield along the X axis.`
`+`
`+	:param height_scale: controls the amplitude of the wave`
`+	:param frequency_x: controls the frequency of the wave`
`+`
`+	"""`
` 	# TODO: fix the frequency units`
` 	verts=[]`
` 	for x in range(num_x):`
` `
` `
` def constant_heightfield(num_x, num_z, height=0.0):`
`-	"""A height field where all the values are the same"""`
`+	"""A heightfield where all the values are the same."""`
` 	# that x and z are integers, not floats, does not matter`
` 	verts=[]`
` 	for x in range(num_x):`
` 	R2_KD = 15.0`
` `
` 	def __init__(self):`
`-		"""Constructor, calls the superclass constructor first"""`
`+		"""Constructor, calls the superclass constructor first."""`
` 		VehicleWithArm.__init__(self)`
` 		dispatcher.connect(self.on_pre_step, signals.SIM_PRE_STEP)`
` `
` 			print('Exception when executing on_pre_step: %s' % str(e))`
` `
` 	def get_arm_compensation(self, q1, q2):`
`-		"""Calculate the control torque with a PD controller"""`
`+		"""Calculate the control torque with a PD controller."""`
` 		time_step = self.sim.time_step`
` 		error_q1 = (self.q1_SP - q1)`
` 		error_q2 = (self.q2_SP - q2)`
` 	sim_program = Example4()`
` 	sim_program.start()`
` `
`-#===============================================================================`
`+#==============================================================================`
` # def shrink_XZ_heightfield(vertices, factor=(1.0,1.0)):`
` #	"""`
` #	test`
` #	for vertex in vertices:`
` #		new_vertices.append((vertex[0]/factor[0], vertex[1], vertex[2]/factor[1]))`
` #	return new_vertices`
`-#===============================================================================`
`+#==============================================================================`

# demos/IROS/example4b_sinusoidal_terrain_with_screenshot_recorder.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.01.16`
`-#`
`-# @author: german`
`+"""Example #4 with a screenshot recorder.`
` `
` """`
`-Example #4 with a screenshot recorder`
`-"""`
`+from example4_sinusoidal_terrain import Example4`
` `
`-from example4_sinusoidal_terrain import Example4`
` `
` class Example4SR(Example4):`
` `
` 	RECORD_PERIODICALLY = True`
` `
` 	def __init__(self):`
`-		"""Constructor, calls the superclass constructor first"""`
` 		Example4.__init__(self)`
` 		self.create_screenshot_recorder(`
` 			self.RECORDER_BASE_FILENAME, self.RECORD_PERIODICALLY)`

# demos/IROS/example5_vehicle_with_user_input.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.01.30`
`-#`
`-# @author: german`
`+"""Example #5. To achieve the same results reported in the paper, the contact`
`+joint mu parameter (in :func:`ars.model.simulator.collision near_callback`)`
`+must be set to 50, instead of the default 500 value.`
` `
` """`
`-Example #5. To achieve the same results reported in the paper,`
`-the contact joint mu parameter (in ars.model.simulator.collision near_callback function)`
`-must be set to 50, instead of the default 500 value.`
`-"""`
` import os`
` import sys`
` `
` 	R2_KD = 3.0 # controller derivative action`
` `
` 	def __init__(self, use_capsule_wheels=False, frictionless_arm=True):`
`-		"""Constructor, calls the superclass constructor first"""`
` 		VehicleWithArm.__init__(self, use_capsule_wheels, frictionless_arm)`
` `
` 		self.key_press_functions.add('d', self.increase_sp)`
` 		self.torque_w2 = torque2`
` `
` 	def increase_sp(self):`
`-		"""Increase angle set point"""`
`+		"""Increase angle set point."""`
` 		self.sp += self.SP_STEP`
` `
` 	def decrease_sp(self):`
`-		"""Decrease angle set point"""`
`+		"""Decrease angle set point."""`
` 		self.sp -= self.SP_STEP`
` `
` 	def get_compensation(self, sp, q, time_step):`
`-		"""Calculate the control torque with a PD controller"""`
`+		"""Calculate the control torque with a PD controller."""`
` 		error = (sp - q)`
` 		error_p = (error - self.previous_error) / time_step`
` 		torque = self.R2_KP * error + self.R2_KD * error_p`

# demos/SimpleArm.py

` #!/usr/bin/env python`
` `
`-# Created on 2011.12.01`
`-#`
`-# @author: german`
`+"""Runs a simulation of a simple arm, with 2 links and 2 rotary joints.`
` `
`-"""Runs a simulation of a simple arm, with 2 links and 2 rotary joints"""`
`-`
`+"""`
` from ars.app import Program`
` import ars.utils.mathematical as mut`
` import ars.constants as cts`

# demos/Vehicle1.py

` #!/usr/bin/env python`
` `
`-# Created on 2011.11.16`
`-#`
`-# @author: german`
`+"""Runs a simulation of a simple vehicle.`
` `
`+"""`
` from ars.app import Program`
` `
` `

# demos/Vehicle2.py

` #!/usr/bin/env python`
` `
`-# Created on 2011.11.16`
`-#`
`-# @author: german`
`+"""Runs a simulation of a vehicle with two powered wheels and one`
`+free-rotating spherical wheel.`
` `
` """`
`-Runs a simulation of a vehicle with two powered wheels and one`
`-free-rotating spherical wheel.`
`-"""`
`-`
` from ars.app import Program`
` import ars.constants as cts`
` `
` 	FLOOR_BOX_SIZE = (20,0.01,20)`
` `
` 	def __init__(self):`
`-		"""Constructor, calls the superclass constructor first"""`
`+		"""Constructor, calls the superclass constructor first."""`
` 		Program.__init__(self)`
` 		self.key_press_functions.add('up', self.go_forwards, True)`
` 		self.key_press_functions.add('down', self.go_backwards, True)`
` 		self.key_press_functions.add('right', self.turn_right, True)`
` `
` 	def create_sim_objects(self):`
`-		"""Implementation of the required method. Creates and sets up all the`
`-		objects of the simulation."""`
`+		"""Implementation of the required method.`
` `
`+		Creates and sets up all the objects of the simulation.`
`+`
`+		"""`
` 		offset = self.OFFSET`
` `
` 		wheelR = self.sim.add_cylinder(0.4, 0.3, (0,0,-0.5), density=1) # length, radius, center, density`
` 		#print(self.sim.get_object(wheelR).actor.set_color((0.8,0,0)))`
` `
` 	def go_forwards(self):`
`-		"""Rotate both powered wheels in the same direction, forwards"""`
`+		"""Rotate both powered wheels in the same direction, forwards."""`
` 		self.sim.get_joint('r1').add_torque(self.TORQUE)`
` 		self.sim.get_joint('r2').add_torque(self.TORQUE)`
` `
` 	def go_backwards(self):`
`-		"""Rotate both powered wheels in the same direction, backwards"""`
`+		"""Rotate both powered wheels in the same direction, backwards."""`
` 		self.sim.get_joint('r1').add_torque(-self.TORQUE)`
` 		self.sim.get_joint('r2').add_torque(-self.TORQUE)`
` `
` 	def turn_left(self):`
`-		"""Rotate both powered wheels in different directions, with a global`
`-		counter-clockwise rotation (from above)"""`
`+		"""Rotate vehicle counter-clockwise (from above)."""`
` 		self.sim.get_joint('r1').add_torque(-self.TORQUE)`
` 		self.sim.get_joint('r2').add_torque(self.TORQUE)`
` `
` 	def turn_right(self):`
`-		"""Rotate both powered wheels in different directions, with a global`
`-		clockwise rotation (from above)"""`
`+		"""Rotate vehicle clockwise (from above)."""`
` 		self.sim.get_joint('r1').add_torque(self.TORQUE)`
` 		self.sim.get_joint('r2').add_torque(-self.TORQUE)`
` `

# demos/Vehicle2WithScreenshots.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.02.07`
`-#`
`-# @author: german`
`+"""Runs a simulation of a vehicle with two powered wheels and one`
`+free-rotating spherical wheel.`
` `
`-"""Runs a simulation of a vehicle with two powered wheels and one`
`-free-rotating spherical wheel"""`
`+"""`
`+from Vehicle2 import Vehicle2`
` `
`-from Vehicle2 import Vehicle2`
` `
` class Vehicle2WithScreenshots(Vehicle2):`
` 	FPS = 100 # this is the recording frequency`

# demos/VehicleWithArm.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.01.03`
`-#`
`-# @author: german`
`-`
`-"""`
`-Runs a simulation of a vehicle with two powered wheels and one`
`+"""Runs a simulation of a vehicle with two powered wheels and one`
` free-rotating spherical wheel. It has a 2-link robotic arm attached,`
` with joints either friction-free or with friction proportional to`
` joint speed.`
` 	Q2_FRICTION_COEFF = 0.01`
` `
` 	def __init__(self, use_capsule_wheels=False, frictionless_arm=True):`
`-		"""Constructor, calls the superclass constructor first"""`
`+		"""Constructor, calls the superclass constructor first."""`
` 		self._use_capsule_wheels = use_capsule_wheels`
` 		self._frictionless_arm = frictionless_arm`
` `
` 		dispatcher.connect(self.on_pre_step, signals.SIM_PRE_STEP)`
` `
` 	def create_sim_objects(self):`
`-		"""Implementation of the required method. Creates and sets up all the`
`-		objects of the simulation."""`
`+		"""Implementation of the required method.`
` `
`+		Creates and sets up all the objects of the simulation.`
`+`
`+		"""`
` 		arm_offset = (0,0.5,0)`
` `
` 		#=======================================================================`
` 		self.link2 = link2`
` `
` 	def go_forwards(self):`
`-		"""Rotate both powered wheels in the same direction, forwards"""`
`+		"""Rotate both powered wheels in the same direction, forwards."""`
` 		self.apply_torque_to_wheels(self.WHEEL_TORQUE, self.WHEEL_TORQUE)`
` `
` 	def go_backwards(self):`
`-		"""Rotate both powered wheels in the same direction, backwards"""`
`+		"""Rotate both powered wheels in the same direction, backwards."""`
` 		self.apply_torque_to_wheels(-self.WHEEL_TORQUE, -self.WHEEL_TORQUE)`
` `
` 	def turn_left(self):`
`-		"""Rotate both powered wheels in different directions, with a global`
`-		counter-clockwise rotation (from above)"""`
`+		"""Rotate vehicle counter-clockwise (from above)."""`
` 		self.apply_torque_to_wheels(-self.WHEEL_TORQUE, self.WHEEL_TORQUE)`
` `
` 	def turn_right(self):`
`-		"""Rotate both powered wheels in different directions, with a global`
`-		clockwise rotation (from above)"""`
`+		"""Rotate vehicle clockwise (from above)."""`
` 		self.apply_torque_to_wheels(self.WHEEL_TORQUE, -self.WHEEL_TORQUE)`
` `
` 	def on_pre_step(self):`

# demos/VehicleWithControlledArm.py

` #!/usr/bin/env python`
` `
`-# Created on 2012.01.30`
`-#`
`-# @author: german`
`-`
`-"""`
`-Runs a simulation of a vehicle with two powered wheels and one`
`+"""Runs a simulation of a vehicle with two powered wheels and one`
` free-rotating spherical wheel. It has a 2-link robotic arm attached,`
` with joints either friction-free or with friction proportional to`
` joint speed. The second joint has a PD controller.`
` 	R2_KD = 0.5 # controller derivative action`
` `
` 	def __init__(self, use_capsule_wheels=False, frictionless_arm=True):`
`-		"""Constructor, calls the superclass constructor first"""`
`+		"""Constructor, calls the superclass constructor first."""`
` 		VehicleWithArm.__init__(self, use_capsule_wheels, frictionless_arm)`
` `
` 		self.key_press_functions.add('d', self.increase_sp)`
` 			print('Exception when executing on_pre_step: %s' % str(e))`
` `
` 	def increase_sp(self):`
`-		"""Increase angle set point"""`
`+		"""Increase angle set point."""`
` 		self.sp += self.SP_STEP`
` `
` 	def decrease_sp(self):`
`-		"""Decrease angle set point"""`
`+		"""Decrease angle set point."""`
` 		self.sp -= self.SP_STEP`
` `
` 	def get_q2(self):`
`-		"""Get the current angle of the second rotary joint"""`
`+		"""Get the current angle of the second rotary joint."""`
` 		return self.sim.get_joint('r2').joint.angle`
` `
` 	def get_compensation(self, sp, q, time_step):`
`-		"""Calculate the control torque with a PD controller"""`
`+		"""Calculate the control torque with a PD controller."""`
` 		error = (sp - q)`
` 		error_p = (error - self.previous_error) / time_step`
` 		torque = self.R2_KP * error + self.R2_KD * error_p`