Anda di halaman 1dari 17

Microprocessor and Microcontroller

Final Report

Automatic Braking System


Kappa Group
Faisal Abdillah Muttaqin 1406641994
Hilmy Arief Baja 1406547074
Luthfi Verlianda Pratama 1406545352
Muhammad Hanif Ibrahim 1406642012
Wibisono Adhi Pradana 1406547055

Electrical Engineering
International Program
Faculty of Engineering
University of Indonesia
Depok

Implementation
The equipments used for our project are slightly different from the proposal. We did not
include the Color and Light sensor, Temperature and Humidity Sensor, Flight Controller, and the
external Gyroscope, however our group use the built-in or internal Gyroscope that is on the
STM32F4 discovery itself. The reason we did not implement those things are due to the trouble
trouble with the interfacing of those sensors with our microcontrollers, moreover, we also
think it does not really fit our projects title and main aim.
In addition, we also changed our microcontroller from Nuvoton NUC140 to STM32F4-
Discovery. This is because most of the implementations that we wanted to be done are more
widely supported with the STM32F4-Discovery microcontroller, in addition to that, our mentor
also told us to better choose the STM32F4 Discovery over Nuvoton as it has more
documentation on the internet. As for compiling the code, our group used CoIDE as the code
compiler.
In general, how our project works is that there is an Ultrasonic Sensor mounted in front
of the car. Then, if the Ultrasonic Sensor detects an object in front of the car, the car will then
automatically brake. Another sensor used which is the internal Gyroscope on the
microcontroller will basically notify the user if the vehicle is tilted sideways indicated by the
blinking of the four LEDs on the microcontroller. The more the vehicle is tilted sideways, the
faster the LEDs will blink. The Ultrasonic Sensor is coded such as that the ESC (Electronic Speed
Controller) is sent a PWM signal from the Sensor to stop the Brushless Motors used, making the
car brake before hitting the object in front of it.

Documentation
Task Division
Faisal Abdillah Muttaqin:
Providing most of the equipments used
Soldering and Combining the Hardware
Calibrating the Electronic Speed Controller
Suggesting the whole idea of how the project should be
Hilmy Arief Baja
Coding and interfacing the Ultrasonic Sensor
Arranging the code to be tidier
Compiling the final report
Luthfi Verlianda Pratama
Coding the PWM signal for the Electronic Speed Controller
Combining the codes
Compiling the final report
Muhammad Hanif Ibrahim
Coding and interfacing the ESP8266
Correcting the code librarys
Compiling the final report
Wibisono Adhi Pradana
Soldering and Combining the Hardware
Designed the component placement on the acrylic
Compiling the final report

Source Code
/* Include core modules */
#include "stm32f4xx.h"
#include "tm_stm32f4_delay.h"
#include "tm_stm32f4_disco.h"
#include "tm_stm32f4_hcsr04.h"
#include "tm_stm32f4_pwm.h"
#include "tm_stm32f4_l3gd20.h"

#include <stdio.h>

