Here we introduce another ship term: heel. Heel is the amount of lean a ship is experiencing. It is very similar to roll with the main difference being that heel is usually a sustained angle (an ongoing, constant roll). Boats can be referred to as “heeling” or “heeling over” (frequently misstated as “keeling over”). We can think of starting_position
as our initial heel angle and see if our subsequent movement
values deviate enough from it to merit moving our whale.
In this example, we used 3 as our threshold, but this is up to you. If the change in our heel angle from our starting value is more than 3, the whale rolls up onto port or starboard. If our heel angle is within 3 of our starting value in either direction, the whale sits flat. Because the direction code is written before this code, the whale should only move in the direction it is facing.
#### Rotation sensors
orientation = sense.get_orientation_degrees()
roll = orientation["roll"]
pitch = orientation["pitch"]
yaw = orientation["yaw"]
if pitch > 180:
pitch = pitch - 360
# Starting orientation values
starting_position = roll * 0.1 + pitch * 0.6 + yaw * 0.3
movement = starting_position
direction = "starboard"
while True:
# Poll the sensors at the start of the loop
orientation = sense.get_orientation_degrees()
roll = orientation["roll"]
pitch = orientation["pitch"]
yaw = orientation["yaw"]
if pitch > 180:
pitch = pitch - 360
# Get a fresh orientation reading for this lap
new_movement = roll * 0.1 + pitch * 0.6 + yaw * 0.3
# No direction change or movement if sensor difference is too small
if abs(movement-new_movement) < 1:
continue
# Change creature direction to port
if new_movement < movement and direction == "starboard":
sense.flip_h()
direction = "port"
# Change creature direction to starboard
if new_movement > movement and direction == "port":
sense.flip_h()
direction = "starboard"
# Compare current orientation to our pre-loop starting orientation
relative_heel = starting_position - new_movement
# Rotate our creature
if relative_heel > 3:
sense.set_rotation(270)
if relative_heel < -3:
sense.set_rotation(90)
if -3 <= relative_heel <= 3:
print("Center!")
sense.set_rotation(0)
# Store this lap's movement value for comparison on the next iteration of the loop
movement = new_movement
# Merely for observing our sensor data for debugging
print(movement)
You did it! Your creature is now moving based on the motion of Wonder! The next step is to bring our color sensor code back into the picture.