if a port is provided, use it otherwise assume it's I2C if a dexterind distance sensor is detected on that port, use it.
(regObj)
| 618 | |
| 619 | |
| 620 | def handle_distance(regObj): |
| 621 | ''' |
| 622 | if a port is provided, use it otherwise assume it's I2C |
| 623 | if a dexterind distance sensor is detected on that port, use it. |
| 624 | |
| 625 | ''' |
| 626 | distance_sensor_output_str = "Distance Sensor" |
| 627 | def read_ultrasonic(port="AD1"): |
| 628 | UltraSonicSensor = get_sensor_instance(port, easysensors.UltraSonicSensor) |
| 629 | print("reading from ultrasonic sensor on port {}".format(port)) |
| 630 | distance = UltraSonicSensor.read() |
| 631 | if distance == 0: |
| 632 | known_sensors[port] = None |
| 633 | sensors["{}: distance".format(port)] = distance |
| 634 | if regObj.group(SENSOR_DISTANCE_PORT2): |
| 635 | port = "AD2" |
| 636 | elif regObj.group(SENSOR_DISTANCE_PORT1): |
| 637 | port = "AD1" |
| 638 | else: |
| 639 | port = "" |
| 640 | # print("using {} ".format(port)) |
| 641 | sensors = {} # default sensors |
| 642 | global distance_sensor |
| 643 | # if no port was supplied, assume a distance sensor on I2C |
| 644 | # we no longer support the ultrasonic sensor on AD1 without it being specified |
| 645 | if port == "" or port == "I2C": |
| 646 | try: |
| 647 | if distance_sensor == None: |
| 648 | # we didn't find a distance sensor on this port |
| 649 | # but maybe one got added |
| 650 | # print("checking if one just got added") |
| 651 | distance_sensor = gpg.init_distance_sensor() |
| 652 | logger.debug("taking reading") |
| 653 | distance = distance_sensor.read() |
| 654 | logger.info("reading done: {}".format(distance)) |
| 655 | # a value of 0 means we couldn't read the distance sensor |
| 656 | if distance == 0: |
| 657 | distance_sensor = None |
| 658 | sensors[distance_sensor_output_str] = 0 |
| 659 | else: |
| 660 | sensors[distance_sensor_output_str] = distance |
| 661 | except Exception as e: |
| 662 | # the read failed, distance sensor probably disconnected |
| 663 | print(e) |
| 664 | distance_sensor = None |
| 665 | sensors[distance_sensor_output_str] = 0 |
| 666 | if port == "AD1" or port == "AD2": |
| 667 | try: |
| 668 | sensor = get_sensor_instance(port, easy_distance_sensor.EasyDistanceSensor) |
| 669 | distance = sensor.read() |
| 670 | print("distance from di sensor {}".format(distance)) |
| 671 | if distance == 0: |
| 672 | # this implies we couldn't read the distance sensor, |
| 673 | # possibly disconnected |
| 674 | known_sensors[port] = None |
| 675 | sensors["{}: {}".format(port, distance_sensor_output_str)] = distance |
| 676 | except Exception as e: |
| 677 | # couldn't detect a dexterind distance sensor. |
no test coverage detected