SunFounder PiDog Kit, Release 1.0
2. Calculate the lean angle of PiDog’s body.
from pidog import Pidog
import time
import math
my_dog = Pidog()
while True:
ax, ay, az = my_dog.accData
body_pitch = math.atan2(ay,ax)/math.pi
*
180%360-180
print(f"Body Degree: {body_pitch:.2f} °" )
time.sleep(0.2)
my_dog.close()
3. While leaning, PiDog keeps its eyes level.
from pidog import Pidog
import time
import math
my_dog = Pidog()
while True:
ax, ay, az = my_dog.accData
body_pitch = math.atan2(ay,ax)/math.pi
*
180%360-180
my_dog.head_move([[0, 0, 0]], pitch_comp=-body_pitch, speed=80)
time.sleep(0.2)
my_dog.close()
2.3.11 Sound Direction Detect
The PiDog has a Sound Direction Sensor Module that detects where sound is coming from, and we can trigger it by
clapping near it.
Using this module is as simple as calling these functions.
Pidog.ears.isdetected()
Returns True if sound is detected, False otherwise.
Pidog.ears.read()
This function returns the direction of the sound source, with a range of 0 to 359; if the sound comes from the front, it
returns 0; if it comes from the right, it returns 90.
An example of how to use this module is as follows:
from pidog import Pidog
my_dog = Pidog()
while True:
if my_dog.ears.isdetected():
(continues on next page)
86 Chapter 2. Play with Python