From: Andrea Manni Date: Thu, 6 Apr 2017 10:13:26 +0000 (+0200) Subject: Aggiunti esempi, funzioni per servo X-Git-Url: http://git.piffa.net/web?a=commitdiff_plain;h=d2bedb8582903f3bf86ed05a84f80560b243230c;p=rover Aggiunti esempi, funzioni per servo --- diff --git a/libraries/rover/examples/drive/drive.ino b/libraries/rover/examples/drive/drive.ino new file mode 100644 index 0000000..fbe868c --- /dev/null +++ b/libraries/rover/examples/drive/drive.ino @@ -0,0 +1,57 @@ +/* L298n motor + +Inclusione delle funzioni nella libreria rover. + +Guida 2WD composta da 2 motori + +- 2 motori DC +- L298n module +- Batteria > 6v + +*/ + +#include +#include + +void setup() { + abilita(); +} + + +void loop() { +digitalWrite(13,HIGH); + +// Avanti + avanti(); + delay(2000); + +digitalWrite(13,LOW); + +// Stop + stop(); + delay(1000); + +// TurnSX + giraSX(); + delay(2000); + +// Stop + stop(); + delay(1000); + +// indietro + indietro(); + delay(2000); + +// Stop + stop(); + delay(1000); + +// TurnDX + giraDX(); + delay(2000); + +// Stop + stop(); + delay(1000); +} diff --git a/libraries/rover/examples/servo/servo.ino b/libraries/rover/examples/servo/servo.ino new file mode 100644 index 0000000..cd63a3b --- /dev/null +++ b/libraries/rover/examples/servo/servo.ino @@ -0,0 +1,55 @@ +/* Rotazione + + Rotazione di un servomotore tramite la librerio Servo.h . + + L'utilizzo della libreria Servo rende inutilizzabile analogWrite() + sui pin 9 e 10 dato che utilizza i timer associati a questi PIN. + + Power: un servo da 9g puo' arrivare ad impegnare 750mA sotto carico + (se viene opposta resistenza al movimento del servo), un SG90 prende + ~52mA se il movimento e' libero. Quindi in fase di test il servo puo' + essere alimentato direttamente da una scheda Arduino (200ma dal PIN 5v) + ma per l'uso finale dovra' essere alimentato autonomamente. + + + Rotazione a SX di 90' + Rotazione a DC di 90' + + Schema: https://www.arduino.cc/en/uploads/Tutorial/sweep_bb.png + http://microbotlabs.com/images/mearm-uno-servo-1.jpg + */ + + +#include "rover.h" +#include "Servo.h" + +// a maximum of eight servo objects can be created + + +void setup() +{ + abilita(); + +} + + +void loop() +{ + // servo DX + servoDX(); // Gira il servo a DX (il verso dipende anche da come e' stato montato) + delay(1000); + + servoMiddle(); // Torna al centro + delay(1000); + + // servo SX + servoSX(); // Gira il servo a SX (il verso dipende anche da come e' stato montato) + delay(1000); + + servoMiddle(); + delay(1000); +} + + + + diff --git a/libraries/rover/examples/ultrasonic/ultrasonic.ino b/libraries/rover/examples/ultrasonic/ultrasonic.ino new file mode 100644 index 0000000..b26408a --- /dev/null +++ b/libraries/rover/examples/ultrasonic/ultrasonic.ino @@ -0,0 +1,32 @@ +/* Ultrasonic rilevatore distanza + +Rilevatore distanza minore di 5cm. +funzione + +HC-SR04 Ping distance sensor +VCC to arduino 5v - GND to arduino GND + + */ +#define dEBUG +#include "rover.h" +#include "Servo.h" + + +void setup() { + abilita(); + //Debug +#ifdef DEBUG +Serial.begin(9600); +#endif +Serial.begin(9600); // Need for distanceMonitor +} + +void loop() { +if (distanceCheck()) { // Verifica se la distanza di un oggetto e' minore di minDistance = 10cm + digitalWrite(13,HIGH); +} else { + digitalWrite(13,LOW); +} +Serial.println(distanceMonitor()); // Ritorna la distanza di un oggetto in cm +delay(50); +} diff --git a/libraries/rover/keywords.txt b/libraries/rover/keywords.txt index d698c40..ff51f4d 100644 --- a/libraries/rover/keywords.txt +++ b/libraries/rover/keywords.txt @@ -23,3 +23,7 @@ stop KEYWORD2 giraSX KEYWORD2 giraDX KEYWORD2 setSpeed KEYWORD2 + + +distanceCheck KEYWORD2 +distanceMonitor KEYWORD2 diff --git a/libraries/rover/rover.cpp b/libraries/rover/rover.cpp index 58bbf2c..a5989b6 100644 --- a/libraries/rover/rover.cpp +++ b/libraries/rover/rover.cpp @@ -13,7 +13,7 @@ Licenza: GPLv3 #define dEBUG -// Configurazione con OUTPUT digitali +// Configurazione parametri: variabili GLOBALI // motor one const int enA = 6; const int in1 = 7; @@ -34,21 +34,33 @@ const byte sx = 10; // Min SX const byte dx = 170; // Maz DX Servo myservo; // Non c'e' bisogno di extern se e' dichiarato in questo scope +// Ultrasuoni +const byte trigPIN = 11; +const byte echoPIN= 12; +const byte ledPIN = 13; +long duration; +int distance; +const int minDistance = 10; + //////////////////////// // Funzioni: // void abilita() { -// Abilita i PINs come OUTPUTS +// Abilita i PINs come OUTPUTS e attacca un servo +// Motors pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(enB, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); - - +// Servo pinMode(servoPIN, OUTPUT); - myservo.attach(servoPIN); + myservo.attach(servoPIN); //la libreria servo deve essere sempre inclusa +// Ultrasonic + pinMode(trigPIN, OUTPUT); + pinMode(echoPIN, INPUT); + pinMode(ledPIN, OUTPUT); } @@ -177,3 +189,45 @@ void servoMiddle() { delay(spausa); } } + + +// Ultrasuoni +boolean distanceCheck() { +// Verifica se la distanza di un oggetto e' minore di minDistance + digitalWrite(trigPIN, LOW); // Prepare for ping + delayMicroseconds(2); // + digitalWrite(trigPIN, HIGH); // Send a ping + delayMicroseconds(10); // + digitalWrite(trigPIN, LOW); // Set down ping + duration = pulseIn(echoPIN, HIGH); + //distance = (duration / 2) / 29.1; // Speed is ~300m/s, + // so it takes ~29.1 milliseconds for a cm. + distance = (duration / 58.2); // Atmegas are not found of divisions + // Distance is half of (out + back) +#ifdef DEBUG +Serial.print("Distanza oggetto: "); +Serial.println(distance); +#endif + if (distance < minDistance) { // This is where the LED On/Off happens + return 1; + } + else { + return 0; + } +} + +int distanceMonitor() { +// Ritorna la distanza di un oggetto in cm + digitalWrite(trigPIN, LOW); // Prepare for ping + delayMicroseconds(2); // + digitalWrite(trigPIN, HIGH); // Send a ping + delayMicroseconds(10); // + digitalWrite(trigPIN, LOW); // Set down ping + duration = pulseIn(echoPIN, HIGH); + //distance = (duration / 2) / 29.1; // Speed is ~300m/s, + // so it takes ~29.1 milliseconds for a cm. + distance = (duration / 58.2); // Atmegas are not found of divisions + // Distance is half of (out + back) + + return distance; +} diff --git a/libraries/rover/rover.h b/libraries/rover/rover.h index 3f0470e..933baa7 100644 --- a/libraries/rover/rover.h +++ b/libraries/rover/rover.h @@ -38,8 +38,10 @@ void giraSX(); void giraDX() ; void stop() ; - void servoDX() ; void servoSX() ; void servoMiddle() ; + +boolean distanceCheck() ; +int distanceMonitor() ; #endif diff --git a/prototypes/ultra/check_func/check_func.ino b/prototypes/ultra/check_func/check_func.ino index 40481b1..815e352 100644 --- a/prototypes/ultra/check_func/check_func.ino +++ b/prototypes/ultra/check_func/check_func.ino @@ -7,14 +7,15 @@ HC-SR04 Ping distance sensor VCC to arduino 5v - GND to arduino GND */ -# define DEBUG +# define dEBUG // Ultrasuoni const byte trig = 11; const byte echo = 12; const byte led = 13; -long duration, distance; -boolean allarm = 0; +long duration; +int distance; +const int minDistance = 10; void setup() { pinMode(trig, OUTPUT); @@ -23,36 +24,57 @@ void setup() { //Debug - Serial.begin (9600); +#ifdef DEBUG +Serial.begin(9600); +#endif +Serial.begin(9600); // Need for distanceMonitor } void loop() { -if (check()) { +if (distanceCheck()) { digitalWrite(led,HIGH); } else { digitalWrite(led,LOW); } - -#ifdef DEBUG -Serial.begin(9600); -#endif +Serial.println(distanceMonitor()); +delay(50); } -boolean check() { +boolean distanceCheck() { digitalWrite(trig, LOW); // Prepare for ping delayMicroseconds(2); // digitalWrite(trig, HIGH); // Send a ping delayMicroseconds(10); // digitalWrite(trig, LOW); // Set down ping duration = pulseIn(echo, HIGH); - distance = (duration/2) / 29.1; // Speed is ~300m/s, + //distance = (duration / 2) / 29.1; // Speed is ~300m/s, // so it takes ~29.1 milliseconds for a cm. + distance = (duration / 58.2); // Atmegas are not found of divisions // Distance is half of (out + back) - if (distance < 5) { // This is where the LED On/Off happens +#ifdef DEBUG +Serial.print("Distanza oggetto: "); +Serial.println(distance); +#endif + if (distance < minDistance) { // This is where the LED On/Off happens return 1; } else { return 0; } } + +int distanceMonitor() { + digitalWrite(trig, LOW); // Prepare for ping + delayMicroseconds(2); // + digitalWrite(trig, HIGH); // Send a ping + delayMicroseconds(10); // + digitalWrite(trig, LOW); // Set down ping + duration = pulseIn(echo, HIGH); + //distance = (duration / 2) / 29.1; // Speed is ~300m/s, + // so it takes ~29.1 milliseconds for a cm. + distance = (duration / 58.2); // Atmegas are not found of divisions + // Distance is half of (out + back) + + return distance; +} diff --git a/prototypes/ultra/check_func_lib/check_func_lib.ino b/prototypes/ultra/check_func_lib/check_func_lib.ino new file mode 100644 index 0000000..ea25c4c --- /dev/null +++ b/prototypes/ultra/check_func_lib/check_func_lib.ino @@ -0,0 +1,32 @@ +/* Ultrasonic rilevatore distanza + +Rilevatore distanza minore di 5cm. +funzione + +HC-SR04 Ping distance sensor +VCC to arduino 5v - GND to arduino GND + + */ +#define dEBUG +#include "rover.h" +#include "Servo.h" + + +void setup() { + abilita(); + //Debug +#ifdef DEBUG +Serial.begin(9600); +#endif +Serial.begin(9600); // Need for distanceMonitor +} + +void loop() { +if (distanceCheck()) { + digitalWrite(13,HIGH); +} else { + digitalWrite(13,LOW); +} +Serial.println(distanceMonitor()); +delay(50); +}