int main(void) {
/*PWM Instance*/
TM_PWM_TIM_t TIM1_Data, TIM4_Data, TIM2_Data;
/* HCSR04 Instance */
TM_HCSR04_t HCSR04;
/* L3GD20 Struct */
TM_L3GD20_t L3GD20_Data;
char buffer[20];

/* Initialize system */
SystemInit();

/* Set PWM to 1kHz frequency on timer TIM4 */


/* 1 kHz = 1ms = 1000us */
TM_PWM_InitTimer(TIM4, &TIM4_Data, 1000);

/* Initialize delay functions */


TM_DELAY_Init();

/* Initialize LEDs on board */


TM_DISCO_LedInit();

/* Turn on LED red */


TM_DISCO_LedOn(LED_RED);
/* Init L3GD20 sensor */
if (TM_L3GD20_Init(TM_L3GD20_Scale_2000) != TM_L3GD20_Result_Ok) {
/* Sensor error, toggles the LED */

TM_DISCO_LedToggle(LED_BLUE);
while (1);
}

/* Initialize distance sensor1 on pins; ECHO: PD0, TRIGGER: PC1 */


if (!TM_HCSR04_Init(&HCSR04, GPIOD, GPIO_PIN_0, GPIOC, GPIO_PIN_1)) {
/* Sensor is not ready to use */
/* Maybe wiring is incorrect */
while (1) {
TM_DISCO_LedToggle(LED_ORANGE | LED_BLUE);
Delayms(100);
}
}

while (1) {
/* Read data */
TM_L3GD20_Read(&L3GD20_Data);

/* Read distance from sensor 1 */


/* Distance is returned in cm and also stored in structure */
TM_HCSR04_Read(&HCSR04);
TM_PWM_InitTimer(TIM4, &TIM4_Data, 1000);

/* Something is going wrong, maybe incorrect pinout */


if (HCSR04.Distance < 0) {
TM_DISCO_LedOn(LED_ORANGE);
TM_DISCO_LedOff(LED_BLUE);
} else if (HCSR04.Distance > 25) {
/* Distance more than 25cm */

/* Initialize PWM on TIM4, Channel 1 and PinsPack 2 = PD14 */


TM_PWM_InitChannel(&TIM4_Data, TM_PWM_Channel_3, TM_PWM_PinsPack_2);

/* Set channel 3 value, 1/5 = 33% duty cycle */


TM_PWM_SetChannel(&TIM4_Data, TM_PWM_Channel_3, TIM4_Data.Period / 5);

/* Initialize PWM on TIM4, Channel 1 and PinsPack 2 = PD13 */


TM_PWM_InitChannel(&TIM4_Data, TM_PWM_Channel_2, TM_PWM_PinsPack_2);

/* Set channel 1 value, 1/5 = 20% duty cycle */


TM_PWM_SetChannel(&TIM4_Data, TM_PWM_Channel_2, TIM4_Data.Period / 5);

/* Turn on the Blue LED, but turning off the Orange LED */
TM_DISCO_LedOn(LED_BLUE);
TM_DISCO_LedOff(LED_ORANGE);
} else {
/* Distance between 0 and 50cm, Turning on both LED's */
TM_DISCO_LedOn(LED_ORANGE | LED_BLUE);
/* Initialize PWM on TIM2, Channel 1 and PinsPack 2 = PD14 */
TM_PWM_InitChannel(&TIM4_Data, TM_PWM_Channel_3, TM_PWM_PinsPack_2);

/* Set channel 1 value, 0% duty cycle*/


TM_PWM_SetChannelMicros(&TIM4_Data, TM_PWM_Channel_3, 0);

/* Initialize PWM on TIM2, Channel 1 and PinsPack 2 = PD13 */


TM_PWM_InitChannel(&TIM4_Data, TM_PWM_Channel_2, TM_PWM_PinsPack_2);

/* Set channel 1 value, 0% duty cycle*/


TM_PWM_SetChannelMicros(&TIM4_Data, TM_PWM_Channel_2, 0);

/* Give some time to sensor */


Delayms(100);
}
}

Arduino Code of the ESP8266 module

#include <SoftwareSerial.h>
#define DEBUG true

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's
pin 2
// and the RX line from the esp to the Arduino's pin 3
void setup()
{
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different

pinMode(11,OUTPUT);
digitalWrite(11,LOW);

pinMode(12,OUTPUT);
digitalWrite(12,LOW);

pinMode(13,OUTPUT);
digitalWrite(13,LOW);

sendData("AT+RST\r\n",2000,DEBUG); // reset module


sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
}

void loop()
{
if(esp8266.available()) // check if the esp is sending a message
{

if(esp8266.find("+IPD,"))
{
delay(1000); // wait for the serial buffer to fill up (read all the serial data)
// get the connection id so that we can then disconnect
int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48

esp8266.find("pin="); // advance cursor to "pin="

int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the
1st number is 1, then multiply to get 10
pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13
then the 2nd number is 3, then add to the first number

digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin

// make close command


String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";

sendData(closeCommand,1000,DEBUG); // close connection


}
}
}

/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response;
debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
String response = "";

esp8266.print(command); // send the read character to the esp8266

long int time = millis();

while( (time+timeout) > millis())


{
while(esp8266.available())
{

// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}

return response;
}

Hardware Diagram:
*Gyroscope (internal) is also used but it is not mentioned as the Gyroscope not an external one.

Project prototype:

Anda mungkin juga menyukai