The PWM sample script that comes with the Jetson.GPIO Python library has finally worked, so make a note of it. First is the video. <img width = "50%" alt = "Screenshot 2020-01-13 14.41.50.png " src = "https://qiita-image" -store.s3.ap-northeast-1.amazonaws.com/0/72479/f86ca2dc-6388-10e9-8586-a338a3766792.png ">
The other five sample scripts were written in the book "Jetson Nano Super Primer".
simple_pwm.py The library's README.md only says "See simple_pwm.py for details on how to use PWM channels", which was difficult to understand compared to other sample scripts.
"The Jetson.GPIO library only supports PWM on the pin to which the hardware PWM controller is connected. Unlike the RPi.GPIO library, the Jetson.GPIO library does not implement software emulated PWM. Jetson Nano Supports two hardware PWM channels. The system pinmux must be configured to connect a hardware PWM controller to the associated pin. If pinmux is not configured, the PWM signal will reach the pin. No. The Jetson.GPIO library does not dynamically change the pinmux configuration to achieve this. Read the L4T documentation for more information on how to configure pinmux. "
The hurdles such as pinmux and L4T (= Linux for Tegra) documents were high, but I finally caught up with my understanding and was able to move it safely. First of all, I was able to run the sample script as long as I had the correct pinmux settings. The Jetson-IO tool added in JetPack 4.3 (r32.3.1) made it easier to set up Pinmux.
(reference) I set up a Pinmux table using the Jetson-IO tool added in JetPack 4.3 (r32.3.1). I tried using two hardware PWMs from Jetson Nano.
First, understand the contents of the included sample script.
--For Jetson Nano, use pin 33 for PWM output. --Set the PWM cycle (cycle) to 20 ms (50 Hz). --Set the duty ratio (pulse width divided by period) to 25%.
simple_pwm.py
import RPi.GPIO as GPIO
import time
output_pins = {
'JETSON_XAVIER': 18,
'JETSON_NANO': 33,
}
output_pin = output_pins.get(GPIO.model, None)
if output_pin is None:
raise Exception('PWM not supported on this board')
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)
p = GPIO.PWM(output_pin, 50)
p.start(25)
print("PWM running. Press CTRL+C to exit.")
try:
while True:
time.sleep(1)
finally:
p.stop()
GPIO.cleanup()
if __name__ == '__main__':
main()
The script needs to be modified because it uses two servo motors (Geekservo 9g 360 ° servo motors).
--Pin setting Change to 2 (32 pins, 33 pins) --PWM cycle 20 ms (50 Hz) → No change --Control pulse 0.5 ms ~ 2.4 ms → Change the duty ratio to 2.5% (rotate in the -90 ° direction) ~ 7.25% (0 ° = rest) ~ 12% (rotate in the + 90 ° direction)
(Modified script)
simple_pwm_servo.py
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
output_pin1 = 32
output_pin2 = 33
def main():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(output_pin1, GPIO.OUT, initial=GPIO.HIGH)
p1 = GPIO.PWM(output_pin1, 50)
GPIO.setup(output_pin2, GPIO.OUT, initial=GPIO.HIGH)
p2 = GPIO.PWM(output_pin2, 50)
print("PWM running. Press CTRL+C to exit.")
try:
while True:
p1.start(2.5)
print("p1 start at 2.5%")
time.sleep(1)
p2.start(2.5)
print("p2 start at 2.5%")
time.sleep(1)
p1.start(7.25)
print("p1 start at 7.25%")
time.sleep(1)
p2.start(7.25)
print("p2 start at 7.25%")
time.sleep(1)
p1.start(12)
print("p1 start at 12%")
time.sleep(1)
p2.start(12)
print("p2 start at 12%")
time.sleep(1)
p1.start(7.25)
print("p1 start at 7.25%")
time.sleep(1)
p2.start(7.25)
print("p2 start at 7.25%")
time.sleep(1)
finally:
p1.stop()
p2.stop()
GPIO.cleanup()
if __name__ == '__main__':
main()
(Execution result)
$ python3 simple_pwm_servo.py
PWM running. Press CTRL+C to exit.
p1 start at 2.5%
p2 start at 2.5%
p1 start at 7.25%
p2 start at 7.25%
p1 start at 12%
p2 start at 12%
p1 start at 7.25%
p2 start at 7.25%
...
Recommended Posts