Prepare multiple servo motors that you want to move. Here, the goal is to move 12 MG996Rs, but I think the same applies to other servo motors and numbers. The Jetson Nano alone can control only a few servo motors at most. Therefore, the servo driver PCA9685 is used. Since the PCA9685 has 16 channels, it can control up to 16 servo motors. I want to run 12 servo motors here, so one is enough.
device | Quantity | Description |
---|---|---|
Jetson Nano | 1 | |
PCA9685 | 1 | Servo driver |
MG996R | 12 | Servomotor |
AC adapter 5V-4A | 2 | Power supply for Jetson Nano and PCA9685 |
Jumper wire female female | 4 | Connect Jetson Nano and PCA9685 |
The Jetson Nano GPIO pin layout looks like this: Connect as follows. Both Jetson Nano and PCA9685 external power supplies use 5V 4A.
Jetson Nano GPIO pin number | <---> | PCA9685 |
---|---|---|
1 | <---> | VCC |
3 | <---> | SDA |
5 | <---> | SCL |
9 | <---> | GND |
Install various libraries by referring to this site.
After the installation is complete, you can check the connection with the following command.
$ sudo i2cdetect -y -r 1
If connected correctly, you should see something like this:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --
joint_control.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Adafruit_PCA9685
import time
# Initialise the PCA9685 using desired address and/or bus:
pwm = Adafruit_PCA9685.PCA9685(address = 0x40, busnum = 1)
# Number of servo
servo_num = 12
# Configure min and max servo pulse lengths
servo_min = 150 # min. pulse length
servo_max = 600 # max. pulse length
servo_offset = 50
# Set frequency to 60[Hz]
pwm.set_pwm_freq(60)
while True:
# Move servo on each channel
for i in range(servo_num):
print('Moving servo on channel: ', i)
pwm.set_pwm(i, 0, servo_min + servo_offset)
time.sleep(1)
for i in range(servo_num):
print('Moving servo on channel: ', i)
pwm.set_pwm(i, 0, servo_max - servo_offset)
time.sleep(1)
# Move servo on all channel
for i in range(servo_num):
print('Moving servo on channel: ', i)
pwm.set_pwm(i, 0, servo_min + servo_offset)
time.sleep(1)
for i in range(servo_num):
print('Moving servo on channel: ', i)
pwm.set_pwm(i, 0, servo_max - servo_offset)
time.sleep(1)
When you're ready, let's move on! If you give execute permission and then execute it, the servo should work.
$ chmod +x joint_control.py
$ python3 joint_control.py
The operation and voltage of the external power supply when 5V-2A is supplied instead of 5V-4A as the external power supply of PCA9685 are as follows. It is enough to move it by itself, but if you move more than one, the current will be insufficient and the voltage will drop, so it will not work well. Pay attention to the rated current.
Recommended Posts