ArduShop.ro

ArduShop.ro
cod tehnic.go.ro_02 reducere 5%, pentru vizitatorii paginilor mele !

miercuri, 5 februarie 2014

Arduino si un servomotor (II)

   In articolul Arduino si un servomotor am folosit un servomotor mititel, acum voi testa unul mai "fortos", model MG996R, care are rotile dintate ale reductorului confectionate din metal, fata de celalalt, care le are din plastic.
   
   Deoarece la comanda de se duce in zero, se simtea ca se forteaza, a fost desfacut, sa constat daca are vreun defect mecanic:
 
 
 
 
   Nedepistand nimic mecanic, am ajuns la concluzia ca este partea de comparare a pozitiei, cea cu potentiometru care poate avea rezistent ala capetele de cursa si cea spre masa mai mare... oricum am rezolvat-o din soft, in sensul ca in loc de comanda de a se duce la 00, se duce la 70, care e ok pentru el, iar 1800 devine 1860.
   Sketch-ul folosit este:
/*
Arduino Servo Test sketch
http://www.hobbytronics.co.uk/arduino-tutorial2-servos
and 
SWEEEP by BARRAGAN http://barraganstudio.com
adapted by niq_ro from http://nicuflorica.blogspot.com
*/

#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
servoMain.attach(9); // servo on digital pin 9
pinMode(13, OUTPUT); // initialize the digital pin no 13 as an output.
}

void loop()
{
// this serve has real 0 degree when comand is 7... 45 is 52=45+7, etc 
   servoMain.write(7);   // Turn Servo Left to 0 degrees
   digitalWrite(13, HIGH);  // turn ON the LED  
   delay(5000);          // Wait 1 second
   digitalWrite(13, LOW);  // turn OFF the LED
   servoMain.write(52);  // Turn Servo Left to 45 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(97);  // Turn Servo back to center position (90 degrees)
   delay(1000);          // Wait 1 second
   servoMain.write(142); // Turn Servo Right to 135 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(186); // Turn Servo Right to 180 degrees
   delay(2000);          // Wait 1 second
   servoMain.write(142); // Turn Servo Right to 135 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(97);  // Turn Servo back to center position (90 degrees)
   delay(1000);          // Wait 1 second
   servoMain.write(52);  // Turn Servo Left to 45 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(7);   // Turn Servo Left to 0 degrees
   digitalWrite(13, HIGH);  // turn ON the LED
   delay(1000);          // Wait 1 second
   digitalWrite(13, LOW);  // turn OFF the LED

// adapted part from SWEEP example
  for(int pos = 7; pos < 187; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    servoMain.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
 delay(1000); 
  for(int pos = 186; pos>=7; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    servoMain.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
delay (1000);
}
   Schema de conectare este aceeasi:
   Am facut un filmulet, numit un servomotor MG996R conectat la Arduino, care arata cum reactioneaza servomotorul la comenzile date de Arduino:
07.02.2014    Pentru a va da seama de diferenta dintre primul servomotor si cel din primul articol, le-am facut poze impreuna:
   Servomotorul din poza (micro servo 9g) e nou, altul decat cel initial, asa ca l-am pus la probe si, am constatat, ca nici el nu se duce in 0, asa ca am modificat putin sketch-ul sa imi fie usor de modificat valorile. la minim am lasat 100, iar la maxim 1700.
/*
Arduino Servo Test sketch
http://www.hobbytronics.co.uk/arduino-tutorial2-servos
and 
SWEEEP by BARRAGAN http://barraganstudio.com
adapted by niq_ro from http://nicuflorica.blogspot.com
*/

#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
servoMain.attach(9); // servo on digital pin 9
pinMode(13, OUTPUT); // initialize the digital pin no 13 as an output.
}

int minim = 0;  // theoretical zero degree
int maxim = 180; // theoretical 180 degree
int eroarejos = 10; // error at minimum (restricted area)
int eroaresus = -10; // error at maximum (restricted area)
int zero = minim + eroarejos; // practical minimum allowed
int plin = maxim + eroaresus; // practical maximum allowed
int jumate = (plin-zero)/2; // half position (90 degree)
int sfert = (plin-zero)/4; // quarter
int farasfert = 3*(plin-zero)/4; // theree quarter


void loop()
{
// this serve has real 0 degree when comand is 7... 45 is 52=45+7, etc 
   servoMain.write(zero);   // Turn Servo Left to 0 degrees
   digitalWrite(13, HIGH);  // turn ON the LED  
   delay(1000);          // Wait 1 second
   digitalWrite(13, LOW);  // turn OFF the LED
   servoMain.write(sfert);  // Turn Servo Left to 45 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(jumate);  // Turn Servo back to center position (90 degrees)
   digitalWrite(13, HIGH);  // turn ON the LED  
   delay(3000);          // Wait 1 second
   digitalWrite(13, LOW);  // turn OFF the LED
   servoMain.write(farasfert); // Turn Servo Right to 135 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(plin); // Turn Servo Right to 180 degrees
   delay(2000);          // Wait 1 second
   servoMain.write(farasfert); // Turn Servo Right to 135 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(jumate);  // Turn Servo back to center position (90 degrees)
   digitalWrite(13, HIGH);  // turn ON the LED  
   delay(3000);          // Wait 1 second
   digitalWrite(13, LOW);  // turn OFF the LED
   servoMain.write(sfert);  // Turn Servo Left to 45 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(zero);   // Turn Servo Left to 0 degrees
   digitalWrite(13, HIGH);  // turn ON the LED
   delay(1000);          // Wait 1 second
   digitalWrite(13, LOW);  // turn OFF the LED

// adapted part from SWEEP example
  for(int pos = zero; pos < plin; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    servoMain.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
 delay(1000); 
  for(int pos = (plin-1); pos>=zero; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    servoMain.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
delay (1000);
}