https://www.jianshu.com/p/f98a69b94deb
1. Jetson.GPIO 库
/opt/nvidia/jetson-gpio
运行Jetson.GPIO前需运行以下步骤,建立GPIO 运行环境
步骤一 – 设置运行权限Setup permissions
1 2 3 4 5 6 7 |
sudo groupadd -f -r gpio sudo usermod -a -G gpio jetbot #$USER sudo cp /opt/nvidia/jetson-gpio/etc/99-gpio.rules /etc/udev/rules.d/ sudo udevadm control --reload-rules && sudo udevadm trigger sudo reboot now |
步骤二 – Python 代码引用
添加如下Python 代码后,才可以调用 import the RPi.GPIO (或者 motorhat library等)
1 2 3 4 |
import sys sys.path.append('/opt/nvidia/jetson-gpio/lib/python/') sys.path.append('/opt/nvidia/jetson-gpio/lib/python/Jetson/GPIO') |
或
步骤三:Python GPIO 案例
GPIO Mode
1 2 3 4 5 6 7 8 9 |
GPIO Mode GPIO.setmode(GPIO.BOARD) # or GPIO.setmode(GPIO.BCM) # or GPIO.setmode(GPIO.CVM) # or GPIO.setmode(GPIO.TEGRA_SOC) |
如下已验证代码案例(测量J41 PIN13,每隔2s 置高置低即0/1/0/1)
#!/usr/bin/env python
#BCM 是Broadcom SOC GPIO序列号,此处需修改为BOARD#即Nano载板的40Pin GPIO序列号,参考上图<Nano_J41_40Pin.png>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#import jetson gpio import sys sys.path.append('/opt/nvidia/jetson-gpio/lib/python/') sys.path.append('/opt/nvidia/jetson-gpio/lib/python/Jetson/GPIO') import Jetson.GPIO as GPIO import time # Pin Definitions output_pin = 13 #J41_BOARD_PIN13---gpio14/GPIO.B06/SPI2_SCK def main(): # Pin Setup: # Board pin-numbering scheme GPIO.setmode(GPIO.BOARD) # set pin as an output pin with optional initial state of HIGH GPIO.setup(output_pin, GPIO.OUT, initial=GPIO.HIGH) print("Starting demo now! Press CTRL+C to exit") curr_value = GPIO.HIGH try: while True: time.sleep(2) # Toggle the output every second print("Outputting {} to pin {}".format(curr_value, output_pin)) GPIO.output(output_pin, curr_value) curr_value ^= GPIO.HIGH finally: GPIO.cleanup() if __name__ == '__main__': main() |
2. 超声波HC-SR04 应用案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#!/usr/bin/env python #BCM 是Broadcom SOC GPIO序列号,此处需修改为BOARD #即Nano载板的40Pin GPIO序列号,参考上图<Nano_J41_40Pin.png> import Jetson.GPIO as GPIO import time # Pin Definitions trig_output_pin = 13 #发射PIN,J41_BOARD_PIN13---gpio14/GPIO.B06/SPI2_SCK echo_input_pin = 18 #接收PIN,J41_BOARD_PIN18---gpio15/GPIO.B07/SPI2_CS0 def main(): # Pin Setup: # Board pin-numbering scheme GPIO.setmode(GPIO.BOARD) # set pin as an output pin with optional initial state of LOW GPIO.setup(trig_output_pin, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(echo_input_pin, GPIO.IN) #value = GPIO.input(echo_input_pin) #print("Value read from pin {} : {}".format(echo_input_pin,value_str)) print("Starting Measure now! Press CTRL+C to exit") try: while True: # Toggle the output every second GPIO.output(trig_output_pin, GPIO.HIGH) time.sleep(0.00001) GPIO.output(trig_output_pin, GPIO.LOW) pulse_start = time.time() while GPIO.input(echo_input_pin)==0: pulse_start = time.time() pulse_end = time.time() while GPIO.input(echo_input_pin)==1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 distance = round(distance, 2) print ("Distance" , distance) finally: GPIO.cleanup() if __name__ == '__main__': main()<code class=" language-python"> |
3. Jetson Nano GPIO一些说明:
Jetson.GPIO Library doesn’t support PWM**
(不支持PWM,如需要PWM需外接一个I2C转PWM信号的板子)
However, the following I2C controlled board can generate PWM signals up to ~1.6kHz and should have Python libraries available.
https://www.adafruit.com/product/815
You could potentially connect this to whatever H-Bridge meets the power requirements of your application. The motor driver we used for JetBot actually combines this chip and multiple H-bridge on the same board.
https://www.adafruit.com/product/2927
PS:
NVIDIA Forum:
Nano GPIO Python support
https://pypi.org/project/Jetson.GPIO/
Jetson Nano GPIO 代码目录及部分说明
GPIO DTS:
hardware/nvidia/platform/t210/porg/kernel-dts/porg-platforms/tegra210-porg-gpio-p3448-0002-a02.dtsi
P37-gpio12-GPIO.B04
P22-gpio13-GPIO.B05
P13-gpio14-GPIO.B06
P18-gpio15-GPIO.B07
VDD-USB-HUB-EN gpio151-GPIO.S07
IMX219 DTS:
hardware\nvidia\platform\t210\porg\kernel-dts\porg-platforms\tegra210-porg-camera-rbpcv2-imx219.dtsi