شِفر (کود) جستجو

کنترل 16 موتور سروو با استفاده از ماجیول PCA9685 و شِفر (کود) آردوینو V2 نمونه #1: یکی یکی

کنترل 16 موتور سروو با استفاده از ماجیول PCA9685 و شِفر (کود) آردوینو V2 نمونه #1: یکی یکی

در این آموزش، یاد خواهیم گرفت که چگونه تا ۱۶ موتور سروو را با استفاده از ماجیول PCA9685 و یک آردوینو کنترل کنیم. PCA9685 یک کنترلر PWM با ۱۶ کانال و ۱۲ بیت است که اجازه کنترل دقیق موتورهای سروو را می‌دهد. با دنبال کردن این راهنما، قادر خواهید بود هر موتور سروو را به‌طور جداگانه کنترل کرده و آن‌ها را به زوایای خاصی تنظیم کنید و به این ترتیب به حرکات متنوع رباتی دست یابید.

ما با بحث در مورد اجزای سخت‌افزاری که برای این پروژه نیاز دارید شروع خواهیم کرد، سپس به دستورالعمل‌های سیم‌کشی دقیق خواهیم پرداخت. پس از آن، به بررسی مرحله به مرحله شِفر (کود) خواهیم پرداخت و شناسه‌های کلیدی و نقش‌های آن‌ها در کنترل موتورهای سرو را برجسته خواهیم کرد. برای درک بهتر، ممکن است بخواهید به ویدئوی همراه (در ویدیو در ۰۰:۰۰) مراجعه کنید.

توضیح سخت‌افزار

جزء اصلی در این پروژه ماجیول PCA9685 است که مسئول تولید سیگنال‌های PWM برای کنترل سروها می‌باشد. هر سرو به یکی از 16 کانال در PCA9685 متصل است که کنترل مستقل را فراهم می‌کند. این ماجیول با استفاده از پروتکل I2C با آردوینو ارتباط برقرار می‌کند و فقط به دو سیم نیاز دارد: SDA و SCL.

علاوه بر PCA9685، به یک بورد Arduino، 16 موتور سروو و یک منبع تغذیه خارجی نیاز دارید. منبع تغذیه خارجی بسیار مهم است زیرا خود Arduino ممکن است قادر به تأمین جریان کافی برای روشن کردن همه سرووها به طور همزمان نباشد. هر سروو معمولاً با ولتاژ 5V کار می‌کند، بنابراین مطمئن شوید که منبع تغذیه‌تان این نیاز را برآورده می‌کند.

جزئیات برگه داده

سازندهNXP نیمه‌رساناها
شماره قطعهPCA9685
ولتاژ منطقی/ورودی-خروجی۲.۳ - ۵.۵ ولت
ولتاژ تغذیه۵ ولت
جریان خروجی (برای هر کانال)۲۵ میلی‌آمپر
راهنمایی فرکانس PWM۶۰ هرتز
آستانه‌های منطقی ورودی0.3VCC (پایین) / 0.7VCC (بالا)
افت ولتاژ / RDS(on)/ اشباع-
محدودیت‌های حرارتیولد
بستهTSSOP-28 / VQFN-28
یادداشت‌ها / واریانس‌ها-
  • یک منبع تغذیه خارجی 5 ولت برای سروها متصل کنید.
  • از I2C برای ارتباط استفاده کنید و SDA را به A4 و SCL را به A5 در آردوینو متصل کنید.
  • اطمینان حاصل کنید که تمام زمین‌ها بین آردوینو و PCA9685 مشترک باشند.
  • عرض پهنای پالس‌ها را براساس سروهای خاص استفاده‌شده تنظیم کنید.
  • در صورت تأمین برق برای چندین سرو همزمان، خنک‌سازی مناسب برای PCA9685 را حفظ کنید.

دستورالعمل‌های سیم‌کشی

برای اتصال ماجیول PCA9685 به آردوینو و موتورهای سرو، ابتدا منبع تغذیه خارجی 5 ولت را به پایه V+ ماجیول PCA9685 وصل کنید. زمین منبع تغذیه را به پایه زمین PCA9685 و همچنین به زمین آردوینو وصل کنید.

سپس پایه‌های SDA و SCL را از PCA9685 به پایه‌های مربوطه روی آردوینو متصل کنید (SDA به A4 و SCL به A5). هر موتور سرو دارای سه سیم خواهد بود: زمین (معمولاً سیاه یا قهوه‌ای)، VCC (عموماً قرمز) و سیگنال (اغلب زرد یا سفید). سیم زمین هر سرو را به پایه زمین PCA9685، سیم VCC را به پایه V+ و سیم سیگنال را به کانال‌های مربوطه (۰-۱۵) روی PCA9685 متصل کنید. مطمئن شوید که سیم سیگنال را به ترتیب صحیح برای هر سرو متصل کنید.

نمونه‌های شِفر (کود) و راهنما

