Raspberry Pi Auto Car - JackieWSC/Onepiece GitHub Wiki
Auto Car Project
Hardware
- Raspberry Pi
- L298N Dual H Bridge DC Motor Drive
- Ultrasonic Module HC-SR04 Distance Sensor
- 4 yellow motors
Software
- C++ language
- Blynk
- version:0.4.10
- working dir:blynk0.4.10/libraries/Blynk/linux/main.cpp
Code
- Setup distance sensor related GPIO
- Use GPIO 2 as OUTPUT
- Use GPIO 3 as INPUT
- Setup L298N Dual H Bridge DC Motor Drive related GPIO
- Use GPIO 17, 18, 23 and 24 as OUTPUT
- Add call back function for
- timer event
- distance sensor
void setup()
{
printf("Setup - uptime: %i\n", uptime);
printf("Setup - milis time: %i\n", millis());
// echoPin and trigPoin for the distance sensor
unsigned int echoPin = 3;
unsigned int trigPin = 2;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(17, OUTPUT);
pinMode(18, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
// Call back function for timer event and distance sensor
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(1000L, distanceSensor);
}
- main loop
void loop()
{
Blynk.run();
timer.run();
autoRun();
}
- Timer event
- Print the running time each 1 second to V10
void myTimerEvent()
{
if (millis() % 10000 == 0)
{
printf("My Timer Event 2 - %i (ms)\n", millis());
}
Blynk.virtualWrite(V10, millis()/1000);
}
- Distance Sensor
- Use sonar to detect the distance to an object like bats do
- the transmitter (trig pin) send a signal high frequency sound
- when the signal finds the object, it is reflected
- the transmitter (echo pin) receives it
- the time between the transmission and reception allows us to know the distance to an object
int getDistance()
{
unsigned int echoPin = 3;
unsigned int trigPin = 2;
digitalWrite (trigPin, LOW);
delayMicroseconds(2);
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite (trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 1000000);
int distance = (duration/2) / 29.1;
return distance;
}
- Auto run logic
- if the distance between 20-50 cm, turn left
- then move forward until the distance is less than 20
void autoRun()
{
if (m_isAuto) {
if (m_distance > 50) {
// forward
Execute(Action::forward);
}
else if (m_distance < 20){
// backward
Execute(Action::backward);
}
else {
// if the distance is 20 to 50 then turn left
Execute(Action::left);
}
}
}
- movement logic
- when GPIO 17 is enable, the left motor move forward
- when GPIO 18 is enable, the left motor move backward
- when GPIO 23 is enable, the right motor move forward
- when GPIO 24 is enable, the right motor move backward
void Execute(bool leftForward, bool leftBackward, bool rightForward, bool rightBackward)
{
//analogWrite(21, 255); //Sets speed variable via PWM
digitalWrite(17, leftForward);
digitalWrite(18, leftBackward);
digitalWrite(23, rightForward);
digitalWrite(24, rightBackward);
}
void Execute(Action action)
{
if ( action != m_lastAction )
{
switch(action)
{
case Action::left:
printf("left\n");
Execute(true, false, true, false);
break;
case Action::right:
printf("right\n");
Execute(false, true, false, true);
break;
case Action::forward:
printf("forward\n");
Execute(false, true, true, false);
break;
case Action::backward:
printf("backward\n");
Execute(true, false, false, true);
break;
case Action::stop:
printf("stop\n");
Execute(false, false, false, false);
break;
default:
printf("default");
}
m_lastAction = action;
}
}
GPIO Connection
Raspberry Pi GPIO Setup to L298N Dual H Bridge DC Motor Drive setup
- PIN 2 (5V PWR) connect to Motor Power supply
- PIN 39 (GND) connect to GND
- PIN 11 (GPIO 17) connect to IN4
- PIN 12 (GPIO 18) connect to IN3
- PIN 16 (GPIO 23) connect to IN2
- PIN 18 (GPIO 24) connect to IN1
L298N Dual H Bridge DC Motor Drive setup
- OUT1 connect to motor GND
- OUT2 connect to motor PWD
- OUT3 connect to motor GND
- OUT4 connect to motor PWD
Raspberry Pi GPIO Setup to Ultrasonic Module HC-SR04 Distance Sensor
- PIN 3 (GPIO 2) connect to Trig
- PIN 4 (5V PWR) connect to VCC (PWD)
- PIN 5 (GPIO 3) connect to Echo
- PIN 6 (GND) connect to GND