cR_17_lezione 05

by / venerdì, 25 Novembre 2016 / Published in Archivio Robotica

 

 

 

LEZIONE n° 5 – MOTORI

 

 

La lezione è sui motori e sulla loro gestione. Si useranno i motori, ma vedremo che abbiamo bisogno di una schield di controllo degli stessi (in pratica stiamo usando l’equivalente di un “ponte H”, il vecchio integrato 298). Per far funzionare i motori sarà necessaria una potenza superiore a quella che possiamo prelevare dalla porta USB del PC. Dovremo necessariamente utilizzare un generatore esterno.

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 8:
/*

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

{

int tempo = 3000;

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(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);

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(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);

}

 

 

listato programma esempio numero 9:
/*

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

}

 

 

Modifiche proposte:

 

n  Cambiare i tempi

n  Cambiare il potenziometro

n  Accendere un led diverso secondo il verso

 

di seguito le slide della lezione

 

 

(56)

TOP