
LEZIONE n° 6 – MOTORI e PROGRAMMAZIONE
La lezione è su un uso avanzato dei motori e sulla loro gestione. Si useranno i motori, ma come già detto abbiamo bisogno di una schield di controllo degli stessi (in pratica 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. Inoltre si vedranno dei semplici rudimenti di programmazione per poter gestire nelle migliori condizioni il robot.
RICORDIAMO I PRINCIPI DELLA PROGRAMMAZIONE DI ARDUINO:
n Variabili:
n Tipo (intero, reale, carattere, etc.)
n Nome (caratteri alfanumerici)
n Strutture (corso avanzato)
n Principali comandi:
n Assegnazione
n Si calcola l’espressione a sinistra ed il risultato viene assegnato alla variabile a destra
n Count = count +1
n IN/OUT
n Istruzioni per immettere o prelevare dei dati
n digitalWrite(pin1,LOW)
n Test
n Viene eseguito il test (vero o falso) e secondo il valore viene eseguita l’istruzione seguente
n If (count <= soglia)
n Istruzione1
n istruzione2
n Cicli
n Vengono eseguite ripetutamente un gruppo di istruzioni
n For (x=0;x==9;x++)
n istruzione1
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 10:
/*
CONTOLLARE DUE MOTORI: 1° POTENZIOMETRO = VERSO // 2° POTENZIOMETRO VELOCITA’
*/
int sensorPin0 = A0;
int sensorPin1 = A1;
int sensorValueSpeed = 0; // variable to store the value coming from the sensor
int sensorValueVersus = 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()
{
sensorValueSpeed = analogRead(sensorPin0); // read the state of the pushbutton value:
Serial.print(” sensor VELOCITA’ = ” ); // print the results to the serial monitor:
Serial.print(sensorValueSpeed);
Serial.print(“\n “);
delay(10);
sensorValueVersus = analogRead(sensorPin1); // read the state of the pushbutton value:
Serial.print(” sensor VERSO = ” ); // print the results to the serial monitor:
Serial.print(sensorValueVersus);
Serial.print(“\n “);
delay(10);
if (sensorValueVersus <= soglia)
{
versusA = versusL;
versusB = versusH;
Serial.print(” VERSO AVANTI \n ” );
delay(10);
}
else
{
versusA = versusH;
versusB = versusL;
Serial.print(” VERSO DIETRO \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
if (sensorValueSpeed <= soglia)
{
speed_outA = speedL;
speed_outB = speedL;
Serial.print(” VELOCITA’ BASSA \n ” );
delay(10);
}
else
{
speed_outA = speedH;
speed_outB = speedH;
Serial.print(” VELOCITA’ ALTA \n ” );
delay(10);
}
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 “);
Serial.print(“\n “);
delay(10);
delay(TEMPO);
}
listato programma esempio numero 11:
int sensorPin1 = A0;
int sensorValueSpeed = 0; // variable to store the value coming from the sensor
int sensorValueVersus = 0; // variable to store the value coming from the sensor
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_outB = 100; //speed
int speedL = 50; //speed sotto i 40 non riescono a girare
int speedH = 120; //speed
int versusL = LOW; //speed
int versusB = 0; //speed
int TEMPO = 510; //soglia
int sogliaMAX = 150; //soglia minima
int soglia_APP = 60; //soglia minima
{
Serial.begin(9600);
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);
}
{
// read the state of the pushbutton value:
sensorValueSpeed = analogRead(sensorPin0);
Serial.print(” sensor VELOCITA’ = ” ); // print the results to the serial monitor:
Serial.print(sensorValueSpeed);
Serial.print(“\n\n “);
delay(10);
sensorValueVersus = analogRead(sensorPin1); // read the state of the pushbutton value:
Serial.print(” sensor VERSO = ” ); // print the results to the serial monitor:
Serial.print(sensorValueVersus);
Serial.print(“\n “);
delay(10);
{
versusA = versusL;
versusB = versusH;
Serial.print(” VERSO AVANTI \n ” );
delay(10);
}
else
{
versusA = versusH;
versusB = versusL;
Serial.print(” VERSO DIETRO \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 = sogliaMIN;
speed_outB = sogliaMIN;
Serial.print(” VELOCITA’ TROPPO BASSA \n ” );
delay(10);
}
else
{
if (sensorValueSpeed >= sogliaMAX)
{
speed_outA = sogliaMAX;
speed_outB = sogliaMAX;
Serial.print(” VELOCITA’ TROPPO ALTA \n ” );
delay(10);
}
else
{
speed_outA = sensorValueSpeed;
speed_outB = sensorValueSpeed;
Serial.print(” VELOCITA’ GIUSTA \n ” );
delay(10);
}
}
analogWrite(pwm_a, speed_outA); //set both motors to run at 100% duty cycle (fast)
analogWrite(pwm_b, speed_outB);
Serial.print(speed_outA);
delay(10);
Serial.print(” velocita motore B = ” );
Serial.print(speed_outB);
Serial.print(“\n “);
delay(10);
}
Modifiche proposte:
n Cambiare i tempi
n Cambiare i potenziometri
n Accendere un led per la velocità e uno per la direzione
Modifiche proposte con elementi di programmazione:
n Contare i cicli eseguiti e stamparli
n Ogni 100 cicli cambiare il verso di rotazione
n Se il valore del sensore è pari accendere un led
di seguito le slide della lezione






















(45)