Hi. I am getting negative numbers using two ultrasonic sensors. My code is below. The single sensor supplied lets the Pcar-x hit a wall at an angle. Using two sensors would avoid that issue.
But I get negative numbers with two verses one. IS there enough power supplied by the Hat for two? Or is my code wrong?
distance1: -1 - distance2: 127
distance1: -1 - distance2: 89
distance1: -1 - distance2: -2
distance1: -1 - distance2: -2
distance1: -1 - distance2: 89
distance1: -1 - distance2: -2
distance1: -1 - distance2: -2
distance1: -1 - distance2: 90
distance1: -1 - distance2: 93
distance1: -1 - distance2: -2
distance1: -1 - distance2: 85
distance1: -1 - distance2: 93
distance1: -1 - distance2: 83
from picarx import Picarx
import time
from robot_hat import Pin, Ultrasonic
POWER = 0 # set forward speed here but set to zero for testing
SafeDistance = 50 # > 40 safe I set this to 50 for testing
DangerDistance = 20 # > 20 && < 40 turn around,
# < 20 backward
tring = ‘D0’
echo = ‘D1’
def main():
try:
#px = Picarx()
rh = Ultrasonic(Pin(tring),Pin(echo)) # tring - yello - , echo - white
px = Picarx(ultrasonic_pins=[‘D2’,‘D3’]) # tring, echo
while True:
distance1 = rh
distance1 = (rh.read())
distance2 = round(px.ultrasonic.read())
print("distance1: ",distance1, " - " " distance2: ",distance2)
time.sleep(0.5)
finally:
px.forward(0)
if name == “main”:
main()