Sonntag, 16. April 2017

RoboCar-UT001 - Auf dem Weg zum Staubsaug-/Rasenmäh-Roboter

Die Komponenten für den Prototypen:
  • China RoboCar Chassis inkl. DC Getriebemotoren 5V mit Encoderrad und 4x AA Batteriefach
  • UltraschallSensor HC-SR04
  • L298N Dual Motor Controller Module 2A
  • Arduino UNO
  • ProtoShield for Arduino UNO
  • 2x Gabellichtschranken
Chassis mit Aufbau
Motoren mit Encoderrad und Batteriehalter 4x AA (6V)


Schaltung für die Gabellichtschranken (blau - GND / orange - +5V / schwarz - GRD / Signal I/O - Dig.PIN 2 und 3)


Was später geändert wird:
  • Der Arduino wird gegen einen ArduinoMicro getauscht
  • Das Batteriefach gegen einen größeren Akku bzw. eine PowerBank
  • Es kommen weitere Kontakt-/Abstandssensoren

Wenn sichergestellt ist, das die Basis so funktioniert wie sie soll, wir das Projekt auf eine entsprechende Mechanik übertragen.

Der Code für den Arduino bis jetzt:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 




// PINs for UltraSonic
#define pingPin 10
#define inPin 8


// PINs for Motor 1 DIR
#define wheel_R_f 6
#define wheel_R_b 7

// PINs for Motor 2 DIR
#define wheel_L_f 12
#define wheel_L_b 13


// PINs for Motor 1 Enable/Speed (PWM)
#define wheel_R_En 5
// PINs for Motor 2 Enable/Speed (PWM)
#define wheel_L_En 11

// Motorspeed
int mspeed = 150;

// Turning circle
int turnAround = 100;

// Current direction
int currentDir = 0;

// Encoder
#define Encoder_R 2
int EncoderCount_R = 0;
#define Encoder_L 3
int EncoderCount_L = 0;



// Setup Serial, Encoder- and MototrPINs
void setup() {

pinMode(wheel_R_f, OUTPUT);
pinMode(wheel_R_b, OUTPUT);
pinMode(wheel_L_f, OUTPUT);
pinMode(wheel_L_b, OUTPUT);
pinMode(wheel_R_En, OUTPUT);
pinMode(wheel_L_En, OUTPUT);


// Setup Encoder
pinMode(Encoder_R,INPUT);
pinMode(Encoder_L,INPUT);
attachInterrupt(0, isr_Encoder_R, CHANGE);
attachInterrupt(1, isr_Encoder_L, CHANGE);
//All Motors STOPmstop();

// Robo goes forward
goforward();
}

void loop()
{

// establish variables for duration of the ping,
// and the distance result in centimeters:
long duration, cm;





// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);


// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);

// convert the time into cm
cm = microsecondsToCentimeters(duration);

// *******************************************************************************
// If the Ultrasonic Sensor detect an object whith a disstance smaler then 15cm,
// stop than go backward for 150ms, turn around 180°, stop, than go forward again
if (cm < 15)
  {
    mstop();
    gobackward();
    delay(1000);
    gobackwardCL();
  //  delay(2500);
    mstop();
    CountReset();
    goforward();
  }


// Automatically synchronize the speed of the wheels
if (EncoderCount_L < EncoderCount_R)
{
  analogWrite(wheel_R_En, mspeed-20);
  analogWrite(wheel_L_En, mspeed+20);
}

if (EncoderCount_R < EncoderCount_L)
{
  analogWrite(wheel_R_En, mspeed+20);
  analogWrite(wheel_L_En, mspeed-20);
}

delay(100);
}

// *******************************************************************************
long microsecondsToCentimeters(long microseconds)
{

// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}




// Function: All Motors STOP
void mstop ()
{
  digitalWrite(wheel_R_b, LOW);
  digitalWrite(wheel_L_b, LOW);
  digitalWrite(wheel_R_f, LOW);
  digitalWrite(wheel_L_f, LOW);
  analogWrite(wheel_R_En, 0);
  analogWrite(wheel_L_En, 0);

currentDir = 0;

  delay(500);

}




// Function: All Motors go forward
void goforward ()
{
  digitalWrite(wheel_R_f, HIGH);
  digitalWrite(wheel_L_f, HIGH);
  digitalWrite(wheel_R_b, LOW);
  digitalWrite(wheel_L_b, LOW);

currentDir = 1;

  for (int ispeed = 100; ispeed <= mspeed; ispeed=ispeed+2)
  {
    analogWrite(wheel_R_En, ispeed);
    analogWrite(wheel_L_En, ispeed);
    delay(20);
  }
 
}



// Function: All Motors go backward
void gobackward ()
{
  digitalWrite(wheel_R_f, LOW);
  digitalWrite(wheel_L_f, LOW);
  digitalWrite(wheel_R_b, HIGH);
  digitalWrite(wheel_L_b, HIGH);

currentDir = 2;
 
  for (int ispeed = 100; ispeed <= mspeed; ispeed=ispeed+2)
  {
    analogWrite(wheel_R_En, ispeed);
    analogWrite(wheel_L_En, ispeed);
    delay(20);
  }

}




// Function: Go in a backward-circle to the left
void gobackwardCL ()
{
  digitalWrite(wheel_R_f, HIGH);
  digitalWrite(wheel_L_f, LOW);
  digitalWrite(wheel_R_b, LOW);
  digitalWrite(wheel_L_b, HIGH);

currentDir = 3;

CountReset();
 
  for (int ispeed = 100; ispeed <= mspeed; ispeed=ispeed+2)
  {
    analogWrite(wheel_R_En, ispeed);
    analogWrite(wheel_L_En, ispeed);
    delay(20);
  }

do
{
  if (EncoderCount_R >= turnAround)
    analogWrite(wheel_R_En, 0);

  if (EncoderCount_L >= turnAround)
    analogWrite(wheel_L_En, 0);

} while (EncoderCount_R < turnAround && EncoderCount_L < turnAround);

 
}




// Function: Go in a backward-circle to the right
void gobackwardCR ()
{
  digitalWrite(wheel_R_f, LOW);
  digitalWrite(wheel_L_f, HIGH);
  digitalWrite(wheel_R_b, HIGH);
  digitalWrite(wheel_L_b, LOW);

currentDir = 4;

CountReset();
 
  for (int ispeed = 100; ispeed <= mspeed; ispeed=ispeed+2)
  {
    analogWrite(wheel_R_En, ispeed);
    analogWrite(wheel_L_En, ispeed);
    delay(20);
  }

do
{
  if (EncoderCount_R >= turnAround)
    analogWrite(wheel_R_En, 0);

  if (EncoderCount_L >= turnAround)
    analogWrite(wheel_L_En, 0);
 
} while (EncoderCount_R < turnAround && EncoderCount_L < turnAround);
 
}



// Reset the EncoderCounts (left and right)
void CountReset()
{
  EncoderCount_L = 0;
  EncoderCount_R = 0;
}




// ISR - Encoder right
void isr_Encoder_R ()
{
   EncoderCount_R++;
}



// ISR - Encoder left
void isr_Encoder_L ()
{
   EncoderCount_L++;
}





++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Vielen Dank für den Code der den Ultraschallsensor belebt geht an: http://www.robodino.de/2011/12/ultraschall-distanz-sensor-hc-sr04.html

Keine Kommentare:

Kommentar veröffentlichen