اکنون کدی که موتورهای سرو را یکی یکی کنترل می‌کند مرور خواهیم کرد. شِفر (کود) با وارد کردن کتابخانه‌های مورد نیاز برای ارتباط I2C و ماجیول PCA9685 آغاز می‌شود. بخش زیر شی PCA9685 را راه‌اندازی می‌کند:

#include 
#include 

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // Initialize PCA9685

در تابع راه‌اندازی، ما نمایشگر مسلسل را راه‌اندازی کرده و فرکانس PWM را برای سروها تنظیم می‌کنیم:

void setup() {
  Serial.begin(9600); // Start serial communication
  pwm.begin(); // Initialize PCA9685
  pwm.setPWMFreq(60); // Set frequency to 60 Hz for servos
}

حلقه اصلی شامل دو حلقه تو در تو است: حلقه خارجی از میان ۱۶ سروو عبور می‌کند، در حالی که حلقه داخلی به تدریج زاویه را از ۰ تا ۱۸۰ درجه تغییر می‌دهد:

void loop() {
  for(int i=0; i<16; i++) {
    for(int angle = 0; angle<181; angle += 10) {
      delay(50); // Wait for servo to move
      pwm.setPWM(i, 0, angleToPulse(angle)); // Set servo position
    }
  }
  delay(1000); // Wait before repeating
}

این ساختار به هر سروو اجازه می‌دهد تا به زاویه معین خود در افزایش‌های ۱۰ درجه حرکت کند و انتقال‌های روانی را فراهم می‌آورد. تابعangleToPulse(int ang)زیر زاویه‌ها را به عرض پالس‌های مناسب برای سروها تبدیل می‌کند:

int angleToPulse(int ang) {
   int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX); // Map angle to pulse width
   return pulse; // Return pulse width
}

این تابع برای تبدیل زاویه مورد نظر به سیگنال PWM که PCA9685 می‌تواند آن را درک کرده و به سرووها ارسال کند، ضروری است. برای جزئیات بیشتر، به یاد داشته باشید که شِفر (کود) کامل در زیر مقاله بارگذاری می‌شود.

نمایش / چه انتظاری باید داشته باشید

زمانی که همه چیز به درستی متصل شده و شِفر (کود) بارگذاری شد، باید ببینید که هر سرو موتورها به ترتیب به زوایای مربوطه خود حرکت می‌کنند. اگر با هرگونه مشکلی مواجه شدید، اتصالات خود را دوباره بررسی کنید و اطمینان حاصل کنید که منبع تغذیه‌ای پایدار برای سرو موتورها دارید. اگر سرو موتورها به طور مورد انتظار پاسخ نمی‌دهند، سیگنال‌های PWM ارسال شده از طریق PCA9685 را تأیید کنید.

برچسب‌های زمانی ویدئو

  • جزئیات ماجیول با تراشه PCA9685
  • 06:14 افزودن کتابخانه‌ لازم برای PCA9685
  • 07:14 بارگذاری شِفر (کود) مثال
  • 07:35 توضیح شِفر (کود)
  • شِفر (کود) ساده Arduino برای PCA9685 11:31
  • ۱۲:۰۰ پیدا کردن حداقل و حداکثر مقدار برای سروو شما
  • ۱۸:۲۷ نگاشت زاویه پالس به عرض پالس
  • 20:05 ایجاد متد جداگانه برای نگاشت
  • ۲۰:۵۵ استفاده از حلقه for برای آزمایش تمام زوایا برای نقشه‌برداری
