首先开启 I2C
因为默认 I2C 是关闭,需要通过下列步骤开启 I2C
打开 /etc/modprobe.d/blacklist.conf 文件,注释掉 blacklist i2c_i801:
# lacklist i2c_i801
打开 /etc/modules 文件,在最后面加入以下代码:
i2c-dev
sudo reboot 重启jetson nano
方法一(这个方法用python3.7以下会报错):
1.导入smbus模块:sudo apt-get install python-smbus
2.导入i2c库:sudo apt-get install i2c-tools
3.导入adafruit库: sudo pip3 install adafruit-circuitpython-servokit(这个有时候装不上,我装了好多次才行,不会装的可以用方法二)
4.硬件接线:
SCL ——接Nano 引脚第5脚(GEN2_I2C_SCL)
SDA ——接Nano 引脚第3脚(GEN2_I2C_SDA)
nano与驱动板接好电源,电机与驱动板的线也接好.
查看i2c设备:
sudo i2cdetect -y -r 1
5.python程序:
import time #引入time库
from adafruit_servokit import ServoKit #引入PCA9685库
kit = ServoKit(channels=16) #明确PCA9685的舵机控制数
kit.servo[0].angle = 24 #通道0上的舵机正转。
time.sleep(1) #休眠一秒
kit.servo[8].angle = 24 #通道8上的舵机正转。
方法二:
sudo apt-get install python-smbus 装一下库
直接在记事本里面写入以下代码,然后用python打开
import time
import math
import smbus
pwm=PCA9685(0x40)
pwm.setPWMFreq(50)
class PCA9685:
# Registers/etc.
__SUBADR1 = 0x02
__SUBADR2 = 0x03
__SUBADR3 = 0x04
__MODE1 = 0x00
__PRESCALE = 0xFE
__LED0_ON_L = 0x06
__LED0_ON_H = 0x07
__LED0_OFF_L = 0x08
__LED0_OFF_H = 0x09
__ALLLED_ON_L = 0xFA
__ALLLED_ON_H = 0xFB
__ALLLED_OFF_L = 0xFC
__ALLLED_OFF_H = 0xFD
def __init__(self, address=0x40, debug=False):
self.bus = smbus.SMBus(1)
self.address = address
self.debug = debug
if (self.debug):
print("Reseting PCA9685")
self.write(self.__MODE1, 0x00)
def write(self, reg, value):
"Writes an 8-bit value to the specified register/address"
self.bus.write_byte_data(self.address, reg, value)
if (self.debug):
# print("I2C: Write 0x%02X to register 0x%02X" % (value, reg))
pass
def read(self, reg):
"Read an unsigned byte from the I2C device"
result = self.bus.read_byte_data(self.address, reg)
if (self.debug):
print("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg))
return result
def setPWMFreq(self, freq):
"Sets the PWM frequency"
prescaleval = 25000000.0 # 25MHz
prescaleval /= 4096.0 # 12-bit
prescaleval /= float(freq)
prescaleval -= 1.0
if (self.debug):
print("Setting PWM frequency to %d Hz" % freq)
print("Estimated pre-scale: %d" % prescaleval)
prescale = math.floor(prescaleval + 0.5)
if (self.debug):
print("Final pre-scale: %d" % prescale)
oldmode = self.read(self.__MODE1)
newmode = (oldmode & 0x7F) | 0x10 # sleep
self.write(self.__MODE1, newmode) # go to sleep
self.write(self.__PRESCALE, int(math.floor(prescale)))
self.write(self.__MODE1, oldmode)
time.sleep(0.005)
self.write(self.__MODE1, oldmode | 0x80)
def setPWM(self, channel, on, off):
"Sets a single PWM channel"
self.write(self.__LED0_ON_L + 4 * channel, on & 0xFF)
self.write(self.__LED0_ON_H + 4 * channel, on >> 8)
self.write(self.__LED0_OFF_L + 4 * channel, off & 0xFF)
self.write(self.__LED0_OFF_H + 4 * channel, off >> 8)
if (self.debug):
# print("channel: %d LED_ON: %d LED_OFF: %d" % (channel, on, off))
pass
def setServoPulse(self, channel, pulse):
"Sets the Servo Pulse,The PWM frequency must be 50HZ"
pulse = int(pulse * 4096 / 20000) # PWM frequency is 50HZ,the period is 20000us
self.setPWM(channel, 0, pulse)
def setMotoPluse(self, channel, pulse):
if pulse > 3000:
self.setPWM(channel, 0, 3000)
else:
self.setPWM(channel, 0, pulse)
if __name__ == '__main__':
pwm = PCA9685(0x40, debug=True)
pwm.setPWMFreq(50)
print("start the control")
while True:
for i in range(500, 2500, 10):
pwm.setServoPulse(8, i)
pwm.setServoPulse(3, i) //控制通道3和通道8的电机
time.sleep(0.02)
感谢这几篇文章的分享:
Dofbot Jetson nano】学习过程分享3
Jetson nano使用PCA9685驱动舵机
Jetson nano驱动舵机(非常简单!)