Estava faltando um projeto de Robótica. Não falta mais. Eis que o Solucionática adquiriu uma plataforma básica (Magician chassis), contendo dois motores, duas caixas de redução, um suporte para 4 pilhas AA, duas rodas de plástico grandes, uma roda omni metálica (uma esfera de aço) e duas bases de plástico com diversas furações e recortes. O fornecedor foi a Sparkfun.
Para que o Robô funcionasse, também foram adquiridos, separadamente, uma Mboard, que nada mais é do que um Arduino Leonardo e um L298 (driver para dois motores) integrados em uma única placa. O sketch é de uma simplicidade franciscana. E assim, há limitações na capacidade do robô: a velocidade de "cruzeiro" é metade da velocidade máxima; o robô só segue pistas com curvas para a esquerda (fórmula Indy!!!!). Esse é o preço pago por querer usar apenas um track sensor onde três é o ideal! Estudem o sketch, sugiram melhorias e assistam ao vídeo!
MBOARD TRACK SENSOR
Sketch do Arduino:
/*
Created by Solucionática
modified 11 Aug 2013
Based in Analog Input sketch
Created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
Also based in Itead Studio demo code:
http://imall.iteadstudio.com/prototyping/electronic-brick/sensor-brick/im121012001.html
The circuit:
* Track sensor attached to digital input 5
* Motor 1 attached to Out 1 and Out 2 of Mboard - Right
* Motor 2 attached to Out 3 and Out 4 of Mboard - Left
* pay attention to the polarity of both motors.
This example code is in the public domain.
*/
int pwmMotor1 = 10; // select the pin for the speed control of motor 1.
int pwmMotor2 = 11; // select the pin for the speed control of motor 2.
const int TRACK = 5; // Mboard pin 3 of nRF24L01+ Module Interface
void setup() {
pinMode(TRACK, INPUT);// declare pin 5 as an INPUT:
pinMode(pwmMotor1, OUTPUT);
pinMode(pwmMotor2, OUTPUT);
}
void loop() {
// set Mboard In1 (D7)=1, In2 (D8)=0, In3 (D12)=1 and In4(D13)=0
// so motor 1 and 2 run foward only.
digitalWrite(7,HIGH);// + Motor 1
digitalWrite(8,LOW);// - Motor 1
digitalWrite(12,HIGH); // + Motor 2
digitalWrite(13,LOW); // - Motor 2
int TrackState = digitalRead(TRACK);
if(TrackState)
{
analogWrite(pwmMotor1, 64); // now we have 25% of the speed of motor 1. Robô turns left
analogWrite(pwmMotor2, 128); // because motor 2 maintains the speed.
delay(1); // a delay to deal with the eletrical noise and mechanical inertia of the motors.
}
else
{
analogWrite(pwmMotor1, 128); // PWM varies from 0 to 255 (full speed), so we have 50% of the // speed in both motors.
analogWrite(pwmMotor2, 128); //
delay(1);
}
}
outro:******************************
int pwmMotor1 = 10; // select the pin for the speed control of motor 1.
int pwmMotor2 = 11; // select the pin for the speed control of motor 2.
const int TRACK = 5; // Mboard pin 3 of nRF24L01+ Module Interface
void setup() {
// initialize serial communication:
pinMode(TRACK, INPUT);// declare pin 5 as an INPUT:
pinMode(pwmMotor1, OUTPUT);
pinMode(pwmMotor2, OUTPUT);
Serial.begin(115200);
// initialize the LED pins:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// read the sensor:
if (Serial.available() > 0) {
int inByte = Serial.read();
// do something different depending on the character received.
// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character. For
// example 'a' = 97, 'b' = 98, and so forth:
switch (inByte) {
case 'f':
frente();
break;
case 't':
tras();
break;
case 'd':
direita_frente();
break;
case 'D':
direita_tras();
break;
case 'e':
esquerda_frente();
break;
case 'E':
esquerda_tras();
break;
default:
digitalWrite(12,LOW); // + Motor 2
digitalWrite(13,LOW); // - Motor 2
digitalWrite(8,LOW);// - Motor 1
digitalWrite(7,LOW);// + Motor 1
analogWrite(pwmMotor1, 0); // now we have 25% of the speed of motor 1. Robô turns left
analogWrite(pwmMotor2, 0);
// turn all the LEDs off:
//for (int thisPin = 7; thisPin < 13; thisPin++) {
//digitalWrite(thisPin, LOW);
//}
}
}
}
void frente() {
// set Mboard In1 (D7)=1, In2 (D8)=0, In3 (D12)=1 and In4(D13)=0
// so motor 1 and 2 run foward only.
digitalWrite(7,HIGH);// + Motor 1
digitalWrite(8,LOW);// - Motor 1
digitalWrite(12,HIGH); // + Motor 2
digitalWrite(13,LOW); // - Motor 2
analogWrite(pwmMotor1, 192); // now we have 25% of the speed of motor 1. Robô turns left
analogWrite(pwmMotor2, 192); // because motor 2 maintains the speed.
delay(1); // a delay to deal with the eletrical noise and mechanical inertia of the motors.
delay(200);
}
void tras() {
// set Mboard In1 (D7)=1, In2 (D8)=0, In3 (D12)=1 and In4(D13)=0
// so motor 1 and 2 run foward only.
digitalWrite(8,HIGH);// - Motor 1
digitalWrite(7,LOW);// + Motor 1
digitalWrite(13,HIGH); // - Motor 2
digitalWrite(12,LOW); // + Motor 2
analogWrite(pwmMotor1, 192); // now we have 25% of the speed of motor 1. Robô turns left
analogWrite(pwmMotor2, 192); // because motor 2 maintains the speed.
// a delay to deal with the eletrical noise and mechanical inertia of the motors.
delay(200);
}
void direita_frente() {
// set Mboard In1 (D7)=1, In2 (D8)=0, In3 (D12)=1 and In4(D13)=0
// so motor 1 and 2 run foward only.
digitalWrite(8,HIGH);// + Motor 1
digitalWrite(7,LOW);// - Motor 1
digitalWrite(12,HIGH); // + Motor 2
digitalWrite(13,LOW); // - Motor 2
analogWrite(pwmMotor1, 192); // now we have 25% of the speed of motor 1. Robô turns left
analogWrite(pwmMotor2, 96); // because motor 2 maintains the speed.
// a delay to deal with the eletrical noise and mechanical inertia of the motors.
delay(200);
}
void direita_tras() {
// set Mboard In1 (D7)=1, In2 (D8)=0, In3 (D12)=1 and In4(D13)=0
// so motor 1 and 2 run foward only.
digitalWrite(8,HIGH);// - Motor 1
digitalWrite(7,LOW);// + Motor 1
digitalWrite(13,HIGH); // - Motor 2
digitalWrite(12,LOW); // + Motor 2
analogWrite(pwmMotor1, 192); // now we have 25% of the speed of motor 1. Robô turns left
analogWrite(pwmMotor2, 96); // because motor 2 maintains the speed.
// a delay to deal with the eletrical noise and mechanical inertia of the motors.
delay(200);
}
void esquerda_frente() {
// set Mboard In1 (D7)=1, In2 (D8)=0, In3 (D12)=1 and In4(D13)=0
// so motor 1 and 2 run foward only.
digitalWrite(7,HIGH);// - Motor 1
digitalWrite(8,LOW);// + Motor 1
digitalWrite(13,HIGH); // - Motor 2
digitalWrite(12,LOW); // + Motor 2
analogWrite(pwmMotor1, 96); // now we have 25% of the speed of motor 1. Robô turns left
analogWrite(pwmMotor2, 192); // because motor 2 maintains the speed.
// a delay to deal with the eletrical noise and mechanical inertia of the motors.
delay(200);
}
void esquerda_tras() {
// set Mboard In1 (D7)=1, In2 (D8)=0, In3 (D12)=1 and In4(D13)=0
// so motor 1 and 2 run foward only.
digitalWrite(7,HIGH);// - Motor 1
digitalWrite(8,LOW);// + Motor 1
digitalWrite(13,HIGH); // - Motor 2
digitalWrite(12,LOW); // + Motor 2
analogWrite(pwmMotor1, 96); // now we have 25% of the speed of motor 1. Robô turns left
analogWrite(pwmMotor2, 192); // because motor 2 maintains the speed.
// a delay to deal with the eletrical noise and mechanical inertia of the motors.
delay(200);
}