Robotiikkapaja (9h)
Robotti auton koodi
/**
Bruno Santos, 2013
feiticeir0@whatgeek.com.pt
Small code to test DC motors - 2x with a L298 Dual H-Bridge Motor Driver
Kimmo ja Pauli muokkasivat 2019
Free to share
**/
#define TRIGGER_PIN 10 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#include <NewPing.h>
#include <Servo.h>
Servo myservo;
int pos = 0;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int distance = 2;
//Testing the DC Motors
//Define Pins
//Motor A
int enableA = 3;
int pinA1 = 4;
int pinA2 = 5;
//Motor B
int enableB = 8;
int pinB1 = 6;
int pinB2 = 7;
void setup() {
Serial.begin (9600);
//configure pin modes
pinMode (enableA, OUTPUT);
pinMode (pinA1, OUTPUT);
pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
pinMode (pinB1, OUTPUT);
pinMode (pinB2, OUTPUT);
myservo.attach(9);
}
void forward2() {
//enabling motor A
Serial.println ("Enabling Motor A");
digitalWrite (enableA, HIGH);
digitalWrite (enableB, HIGH);
//do something
//forward
Serial.println ("Forward");
digitalWrite (pinA2, HIGH);
digitalWrite (pinA1, LOW);
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
delay (2000);
digitalWrite (pinA2, LOW);
digitalWrite (pinA1, LOW);
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, LOW);
delay (1000);
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW);
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
delay (1000);
digitalWrite (pinA2, LOW);
digitalWrite (pinA1, LOW);
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, LOW);
delay(1000);
digitalWrite (pinA2, HIGH);
digitalWrite (pinA1, LOW);
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
delay (2000);
digitalWrite (enableB, LOW);
digitalWrite (enableA, LOW);
}
void forward() {
//enabling motor A
Serial.println ("Enabling Motor A+B");
digitalWrite (enableA, HIGH);
digitalWrite (enableB, HIGH);
//do something
//forward
Serial.println ("Forward");
digitalWrite (pinA2, HIGH);
digitalWrite (pinA1, LOW);
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
}
void stop() {
Serial.println ("Stop");
digitalWrite (pinA2, LOW);
digitalWrite (pinA1, LOW);
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, LOW);
delay (200);
digitalWrite (enableB, LOW);
digitalWrite (enableA, LOW);
}
void vaihda_suunta() {
int oikea=0;
int vasen=0;
pos=10;
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(500);
vasen = sonar.ping_cm();
delay(15);
pos = 170;
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(500);
oikea = sonar.ping_cm();
delay(15);
pos = 90;
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(500);
if (oikea == 0) oikea = 500;
if (vasen == 0) vasen = 500;
if (oikea < vasen) {
Serial.println ("Käännyn Oikealle!");
Serial.println (oikea);
delay(1000);
}
else {
Serial.println ("Käännyn Vasemmalle");
Serial.println (vasen);
delay(1000);
}
}
void loop() {
distance = sonar.ping_cm();
Serial.println(distance);
if ((distance < 30) && (distance > 2) ){
stop();
vaihda_suunta();
} else {
//forward();
}
delay (50);
}
Kaivuritesti 1
Servo servo_1; //initialize a servo object for the connected servo
Servo servo_2; //initialize a servo object for the connected servo
Servo servo_3; //initialize a servo object for the connected servo
Servo servo_4; //initialize a servo object for the connected servo
int angle1 = 0;
int angle2 = 0;
int angle3 = 0;
int angle4 = 0;
int potentio1 = A0; // initialize the A0analog pin for potentiometer
int potentio2 = A1; // initialize the A1analog pin for potentiometer
int potentio3 = A2; // initialize the A2analog pin for potentiometer
int potentio4 = A3; // initialize the A3analog pin for potentiometer
#define SERVO1PIN 7
#define SERVO2PIN 9
#define SERVO3PIN 10
#define SERVO4PIN 6
void setup()
{
servo_1.attach(SERVO1PIN); // attach the signal pin of servo to pin9 of arduino
}
void loop()
{
angle1 = analogRead(potentio1); // reading the potentiometer value between 0 and 1023
angle1 = map(angle1, 0, 1023, 60, 140); // scaling the potentiometer value to angle value for servo between 0 and 180)
servo_1.write(angle1); //command to rotate the servo to the specified angle
delay(5);
}
Robotti testi
* Bruno Santos, 2013
* feiticeir0@whatgeek.com.pt
* Small code to test DC motors - 2x with a L298 Dual H-Bridge Motor Driver
* Free to share
**/
//Testing the DC Motors
//Define Pins
//Motor A
int enableA = 10;
int pinA1 = 2;
int pinA2 = 3;
//Motor B
int enableB = 9;
int pinB1 = 4;
int pinB2 = 5;
void setup() {
Serial.begin (9600);
//configure pin modes
pinMode (enableA, OUTPUT);
pinMode (pinA1, OUTPUT);
pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
pinMode (pinB1, OUTPUT);
pinMode (pinB2, OUTPUT);
}
void loop() {
//enabling motor A
Serial.println ("Enabling Motor A");
digitalWrite (enableA, HIGH);
//do something
//forward
Serial.println ("Forward");
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW);
//5s forward
delay (5000);
//reverse
digitalWrite (pinA1,LOW);
digitalWrite (pinA2,HIGH);
//5s backwards
delay (5000);
//stop
digitalWrite (enableA, LOW);
delay (5000);
//enabling motor B
//Since motor B is mounted reversed, PINs must be exchanged
Serial.println ("Enabling Motor A");
digitalWrite (enableB, HIGH);
//do something
//forward
Serial.println ("Forward");
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, HIGH);
//5s forward
delay (5000);
//reverse
digitalWrite (pinB1,HIGH);
digitalWrite (pinB2,LOW);
//5s backwards
delay (5000);
//stop
digitalWrite (enableB, LOW);
delay (5000);
}
Tervetuloa robotiikkapajaan
Kesto: 9h
Alusta: Arduino Uno, Arduino Nano
Kuvaus: Pajan tavoitteena on rakentaa ja koodata liikkuva robottiauto. Ohjauksen voi toteuttaan langallisella ohjaimella, Bluetoothilla ja puhelimella tai täysin autonomisena. Materiaalia voi käyttää myös itseopiskelumateriaalina.
Tervetuloa rakentamaan robottiautoa.
Pajan sisältö
- Aloitus (30 min) - Asennataan Arduino Ide ja testataan Arduino Unon ohjelmointia.
- Oppitunti 1 ( 2h 20 min ) - Valot, ultraäänianturi, etäisyysmittari ja servomoottori.
- Oppitunti 2 ( 1h 30 min ) - DC-moottori, moottoriohjaimet ja Arduino lisälevyt.
- Oppitunti 3 ( 2h ) - Joystick- vai Bluetooth - ohjaus? Kootaan kaikki yhteen.
- Oppitunti 4 ( 1h 30 min ) - Eteen, taakse, oikealle ja vasemmalle.
- Oppitunti 5 - päätös ( 1h ) - Viimeiset säädöt.
Testikoodia peliohjaimelle - Joystick Sensor Serial Read
Testikoodi:
/*
TNG Robotics | www.tngrobotics.ca
Title | Joystick Sensor Serial Read
Connect | Servo to Arduino digital pin 5
Published | October 29, 2016
*/
const int VRx = 0; // Connect to Analog Pin 0
const int VRy = 1; // Connect to Analog Pin 1
const int SW = 4; // Connect to Digital Pin 4
void setup() {
pinMode(SW, INPUT);
digitalWrite(SW, HIGH);
Serial.begin(9600);
}
void loop() {
Serial.print("x-axis tilt: ");
Serial.println(analogRead(VRx));
Serial.print("y-axis tilt: ");
Serial.println(analogRead(VRy));
Serial.print("switch: ");
Serial.println(digitalRead(SW));
delay(800);
}
Kaivuri testi neljällä servolla
#include <Servo.h> //Servo library Servo servo_1; //initialize a servo object for the connected servo Servo servo_2; //initialize a servo object for the connected servo Servo servo_3; //initialize a servo object for the connected servo Servo servo_4; //initialize a servo object for the connected servo int angle1 = 0; int angle2 = 0; int angle3 = 0; int angle4 = 0; int potentio1 = A1; // initialize the A0analog pin for potentiometer int potentio2 = A0; // initialize the A1analog pin for potentiometer int potentio3 = A2; // initialize the A1analog pin for potentiometer int potentio4 = A3; // initialize the A1analog pin for potentiometer #define SERVO1PIN 7 #define SERVO2PIN 9 #define SERVO3PIN 10 #define SERVO4PIN 6 void setup() { servo_1.attach(SERVO1PIN); // attach the signal pin of servo to pin9 of arduino servo_2.attach(SERVO2PIN); // attach the signal pin of servo to pin9 of arduino servo_3.attach(SERVO3PIN); // attach the signal pin of servo to pin9 of arduino servo_4.attach(SERVO4PIN); // attach the signal pin of servo to pin9 of arduino } void loop() { angle1 = analogRead(potentio1); // reading the potentiometer value between 0 and 1023 angle1 = map(angle1, 0, 1023, 60, 140); // scaling the potentiometer value to angle value for servo between 0 and 180) servo_1.write(angle1); //command to rotate the servo to the specified angle delay(5); angle2 = analogRead(potentio2); // reading the potentiometer value between 0 and 1023 angle2 = map(angle2, 0, 1023, 0, 179); // scaling the potentiometer value to angle value for servo between 0 and 180) servo_2.write(angle2); //command to rotate the servo to the specified angle delay(5); angle3 = analogRead(potentio3); // reading the potentiometer value between 0 and 1023 angle3 = map(angle3, 0, 1023, 30, 160); // scaling the potentiometer value to angle value for servo between 0 and 180) servo_3.write(angle3); //command to rotate the servo to the specified angle delay(5); angle4 = analogRead(potentio4); // reading the potentiometer value between 0 and 1023 angle4 = map(angle4, 0, 1023, 30, 160); // scaling the potentiometer value to angle value for servo between 0 and 180) servo_4.write(angle4); //command to rotate the servo to the specified angle delay(5); }