[PYTHON] I tried to control multiple servo motors MG996R using the servo driver PCA9685.

Hardware preparation

What to prepare

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

circuit diagram

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

servo_test.png

top.jpg

detail.jpg

Software preparation

Library installation

Install various libraries by referring to this site.

Connection confirmation

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 -- -- -- -- -- -- --

Motor control program

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)

Move the servo

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

Couldn't find video ...

bonus

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. Couldn't find video ...

reference

  1. https://kokensha.xyz/jetson/jetson-nano-pca9685-i2c-error-resolution/

Recommended Posts

I tried to control multiple servo motors MG996R using the servo driver PCA9685.
I tried to approximate the sin function using chainer
I tried to identify the language using CNN + Melspectogram
I tried to complement the knowledge graph using OpenKE
I tried to compress the image using machine learning
I tried to simulate ad optimization using the bandit algorithm.
I tried using "Syncthing" to synchronize files on multiple PCs
[TF] I tried to visualize the learning result using Tensorboard
I tried to approximate the sin function using chainer (re-challenge)
I tried to output the access log to the server using Node.js
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
I tried to move the ball
I tried using the checkio API
I tried to estimate the interval.
I tried to digitize the stamp stamped on paper using OpenCV
I became horror when I tried to detect the features of anime faces using PCA and NMF.
I tried using Azure Speech to Text.
I tried to recognize the wake word
I tried to classify text using TensorFlow
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried using the BigQuery Storage API
I tried to predict Covid-19 using Darts
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to execute SQL from the local environment using Looker SDK
I tried to get the batting results of Hachinai using image processing
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I tried to summarize various sentences using the automatic summarization API "summpy"
I tried to extract and illustrate the stage of the story using COTOHA
I tried the common story of using Deep Learning to predict the Nikkei 225
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried to analyze the New Year's card by myself using python
I tried to control the network bandwidth and delay with the tc command
I tried to predict the deterioration of the lithium ion battery using the Qore SDK
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
I tried using scrapy for the first time
vprof --I tried using the profiler for Python
[Python] I tried to judge the member image of the idol group using Keras
I tried to optimize while drying the laundry
I tried to save the data with discord
I tried to synthesize WAV files using Pydub.
I tried using the Python library "pykakasi" that can convert kanji to romaji.
I tried to explain multiple regression analysis as easily as possible using concrete examples.
I tried using PyCaret at the fastest speed
I tried using the Google Cloud Vision API
I tried to touch the API of ebay
I tried to correct the keystone of the image
I tried using the Datetime module by Python
Qiita Job I tried to analyze the job offer
I tried using the image filter of OpenCV
LeetCode I tried to summarize the simple ones
I tried using the functional programming library toolz
[Introduction to PID] I tried to control and play ♬
I tried to implement the traveling salesman problem
I tried to make a ○ ✕ game using TensorFlow
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to predict the victory or defeat of the Premier League using the Qore SDK
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
Python practice 100 knocks I tried to visualize the decision tree of Chapter 5 using graphviz