267-PCA9685 Video V2, Arduino Code-1 to run servo one by one all servos from 0 to 180°
زبان: C++
/*
 * Original source: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
 * PCA9685 Video V2, Arduino Code-1 
 * This is the Arduino code PAC6985 16 channel servo controller
 * watch the video for details (V1) and demo http://youtu.be/y8X9X10Tn1k
 *  This code is #1 for V2 Video Watch the video :https://youtu.be/bal2STaoQ1M
get this code and wiring from https://robojax.com/RTJ243
 *  I have got 3 codes as follow:
   #1-Arduino Code to run one by one all servos from 0 to 180°   
   #2-Arduino Code to control specific servos with specific angle
   #3-Arduino Code to run 2 or all servos at together
   
 * Written/updated by Ahmad Shamshiri for Robojax Video channel www.Robojax.com
 * Date: Dec 16, 2017, in Ajax, Ontario, Canada

 * Watch video for this code: 
 * 
 * Related Videos
V5 video of PCA9685 32 Servo with ESP32 with WiFi https://youtu.be/bvqfv-FrrLM
V4 video of PCA9685 32 Servo with ESP32 (no WiFi): https://youtu.be/JFdXB8Za5Os
V3 video of PCA9685 how to control 32 Servo motors https://youtu.be/6P21wG7N6t4
V2 Video of PCA9685 3 different ways to control Servo motors: https://youtu.be/bal2STaoQ1M
V1 Video introduction to PCA9685 to control 16 Servo  https://youtu.be/y8X9X10Tn1k

 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * this code has been downloaded from https://robojax.com


or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/*************************************************** 
  This is an example for our Adafruit 16-channel PWM & Servo driver
  Servo test - this will drive 16 servos, one after the other

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/815

  These displays use I2C to communicate, 2 pins are required to  
  interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you 
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
// Watch video V1 to understand the two lines below: http://youtu.be/y8X9X10Tn1k
#define SERVOMIN  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  575 // this is the 'maximum' pulse length count (out of 4096)

// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

  pwm.begin();
  
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  //yield();
}

// the code inside loop() has been updated by Robojax
void loop() {

//watch video for details: https://youtu.be/bal2STaoQ1M
for(int i=0; i<16; i++)
  {
    for( int angle =0; angle<181; angle +=10){
      delay(50);
        pwm.setPWM(i, 0, angleToPulse(angle) );
        // see YouTube video for details (robojax)
       
    }
 
  }
 
  // robojax PCA9865 16 channel Servo control
  delay(1000);// wait for 1 second
 
}

/*
/* angleToPulse(int ang)
 * @brief gets angle in degree and returns the pulse width
 * @param "ang" is integer representing angle from 0 to 180
 * @return returns integer pulse width
 * Usage to use 65 degree: angleToPulse(65);
 * Written by Ahmad Shamshiri on Sep 17, 2019. 
 * in Ajax, Ontario, Canada
 * www.Robojax.com 
 */
 
int angleToPulse(int ang){
   int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max 
   Serial.print("Angle: ");Serial.print(ang);
   Serial.print(" pulse: ");Serial.println(pulse);
   return pulse;
}
268-PCA9685 Video V2, Arduino Code-2 to control specific servos with specific angles (one or more servos)
زبان: C++
/*
 * Original sourse: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
 * PCA9685 Video V2, Arduino Code-2 
 * This is the Arduino code PAC6985 16 channel servo controller
 * watch the video for details (V1) and demo http://youtu.be/y8X9X10Tn1k
 *  This code is #2 for V2 Video Watch the video :https://youtu.be/bal2STaoQ1M
get codes and wring from https://robojax.com/RTJ243 
 *  I have got 3 codes as follow:
   #1-Arduino Code to run one by one all servos from 0 to 180°   
   #2-Arduino Code to control specific servos with specific angle
   #3-Arduino Code to run 2 or all servos at together
 * 
 * Written/updated by Ahmad Shamshiri for Robojax Video channel www.Robojax.com
 * Date: Dec 16, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * this code has been downloaded from https://robojax.com

 * Watch video for this code: 
 * 
 * Related Videos
V5 video of PCA9685 32 Servo with ESP32 with WiFi https://youtu.be/bvqfv-FrrLM
V4 video of PCA9685 32 Servo with ESP32 (no WiFi): https://youtu.be/JFdXB8Za5Os
V3 video of PCA9685 how to control 32 Servo motors https://youtu.be/6P21wG7N6t4
V2 Video of PCA9685 3 different ways to control Servo motors: https://youtu.be/bal2STaoQ1M
V1 Video introduction to PCA9685 to control 16 Servo  https://youtu.be/y8X9X10Tn1k
 
 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

****************************
Get early access to my videos via Patreon and have  your name mentioned at end of very 
videos I publish on YouTube here: http://robojax.com/L/?id=63 (watch until end of this video to list of my Patrons)
****************************

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you 
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
// Watch video V1 to understand the two lines below: http://youtu.be/y8X9X10Tn1k
#define SERVOMIN  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  575 // this is the 'maximum' pulse length count (out of 4096)

// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

  pwm.begin();
  
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  //yield();
}

// the code inside loop() has been updated by Robojax
void loop() {
//watch video for details: https://youtu.be/bal2STaoQ1M
for(int i=0; i<16; i++)
  {
    for( int angle =0; angle<181; angle +=10){
      delay(50);
        pwm.setPWM(5, 0, angleToPulse(angle) );
        pwm.setPWM(8, 0, angleToPulse(angle) ); 
        pwm.setPWM(15, 0, angleToPulse(angle) );         
    }
 
  }
 
// robojax PCA9865 16 channel Servo control
  delay(1000);
 
}

/*
 * angleToPulse(int ang)
 * gets angle in degree and returns the pulse width
 * also prints the value on seial monitor
 * written by Ahmad Shamshiri for Robojax, Robojax.com
 */
int angleToPulse(int ang){
   int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max 
   Serial.print("Angle: ");Serial.print(ang);
   Serial.print(" pulse: ");Serial.println(pulse);
   return pulse;
}

منابع و مراجع

هنوز هیچ منبعی موجود نیست.

فایل‌ها📁

هیچ فایلی موجود نیست.