|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# https://www.dexterindustries.com/GoPiGo/ |
| 4 | +# https://github.com/DexterInd/GoPiGo3 |
| 5 | +# |
| 6 | +# Copyright (c) 2017 Dexter Industries |
| 7 | +# Released under the MIT license (http://choosealicense.com/licenses/mit/). |
| 8 | +# For more information see https://github.com/DexterInd/GoPiGo3/blob/master/LICENSE.md |
| 9 | +# |
| 10 | +# This code is an example for using the grove ultrasonic sensor with the GoPiGo3. |
| 11 | +# |
| 12 | +# Hardware: Connect a grove ultrasonic sensor to port AD1 and another to port AD2 of the GoPiGo3. |
| 13 | +# |
| 14 | +# Results: When you run this program, the distance measured with the ultrasonic sensors should be printed. |
| 15 | + |
| 16 | +from __future__ import print_function # use python 3 syntax but make it compatible with python 2 |
| 17 | +from __future__ import division # '' |
| 18 | + |
| 19 | +import time # import the time library for the sleep function |
| 20 | +import gopigo3 # import the GoPiGo3 drivers |
| 21 | + |
| 22 | +GPG = gopigo3.GoPiGo3() # Create an instance of the GoPiGo3 class. GPG will be the GoPiGo3 object. |
| 23 | + |
| 24 | +sensor1 = 0 |
| 25 | +sensor2 = 0 |
| 26 | + |
| 27 | +try: |
| 28 | + # configure both ultrasonic sensors |
| 29 | + GPG.set_grove_type(GPG.GROVE_1, GPG.GROVE_TYPE.US) |
| 30 | + GPG.set_grove_type(GPG.GROVE_2, GPG.GROVE_TYPE.US) |
| 31 | + |
| 32 | + # wait for the sensors to get configured |
| 33 | + time.sleep(0.05) |
| 34 | + |
| 35 | + while(True): |
| 36 | + # try reading sensor 1 |
| 37 | + try: |
| 38 | + sensor1 = GPG.get_grove_value(GPG.GROVE_1) |
| 39 | + except gopigo3.SensorError as error: |
| 40 | + pass |
| 41 | + |
| 42 | + # try reading sensor 2 |
| 43 | + try: |
| 44 | + sensor2 = GPG.get_grove_value(GPG.GROVE_2) |
| 45 | + except gopigo3.SensorError as error: |
| 46 | + pass |
| 47 | + |
| 48 | + # print the sensor values |
| 49 | + print("Sensor 1: %4dmm Sensor 2: %4dmm" % (sensor1, sensor2)) |
| 50 | + |
| 51 | + # slow down a little |
| 52 | + time.sleep(0.05) |
| 53 | + |
| 54 | +except KeyboardInterrupt: # except the program gets interrupted by Ctrl+C on the keyboard. |
| 55 | + GPG.reset_all() # Unconfigure the sensors, disable the motors, and restore the LED to the control of the GoPiGo3 firmware. |
| 56 | + |
| 57 | +except Exception, e: |
| 58 | + print(e) |
| 59 | + GPG.reset_all() |
0 commit comments