corso 2018 L6
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:
- Variabili:
- Tipo (intero, reale, carattere, etc.)
- Nome (caratteri alfanumerici)
- Strutture (corso avanzato)
- Principali comandi:
- Assegnazione
- Si calcola l’espressione a sinistra ed il risultato viene assegnato alla variabile a destra
- Assegnazione
-
-
- Count = count +1
-
-
- IN/OUT
- Istruzioni per immettere o prelevare dei dati
- IN/OUT
-
-
- digitalWrite(pin1,LOW)
-
-
- Test
- Viene eseguito il test (vero o falso) e secondo il valore viene eseguita l’istruzione seguente
- Test
-
-
- If (count <= soglia)
- Istruzione1
- istruzione2
- If (count <= soglia)
- Cicli
- Vengono eseguite ripetutamente un gruppo di istruzioni
- For (x=0;x==9;x++)
- 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:
/*
controllare la velocità da sensore
*/
int sensorPin0 = A1;
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_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 = 120; //speed
int versusH = HIGH; //speed
int versusL = LOW; //speed
int versusA = 0; //speed
int versusB = 0; //speed
int soglia = 500; //soglia
int TEMPO = 510; //soglia
int sogliaMIN = 100; //soglia minima
int sogliaMAX = 150; //soglia minima
int soglia_APP = 60; //soglia minima
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:
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);
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
sensorValueSpeed = sensorValueSpeed/4; // adattamento della soglia
if (sensorValueSpeed <= sogliaMIN)
{
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);
/* Serial.print(“velocita CONTROLLATA ” );
Serial.print(sensorValueSpeed);
Serial.print(“\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 “);
delay(10);
delay(TEMPO);
}
Modifiche proposte:
- Cambiare i tempi
- Cambiare i potenziometri
- Accendere un led per la velocità e uno per la direzione
Provare a fare le modifiche in modo autonomo altrimenti nel caso della modifica numero 3 sfruttare i seguenti
SUGGERIMENTI:
1) trovare i pin dei 2 Led
2) nel setup definire i pin dei Led come uscite
3) trovare dove inserire l’accensione
4) accendere un Led e spegnere l’altro
Modifiche proposte con elementi di programmazione:
- Contare i cicli eseguiti e stamparli
- Ogni 100 cicli cambiare il verso di rotazione
- Se il valore del sensore è pari accendere un led
Provare a fare le modifiche in modo autonomo altrimenti nel caso della modifica numero 1 sfruttare i seguenti
SUGGERIMENTI:
1) definire il nome della variabile da usare come contatore
2) definire il tipo della variabile
3) inizializzare il contatore una sola volta ed incrementarlo ad ogni ciclo
4) stamparne il valore
di seguito le slide della lezione
(49)
Articolo in Evidenza
Le categorie
- .Dicono di NOI
- Album fotografici Francesco
- Album Fotografici Iduesarchiaponi
- Album Fotografici Paolo
- Alta Fotografia
- Altro
- Antichi eserciti ed armi
- Archeologia
- Archeologia1
- Archeologia2
- Archivio Robotica
- Circuiti per Robotica
- Città Fantasma
- Collaboratori
- Corso Robotica 19-20
- Didattica
- Didattica a distanza
- Esempi di Robotica
- Eventi
- Fotografia
- Francesco
- Francesco e Paolo
- Gallerie Fotografiche Francesco
- Gare di Robotica
- Gli Svaghi nel mondo Antico
- I Cibi Romani
- Il blog
- L'Italia che non ti aspetti
- Laboratorio Robotica
- Le sette Meraviglie del mondo
- Le Sette meraviglie del Mondo moderne
- Lezioni On-line
- Link Utili
- Miti e Leggende
- Mitologia Norena
- Notizie
- Paolo
- Per non dimenticare
- Popoli e Civiltà
- Robotica
- Roma Nascosta
- Scrittori e Poeti Antichi
- Scuola
- Si dice… Perchè
- Soluzioni didattica a distanza
- Storie e Personaggi Romani
- Teatro
- Viaggiando …..
- Viaggiare
- Video
perché mi da’ questo messaggio di errore dopo che l’ho copiato?? :
[code]
/*
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);
}
[/code] :exit status 1
stray '\302' in programm.
Ho controllato e grazie anche al fatto che l’aveva già scoperto un ragazzo del corso la risposta è semplice.
PER MOTIVI MISTERIOSI (LEGATI AL PERSONAL COMPUTER)
VENGONO CAMBIATE LE VIRGOLETTE NEI COMMENTI – SOLUZIONE RISCRIVERLE
INOLTRE PER STAMPARE LE VARIABILI LE AGGIUNGE – SOLUZIONE TOGLIERLE
Ho provato a farlo anche con il mio PC e lo HA FATTO ANCHE A ME. SUL PROSSIMO ARTICOLO LO SCRIVERO BELLO IN EVIDENZA