Lezione 6

by / domenica, 13 Dicembre 2015 / Published in Archivio Robotica

LEZIONE n° 6 – MOTORI

La lezione è sui MOTORI IN CORRENTE CONTINUA e la loro gestione.

Nell’esempio n° 9 si impara a gestire i Motori. In questo caso oltre a quanto utilizzato fino ad ora

Per provare a fare le modifiche proposte copiare il listato (colorato in rosso) ed incollarlo in un editor di solo testo [Notepad ad esempio]

listato programma esempio numero 9:

/*
  contollo motori
*/

//const int ledPin =  13;      // the number of the LED pin
int pwm_a = 3;           //PWM control for motor outputs 1 and 2 is on digital pin 10
int pwm_b = 11;          //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_b = 13;          //direction control for motor outputs 3 and 4 is on digital pin 13
// TAVOLA UNO
int dir_a = 12;          //direction control for motor outputs 1 and 2 is on digital pin 12
// TAVOLA DUE
//int dir_a = 4;          //direction control for motor outputs 1 and 2 is on digital pin 12 
int speed_outA = 100;          //speed
int speed_outB = 100;          //speed
int speedL = 50;          //speed  sotto i 40 non riescono a girare
int speedH = 100;          //speed
void setup()
{
  Serial.begin(9600);
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
 
  analogWrite(pwm_a, speedL);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
  analogWrite(pwm_b, speedL);
 
}
void loop()
{
    Serial.print(”     AVANTI   \n  ” );                        
    delay(10);
    digitalWrite(dir_a, HIGH);  //Set motor direction, 1 low, 2 high
    digitalWrite(dir_b, LOW);  //Set motor direction, 3 high, 4 low
    speed_outA = speedL;
    speed_outB = speedL;
    analogWrite(pwm_a, speed_outA);  //set both motors to run at 100% duty cycle (fast)
    analogWrite(pwm_b, speed_outB);
    Serial.print(“velocita motore A =    ” );                      
    Serial.print(speed_outA);     
    delay(10);
    Serial.print(”  velocita motore  B =    ” );                      
    Serial.print(speed_outB);     
    Serial.print(“\n  “);   
    delay(10);
  //  digitalWrite(ledPin, HIGH); 
 
    delay(4000);
    speed_outA = speedH;
    speed_outB = speedH;
    analogWrite(pwm_a, speed_outA);  //set both motors to run at 100% duty cycle (fast)
    analogWrite(pwm_b, speed_outB);
    Serial.print(“velocita motore A =    ” );                      
    Serial.print(speed_outA);     
    delay(10);
    Serial.print(”  velocita motore  B =    ” );                      
    Serial.print(speed_outB);     
    Serial.print(“\n  “);   
    delay(10);
//    digitalWrite(ledPin, LOW);
    delay(4000);
    Serial.print(”     INDIETRO   \n  ” );                      
    delay(10);
    digitalWrite(dir_a, LOW);  //Set motor direction, 1 low, 2 high
    digitalWrite(dir_b, HIGH);  //Set motor direction, 3 high, 4 low
    speed_outA = speedL;
    speed_outB = speedL;
    analogWrite(pwm_a, speed_outA);  //set both motors to run at 100% duty cycle (fast)
    analogWrite(pwm_b, speed_outB);
    Serial.print(“velocita motore A =    ” );                      
    Serial.print(speed_outA);     
    delay(10);
    Serial.print(”  velocita motore  B =    ” );                      
    Serial.print(speed_outB);     
    Serial.print(“\n  “);   
    delay(10);

  //  digitalWrite(ledPin, HIGH); 

    delay(4000);

    speed_outA = speedH;
    speed_outB = speedH;
    analogWrite(pwm_a, speed_outA);  //set both motors to run at 100% duty cycle (fast)
    analogWrite(pwm_b, speed_outB);
    Serial.print(“velocita motore A =    ” );                      
    Serial.print(speed_outA);     
    delay(10);
    Serial.print(”  velocita motore  B =    ” );                      
    Serial.print(speed_outB);     
  Serial.print(“\n  “);   
    delay(10);
//    digitalWrite(ledPin, LOW);
    delay(4000);
}

 

Nell’esempio n° 10 il controllo della direzione viene letto da un potenziometro esterno.

listato programma esempio numero 10:

 

/*
    controllo di una coppia di motori secondo il valore letto da due potenziometri
*/
int sensorPin = A0;
int sensorValue = 0;  // variable to store the value coming from the sensor
int pwm_a = 3;           //PWM control for motor outputs 1 and 2 is on digital pin 10
int pwm_b = 11;          //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12;          //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13;          //direction control for motor outputs 3 and 4 is on digital pin 13
int speed_outA = 100;          //speed
int speed_outB = 100;          //speed
int speedL = 50;          //speed  sotto i 40 non riescono a girare
int speedH = 100;          //speed
int versusH = HIGH;          //speed
int versusL = LOW;          //speed
int versusA = 0;          //speed
int versusB = 0;          //speed
int soglia = 500;          //soglia
int TEMPO = 2000;          //soglia
void setup()
{
  Serial.begin(9600);
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);  analogWrite(pwm_a, speedL);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
  analogWrite(pwm_b, speedL);
 
}

void loop()
{
    // read the state of the pushbutton value:
    sensorValue = analogRead(sensorPin);   
          // print the results to the serial monitor:
          Serial.print(“\n  “);   
          Serial.print(“sensor = ” );                      
          Serial.print(sensorValue);     
          Serial.print(“\n\n  “);       
    delay(10);                    
    if (sensorValue <= soglia){
        versusA = versusH;
        versusB = versusL;
        Serial.print(”     AVANTI   \n  ” );                        
        delay(10);
    }
    else {
        versusA = versusL;
        versusB = versusH;
        Serial.print(”     INDIETRO   \n  ” );                        
        delay(10);
    }
   
    digitalWrite(dir_a, versusA);  //Set motor direction, 1 low, 2 high
    digitalWrite(dir_b, versusB);  //Set motor direction, 3 high, 4 low
    speed_outA = speedL;
    speed_outB = speedL;
    analogWrite(pwm_a, speed_outA);  //set both motors to run at 100% duty cycle (fast)
    analogWrite(pwm_b, speed_outB);
    Serial.print(“velocita motore A =    ” );                      
    Serial.print(speed_outA);     
    delay(10);
    Serial.print(”  velocita motore  B =    ” );                      
    Serial.print(speed_outB);     
    Serial.print(“\n  “);   
    delay(10);
  //  digitalWrite(ledPin, HIGH); 
 
    delay(TEMPO);
    speed_outA = speedH;
    speed_outB = speedH;
    analogWrite(pwm_a, speed_outA);  //set both motors to run at 100% duty cycle (fast)
    analogWrite(pwm_b, speed_outB);
    Serial.print(“velocita motore A =    ” );                      
    Serial.print(speed_outA);     
    delay(10);
    Serial.print(”  velocita motore  B =    ” );                      
    Serial.print(speed_outB);     
    Serial.print(“\n  “);   
    delay(10);
//    digitalWrite(ledPin, LOW);
    delay(TEMPO);
}

LE MODIFICHE PROPOSTE PER ENTRAMBI GLI ESERCIZI SONO:

  • MODIFICARE I TEMPI
  • CAMBIARE IL POTENZIOMETRO
  • ACCENDERE UNO O PIU’ LED DIVERSI

di seguito le slide della lezione

(52)

Tagged under: , ,
TOP